chore: import upstream snapshot with attribution
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""Tests for the in-memory session storage backend."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from core.agent_harness.session import InMemorySessionStorage
|
||||
from surfaces.interactive_shell.session import Session
|
||||
|
||||
|
||||
def _session(storage: InMemorySessionStorage) -> Session:
|
||||
return Session(storage=storage)
|
||||
|
||||
|
||||
def test_open_then_record_appends_turn() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
session.record("chat", "hello world")
|
||||
|
||||
records = storage.read(session.session_id)
|
||||
assert records[0]["type"] == "session"
|
||||
assert records[0]["version"] == 2
|
||||
turns = [r for r in records if r["type"] == "custom_message"]
|
||||
assert turns[0]["custom_type"] == "turn_stub"
|
||||
assert turns[0]["kind"] == "chat"
|
||||
assert turns[0]["text"] == "hello world"
|
||||
|
||||
|
||||
def test_record_noop_when_not_opened() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
session.record("chat", "hi") # no open_session
|
||||
assert storage.read(session.session_id) == []
|
||||
|
||||
|
||||
def test_flush_writes_session_end_with_counts() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
session.record("chat", "q1")
|
||||
session.record("alert", "boom")
|
||||
storage.flush(session)
|
||||
|
||||
leaf = storage.read(session.session_id)[-1]
|
||||
assert leaf["type"] == "leaf"
|
||||
assert leaf["total_turns"] == 2
|
||||
|
||||
|
||||
def test_flush_deletes_empty_session() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
storage.flush(session)
|
||||
assert storage.read(session.session_id) == []
|
||||
|
||||
|
||||
def test_flush_is_idempotent() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
session.record("chat", "hi")
|
||||
storage.flush(session)
|
||||
storage.flush(session)
|
||||
leaves = [r for r in storage.read(session.session_id) if r["type"] == "leaf"]
|
||||
assert len(leaves) == 1
|
||||
|
||||
|
||||
def test_append_turn_detail_writes_message_entries() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
storage.append_turn_detail(session.session_id, "chat", "hello", response="hi")
|
||||
|
||||
records = storage.read(session.session_id)
|
||||
messages = [r for r in records if r["type"] == "message"]
|
||||
assert [(r["role"], r["content"]) for r in messages] == [("user", "hello"), ("assistant", "hi")]
|
||||
|
||||
|
||||
def test_append_tool_call_reopens_finalized_session() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
session.record("chat", "do a thing")
|
||||
storage.flush(session)
|
||||
storage.append_tool_call(session.session_id, tool="t", arguments={}, result="{}", ok=True)
|
||||
|
||||
records = storage.read(session.session_id)
|
||||
assert any(r["type"] == "tool_call" for r in records)
|
||||
assert any(r["type"] == "tool_result" for r in records)
|
||||
|
||||
|
||||
def test_append_investigation_result_returns_id() -> None:
|
||||
storage = InMemorySessionStorage()
|
||||
session = _session(storage)
|
||||
storage.open_session(session)
|
||||
inv_id = storage.append_investigation_result(
|
||||
session.session_id, {"root_cause": "leak", "problem_md": "report"}, trigger="t"
|
||||
)
|
||||
inv = next(r for r in storage.read(session.session_id) if r["type"] == "investigation_result")
|
||||
assert inv["investigation_id"] == inv_id
|
||||
assert inv["root_cause"] == "leak"
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Shell-session invariants: the ``Session`` subclass and its terminal/alerts facets.
|
||||
|
||||
Pins that the shell ``Session`` composes ``SessionCore`` plus the ``terminal`` and
|
||||
``alerts`` facets, that each relocated field cluster lives on its facet, and that the
|
||||
facet delegation (analytics staging, incoming-alert cap) behaves. Core-only invariants
|
||||
live in ``tests/core/agent_harness/session/test_session_characterization.py``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
|
||||
from core.agent_harness.session.persistence.memory import InMemorySessionStorage
|
||||
from core.agent_harness.session.session_core import SessionCore
|
||||
from core.domain.alerts.inbox import IncomingAlert
|
||||
from surfaces.interactive_shell.session.session import Session
|
||||
|
||||
# Core fields are inherited from SessionCore; the shell adds these two facets.
|
||||
_CORE_FIELD_COUNT = 19
|
||||
_FACET_FIELDS = ("alerts", "terminal")
|
||||
|
||||
|
||||
def _session() -> Session:
|
||||
return Session(storage=InMemorySessionStorage())
|
||||
|
||||
|
||||
def test_session_is_a_session_core_with_two_facets() -> None:
|
||||
assert issubclass(Session, SessionCore)
|
||||
field_names = {f.name for f in dataclasses.fields(Session)}
|
||||
assert "terminal" in field_names
|
||||
assert "alerts" in field_names
|
||||
assert len(field_names) == _CORE_FIELD_COUNT + len(_FACET_FIELDS)
|
||||
|
||||
|
||||
def test_alert_inbox_facet_holds_the_relocated_alert_state() -> None:
|
||||
inbox = _session().alerts
|
||||
assert hasattr(inbox, "entries") # was Session.incoming_alerts
|
||||
assert hasattr(inbox, "_max") # was Session._INCOMING_ALERTS_MAX
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_theme_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in ("active_theme_name", "pending_theme_refresh", "trust_mode"):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_prompt_toolkit_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in (
|
||||
"prompt_history_backend",
|
||||
"prompt_app",
|
||||
"main_loop",
|
||||
"prompt_refresh_fn",
|
||||
"fleet_sampler_starter",
|
||||
):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_pending_prompt_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in (
|
||||
"pending_prompt_default",
|
||||
"pending_prompt_autosubmit",
|
||||
"exclusive_stdin_active",
|
||||
"agent_turn_executed_slashes",
|
||||
):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_background_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in (
|
||||
"background_mode_enabled",
|
||||
"background_investigations",
|
||||
"background_notification_preferences",
|
||||
"background_notices",
|
||||
"_background_notices_lock",
|
||||
):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_metrics_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in ("metrics", "history_generation"):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_terminal_facet_holds_the_analytics_staging_cluster() -> None:
|
||||
terminal = _session().terminal
|
||||
for f in ("_turn_outcome_hint", "_pending_turn_llm", "_pending_turn_error"):
|
||||
assert hasattr(terminal, f)
|
||||
|
||||
|
||||
def test_analytics_staging_pop_methods_consume_exactly_once() -> None:
|
||||
terminal = _session().terminal
|
||||
terminal.set_turn_outcome_hint("handled")
|
||||
assert terminal.pop_turn_outcome_hint() == "handled"
|
||||
assert terminal.pop_turn_outcome_hint() is None
|
||||
terminal.set_turn_outcome_hint(" ")
|
||||
assert terminal.pop_turn_outcome_hint() is None
|
||||
|
||||
|
||||
def test_incoming_alerts_are_capped_and_drop_oldest_first() -> None:
|
||||
session = _session()
|
||||
cap = session.alerts._max
|
||||
for i in range(cap + 5):
|
||||
session.record_incoming_alert(IncomingAlert(text=f"alert-{i}"))
|
||||
assert len(session.alerts.entries) == cap
|
||||
assert session.alerts.entries[0].text == "alert-5"
|
||||
assert session.alerts.entries[-1].text == f"alert-{cap + 4}"
|
||||
Reference in New Issue
Block a user