from __future__ import annotations

from typing import Any


def store(key: str, value: Any, *, runtime: Any) -> dict[str, Any]:
    runtime.memory[key] = value
    return {"key": key, "stored": True}


def get_value(key: str, *, runtime: Any) -> Any:
    if key not in runtime.memory:
        raise KeyError(f"missing memory key: {key}")
    return runtime.memory[key]


def delete(key: str, *, runtime: Any) -> dict[str, Any]:
    if key not in runtime.memory:
        raise KeyError(f"missing memory key: {key}")
    del runtime.memory[key]
    return {"key": key, "deleted": True}


def list_keys(*, runtime: Any) -> list[str]:
    return sorted(runtime.memory)


def clear(*, runtime: Any) -> dict[str, Any]:
    count = len(runtime.memory)
    runtime.memory.clear()
    return {"cleared": count}
