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(maker-core): bwrap 沙箱初始化失败的 exec 结果追加宿主归因(#3793) (#3799)

fico-hube5bf03c90fbe
diff --git a/packages/maker-core/src/agents/codex/sandbox-init-failure.test.ts b/packages/maker-core/src/agents/codex/sandbox-init-failure.test.ts
new file mode 100644
index 0000000000..d663b2e196
--- /dev/null
+++ b/packages/maker-core/src/agents/codex/sandbox-init-failure.test.ts
@@ -0,0 +1,92 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ CODEX_SANDBOX_INIT_FAILURE_NOTE,
+ annotateSandboxInitFailure,
+ commandInvokesBwrap,
+ isBwrapSandboxInitFailureOutput,
+} from './sandbox-init-failure.js';
+
+describe('isBwrapSandboxInitFailureOutput (#3793)', () => {
+ it('locks the reported RTM_NEWADDR init-failure shape', () => {
+ expect(isBwrapSandboxInitFailureOutput(
+ 'bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted\n',
+ )).toBe(true);
+ });
+
+ it('accepts multi-line pure bwrap diagnostics', () => {
+ expect(isBwrapSandboxInitFailureOutput([
+ 'bwrap: setting up uid map: Permission denied',
+ '',
+ 'bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted',
+ ].join('\n'))).toBe(true);
+ });
+
+ it('rejects output once any command line appears (command actually ran)', () => {
+ expect(isBwrapSandboxInitFailureOutput(
+ 'bwrap: some warning\nls: cannot access /x: No such file or directory\n',
+ )).toBe(false);
+ expect(isBwrapSandboxInitFailureOutput(
+ 'total 0\ndrwxr-xr-x 2 u u 40 .\n',
+ )).toBe(false);
+ });
+
+ it('rejects empty and null output', () => {
+ expect(isBwrapSandboxInitFailureOutput('')).toBe(false);
+ expect(isBwrapSandboxInitFailureOutput('\n \n')).toBe(false);
+ expect(isBwrapSandboxInitFailureOutput(null)).toBe(false);
+ expect(isBwrapSandboxInitFailureOutput(undefined)).toBe(false);
+ });
+});
+
+describe('commandInvokesBwrap', () => {
+ it('matches direct and pathed bwrap invocations', () => {
+ expect(commandInvokesBwrap('bwrap --ro-bind / / ls')).toBe(true);
+ expect(commandInvokesBwrap('/usr/bin/bwrap --help')).toBe(true);
+ expect(commandInvokesBwrap('sudo bwrap --unshare-net true')).toBe(true);
+ expect(commandInvokesBwrap('a && bwrap x')).toBe(true);
+ });
+
+ it('does not match commands that merely mention bwrap as data', () => {
+ expect(commandInvokesBwrap('ls bwrap.log')).toBe(false);
+ expect(commandInvokesBwrap('grep bwrapped notes.txt')).toBe(false);
+ expect(commandInvokesBwrap('pwd')).toBe(false);
+ expect(commandInvokesBwrap(null)).toBe(false);
+ });
+});
+
+describe('annotateSandboxInitFailure', () => {
+ // review P1:用户命令自身调用 bwrap 时,诊断行来自内层 bwrap,不得归因外层沙箱。
+ it('skips annotation when the failed command itself invokes bwrap', () => {
+ const text = 'bwrap: unknown option --bogus\n';
+ expect(annotateSandboxInitFailure(text, true, 'bwrap --bogus ls')).toBe(text);
+ });
+
+ it('still annotates a non-bwrap command with pure bwrap diagnostics', () => {
+ const out = annotateSandboxInitFailure(
+ 'bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted\n',
+ true,
+ 'ls',
+ );
+ expect(out).toContain(CODEX_SANDBOX_INIT_FAILURE_NOTE);
+ });
+
+ it('appends the host note for a failed init-failure item', () => {
+ const out = annotateSandboxInitFailure(
+ 'bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted\n',
+ true,
+ );
+ expect(out).toContain('Failed RTM_NEWADDR');
+ expect(out).toContain(CODEX_SANDBOX_INIT_FAILURE_NOTE);
+ });
+
+ it('leaves ordinary command failures untouched', () => {
+ const text = 'tsc: error TS2322: type mismatch\n';
+ expect(annotateSandboxInitFailure(text, true)).toBe(text);
+ });
+
+ it('never annotates successful items even with bwrap-looking output', () => {
+ const text = 'bwrap: some diagnostic\n';
+ expect(annotateSandboxInitFailure(text, false)).toBe(text);
+ });
+});
diff --git a/packages/maker-core/src/agents/codex/sandbox-init-failure.ts b/packages/maker-core/src/agents/codex/sandbox-init-failure.ts
new file mode 100644
index 0000000000..9075bc8161
--- /dev/null
+++ b/packages/maker-core/src/agents/codex/sandbox-init-failure.ts
@@ -0,0 +1,71 @@
+/**
+ * Codex 命令沙箱**初始化失败**的输出形状检测(#3793)。
+ *
+ * 受限 Linux 宿主(容器 / 禁用非特权 user namespace / 缺 CAP_NET_ADMIN)上,
+ * codex 的 bubblewrap 沙箱会在配置 namespace / loopback 阶段直接死掉
+ * (如 `bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted`),
+ * 用户命令从未执行。此时 exec item 以 failed 收尾,aggregatedOutput 里
+ * **只有** bwrap 自身的诊断行 —— 命令一旦真正跑起来,bwrap 即静默,输出
+ * 全部来自命令本身。据此把「全部非空行都是 `bwrap: ` 前缀」作为初始化
+ * 失败的判定形状:任何一行命令输出都会破坏该形状,不会误伤命令自身失败。
+ *
+ * 检出后在 tool_result 末尾追加宿主标注:模型不再对同一命令盲目重试,
+ * 用户在工具卡里能看到归因。恢复入口(重试按钮 / 切 Full access 确认)
+ * 属 renderer / 产品面,不在本模块范围。
+ */
+
+const BWRAP_LINE_PREFIX = 'bwrap: ';
+
+/** 判定形状只看头部若干行,防御异常超长输出(热路径仅 failed item 走到这里)。 */
+const MAX_SHAPE_LINES = 64;
+
+export function isBwrapSandboxInitFailureOutput(output: string | null | undefined): boolean {
+ if (!output) return false;
+ const lines = output.split('\n');
+ let nonEmpty = 0;
+ for (const rawLine of lines) {
+ const line = rawLine.trim();
+ if (line.length === 0) continue;
+ nonEmpty += 1;
+ if (nonEmpty > MAX_SHAPE_LINES) return false;
+ if (!line.startsWith(BWRAP_LINE_PREFIX)) return false;
+ }
+ return nonEmpty > 0;
+}
+
+/**
+ * 追加进 tool_result 的宿主标注。英文(模型消费面,与其余宿主注入一致);
+ * `[Cindy]` 前缀明示这是宿主归因而非命令输出。
+ */
+export const CODEX_SANDBOX_INIT_FAILURE_NOTE =
+ '[Cindy] The Codex command sandbox (bubblewrap) failed to initialize on this restricted Linux host — ' +
+ 'the command above was never executed. This typically means the host blocks unprivileged user ' +
+ 'namespaces or network namespace setup (CAP_NET_ADMIN). Retrying the same command will fail ' +
+ 'identically; do not retry. Tell the user the sandbox cannot start in this environment and that ' +
+ 'they can either switch this task to Full access (their explicit choice) or run Cindy on a host ' +
+ 'that permits sandbox namespaces.';
+
+/**
+ * 命令本身就在调用 bwrap 时不做归因:此时 `bwrap: ` 诊断行来自**用户命令内层**
+ * 的 bwrap(参数错误 / 嵌套沙箱被拒等),把它归因为 Codex 外层沙箱初始化失败并
+ * 建议切 Full access 是误导(review P1)。外层沙箱真的起不来时,任意命令都会
+ * 中招,下一条非 bwrap 命令仍会得到标注 —— 跳过的漏报代价可忽略。
+ */
+const COMMAND_INVOKES_BWRAP_RE = /(^|[\s/\\;|&('"`])bwrap(\s|$|['"`)])/;
+
+export function commandInvokesBwrap(command: string | null | undefined): boolean {
+ if (!command) return false;
+ return COMMAND_INVOKES_BWRAP_RE.test(command);
+}
+
+/** failed exec item 收口时对 fullText 的最终改写:检出初始化失败则追加标注。 */
+export function annotateSandboxInitFailure(
+ fullText: string,
+ isError: boolean,
+ command?: string | null,
+): string {
+ if (!isError || commandInvokesBwrap(command) || !isBwrapSandboxInitFailureOutput(fullText)) {
+ return fullText;
+ }
+ return `${fullText.trimEnd()}\n\n${CODEX_SANDBOX_INIT_FAILURE_NOTE}`;
+}
diff --git a/packages/maker-core/src/agents/codex/translator.test.ts b/packages/maker-core/src/agents/codex/translator.test.ts
index c7a2a16e4c..29eafb31b0 100644
--- a/packages/maker-core/src/agents/codex/translator.test.ts
+++ b/packages/maker-core/src/agents/codex/translator.test.ts
@@ -1536,6 +1536,67 @@ describe('translateItemNotification commandExecution output normalization', () =
isError: false,
});
});
+
+ // #3793:bwrap 沙箱初始化失败(命令从未执行)的 failed exec 追加宿主归因标注;
+ // 普通失败不受影响。
+ it('annotates a failed exec whose output is pure bwrap init diagnostics', async () => {
+ const rt = newCodexRuntimeState();
+ const q = createAsyncQueue<AgentEvent>();
+ const ctx = makeCtx(rt);
+
+ translateItemNotification(
+ 'completed',
+ {
+ threadId: 'thread-1',
+ turnId: 'turn-1',
+ item: {
+ type: 'commandExecution',
+ id: 'cmd-sandbox',
+ command: 'ls',
+ status: 'failed',
+ aggregatedOutput: 'bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted\n',
+ exitCode: 1,
+ },
+ },
+ q,
+ ctx,
+ );
+
+ const events = await collect(q);
+ const full = events.find((event) => event.type === 'tool_result_full');
+ const fullText = (full?.data as { fullText?: string }).fullText ?? '';
+ expect(fullText).toContain('Failed RTM_NEWADDR');
+ expect(fullText).toContain('[Cindy] The Codex command sandbox (bubblewrap) failed to initialize');
+ expect(full?.data).toMatchObject({ isError: true });
+ });
+
+ it('does not annotate an ordinary failed exec', async () => {
+ const rt = newCodexRuntimeState();
+ const q = createAsyncQueue<AgentEvent>();
+ const ctx = makeCtx(rt);
+
+ translateItemNotification(
+ 'completed',
+ {
+ threadId: 'thread-1',
+ turnId: 'turn-1',
+ item: {
+ type: 'commandExecution',
+ id: 'cmd-fail',
+ command: 'tsc',
+ status: 'failed',
+ aggregatedOutput: 'error TS2322: type mismatch\n',
+ exitCode: 2,
+ },
+ },
+ q,
+ ctx,
+ );
+
+ const events = await collect(q);
+ const full = events.find((event) => event.type === 'tool_result_full');
+ expect((full?.data as { fullText?: string }).fullText).toBe('error TS2322: type mismatch\n');
+ });
});
describe('translateItemNotification collabAgentToolCall', () => {
diff --git a/packages/maker-core/src/agents/codex/translator.ts b/packages/maker-core/src/agents/codex/translator.ts
index db1bbd4639..197f62281b 100644
--- a/packages/maker-core/src/agents/codex/translator.ts
+++ b/packages/maker-core/src/agents/codex/translator.ts
@@ -47,6 +47,7 @@ import {
isContextOverflowErrorMessage,
} from '../shared/context-overflow-error.js';
import { commandExecutionDisplayInput, type CommandExecutionDisplayInput } from './command-display.js';
+import { annotateSandboxInitFailure } from './sandbox-init-failure.js';
import { codexErrorInfoTag } from './app-server/protocol.js';
import {
formatTerminalRateLimitRetryMessage,
@@ -1405,7 +1406,13 @@ function handleCommandExecution(
type: 'tool_result_full',
data: {
toolUseId: item.id,
- fullText: stripTerminalControlSequences(item.aggregatedOutput ?? ''),
+ // #3793:bwrap 沙箱初始化失败(命令从未执行)时追加宿主归因标注,
+ // 模型不再盲目重试,用户在工具卡里能看到原因。健康与普通失败路径原样。
+ fullText: annotateSandboxInitFailure(
+ stripTerminalControlSequences(item.aggregatedOutput ?? ''),
+ isError,
+ item.command,
+ ),
isError,
},
source: 'codex',