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(pi): 保留启动失败的脱敏错误摘要 (#4626)

FicoWang2848dbf97cc1
diff --git a/apps/desktop/src/main/maker-host/__tests__/piRemoteTransport.test.ts b/apps/desktop/src/main/maker-host/__tests__/piRemoteTransport.test.ts
index 0a3d400a70..a2f28d6c59 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piRemoteTransport.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piRemoteTransport.test.ts
@@ -340,6 +340,61 @@ describe('SSH stdout buffer overflow guard', () => {
return { handlers, handle };
}
+ it.each(['close', 'error'] as const)('redacts complete stderr lines and flushes the final tail before %s', async (end) => {
+ const { handlers, handle } = fakeChannel();
+ const logger = fakeLogger();
+ const transport = createSshPiTransport({
+ remoteHost: { id: 'test-host', execStream: vi.fn().mockResolvedValue(handle) } as unknown as RemoteHost,
+ binaryPath: '/remote/pi', args: ['--mode', 'rpc'], cwd: '/remote/workdir', env: {}, logger,
+ });
+ const lines: string[] = [];
+ transport.onStderr!((line) => lines.push(line));
+ const atClose: string[][] = [];
+ transport.onClose(() => atClose.push([...lines]));
+ await vi.waitFor(() => expect(handlers.onStderr).toBeDefined());
+ handlers.onStderr!('sessionTo');
+ handlers.onStderr!('ken=FAKE_OPAQUE_CREDENTIAL\r');
+ expect(lines).toEqual([]);
+ expect(logger.warn).not.toHaveBeenCalled();
+ handlers.onStderr!('\npassword="fake spaced');
+ handlers.onStderr!(' password"\nError: Failed to load extension placeholder');
+ if (end === 'close') handlers.onClose!({ code: 1, signal: null });
+ else handlers.onError!(new Error('channel failed'));
+ expect(lines).toHaveLength(3);
+ expect(lines[0]).toContain('[REDACTED]');
+ expect(lines[1]).toContain('[REDACTED]');
+ expect(lines[2]).toBe('Error: Failed to load extension placeholder');
+ expect(atClose).toEqual([lines]);
+ const output = JSON.stringify({ lines, logs: logger.warn.mock.calls });
+ expect(output).not.toContain('FAKE_OPAQUE_CREDENTIAL');
+ expect(output).not.toContain('fake spaced');
+ expect(output).not.toContain(' password');
+ handlers.onStderr!('late secret');
+ expect(lines).toHaveLength(3);
+ });
+
+ it('discards an entire oversized stderr line including its later chunks, then resumes', async () => {
+ const { handlers, handle } = fakeChannel();
+ const logger = fakeLogger();
+ const transport = createSshPiTransport({
+ remoteHost: { id: 'test-host', execStream: vi.fn().mockResolvedValue(handle) } as unknown as RemoteHost,
+ binaryPath: '/remote/pi', args: ['--mode', 'rpc'], cwd: '/remote/workdir', env: {}, logger,
+ });
+ const lines: string[] = [];
+ transport.onStderr!((line) => lines.push(line));
+ await vi.waitFor(() => expect(handlers.onStderr).toBeDefined());
+ handlers.onStderr!('sessionToken=' + 'x'.repeat(16 * 1024));
+ handlers.onStderr!('FAKE_SECRET_TAIL');
+ expect(lines).toEqual([]);
+ expect(logger.warn).not.toHaveBeenCalled();
+ expect(handle.kill).not.toHaveBeenCalled();
+ handlers.onStderr!('\nError: missing extension\n');
+ handlers.onStderr!('password=' + 'x'.repeat(16 * 1024) + 'FAKE_FINAL_TAIL');
+ handlers.onClose!({ code: 1, signal: null });
+ expect(lines).toEqual(['Error: missing extension']);
+ expect(JSON.stringify(logger.warn.mock.calls)).not.toContain('FAKE');
+ });
+
it('resyncs after an oversized unterminated line instead of closing the transport', async () => {
const execStream = vi.fn();
const host = { id: 'test-host', execStream } as unknown as RemoteHost;
diff --git a/apps/desktop/src/main/maker-host/pi-remote-transport.ts b/apps/desktop/src/main/maker-host/pi-remote-transport.ts
index fc3b781baf..1c5885bbdc 100644
--- a/apps/desktop/src/main/maker-host/pi-remote-transport.ts
+++ b/apps/desktop/src/main/maker-host/pi-remote-transport.ts
@@ -27,6 +27,8 @@
import { createHash } from 'node:crypto';
import { StringDecoder } from 'node:string_decoder';
+import { redactSensitiveText } from '@cindy/maker-shared/error-redaction';
+
import type { RemoteHost, ExecStreamHandle } from '@cindy/maker-remote-ssh';
import { probeRemoteAgent, probePiManager } from '@cindy/maker-remote-ssh';
@@ -65,6 +67,7 @@ export interface SshPiTransportOptions {
}
const DEFAULT_HANDSHAKE_TIMEOUT_MS = 15_000;
+const MAX_STDERR_LINE_CHARS = 16 * 1024;
// 轮 40-w4-t5 CRITICAL:key-aware 敏感字段名 —— 值形状正则覆盖不了 64-hex
// sessionToken / 自定义 MCP header 值, 字段名命中即整体替换。
@@ -526,6 +529,8 @@ function createSshPiChannelTransport(
const closeHandlers = new Set<PiCloseHandler>();
const stderrHandlers = new Set<(line: string) => void>();
const pendingWrites: Array<{ line: string; resolve: () => void; reject: (err: Error) => void }> = [];
+ let stderrBuffer = '';
+ let skippingOversizedStderr = false;
/** stdout 行切分缓冲(ssh channel 文本块可能跨行/半行)。 */
let stdoutBuffer = '';
let skippingOversizedLine = false;
@@ -554,6 +559,8 @@ function createSshPiChannelTransport(
if (closed) return;
closed = true;
flushStdoutTail();
+ if (!skippingOversizedStderr && stderrBuffer) emitStderrLine(stderrBuffer);
+ stderrBuffer = '';
if (handshakeTimer) { clearTimeout(handshakeTimer); handshakeTimer = null; }
clearBackpressureTimer();
drainListenerAttached = false;
@@ -580,6 +587,36 @@ function createSshPiChannelTransport(
for (const handler of stderrHandlers) handler(line);
};
+ // SSH callbacks deliver chunks, whereas PiTransport promises complete lines.
+ // Redact only after framing; never expose a truncated secret from an oversized line.
+ const emitStderrLine = (line: string): void => {
+ if (!line.trim()) return;
+ const redacted = redactCredentialText(redactSensitiveText(line.replace(/\r$/, '')));
+ logger.warn('pi ssh stderr', { line: redacted.slice(0, 500) });
+ fireStderr(redacted);
+ };
+ const consumeStderr = (chunk: string): void => {
+ if (closed) return;
+ let start = 0;
+ while (start < chunk.length) {
+ const newline = chunk.indexOf('\n', start);
+ const end = newline === -1 ? chunk.length : newline;
+ if (!skippingOversizedStderr) {
+ if (stderrBuffer.length + end - start > MAX_STDERR_LINE_CHARS) {
+ stderrBuffer = '';
+ skippingOversizedStderr = true;
+ } else {
+ stderrBuffer += chunk.slice(start, end);
+ }
+ }
+ if (newline === -1) break;
+ if (!skippingOversizedStderr) emitStderrLine(stderrBuffer);
+ stderrBuffer = '';
+ skippingOversizedStderr = false;
+ start = newline + 1;
+ }
+ };
+
/** 轮 23-H4 HIGH:背压感知的写入 —— channel.write 返回 false(ssh2 缓冲满)
* 时**不 resolve**, 等 channel drain 再写下一条; 带背压超时(防 drain 永不
* 触发挂死)fireClose。写入抛错 reject 该条。 */
@@ -761,16 +798,7 @@ function createSshPiChannelTransport(
});
envWritten = true;
drainPending();
- ch.onStderr((s) => {
- const trimmed = s.trim();
- if (trimmed) {
- // 轮 40-w4-t5 CRITICAL:direct fallback 的 stderr 绕过 daemon 侧 scrub,
- // 进桌面日志前 key-aware 脱敏(env 凭证/64-hex sessionToken)。
- const redacted = redactCredentialText(trimmed);
- logger.warn('pi ssh stderr', { line: redacted.slice(0, 500) });
- fireStderr(redacted);
- }
- });
+ ch.onStderr(consumeStderr);
ch.onClose((info) => {
// 尾部 flush 统一由 fireClose 里的 flushStdoutTail 执行(幂等)——
// 这里不再自己 flush:fireClose 可能已被其它路径(队列溢出/缓冲超限/
diff --git a/packages/maker-core/src/agents/pi/rpc-client.startup.test.ts b/packages/maker-core/src/agents/pi/rpc-client.startup.test.ts
new file mode 100644
index 0000000000..2fb8d6b017
--- /dev/null
+++ b/packages/maker-core/src/agents/pi/rpc-client.startup.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { PiRpcProcess, createPiStdioTransport } from './rpc-client.js';
+
+describe('Pi startup diagnostics over real stdio (#4625)', () => {
+ it('carries drained stderr into the pending and subsequent startup requests', async () => {
+ const logger = {
+ trace: vi.fn(), debug: vi.fn(), info: vi.fn(), warn: vi.fn(),
+ error: vi.fn(), fatal: vi.fn(), child: vi.fn(),
+ };
+ logger.child.mockReturnValue(logger);
+ // No Pi account, user configuration, package install, or network required.
+ // Exercise the real child-process exit/stdio framing path with a failing CLI.
+ const transport = createPiStdioTransport({
+ binaryPath: process.execPath,
+ args: ['-e', `process.stderr.write('Error: Failed to load extension "placeholder"\\n');
+ process.stderr.write('Hint: Start without extensions using "pi -ne".');
+ process.exitCode = 1;`],
+ cwd: process.cwd(), env: {}, logger,
+ });
+ const proc = new PiRpcProcess({ transport, logger, onEvent: vi.fn(), onExit: vi.fn() });
+ try {
+ await expect(proc.request({ type: 'get_state' })).rejects.toThrow(
+ 'Error: Failed to load extension "placeholder"\nHint: Start without extensions using "pi -ne".',
+ );
+ await expect(proc.request({ type: 'get_state' })).rejects.toThrow('code=1');
+ } finally {
+ await proc.close();
+ }
+ });
+});
diff --git a/packages/maker-core/src/agents/pi/rpc-client.test.ts b/packages/maker-core/src/agents/pi/rpc-client.test.ts
index 0b210e3620..1d6700a502 100644
--- a/packages/maker-core/src/agents/pi/rpc-client.test.ts
+++ b/packages/maker-core/src/agents/pi/rpc-client.test.ts
@@ -218,3 +218,124 @@ describe('PiRpcProcess process observer', () => {
).not.toThrow();
});
});
+
+describe('PiRpcProcess startup failure diagnostics (#4625)', () => {
+ it('preserves extension failure and native recovery hint when startup exits', async () => {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const proc = createProcess();
+ const rejected = expect(proc.request({ type: 'get_state' })).rejects.toThrow(
+ 'Failed to load extension "placeholder"\nHint: Start without extensions using "pi -ne".',
+ );
+ child.stderr.emit('data', Buffer.from('Error: Failed to load extension "placeholder"\n'));
+ child.stderr.emit('data', Buffer.from('Hint: Start without extensions using "pi -ne".\n'));
+ child.emit('close', 1, null);
+ await rejected;
+ await expect(proc.request({ type: 'get_state' })).rejects.toThrow('Failed to load extension');
+ });
+
+ it('retains startup diagnostics if exit precedes the first request', async () => {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const proc = createProcess();
+ child.stderr.emit('data', Buffer.from('Cannot find module placeholder\n'));
+ child.emit('close', 1, null);
+ await expect(proc.request({ type: 'get_state' })).rejects.toThrow(
+ 'pi process exited (code=1, signal=null)\nPi startup stderr:\nCannot find module placeholder',
+ );
+ });
+
+ it('redacts complete stderr lines before bounding the diagnostic tail', async () => {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const proc = createProcess();
+ const error = proc.request({ type: 'get_state' }).catch((err: Error) => err);
+ child.stderr.emit('data', Buffer.from('obsolete warning\n'));
+ child.stderr.emit('data', Buffer.from(`sessionToken=${'a'.repeat(4000)}\n`));
+ child.stderr.emit('data', Buffer.from(`${'x'.repeat(4000)}\n`));
+ // Split a synthetic credential across byte chunks, not logical stderr lines.
+ child.stderr.emit('data', Buffer.from('Error: Failed to load extension; api_key=sk-ant-'));
+ child.stderr.emit('data', Buffer.from('FAKEONLY0123456789\n'));
+ child.emit('close', 1, null);
+ const result = await error as Error;
+ expect(result.message).toContain('Failed to load extension');
+ expect(result.message).toContain('[REDACTED]');
+ expect(result.message).not.toContain('FAKEONLY');
+ expect(result.message).not.toContain('aaaa');
+ expect(result.message).not.toContain('obsolete warning');
+ expect(result.message.length).toBeLessThan(2200);
+ });
+
+ it('does not attach old startup warnings or running stderr after a valid RPC response', async () => {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const proc = createProcess();
+ child.stderr.emit('data', Buffer.from('startup warning\n'));
+ const ready = proc.request({ type: 'get_state' });
+ child.stdout.emit('data', Buffer.from(JSON.stringify({
+ type: 'response', id: 'c1', command: 'get_state', success: true, data: {},
+ }) + '\n'));
+ await ready;
+ const rejected = expect(proc.request({ type: 'get_state' })).rejects.toThrow(
+ /^pi process exited \(code=1, signal=null\)$/,
+ );
+ child.stderr.emit('data', Buffer.from('running warning with unrelated content\n'));
+ child.emit('close', 1, null);
+ await rejected;
+ });
+
+ it('redacts raw transport diagnostics including headers and quoted secrets', async () => {
+ let stderr!: (line: string) => void;
+ let exit!: (info: { code: number | null; signal: NodeJS.Signals | null; reason: string }) => void;
+ const logger = { trace: vi.fn(), debug: vi.fn(), info: vi.fn(), warn: vi.fn(),
+ error: vi.fn(), fatal: vi.fn(), child: vi.fn() };
+ const proc = new PiRpcProcess({
+ transport: {
+ pid: undefined, isClosed: () => false, close: async () => {},
+ writeLine: async () => {}, onLine: () => () => {},
+ onStderr: (handler) => { stderr = handler; return () => {}; },
+ onClose: (handler) => { exit = handler; return () => {}; },
+ }, logger, onEvent: vi.fn(), onExit: vi.fn(),
+ });
+ const error = proc.request({ type: 'get_state' }).catch((err: Error) => err);
+ stderr('Error: Failed to load extension placeholder');
+ stderr('Cannot find module "/Users/fake user/private-project/node_modules/placeholder"');
+ stderr(String.raw`from 'C:\Users\fake user\private-project\pi.exe'`);
+ stderr('from /Users/fake user/private-project/pi.exe');
+ stderr('/Users/alice/acme,secret/[private](one)/node_modules/bad.js: no such file');
+ stderr(String.raw`C:\Users\alice\acme,secret\[private](one)\bad.js: permission denied`);
+ stderr('/config.json: no such file');
+ stderr(' at internalLoader (/private/build/internal.ts:123:4)');
+ stderr('Authorization: Basic FAKEBASE64VALUE');
+ stderr('password="fake spaced password"');
+ stderr('sessionToken=FAKECUSTOMOPAQUEVALUE');
+ exit({ code: 1, signal: null, reason: 'unused raw transport reason' });
+ const result = await error as Error;
+ expect(result.message).toContain('Failed to load extension placeholder');
+ expect(result.message).toContain('[REDACTED]');
+ expect(result.message).not.toContain('FAKE');
+ expect(result.message).not.toContain('fake spaced password');
+ expect(result.message).toContain('<path:placeholder>');
+ expect(result.message).toContain('<path:pi.exe>');
+ expect(result.message).not.toContain('fake user');
+ expect(result.message).not.toContain('private-project');
+ expect(result.message).not.toContain('internalLoader');
+ expect(result.message).not.toContain('acme');
+ expect(result.message).not.toContain('secret/');
+ expect(result.message).not.toContain('[private]');
+ expect(result.message).toContain('<path:bad.js>: no such file');
+ expect(result.message).toContain('<path:bad.js>: permission denied');
+ expect(result.message).toContain('<path:config.json>: no such file');
+ });
+
+ it('keeps the generic fallback for a fresh process with no stderr', async () => {
+ const child = makeChild();
+ mocks.spawn.mockReturnValue(child);
+ const proc = createProcess();
+ const rejected = expect(proc.request({ type: 'get_state' })).rejects.toThrow(
+ /^pi process exited \(code=1, signal=null\)$/,
+ );
+ child.emit('close', 1, null);
+ await rejected;
+ });
+});
diff --git a/packages/maker-core/src/agents/pi/rpc-client.ts b/packages/maker-core/src/agents/pi/rpc-client.ts
index 51e509aabe..dae60ae0aa 100644
--- a/packages/maker-core/src/agents/pi/rpc-client.ts
+++ b/packages/maker-core/src/agents/pi/rpc-client.ts
@@ -13,6 +13,7 @@
* 本类只做 JSONL framing 之上的请求/响应关联与事件分发, 不感知字节流来源。
*/
+import { redactSensitiveText } from '@cindy/maker-shared/error-redaction';
import type { Logger } from '../../interfaces/logger.js';
import type { PiTransport } from './transport.js';
@@ -64,6 +65,7 @@ export interface PiRpcSpawnOptions {
}
const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
+const MAX_STARTUP_STDERR_CHARS = 2000;
// 轮 40-w4-t5 CRITICAL:key-aware 敏感字段名 —— 值形状正则覆盖不了 64-hex
// sessionToken / 自定义 MCP header 值, 字段名命中即整体替换。
@@ -88,6 +90,20 @@ function redactCredentialText(text: string): string {
return out;
}
+/** Error surfaces need the failing module name, not machine paths or stacks. */
+function sanitizeStartupDiagnostic(line: string): string {
+ if (/^\s*at(?:\s|$)/.test(line)) return '';
+ const pathLabel = (value: string): string => {
+ const name = value.split(/[\\/]/).filter(Boolean).pop() ?? '';
+ return `<path:${name}>`;
+ };
+ return line
+ .replace(/(["'])((?:file:\/\/\/|[A-Za-z]:[\\/]|\/|\\\\)[^"'\r\n]*)\1/g,
+ (_match, quote: string, value: string) => `${quote}${pathLabel(value)}${quote}`)
+ .replace(/(?<![\w:/\\])(?:file:\/\/\/|[A-Za-z]:[\\/]|\/|\\\\)[^\r\n"'<>]+?(?=:\s|["'<>\r\n]|$)/g,
+ (value) => pathLabel(value));
+}
+
/**
* 帧诊断日志的标签白名单:pi 协议的事件类型 / role / stopReason / 块类型都是短
* 标识符。扩展或畸形事件可能把任意正文塞进这些字段 —— 不符合标识符形态的一律
@@ -113,6 +129,11 @@ export class PiRpcProcess {
commandType: string;
}>();
private closed = false;
+ // Extension loading can fail before the first RPC response. Keep only a
+ // redacted, bounded tail until RPC becomes responsive, never running output.
+ private startupStderr = '';
+ private receivedRpcResponse = false;
+ private exitError: Error | null = null;
/**
* In-flight/successful close is shared. A failed close clears the gate so a
* later call can retry remote termination instead of reporting a false
@@ -137,12 +158,24 @@ export class PiRpcProcess {
if (line.trim().length === 0) return;
// 轮 40-w4-t5 CRITICAL:stderr 可能含 env 凭证(崩溃 dump/依赖 debug 输出),
// 进桌面日志前 key-aware 脱敏(值形状正则覆盖不了 64-hex sessionToken)。
- this.logger.warn('pi stderr', { line: redactCredentialText(line).slice(0, 2000) });
- opts.onStderrLine?.(redactCredentialText(line));
+ const redacted = redactCredentialText(redactSensitiveText(line));
+ this.logger.warn('pi stderr', { line: redacted.slice(0, 2000) });
+ if (!this.receivedRpcResponse && !this.closed) {
+ // Redact the whole line before truncating, so a credential straddling
+ // the tail boundary cannot lose its identifying prefix and leak.
+ const diagnostic = sanitizeStartupDiagnostic(redacted);
+ if (diagnostic) {
+ this.startupStderr = `${this.startupStderr}${this.startupStderr ? '\n' : ''}${diagnostic}`
+ .slice(-MAX_STARTUP_STDERR_CHARS);
+ }
+ }
+ opts.onStderrLine?.(redacted);
});
this.transport.onClose((info) => {
this.closed = true;
- this.failAllPending(new Error(`pi process exited (code=${info.code}, signal=${info.signal})`));
+ this.exitError = this.createExitError(`pi process exited (code=${info.code}, signal=${info.signal})`);
+ this.startupStderr = '';
+ this.failAllPending(this.exitError);
opts.onExit({ code: info.code, signal: info.signal });
});
this.transport.onOversizedFrame?.(() => this.failOversizedPending());
@@ -152,6 +185,12 @@ export class PiRpcProcess {
return this.transport.pid;
}
+ private createExitError(message: string): Error {
+ return new Error(this.startupStderr
+ ? `${message}\nPi startup stderr:\n${this.startupStderr}`
+ : message);
+ }
+
get isClosed(): boolean {
return this.closed || this.transport.isClosed();
}
@@ -167,7 +206,7 @@ export class PiRpcProcess {
refreshTimeoutOnEvent?: (event: PiRpcEvent) => boolean;
} = {},
): Promise<PiRpcResponse> {
- if (this.isClosed) throw new Error('pi process already exited');
+ if (this.isClosed) throw this.exitError ?? this.createExitError('pi process already exited');
const id = `c${this.nextRequestId++}`;
const payload = JSON.stringify({ ...command, id });
@@ -302,6 +341,8 @@ export class PiRpcProcess {
}
clearTimeout(entry.timer);
this.pending.delete(id);
+ this.receivedRpcResponse = true;
+ this.startupStderr = '';
entry.resolve(resp);
} else {
// 无 id 的响应(如 parse error)或迟到响应 —— 记日志不丢语义。