[Bug]: Rejected message requests create an empty session before validating the parts
Rejected message requests create an empty session before validating the parts
Bug Description
Both session message endpoints auto-create the session before validating the message parts.
POST /api/v1/sessions/{session_id}/messages:
async def _add() -> dict[str, Any]:
session = await service.sessions.open(session_id, _ctx, auto_create=True)
parts = _resolve_message_parts(request, _ctx)
POST /api/v1/sessions/{session_id}/messages/batch:
async def _batch_add() -> dict[str, Any]:
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)
_resolve_message_parts() rejects invalid parts — for example an image_url part with an empty URL raises HTTPException(400), and a context/tool part URI that fails authorization raises 403. By then service.sessions.open(..., auto_create=True) has already persisted a new session (ensure_exists() writes the session directory, messages.jsonl and metadata), so a request that returns 400/403 leaves an empty session behind. The batch variant is the clearest case: one valid message followed by an invalid one creates the session while storing no message at all.
A rejected request should not mutate state. The telemetry rejection path in the same handlers already behaves that way (run_operation validates telemetry before calling fn), so this is an ordering oversight rather than a contract.
Steps to Reproduce
- Call
POST /api/v1/sessions/no-such-session/messageswith:{"role": "user", "parts": [{"type": "image_url", "image_url": {"url": ""}}]} - The request fails with 400.
GET /api/v1/sessionsnow listsno-such-session, andGET /api/v1/sessions/no-such-sessionshows a session with zero messages.- Alternatively call
POST /api/v1/sessions/no-such-session/messages/batchwith one valid{"role": "user", "content": "valid"}and the invalid message above: the request fails with 400 and the session exists with zero messages.
Expected Behavior
A missing session is auto-created only when the request is accepted and the message is stored; a rejected request leaves no state behind.
Actual Behavior
The session is created before validation, so every rejected request permanently adds an empty session, and the batch variant can create a session without storing any message.
Environment
- OS: Windows 11
- OpenViking version/commit: main @
b2031597
Additional Context
Fix submitted as PR #7640: message parts (and, for the batch endpoint, the complete spec list) are resolved before service.sessions.open(...). Missing-session auto-creation still happens for valid requests. Regression tests with a fake session service assert a 400 for an invalid image_url part and that sessions.open() was never called, for both endpoints.