Skip to content

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

perf(session): 减少消息写入和 Session 接口的重复读取 (#6270)

Qin Haojie28861723618a
diff --git a/openviking/server/mcp_endpoint.py b/openviking/server/mcp_endpoint.py
index 4ef60297c..1a06ced05 100644
--- a/openviking/server/mcp_endpoint.py
+++ b/openviking/server/mcp_endpoint.py
@@ -1275,7 +1275,7 @@ async def remember(messages: list[StoreMessage]) -> str:
service = get_service()
ctx = _get_ctx()
session_id = f"mcp-store-{uuid.uuid4().hex[:12]}"
- session = await service.sessions.get(session_id, ctx, auto_create=True)
+ session = await service.sessions.open(session_id, ctx, auto_create=True)
added = 0
for msg in messages:
if msg.content:
diff --git a/openviking/server/routers/sessions.py b/openviking/server/routers/sessions.py
index c0c0d309f..20dacb148 100644
--- a/openviking/server/routers/sessions.py
+++ b/openviking/server/routers/sessions.py
@@ -452,7 +452,7 @@ async def list_tool_results(
):
"""List externalized tool results for a session."""
service = get_service()
- session = await service.sessions.get(session_id, _ctx, auto_create=False)
+ session = await service.sessions.open(session_id, _ctx, auto_create=False)
result = await session.list_tool_results(tool_name=tool_name, limit=limit)
return Response(status="ok", result=_to_jsonable(result))
@@ -474,7 +474,7 @@ async def read_tool_result(
details={"field": "limit", "value": limit},
)
service = get_service()
- session = await service.sessions.get(session_id, _ctx, auto_create=False)
+ session = await service.sessions.open(session_id, _ctx, auto_create=False)
result = await session.read_tool_result(
tool_result_id,
offset=offset,
@@ -495,7 +495,7 @@ async def search_tool_result(
):
"""Search within an externalized tool result."""
service = get_service()
- session = await service.sessions.get(session_id, _ctx, auto_create=False)
+ session = await service.sessions.open(session_id, _ctx, auto_create=False)
result = await session.search_tool_result(
tool_result_id,
query=q,
@@ -535,7 +535,7 @@ async def get_session_archive(
from openviking_cli.exceptions import NotFoundError
service = get_service()
- session = await service.sessions.get(session_id, _ctx, auto_create=False)
+ session = await service.sessions.open(session_id, _ctx, auto_create=False)
try:
result = await session.get_session_archive(archive_id)
except NotFoundError:
@@ -732,7 +732,7 @@ async def add_message(
service = get_service()
async def _add() -> dict[str, Any]:
- session = await service.sessions.get(session_id, _ctx, auto_create=True)
+ session = await service.sessions.open(session_id, _ctx, auto_create=True)
parts = _resolve_message_parts(request, _ctx)
specs = [
@@ -787,7 +787,7 @@ async def batch_add_messages(
service = get_service()
async def _batch_add() -> dict[str, Any]:
- session = await service.sessions.get(session_id, _ctx, auto_create=True)
+ session = await service.sessions.open(session_id, _ctx, auto_create=True)
specs = []
for msg_request in request.messages:
parts = _resolve_message_parts(msg_request, _ctx)
diff --git a/openviking/service/resource_memory_link_service.py b/openviking/service/resource_memory_link_service.py
index d28ac37db..04a029645 100644
--- a/openviking/service/resource_memory_link_service.py
+++ b/openviking/service/resource_memory_link_service.py
@@ -176,7 +176,7 @@ class ResourceMemoryLinkService:
commit_result: Dict[str, Any] = {}
async with self._reason_session_lock:
- session = await self._session_service.get(
+ session = await self._session_service.open(
session_id,
ctx,
auto_create=True,
@@ -238,7 +238,7 @@ class ResourceMemoryLinkService:
task_result: Optional[Dict[str, Any]] = None
async with self._reason_session_lock:
- session = await self._session_service.get(
+ session = await self._session_service.open(
session_id,
ctx,
auto_create=True,
diff --git a/openviking/service/session_service.py b/openviking/service/session_service.py
index d8f2f6605..18f5a75aa 100644
--- a/openviking/service/session_service.py
+++ b/openviking/service/session_service.py
@@ -222,7 +222,7 @@ class SessionService:
*,
session_uri: Optional[str] = None,
) -> Session:
- """Create a new session or load an existing one.
+ """Construct a Session handle without reading or creating persisted data.
Args:
session_id: Session ID, creates a new session (auto-generated ID) if None
@@ -317,24 +317,29 @@ class SessionService:
self._record_lifecycle_metric("create", "error")
raise
- async def get(
+ async def open(
self, session_id: str, ctx: RequestContext, *, auto_create: bool = False
) -> Session:
- """Get an existing session.
+ """Resolve a persisted Session without loading its messages or metadata.
- Args:
- session_id: Session ID
- ctx: Request context
- auto_create: If True, create the session when it does not exist.
- Default is False (raise NotFoundError).
+ Mutations load their state under their own lock. Archive and tool-result
+ readers only need a validated Session URI. Creation defaults live here
+ so those callers do not duplicate provisioning rules.
"""
+ session = self.session(ctx, session_id)
+ if not await session.exists():
+ if not auto_create:
+ raise NotFoundError(session_id, "session")
+ session.meta.auto_commit_policy = self._new_session_auto_commit_policy()
+ await session.ensure_exists()
+ return session
+
+ async def get(
+ self, session_id: str, ctx: RequestContext, *, auto_create: bool = False
+ ) -> Session:
+ """Get a Session with its current messages and metadata loaded."""
try:
- session = self.session(ctx, session_id)
- if not await session.exists():
- if not auto_create:
- raise NotFoundError(session_id, "session")
- session.meta.auto_commit_policy = self._new_session_auto_commit_policy()
- await session.ensure_exists()
+ session = await self.open(session_id, ctx, auto_create=auto_create)
await session.load()
self._record_lifecycle_metric("get", "ok")
return session
@@ -469,7 +474,7 @@ class SessionService:
archive_uri, archived
"""
self._ensure_initialized()
- session = await self.get(session_id, ctx)
+ session = await self.open(session_id, ctx)
commit_kwargs: Dict[str, Any] = {"keep_recent_count": keep_recent_count}
optional_retention = {
"enable_working_memory": enable_working_memory,
@@ -582,18 +587,17 @@ class SessionService:
policy value of ``None`` disables automatic commits.
"""
self._ensure_initialized()
- session = await self.get(session_id, ctx)
+ session = await self.open(session_id, ctx)
normalized_event_tags = (
normalize_search_tags(event_tags, discard_invalid=True)
if event_tags is not None
else None
)
- if normalized_event_tags is not None or update_auto_commit_policy:
- await session.update_config(
- event_search_tags=normalized_event_tags,
- auto_commit_policy=auto_commit_policy,
- update_auto_commit_policy=update_auto_commit_policy,
- )
+ await session.update_config(
+ event_search_tags=normalized_event_tags,
+ auto_commit_policy=auto_commit_policy,
+ update_auto_commit_policy=update_auto_commit_policy,
+ )
return session
async def maybe_schedule_auto_commit(
diff --git a/openviking/session/session.py b/openviking/session/session.py
index abfbbabb9..9bf02dc20 100644
--- a/openviking/session/session.py
+++ b/openviking/session/session.py
@@ -10,9 +10,20 @@ import copy
import inspect
import json
import re
+from contextlib import asynccontextmanager
from dataclasses import dataclass, field, replace
from datetime import datetime, timezone
-from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Optional, Protocol
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ AsyncIterator,
+ Awaitable,
+ Callable,
+ Dict,
+ List,
+ Optional,
+ Protocol,
+)
from uuid import uuid4
from openviking.core.context import ContextLevel
@@ -299,7 +310,7 @@ class SessionMeta:
# Working-Memory v2: token accounting for sliding window + keep window.
# pending_tokens is the cumulative estimated_tokens of messages that fall
# OUTSIDE the recent-keep window and will be archived on the next commit.
- # Maintained O(1) inside add_message(); rebuilt from messages on load.
+ # Rebuilt from the current message snapshot on load and append.
pending_tokens: int = 0
# keep_recent_count is the last value passed from the plugin through
# POST /sessions/{id}/commit body. It is remembered so subsequent
@@ -504,26 +515,36 @@ class Session:
policy = await self._memory_policy_provider()
return MemoryPolicy.from_dict(policy)
- async def load(self):
- """Load session data from storage."""
- if self._loaded:
- return
+ async def load(self) -> None:
+ """Load session data once for readers of this instance."""
+ if not self._loaded:
+ await self._reload()
+ await self._rebuild_pending_tokens()
+ self._loaded = True
+ async def _reload(self) -> Optional[str]:
+ """Restore live state from storage and return the original JSONL for rewriting.
+
+ Mutations call this under the Session and live-file locks, regardless
+ of whether this instance was previously loaded by another request.
+ """
+ self._loaded = False
try:
content = await self._viking_fs.read_file(
f"{self._session_uri}/messages.jsonl", ctx=self.ctx
)
- self._messages = [
- Message.from_dict(json.loads(line))
- for line in content.strip().split("\n")
- if line.strip()
- ]
except Exception as exc:
if not _is_storage_not_found(exc):
raise
- logger.debug(f"Session {self.session_id} not found, starting fresh")
+ content = None
+ self._messages = self._parse_live_messages(content or "")
+ await self._load_meta()
+ self._meta.message_count = len(self._messages)
+ return content
- # Load .meta.json
+ async def _load_meta(self) -> bool:
+ """Restore persisted metadata, including supported pre-meta sessions."""
+ found = True
try:
meta_content = await self._viking_fs.read_file(
f"{self._session_uri}/.meta.json", ctx=self.ctx
@@ -534,6 +555,9 @@ class Session:
except Exception as exc:
if not _is_storage_not_found(exc):
raise
+ found = False
+ self._compression.compression_index = 0
+ self._stats.compression_count = 0
# Old session without meta — derive from existing data
try:
history_items = await self._viking_fs.ls(
@@ -553,10 +577,6 @@ class Session:
self._meta.commit_count = self._compression.compression_index
self._meta.total_message_count = None
- # message_count mirrors the live message list, maintained by every write
- # path. Recompute on load so a stale persisted value can't drift.
- self._meta.message_count = len(self._messages)
-
if not self._meta.created_by_account_id:
self._meta.created_by_account_id = self.ctx.account_id
if not self._meta.created_by_user_id:
@@ -570,13 +590,7 @@ class Session:
self._meta.auto_commit_policy = AutoCommitPolicy.from_dict(
self._meta.auto_commit_policy
).to_dict()
- # WM v2: always rebuild pending_tokens from current messages so the
- # counter stays consistent across restarts and is also backfilled for
- # legacy sessions whose .meta.json predates these fields. O(n) once,
- # subsequent add_message() maintains it in O(1).
- await self._rebuild_pending_tokens()
-
- self._loaded = True
+ return found
async def _rebuild_pending_tokens(self) -> None:
"""Recompute ``pending_tokens`` from the current message list.
@@ -657,6 +671,31 @@ class Session:
finally:
await self._viking_fs._async_agfs.pathlock_release(lease)
+ @asynccontextmanager
+ async def _state_lock(self, *filenames: str) -> AsyncIterator[Dict[str, Any]]:
+ """Serialize Session mutations, then protect their root-file snapshots.
+
+ A directory Exact lease is a Session mutex, not a lease for its children.
+ Acquire child locks with the same owner and keep archive writes independent.
+ """
+ session_path = self._viking_fs._uri_to_path(self._session_uri, ctx=self.ctx)
+ agfs = self._viking_fs._async_agfs
+ session_lease = await agfs.pathlock_acquire_exact(
+ session_path, timeout_secs=_SESSION_PHASE1_LOCK_TIMEOUT_SECONDS
+ )
+ try:
+ file_lease = await agfs.pathlock_acquire_exact_batch(
+ [f"{session_path}/{name}" for name in filenames],
+ timeout_secs=_SESSION_PHASE1_LOCK_TIMEOUT_SECONDS,
+ owner_lease_ref=session_lease,
+ )
+ try:
+ yield file_lease
+ finally:
+ await agfs.pathlock_release(file_lease)
+ finally:
+ await agfs.pathlock_release(session_lease)
+
async def _save_meta(self, lease_ref: Optional[Any] = None) -> None:
"""Persist .meta.json to storage using an optional held PathLock lease."""
if not self._viking_fs:
@@ -678,20 +717,19 @@ class Session:
) -> None:
"""Update mutable session config without overwriting concurrent meta changes."""
update_auto_commit_policy = update_auto_commit_policy or auto_commit_policy is not None
- session_path = self._viking_fs._uri_to_path(self._session_uri, ctx=self.ctx)
- lease = await self._viking_fs._async_agfs.pathlock_acquire_exact(
- session_path, timeout_secs=_SESSION_PHASE1_LOCK_TIMEOUT_SECONDS
- )
- try:
- try:
- meta_content = await self._viking_fs.read_file(
- f"{self._session_uri}/.meta.json",
- ctx=self.ctx,
- )
- self._meta = SessionMeta.from_dict(json.loads(meta_content))
- except Exception as exc:
- if not _is_storage_not_found(exc):
- raise
+ async with self._state_lock(".meta.json") as lease:
+ if not await self._load_meta():
+ # Legacy sessions have no persisted counters to preserve.
+ try:
+ self._messages = await self._read_live_messages_strict()
+ except Exception as exc:
+ if not _is_storage_not_found(exc):
+ raise
+ self._messages = []
+ self._meta.message_count = len(self._messages)
+ await self._rebuild_pending_tokens()
+ if event_search_tags is None and not update_auto_commit_policy:
+ return
if event_search_tags is not None:
self._meta.event_search_tags = list(event_search_tags)
if update_auto_commit_policy:
@@ -701,9 +739,7 @@ class Session:
existing = dict(self._meta.auto_commit_policy or {})
existing.update(auto_commit_policy)
self._meta.auto_commit_policy = AutoCommitPolicy.from_dict(existing).to_dict()
- await self._save_meta()
- finally:
- await self._viking_fs._async_agfs.pathlock_release(lease)
+ await self._save_meta(lease_ref=lease)
async def update_event_search_tags(self, event_search_tags: List[str]) -> None:
"""Update event-memory default tags."""
@@ -734,72 +770,40 @@ class Session:
be overwritten even though add_message already returned successfully.
"""
if not messages:
+ if self._viking_fs:
+ await self.load()
return
if not self._viking_fs:
await self._apply_appended_messages_to_state(messages)
return
- session_path = self._viking_fs._uri_to_path(self._session_uri, ctx=self.ctx)
- lease = await self._viking_fs._async_agfs.pathlock_acquire_exact(
- session_path, timeout_secs=_SESSION_PHASE1_LOCK_TIMEOUT_SECONDS
- )
snapshot: Optional[SessionExpirySnapshot] = None
snapshot_error: Optional[Exception] = None
- metadata_reload_error: Optional[Exception] = None
- try:
- live_messages_missing = False
- try:
- self._messages = await self._read_live_messages_strict()
- except Exception as exc:
- if not _is_storage_not_found(exc):
- raise
- self._messages = []
- live_messages_missing = True
- in_memory_meta = self._meta
- try:
- meta_content = await self._viking_fs.read_file(
- f"{self._session_uri}/.meta.json",
- ctx=self.ctx,
- )
- self._meta = SessionMeta.from_dict(json.loads(meta_content))
- except Exception as exc:
- # Legacy/malformed metadata must not prevent an otherwise safe
- # append. Message correctness remains rooted in messages.jsonl.
- self._meta = in_memory_meta
- metadata_reload_error = exc
-
+ async with self._state_lock("messages.jsonl", ".meta.json") as lease:
+ content = await self._reload()
await self._apply_appended_messages_to_state(messages)
batch_content = "".join(message.to_jsonl() + "\n" for message in messages)
- if live_messages_missing:
- await self._viking_fs.write_file(
- f"{self._session_uri}/messages.jsonl",
- batch_content,
- ctx=self.ctx,
- )
- else:
- await self._viking_fs.append_file(
- f"{self._session_uri}/messages.jsonl",
- batch_content,
- ctx=self.ctx,
- )
- await self._save_meta()
+ # Preserve existing JSONL verbatim, as append_file did. Existing
+ # content is already stored bytes; only initial creation used the
+ # text-write privacy transformation.
+ persisted_content = (
+ batch_content if content is None else (content + batch_content).encode("utf-8")
+ )
+ await self._viking_fs.write_file(
+ f"{self._session_uri}/messages.jsonl",
+ persisted_content,
+ ctx=self.ctx,
+ lease_ref=lease,
+ )
+ await self._save_meta(lease_ref=lease)
+ self._loaded = True
if self._expiry_reporter is not None:
try:
- # _save_meta() made this exact in-memory state authoritative
- # under the Session path lock, even when the preceding reload
- # failed. Reporting it prevents the previous due time from
- # firing early and repairs a missing expiry registration.
+ # Report only the state persisted under the Session lock.
snapshot = self._build_expiry_snapshot_locked()
except Exception as exc:
snapshot_error = exc
- finally:
- await self._viking_fs._async_agfs.pathlock_release(lease)
if self._expiry_reporter is not None:
- if metadata_reload_error is not None:
- logger.warning(
- "Session metadata reload failed before append; reporting repaired state",
- exc_info=metadata_reload_error,
- )
if snapshot is not None:
await self._expiry_reporter.report_after_append(snapshot)
else:
@@ -830,16 +834,9 @@ class Session:
msg_tokens = int(msg.estimated_tokens or 0)
self._stats.total_tokens += msg_tokens
- if self._meta.retention_mode != RETENTION_MODE_TURN_BUDGET:
- keep = int(self._meta.keep_recent_count or 0)
- if keep <= 0:
- self._meta.pending_tokens += msg_tokens
- elif len(self._messages) > keep:
- pushed_out = self._messages[-(keep + 1)]
- self._meta.pending_tokens += int(pushed_out.estimated_tokens or 0)
-
- if self._meta.retention_mode == RETENTION_MODE_TURN_BUDGET:
- await self._rebuild_pending_tokens()
+ # Rebuild once from the post-append snapshot. Persisted counters may be
+ # stale after a message write succeeded but its metadata write failed.
+ await self._rebuild_pending_tokens()
self._meta.message_count = len(self._messages)
if self._meta.total_message_count is not None:
@@ -1433,22 +1430,13 @@ class Session:
)
try:
self._messages = await self._read_live_messages_strict()
- try:
- meta_content = await self._viking_fs.read_file(
- f"{self._session_uri}/.meta.json",
- ctx=self.ctx,
- )
- self._meta = SessionMeta.from_dict(json.loads(meta_content))
- if (
- memory_policy is None
- and self._meta.memory_policy is None
- and in_memory_default_memory_policy is not None
- ):
- self._meta.memory_policy = in_memory_default_memory_policy
- except Exception:
- # The root JSONL remains authoritative for message correctness;
- # legacy sessions may not have metadata yet.
- pass
+ await self._load_meta()
+ if (
+ memory_policy is None
+ and self._meta.memory_policy is None
+ and in_memory_default_memory_policy is not None
+ ):
+ self._meta.memory_policy = in_memory_default_memory_policy
effective_event_tags = _resolve_event_search_tags(
event_tags, self._meta.event_search_tags
@@ -2970,6 +2958,11 @@ class Session:
f"{self._session_uri}/messages.jsonl",
ctx=self.ctx,
)
+ return self._parse_live_messages(content)
+
+ @staticmethod
+ def _parse_live_messages(content: str) -> List[Message]:
+ """Parse live JSONL without dropping corrupt rows or Unicode separators."""
messages: List[Message] = []
# Split on "\n" only, not str.splitlines(): the latter also treats
# U+2028 / U+2029 / NEL (\x85) / \r / \v / \f as line boundaries, which
diff --git a/tests/server/test_api_sessions.py b/tests/server/test_api_sessions.py
index ceb02930b..769e9347f 100644
--- a/tests/server/test_api_sessions.py
+++ b/tests/server/test_api_sessions.py
@@ -321,7 +321,7 @@ async def test_get_session_context_rejects_negative_token_budget(client: httpx.A
assert body["error"]["details"] == {"field": "token_budget", "value": -1}
-async def test_tool_result_externalization_read_and_search(client: httpx.AsyncClient):
+async def test_tool_result_externalization_read_and_search(client: httpx.AsyncClient, service):
session_id = "tool-result-api-session"
raw = "alpha\n" + ("needle-" * 4000) + "\nomega"
@@ -353,6 +353,9 @@ async def test_tool_result_externalization_read_and_search(client: httpx.AsyncCl
assert "kind: text" in part["tool_output"]
assert "openviking_tool_result_search" in part["tool_output"]
+ ctx = RequestContext(user=DEFAULT_USER, role=Role.ROOT)
+ await service.viking_fs.write_file(f"{session_uri}/messages.jsonl", "invalid JSONL", ctx=ctx)
+
tool_result_id = part["tool_output_ref"].rsplit("/", 1)[-1]
read_resp = await client.get(
f"/api/v1/sessions/{session_id}/tool-results/{tool_result_id}?offset=0&limit=-1"
@@ -854,6 +857,8 @@ async def test_session_load_recovers_message_count_from_live_messages(service):
async def test_write_responses_return_pending_tokens_for_commit_policy(
client: httpx.AsyncClient,
+ service,
+ monkeypatch,
):
"""A commit policy must be able to decide from the write response alone.
@@ -863,6 +868,16 @@ async def test_write_responses_return_pending_tokens_for_commit_policy(
create_resp = await client.post("/api/v1/sessions", json={})
session_id = create_resp.json()["result"]["session_id"]
+ session_uri = create_resp.json()["result"]["uri"]
+ reads = []
+ original_read = service.viking_fs._async_agfs.read
+
+ async def read(path, *args, **kwargs):
+ reads.append(path.rsplit("/", 1)[-1])
+ return await original_read(path, *args, **kwargs)
+
+ monkeypatch.setattr(service.viking_fs._async_agfs, "read", read)
+
add_resp = await client.post(
f"/api/v1/sessions/{session_id}/messages",
json=_message_request("user", content="Hello, world!"),
@@ -870,6 +885,13 @@ async def test_write_responses_return_pending_tokens_for_commit_policy(
assert add_resp.status_code == 200
after_add = add_resp.json()["result"]["pending_tokens"]
assert after_add > 0
+ assert reads.count("messages.jsonl") == reads.count(".meta.json") == 1
+
+ ctx = RequestContext(user=DEFAULT_USER, role=Role.ROOT)
+ meta = json.loads(await service.viking_fs.read_file(f"{session_uri}/.meta.json", ctx=ctx))
+ meta["pending_tokens"] = 0
+ await service.viking_fs.write_file(f"{session_uri}/.meta.json", json.dumps(meta), ctx=ctx)
+ reads.clear()
batch_resp = await client.post(
f"/api/v1/sessions/{session_id}/messages/batch",
@@ -883,12 +905,24 @@ async def test_write_responses_return_pending_tokens_for_commit_policy(
assert batch_resp.status_code == 200
after_batch = batch_resp.json()["result"]["pending_tokens"]
assert after_batch > after_add
+ assert reads.count("messages.jsonl") == reads.count(".meta.json") == 1
# The write-returned value must match what get_session would have reported,
# which is exactly the extra round trip this field removes.
get_resp = await client.get(f"/api/v1/sessions/{session_id}")
assert get_resp.json()["result"]["pending_tokens"] == after_batch
+ empty_resp = await client.post(
+ f"/api/v1/sessions/{session_id}/messages/batch", json={"messages": []}
+ )
+ assert empty_resp.status_code == 200
+ assert empty_resp.json()["result"] == {
+ "session_id": session_id,
+ "message_count": 3,
+ "pending_tokens": after_batch,
+ "added": 0,
+ }
+
async def test_add_message_accepts_image_part(client: httpx.AsyncClient, service):
create_resp = await client.post("/api/v1/sessions", json={})
diff --git a/tests/server/test_mcp_endpoint.py b/tests/server/test_mcp_endpoint.py
index 4fa6dee1d..0694527cd 100644
--- a/tests/server/test_mcp_endpoint.py
+++ b/tests/server/test_mcp_endpoint.py
@@ -1377,7 +1377,7 @@ async def test_store_skips_empty_message_content(service, monkeypatch):
self.messages.append((role, parts, peer_id, created_at))
fake_session = FakeSession()
- monkeypatch.setattr(service.sessions, "get", AsyncMock(return_value=fake_session))
+ monkeypatch.setattr(service.sessions, "open", AsyncMock(return_value=fake_session))
monkeypatch.setattr(
service.sessions,
"commit_async",
diff --git a/tests/service/test_resource_memory_link_service.py b/tests/service/test_resource_memory_link_service.py
index 7136ca46d..6a7269605 100644
--- a/tests/service/test_resource_memory_link_service.py
+++ b/tests/service/test_resource_memory_link_service.py
@@ -167,7 +167,7 @@ class _FakeSessionService:
)
return self.session
- async def get(self, session_id, ctx, auto_create=False):
+ async def open(self, session_id, ctx, auto_create=False):
self.got.append(
{
"ctx": ctx,
@@ -297,8 +297,8 @@ async def test_on_resource_added_reuses_same_reason_session(request_context):
worker_waiting = threading.Event()
class BlockingSessionService(_FakeSessionService):
- async def get(self, session_id, ctx, auto_create=False):
- session = await super().get(session_id, ctx, auto_create=auto_create)
+ async def open(self, session_id, ctx, auto_create=False):
+ session = await super().open(session_id, ctx, auto_create=auto_create)
if len(self.got) == 1:
entered.set()
await release.wait()
diff --git a/tests/service/test_session_service_metrics.py b/tests/service/test_session_service_metrics.py
index beb2af7f2..6b63df44b 100644
--- a/tests/service/test_session_service_metrics.py
+++ b/tests/service/test_session_service_metrics.py
@@ -57,7 +57,7 @@ async def test_commit_async_keeps_working_when_session_metrics_fail(
def _boom(*_args, **_kwargs):
raise RuntimeError("metrics failed")
- monkeypatch.setattr(service, "get", AsyncMock(return_value=session))
+ monkeypatch.setattr(service, "open", AsyncMock(return_value=session))
monkeypatch.setattr(SessionLifecycleDataSource, "record_lifecycle", staticmethod(_boom))
monkeypatch.setattr(SessionLifecycleDataSource, "record_archive", staticmethod(_boom))
monkeypatch.setattr(session_service_module.logger, "debug", debug)
diff --git a/tests/session/test_session_commit_race.py b/tests/session/test_session_commit_race.py
index 5baf62498..cd756b906 100644
--- a/tests/session/test_session_commit_race.py
+++ b/tests/session/test_session_commit_race.py
@@ -62,7 +62,7 @@ class TestCommitRace:
async def commit_and_add():
"""Start commit, then add a message after Phase 1 completes."""
- commit_task = asyncio.create_task(session.commit_async())
+ commit_task = asyncio.create_task(session.commit_async(enable_working_memory=True))
# Wait until Phase 1 is done (lock released, messages cleared)
await phase1_done.wait()
# Add message while commit is in Phase 2 (after lock released)
diff --git a/tests/unit/session/test_event_tag_concurrency.py b/tests/unit/session/test_event_tag_concurrency.py
index d35fefb95..427133800 100644
--- a/tests/unit/session/test_event_tag_concurrency.py
+++ b/tests/unit/session/test_event_tag_concurrency.py
@@ -20,8 +20,12 @@ class _PathLock:
self.acquired += 1
return "lease-1"
+ async def pathlock_acquire_exact_batch(self, paths, timeout_secs, owner_lease_ref):
+ self.acquired += 1
+ return "file-lease"
+
async def pathlock_release(self, lease):
- assert lease == "lease-1"
+ assert lease in {"lease-1", "file-lease"}
self.released += 1
@@ -40,6 +44,9 @@ class _MetaVikingFS:
del uri, ctx
return "/sessions/session-1"
+ async def exists(self, uri, ctx=None):
+ return uri in self.files
+
async def read_file(self, uri, ctx=None):
del ctx
if uri not in self.files:
@@ -133,6 +140,4 @@ async def test_update_config_updates_policy_and_tags_in_one_locked_write():
assert saved_meta.message_count == 41
assert saved_meta.pending_tokens == 8200
assert len(viking_fs.writes) == 1
- assert viking_fs.writes[0][2] is None
- assert viking_fs._async_agfs.acquired == 1
- assert viking_fs._async_agfs.released == 1
+ assert viking_fs._async_agfs.acquired == viking_fs._async_agfs.released
diff --git a/tests/utils/mock_agfs.py b/tests/utils/mock_agfs.py
index 44307e903..9bb80f310 100644
--- a/tests/utils/mock_agfs.py
+++ b/tests/utils/mock_agfs.py
@@ -181,14 +181,18 @@ class MockLocalAGFS:
owner_lease_ref=None,
):
del ctx, owner_lease_ref
- (request,) = requests
- path = request["path"]
- with self._pathlocks_guard:
- lock = self._pathlocks.setdefault(path, threading.Lock())
-
- acquired = await asyncio.to_thread(lock.acquire, timeout=timeout_secs)
- if not acquired:
- raise TimeoutError(f"timed out acquiring test path lock: {path}")
+ acquired = []
+ try:
+ for path in sorted({request["path"] for request in requests}):
+ with self._pathlocks_guard:
+ lock = self._pathlocks.setdefault(path, threading.Lock())
+ if not await asyncio.to_thread(lock.acquire, timeout=timeout_secs):
+ raise TimeoutError(f"timed out acquiring test path lock: {path}")
+ acquired.append(lock)
+ except BaseException:
+ for lock in reversed(acquired):
+ lock.release()
+ raise
lease_ref = str(uuid.uuid4())
lease = {
@@ -198,12 +202,13 @@ class MockLocalAGFS:
"owned": True,
}
with self._pathlocks_guard:
- self._pathlock_leases[lease_ref] = lock
+ self._pathlock_leases[lease_ref] = acquired
return lease
def pathlock_release(self, ctx, owned_lease_ref):
del ctx
lease_ref = owned_lease_ref["lease_ref"]
with self._pathlocks_guard:
- lock = self._pathlock_leases.pop(lease_ref)
- lock.release()
+ locks = self._pathlock_leases.pop(lease_ref)
+ for lock in reversed(locks):
+ lock.release()