4b6817381b
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
71 lines
2.8 KiB
Python
71 lines
2.8 KiB
Python
"""Filesystem helpers shared by the JSONL session storage and repository.
|
|
|
|
One JSONL file per session lives under ``~/.opensre/sessions/``. Both the
|
|
per-session storage writer and the cross-session repository resolve paths and
|
|
derive display names through these helpers so the on-disk layout has a single
|
|
owner.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from core.agent_harness.session.persistence.ports import CHAT_KINDS
|
|
|
|
_NAME_MAX_CHARS = 50
|
|
|
|
|
|
def sessions_dir() -> Path:
|
|
from config.constants import OPENSRE_HOME_DIR
|
|
|
|
return OPENSRE_HOME_DIR / "sessions"
|
|
|
|
|
|
def session_path(session_id: str) -> Path:
|
|
return sessions_dir() / f"{session_id}.jsonl"
|
|
|
|
|
|
def derive_name(lines: list[str]) -> str:
|
|
"""Derive a human-readable session name from the first substantive turn.
|
|
|
|
Prefers turn_detail.prompt (full text) over the turn stub. Falls back
|
|
to the empty string if no usable turn exists.
|
|
"""
|
|
# Prefer v2 message entries.
|
|
for line in lines[1:]:
|
|
with contextlib.suppress(json.JSONDecodeError):
|
|
rec = json.loads(line)
|
|
if rec.get("type") == "message" and rec.get("role") == "user":
|
|
metadata = rec.get("metadata") if isinstance(rec.get("metadata"), dict) else {}
|
|
kind = metadata.get("kind", "chat")
|
|
if kind in CHAT_KINDS | {"alert"}:
|
|
text = (rec.get("content") or "").strip().replace("\n", " ")
|
|
if text:
|
|
return text[:_NAME_MAX_CHARS] + ("…" if len(text) > _NAME_MAX_CHARS else "")
|
|
# Prefer first turn_detail (has full prompt, no truncation)
|
|
for line in lines[1:]:
|
|
with contextlib.suppress(json.JSONDecodeError):
|
|
rec = json.loads(line)
|
|
if rec.get("type") == "turn_detail" and rec.get("kind") in CHAT_KINDS | {"alert"}:
|
|
text = (rec.get("prompt") or "").strip().replace("\n", " ")
|
|
if text:
|
|
return text[:_NAME_MAX_CHARS] + ("…" if len(text) > _NAME_MAX_CHARS else "")
|
|
# Fall back to turn stub text (covers cli_agent/follow_up/alert kinds)
|
|
for line in lines[1:]:
|
|
with contextlib.suppress(json.JSONDecodeError):
|
|
rec = json.loads(line)
|
|
is_v1_turn = rec.get("type") == "turn"
|
|
is_v2_stub = (
|
|
rec.get("type") == "custom_message" and rec.get("custom_type") == "turn_stub"
|
|
)
|
|
if (is_v1_turn or is_v2_stub) and rec.get("kind") in CHAT_KINDS | {
|
|
"alert",
|
|
"incoming_alert",
|
|
}:
|
|
text = (rec.get("text") or "").strip().replace("\n", " ")
|
|
if text:
|
|
return text[:_NAME_MAX_CHARS] + ("…" if len(text) > _NAME_MAX_CHARS else "")
|
|
return ""
|