Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

test(oc2ov): fail fast on empty OpenClaw output (#3250)

huangruiteng6de1cd2c6e74
diff --git a/.github/workflows/oc2ov_test.yml b/.github/workflows/oc2ov_test.yml
index 081c5e9f0..2f61cd3fd 100644
--- a/.github/workflows/oc2ov_test.yml
+++ b/.github/workflows/oc2ov_test.yml
@@ -128,6 +128,12 @@ jobs:
fi
done
+ echo "Checking a real OpenClaw JSON response..."
+ openclaw --version || true
+ cd tests/oc2ov_test
+ python3 utils/openclaw_cli_smoke.py \
+ --session-id "oc2ov-ci-smoke-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
+
- name: Install test dependencies
run: |
echo "📦 Setting up Python virtual environment..."
diff --git a/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py b/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
index 597be7299..0fe0db2cc 100644
--- a/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
+++ b/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
@@ -19,6 +19,7 @@ from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
import requests
+from utils.cli_diagnostics import format_openclaw_cli_failure
from utils.openclaw_cli_client import _wait_for_session_lock_release
from utils.test_utils import SessionIdManager
@@ -377,7 +378,18 @@ class MemoryV2TestSuite:
_wait_for_session_lock_release(session_id)
if result.returncode != 0:
- raise Exception(f"OpenClaw command failed: {result.stderr}")
+ raise Exception(
+ format_openclaw_cli_failure(
+ "OpenClaw command failed", result.stderr, result.returncode
+ )
+ )
+
+ if not result.stdout.strip():
+ raise Exception(
+ format_openclaw_cli_failure(
+ "OpenClaw command returned empty stdout", result.stderr, result.returncode
+ )
+ )
try:
return json.loads(result.stdout)
diff --git a/tests/oc2ov_test/tests/test_cli_diagnostics.py b/tests/oc2ov_test/tests/test_cli_diagnostics.py
new file mode 100644
index 000000000..e1d04c13a
--- /dev/null
+++ b/tests/oc2ov_test/tests/test_cli_diagnostics.py
@@ -0,0 +1,54 @@
+import subprocess
+from unittest.mock import patch
+
+from cli_diagnostics import compact_subprocess_diagnostic
+from openclaw_cli_client import OpenClawCLIClient
+from openclaw_cli_smoke import main, run_smoke
+
+
+def test_compact_subprocess_diagnostic_redacts_and_truncates():
+ value = "api_key=super-secret Bearer bearer-secret sk-1234567890 " + ("x" * 1000)
+
+ diagnostic = compact_subprocess_diagnostic(value, limit=80)
+
+ assert "super-secret" not in diagnostic
+ assert "bearer-secret" not in diagnostic
+ assert "sk-1234567890" not in diagnostic
+ assert diagnostic.endswith("...<truncated>")
+
+
+@patch("openclaw_cli_client._wait_for_session_lock_release", return_value=True)
+@patch("openclaw_cli_client.subprocess.run")
+def test_client_reports_safe_stderr_when_stdout_is_empty(run, _wait):
+ run.return_value = subprocess.CompletedProcess(
+ args=[], returncode=0, stdout="", stderr="token=private-value provider unavailable"
+ )
+
+ result = OpenClawCLIClient().send_message("hello")
+
+ assert result["success"] is False
+ assert "命令返回空输出" in result["error"]
+ assert "token=<redacted>" in result["error"]
+ assert "private-value" not in result["error"]
+
+
+@patch("openclaw_cli_smoke.subprocess.run")
+def test_smoke_rejects_empty_stdout_with_safe_diagnostic(run):
+ run.return_value = subprocess.CompletedProcess(
+ args=[], returncode=0, stdout="", stderr="Authorization: hidden-value"
+ )
+
+ try:
+ run_smoke("test-session")
+ except RuntimeError as exc:
+ assert "empty stdout" in str(exc)
+ assert "Authorization: <redacted>" in str(exc)
+ assert "hidden-value" not in str(exc)
+ else:
+ raise AssertionError("run_smoke should reject empty stdout")
+
+
+@patch("openclaw_cli_smoke.run_smoke", side_effect=RuntimeError("bounded failure"))
+def test_smoke_main_returns_nonzero_and_emits_action_error(_run, capsys):
+ assert main(["--session-id", "test-session"]) == 1
+ assert capsys.readouterr().out.strip() == "::error::bounded failure"
diff --git a/tests/oc2ov_test/utils/cli_diagnostics.py b/tests/oc2ov_test/utils/cli_diagnostics.py
new file mode 100644
index 000000000..790d364e7
--- /dev/null
+++ b/tests/oc2ov_test/utils/cli_diagnostics.py
@@ -0,0 +1,37 @@
+"""Public-safe diagnostics for OpenClaw subprocess failures."""
+
+from __future__ import annotations
+
+import re
+
+MAX_DIAGNOSTIC_CHARS = 800
+
+_SECRET_PATTERNS = (
+ (re.compile(r"(?i)\b(bearer)\s+\S+"), r"\1 <redacted>"),
+ (
+ re.compile(
+ r"(?i)\b(api[_-]?key|authorization|access[_-]?token|token|secret)"
+ r"(\s*[:=]\s*)([^\s,;]+)"
+ ),
+ r"\1\2<redacted>",
+ ),
+ (re.compile(r"\bsk-[A-Za-z0-9_-]{8,}\b"), "<redacted>"),
+)
+
+
+def compact_subprocess_diagnostic(value: str, limit: int = MAX_DIAGNOSTIC_CHARS) -> str:
+ """Redact common credential shapes and bound subprocess output for CI logs."""
+ compact = " ".join((value or "").split())
+ for pattern, replacement in _SECRET_PATTERNS:
+ compact = pattern.sub(replacement, compact)
+ if not compact:
+ return "<empty>"
+ if len(compact) <= limit:
+ return compact
+ return f"{compact[:limit]}...<truncated>"
+
+
+def format_openclaw_cli_failure(message: str, stderr: str, returncode: int) -> str:
+ """Build a bounded error without copying raw OpenClaw stderr."""
+ diagnostic = compact_subprocess_diagnostic(stderr)
+ return f"{message} (exit={returncode}; stderr={diagnostic})"
diff --git a/tests/oc2ov_test/utils/openclaw_cli_client.py b/tests/oc2ov_test/utils/openclaw_cli_client.py
index 13309057f..af1c5ee80 100644
--- a/tests/oc2ov_test/utils/openclaw_cli_client.py
+++ b/tests/oc2ov_test/utils/openclaw_cli_client.py
@@ -10,6 +10,11 @@ import subprocess
import time
from typing import Any, Dict, Optional
+try:
+ from .cli_diagnostics import format_openclaw_cli_failure
+except ImportError: # pragma: no cover - direct test utility import
+ from cli_diagnostics import format_openclaw_cli_failure
+
logger = logging.getLogger(__name__)
OPENCLAW_HOME = os.path.expanduser("~/.openclaw")
@@ -103,14 +108,18 @@ class OpenClawCLIClient:
_wait_for_session_lock_release(target_session_id)
if result.returncode != 0:
- error_msg = f"命令执行失败: {result.stderr}"
+ error_msg = format_openclaw_cli_failure(
+ "命令执行失败", result.stderr, result.returncode
+ )
logger.error("=" * 80)
logger.error(error_msg)
logger.error("=" * 80)
return {"error": error_msg, "success": False}
if not result.stdout.strip():
- error_msg = "命令返回空输出"
+ error_msg = format_openclaw_cli_failure(
+ "命令返回空输出", result.stderr, result.returncode
+ )
logger.error("=" * 80)
logger.error(error_msg)
logger.error("=" * 80)
diff --git a/tests/oc2ov_test/utils/openclaw_cli_smoke.py b/tests/oc2ov_test/utils/openclaw_cli_smoke.py
new file mode 100644
index 000000000..c00eda489
--- /dev/null
+++ b/tests/oc2ov_test/utils/openclaw_cli_smoke.py
@@ -0,0 +1,68 @@
+"""Fail-fast smoke for the OpenClaw JSON command used by OC2OV P0 tests."""
+
+from __future__ import annotations
+
+import argparse
+import json
+import subprocess
+from typing import Sequence
+
+try:
+ from .cli_diagnostics import compact_subprocess_diagnostic, format_openclaw_cli_failure
+except ImportError: # pragma: no cover - direct workflow script execution
+ from cli_diagnostics import compact_subprocess_diagnostic, format_openclaw_cli_failure
+
+
+def run_smoke(session_id: str, timeout: int = 180) -> None:
+ command = [
+ "openclaw",
+ "agent",
+ "--session-id",
+ session_id,
+ "--message",
+ "Reply exactly READY.",
+ "--json",
+ ]
+ result = subprocess.run(command, capture_output=True, text=True, timeout=timeout)
+ if result.returncode != 0:
+ raise RuntimeError(
+ format_openclaw_cli_failure(
+ "OpenClaw JSON smoke command failed", result.stderr, result.returncode
+ )
+ )
+
+ stdout = result.stdout.strip()
+ if not stdout:
+ raise RuntimeError(
+ format_openclaw_cli_failure(
+ "OpenClaw JSON smoke returned empty stdout", result.stderr, result.returncode
+ )
+ )
+
+ try:
+ json.loads(stdout)
+ except json.JSONDecodeError as exc:
+ diagnostic = compact_subprocess_diagnostic(stdout)
+ raise RuntimeError(
+ f"OpenClaw JSON smoke returned invalid JSON: {diagnostic}"
+ ) from exc
+
+
+def main(argv: Sequence[str] | None = None) -> int:
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--session-id", required=True)
+ parser.add_argument("--timeout", type=int, default=180)
+ args = parser.parse_args(argv)
+
+ try:
+ run_smoke(args.session_id, timeout=args.timeout)
+ except (OSError, subprocess.TimeoutExpired, RuntimeError) as exc:
+ print(f"::error::{exc}")
+ return 1
+
+ print("OpenClaw JSON smoke passed")
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())