OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Rejected message requests create an empty session before validating the parts

Open Kiko0301 opened this issue 2 weeks ago • 0 comments

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

  1. Call POST /api/v1/sessions/no-such-session/messages with:
    {"role": "user", "parts": [{"type": "image_url", "image_url": {"url": ""}}]}
    
  2. The request fails with 400.
  3. GET /api/v1/sessions now lists no-such-session, and GET /api/v1/sessions/no-such-session shows a session with zero messages.
  4. Alternatively call POST /api/v1/sessions/no-such-session/messages/batch with 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.

Kiko0301 avatar Nov 14 '26 09:11 Kiko0301