File size: 2,156 Bytes
63c75d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
"""Standalone smoke test for session snapshot generation.

This intentionally avoids pytest so the sidecar can be checked in constrained
gateway/runtime shells where pytest is not installed.
"""
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from reviewer import session_snapshot as snap


def main() -> int:
    summary = {
        "session_id": "s1",
        "agent_id": "ops",
        "last_event_at": "2026-05-12T12:00:00+00:00",
        "event_count": 4,
        "tool_result_count": 2,
        "error_count": 1,
        "noisy_tool_results": 0,
        "last_entry_idx": 4,
        "hints": ["1 error(s)"],
    }
    activity = [
        {"session_id": "s1", "agent_id": "ops", "entry_idx": 1, "role": "user", "preview": "deploy to Cloud Run?", "clean_text": "deploy to Cloud Run?", "is_error": 0},
        {"session_id": "s1", "agent_id": "ops", "entry_idx": 2, "role": "toolResult", "tool_name": "exec", "preview": "permission denied", "clean_text": "permission denied", "is_error": 1},
    ]
    original_activity = snap.get_session_activity
    original_recent = snap.get_recent_sessions
    try:
        snap.get_session_activity = lambda session_id, limit: activity
        result = snap.build_session_snapshot(summary, activity_limit=20)
        assert result["schema"] == "openclaw.session.v1"
        assert result["id"] == "s1"
        assert result["state"]["state"] == "warning"
        assert "approval_or_permission" in result["risk"]["flags"]
        assert "external_infra" in result["risk"]["flags"]
        assert result["outputs"]["recent_events"][1]["event_type"] == "tool_error"

        snap.get_recent_sessions = lambda limit: [summary]
        collection = snap.build_recent_session_snapshots(limit=1, activity_limit=1)
        assert collection["schema"] == "openclaw.session.v1.collection"
        assert collection["count"] == 1
    finally:
        snap.get_session_activity = original_activity
        snap.get_recent_sessions = original_recent
    print("session_snapshot_smoke: ok")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())