Files
wehub-resource-sync 4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

119 lines
3.8 KiB
Python

"""Tests for optional ``@traceable`` — free when session tracing is inactive."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
from core.agent_harness.session.persistence.jsonl_storage import JsonlSessionStorage
from platform.observability.trace.hook import traceable
from platform.observability.trace.spans import (
NoopSessionTraceSink,
bind_session_trace,
set_session_trace_sink,
)
from surfaces.interactive_shell.session.trace_sink import JsonlSessionTraceSink
@pytest.fixture(autouse=True)
def _reset_session_trace_sink() -> Any:
set_session_trace_sink(NoopSessionTraceSink())
yield
set_session_trace_sink(NoopSessionTraceSink())
def test_traceable_is_near_free_passthrough_when_noop() -> None:
@traceable("investigation")
def traced_function() -> str:
return "ok"
assert traced_function() == "ok"
assert traced_function.__name__ == "traced_function"
# Wrapper exists so an active sink can emit spans, but call semantics
# stay identical when the default noop sink is registered.
assert callable(traced_function)
def test_traceable_preserves_args_kwargs_return_value_and_metadata() -> None:
@traceable("span-name")
def traced_function(value: int, *, suffix: str) -> str:
"""Original docstring."""
return f"{value}{suffix}"
assert traced_function(7, suffix="ms") == "7ms"
assert traced_function.__name__ == "traced_function"
assert traced_function.__doc__ == "Original docstring."
def test_traceable_defaults_name_to_qualname_when_blank() -> None:
@traceable("")
def named_fn() -> str:
return "x"
assert named_fn() == "x"
def test_traceable_emits_component_span_when_sink_active(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(
"core.agent_harness.session.persistence.jsonl_storage.session_path",
lambda session_id: tmp_path / f"{session_id}.jsonl",
)
storage = JsonlSessionStorage()
session_id = "sess-traceable"
path = tmp_path / f"{session_id}.jsonl"
path.write_text(
json.dumps({"type": "session", "version": 2, "id": session_id}) + "\n",
encoding="utf-8",
)
set_session_trace_sink(JsonlSessionTraceSink(storage=storage))
@traceable(name="investigation")
def run_investigation() -> str:
return "done"
with bind_session_trace(session_id):
assert run_investigation() == "done"
lines = [json.loads(line) for line in path.read_text(encoding="utf-8").strip().splitlines()]
kinds = {(rec["span_kind"], rec["name"]) for rec in lines if rec.get("type") == "trace_span"}
assert ("component", "investigation") in kinds
def test_traceable_marks_error_status_when_callable_raises(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(
"core.agent_harness.session.persistence.jsonl_storage.session_path",
lambda session_id: tmp_path / f"{session_id}.jsonl",
)
storage = JsonlSessionStorage()
session_id = "sess-traceable-err"
path = tmp_path / f"{session_id}.jsonl"
path.write_text(
json.dumps({"type": "session", "version": 2, "id": session_id}) + "\n",
encoding="utf-8",
)
set_session_trace_sink(JsonlSessionTraceSink(storage=storage))
@traceable("failing_component")
def boom() -> None:
raise ValueError("nope")
with bind_session_trace(session_id), pytest.raises(ValueError, match="nope"):
boom()
spans = [
json.loads(line)
for line in path.read_text(encoding="utf-8").strip().splitlines()
if json.loads(line).get("type") == "trace_span"
]
assert len(spans) == 1
assert spans[0]["span_kind"] == "component"
assert spans[0]["name"] == "failing_component"
assert spans[0]["status"] == "error"