fix(codex): preserve workspace and tool failures

This commit is contained in:
celanwang 2026-10-29 14:15:31 +08:00 • committed by Cursor Agent
parent 1b4e5198dc
commit 9954d63bc5
No known key found for this signature in database
5 changed files with 70 additions and 13 deletions

View file

@ -101,9 +101,10 @@ startup in multi-workspace configurations.
The binding propagates through nested job calls and agent tool calls (`job_tools`) as a server-owned constraint
that models cannot override. For Codex with multiple workspaces, `job_tools` requires the resident HTTP service
with MCP enabled, and the selected jobs must be exposed by that service. Its STDIO bridge forwards to this service
without creating another Application. The multi-workspace one-shot CLI combination is not supported; `mcp_config`
retains its legacy meaning only for single-workspace Codex job tools.
with MCP enabled and `tool_error_on_failure: true`; the selected jobs must be exposed by that service. Its STDIO
bridge forwards to this service without creating another Application and preserves failed Jobs as tool errors.
The multi-workspace one-shot CLI combination is not supported; `mcp_config` retains its legacy meaning only for
single-workspace Codex job tools.
Background and cron jobs bind through their job configuration, so extra workspaces are watched by declaring
explicit per-alias variants:

View file

@ -102,9 +102,10 @@ Job 调用接受可选的 `workspace` 字段指定绑定的别名,例如
在多 workspace 配置下会在启动时被拒绝。
绑定会经嵌套 Job 调用和 agent 工具调用(`job_tools`)传播,作为模型无法覆盖的服务端约束。
多 workspace 下的 Codex `job_tools` 需要启用 MCP 的常驻 HTTP 服务,且所选 Job 必须由该服务暴露;
其 STDIO bridge 转发到该服务,不会创建另一个 Application。多 workspace 与 one-shot CLI 的组合暂不支持;
`mcp_config` 仅在单 workspace 的 Codex job tools 中保留原有语义。
多 workspace 下的 Codex `job_tools` 需要启用 MCP 且配置 `tool_error_on_failure: true` 的常驻 HTTP 服务,
且所选 Job 必须由该服务暴露;其 STDIO bridge 转发到该服务,不会创建另一个 Application,并会将失败的
Job 保留为 tool error。多 workspace 与 one-shot CLI 的组合暂不支持;`mcp_config` 仅在单 workspace 的
Codex job tools 中保留原有语义。
Background 和 cron Job 通过 Job 配置绑定别名,额外 workspace 的 watch 通过显式配置按别名的变体实现:

View file

@ -257,6 +257,8 @@ class CodexAgentWrapper(BaseAgentWrapper):
@staticmethod
def _validate_resident_jobs(service, jobs) -> None:
if not service.tool_error_on_failure:
raise ValueError("Multi-workspace Codex job_tools require resident MCP tool_error_on_failure=true")
unavailable = [
job.name
for job in jobs
@ -293,11 +295,14 @@ class CodexAgentWrapper(BaseAgentWrapper):
"--workspace",
str(self.workspace_path),
]
if workspaces:
alias = str(kwargs.get("workspace") or next(iter(workspaces)))
args.extend(["--workspace-alias", alias])
for name in job_names:
args.extend(["--job", name])
args.extend(["--tool-context-id", str(kwargs.get("tool_context_id") or "")])
injected_job_kwargs = dict(kwargs.get("injected_job_kwargs") or {})
if multi_workspace and (workspace := kwargs.get("workspace")):
if workspaces and (workspace := kwargs.get("workspace")):
injected_job_kwargs["workspace"] = workspace
if injected_job_kwargs:
args.extend(["--injected-job-kwargs", json.dumps(injected_job_kwargs, sort_keys=True)])

View file

@ -20,6 +20,7 @@ def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Expose selected ReMe jobs over FastMCP STDIO")
parser.add_argument("--config", default="default", help="ReMe config name or file path")
parser.add_argument("--workspace", help="Single-workspace directory for the legacy child service")
parser.add_argument("--workspace-alias", help="Configured alias retained by the legacy child service")
parser.add_argument("--endpoint", help="Resident streamable-HTTP MCP endpoint for forwarding")
parser.add_argument("--job", dest="jobs", action="append", required=True, help="ReMe job name; repeat as needed")
parser.add_argument("--tool-context-id", default="", help="Context id injected into every job call")
@ -161,9 +162,11 @@ def main() -> None:
return
if not args.workspace:
raise ValueError("--workspace is required when --endpoint is not supplied")
workspace = str(Path(args.workspace).absolute())
workspace_dir = {args.workspace_alias: workspace} if args.workspace_alias else workspace
config = resolve_app_config(
config=args.config,
workspace_dir=str(Path(args.workspace).absolute()),
workspace_dir=workspace_dir,
enable_logo=False,
log_to_console=False,
log_to_file=False,

View file

@ -331,7 +331,7 @@ async def test_codex_bridge_uses_resident_mcp_endpoint(tmp_path):
from reme.components.service import HttpService
context = _context(tmp_path)
context.service = HttpService(app_context=context)
context.service = HttpService(app_context=context, tool_error_on_failure=True)
context.jobs = {"search_job": _job(context, "search_job")}
wrapper = CodexAgentWrapper(app_context=context)
@ -452,13 +452,21 @@ async def test_codex_thread_cwd_follows_bound_workspace(tmp_path):
# ----- Codex bridge: real child-process execution ----------------------------
def _codex_test_app(tmp_path):
def _codex_test_app(tmp_path, workspace_dir=None):
if workspace_dir is None:
workspace_dir = {"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")}
return Application(
workspace_dir={"alpha": str(tmp_path / "alpha"), "beta": str(tmp_path / "beta")},
workspace_dir=workspace_dir,
enable_logo=False,
log_to_console=False,
log_to_file=False,
service={"backend": "http", "host": "127.0.0.1", "port": 0, "web_enabled": False},
service={
"backend": "http",
"host": "127.0.0.1",
"port": 0,
"web_enabled": False,
"tool_error_on_failure": True,
},
components={
"tokenizer": {"default": {"backend": "regex"}},
"file_store": {"default": {"backend": "local", "embedding_store": "", "tag_index": ""}},
@ -551,6 +559,13 @@ async def test_codex_bridge_reads_resident_parent_state(tmp_path):
client.call_tool("write_note", {"path": "daily/bridge.md", "content": "via-bridge-beta"}),
timeout=30,
)
failure = await client.call_tool(
"write_note",
{"path": "../escape.md", "content": "must-not-write"},
raise_on_error=False,
)
assert failure.is_error
assert not (tmp_path / "escape.md").exists()
assert "parent-resident-beta" in str(read.content[0].text)
assert "via-bridge-beta" in (tmp_path / "beta" / "daily" / "bridge.md").read_text(encoding="utf-8")
assert not (tmp_path / "alpha" / "daily" / "bridge.md").exists()
@ -573,9 +588,38 @@ async def test_codex_bridge_reads_resident_parent_state(tmp_path):
await wrapper.close()
@pytest.mark.asyncio
@pytest.mark.parametrize("named_alias", [False, True])
async def test_codex_legacy_bridge_preserves_single_workspace_binding(tmp_path, named_alias):
from fastmcp import Client
from fastmcp.client import StdioTransport
alias = "alice" if named_alias else "default"
root = tmp_path / alias
workspace_dir = {alias: str(root)} if named_alias else str(root)
app = _codex_test_app(tmp_path, workspace_dir=workspace_dir)
wrapper = CodexAgentWrapper(app_context=app.context)
server_config = wrapper._mcp_server_config({"job_tools": ["write_note"], "workspace": alias})
transport = StdioTransport(
command=server_config["command"],
args=server_config["args"],
cwd=server_config["cwd"],
keep_alive=False,
env=dict(os.environ),
)
async with Client(transport, timeout=None, init_timeout=30) as client:
await asyncio.wait_for(
client.call_tool("write_note", {"path": "daily/legacy.md", "content": alias}),
timeout=30,
)
assert alias in (root / "daily" / "legacy.md").read_text(encoding="utf-8")
await wrapper.close()
@pytest.mark.asyncio
async def test_codex_multi_workspace_rejects_unsupported_job_transport(tmp_path):
from reme.components.service import CliService
from reme.components.service import CliService, HttpService
context = _context(tmp_path)
context.service = CliService(app_context=context)
@ -588,4 +632,7 @@ async def test_codex_multi_workspace_rejects_unsupported_job_transport(tmp_path)
wrapper._mcp_server_config(
{"job_tools": ["search_job"], "workspace": "beta", "mcp_config": "custom.yaml"},
)
context.service = HttpService(app_context=context, tool_error_on_failure=False)
with pytest.raises(ValueError, match="tool_error_on_failure=true"):
wrapper._mcp_server_config({"job_tools": ["search_job"], "workspace": "beta"})
await wrapper.close()