Files
DeepTutor/deeptutor/services/codex_auth/storage.py
Bingxi Zhao (Frank) 47d05809ea release: v1.5.5
Maintenance release on top of v1.5.4, with two new ways to bring a model.

- OpenAI Codex is a first-party OAuth provider (#690): browser sign-in
  against your own ChatGPT plan replaces the API-key fields, credentials
  stay in <user-root>/private/openai-codex/ with owner-only permissions,
  and the managed profile is owner-bound so it is never handed out through
  grants or made active over an already-configured LLM.
- Eden AI joins as the 35th LLM binding (#671), an OpenAI-compatible
  gateway addressed as <provider>/<model>.
- Knowledge bases answer from a real document inventory instead of
  guessing from retrieval hits: a per-KB inventory rides the system prompt
  and a new kb_files tool enumerates on demand with glob/substring
  filters, mounted under rag's gate and deniable per partner.
- The rag tool cites the chunks, entities, and reports retrieval actually
  returned (#694) rather than an echo of its own query; the local LightRAG
  pipeline still surfaces nothing to cite.
- GraphRAG indexing runs on a worker thread with its own asyncio loop
  (#695), so UVICORN_LOOP=asyncio is no longer needed, and two config
  faults that broke the first run are fixed (#699).
- Assorted: unique optimistic message ids (#698, a v1.5.4 regression that
  dropped the assistant reply from the visible thread), partner-chat
  manual scrolling respected (#704), claude-opus-5 recognized as
  effort-based (#703), Kimi models omit temperature outright, and
  deeptutor start keeps relaying logs on legacy Windows code pages (#702).
- Typing: narrow the loopback callback server to asyncio.Server and gate
  the msvcrt lock path on sys.platform so it type-checks off Windows.

Release notes: assets/releases/ver1-5-5.md
2026-07-26 23:19:09 +08:00

250 lines
8.6 KiB
Python

"""Private, generation-safe storage for DeepTutor Codex credentials."""
from __future__ import annotations
from collections.abc import Iterator, Mapping
from contextlib import contextmanager
from dataclasses import replace
import json
import os
from pathlib import Path
import stat
import sys
import tempfile
import threading
from typing import Any, BinaryIO
from .contracts import CodexAuthError, CodexCredentials
_SCHEMA_VERSION = 1
def _is_reparse_point(path: Path) -> bool:
try:
attributes = getattr(os.lstat(path), "st_file_attributes", 0)
except FileNotFoundError:
return False
return bool(attributes & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0))
def _unsafe_path() -> CodexAuthError:
return CodexAuthError(
"unsafe_storage_path",
"Codex authentication storage uses an unsafe filesystem path.",
500,
)
def _assert_safe_directory(path: Path) -> None:
if path.is_symlink() or _is_reparse_point(path):
raise _unsafe_path()
if path.exists() and not path.is_dir():
raise _unsafe_path()
def _assert_safe_regular_path(path: Path) -> None:
_assert_safe_directory(path.parent)
if path.is_symlink() or _is_reparse_point(path):
raise _unsafe_path()
if path.exists() and not path.is_file():
raise _unsafe_path()
@contextmanager
def _locked_file(path: Path) -> Iterator[BinaryIO]:
_assert_safe_regular_path(path)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a+b") as handle:
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)
handle.seek(0, os.SEEK_END)
if handle.tell() == 0:
handle.write(b"\0")
handle.flush()
handle.seek(0)
# ``sys.platform`` rather than ``os.name``: the checker narrows on it,
# so the Windows-only ``msvcrt`` branch type-checks off Windows too.
if sys.platform == "win32":
import msvcrt
msvcrt.locking(handle.fileno(), msvcrt.LK_LOCK, 1)
else:
import fcntl
fcntl.flock(handle.fileno(), fcntl.LOCK_EX)
try:
yield handle
finally:
handle.seek(0)
if sys.platform == "win32":
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
else:
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
def _atomic_write_json(path: Path, payload: Mapping[str, Any]) -> None:
_assert_safe_regular_path(path)
path.parent.mkdir(parents=True, exist_ok=True)
fd, temp_name = tempfile.mkstemp(
prefix=f".{path.name}.",
suffix=".tmp",
dir=path.parent,
)
temp_path = Path(temp_name)
try:
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as handle:
json.dump(payload, handle, ensure_ascii=False, indent=2)
handle.write("\n")
handle.flush()
os.fsync(handle.fileno())
os.chmod(temp_path, stat.S_IRUSR | stat.S_IWUSR)
os.replace(temp_path, path)
finally:
temp_path.unlink(missing_ok=True)
class CodexCredentialStore:
"""Store credentials only below DeepTutor's explicitly supplied user root."""
def __init__(self, user_root: Path) -> None:
self.root = Path(user_root) / "private" / "openai-codex"
self.credentials_path = self.root / "credentials.v1.json"
self.state_path = self.root / "state.v1.json"
self.catalog_cache_path = self.root / "models-cache.v1.json"
self.lock_path = self.root / "auth.lock"
self._thread_lock = threading.Lock()
def _ensure_root(self) -> None:
private_root = self.root.parent
private_root.mkdir(parents=True, exist_ok=True)
_assert_safe_directory(private_root)
self.root.mkdir(exist_ok=True)
_assert_safe_directory(self.root)
os.chmod(private_root, stat.S_IRWXU)
os.chmod(self.root, stat.S_IRWXU)
@contextmanager
def _locked(self) -> Iterator[None]:
with self._thread_lock:
self._ensure_root()
with _locked_file(self.lock_path):
yield
@staticmethod
def _read_json(path: Path, *, code: str, message: str) -> dict[str, Any] | None:
_assert_safe_regular_path(path)
if not path.exists():
return None
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
raise CodexAuthError(code, message, 500) from exc
if not isinstance(payload, dict):
raise CodexAuthError(code, message, 500)
return payload
def _read_state_unlocked(self) -> dict[str, Any]:
state = self._read_json(
self.state_path,
code="state_corrupt",
message="Stored Codex authentication state is invalid.",
)
if state is None:
return {"schema_version": _SCHEMA_VERSION, "generation": 0}
generation = state.get("generation")
if isinstance(generation, bool) or not isinstance(generation, int) or generation < 0:
raise CodexAuthError(
"state_corrupt",
"Stored Codex authentication state is invalid.",
500,
)
return state
@staticmethod
def _generation_changed() -> CodexAuthError:
return CodexAuthError(
"generation_changed",
"Codex authentication changed while this operation was running.",
409,
)
def current_generation(self) -> int:
with self._locked():
return int(self._read_state_unlocked()["generation"])
def load_credentials(self) -> CodexCredentials | None:
with self._locked():
state_generation = int(self._read_state_unlocked()["generation"])
payload = self._read_json(
self.credentials_path,
code="credential_corrupt",
message="Stored Codex credentials are invalid.",
)
if payload is None:
return None
credentials = CodexCredentials.from_dict(payload)
if credentials.generation != state_generation:
return None
return credentials
def commit_credentials(
self,
credentials: CodexCredentials,
expected_generation: int,
) -> CodexCredentials:
with self._locked():
state = self._read_state_unlocked()
current_generation = int(state["generation"])
if current_generation != expected_generation:
raise self._generation_changed()
next_generation = current_generation + 1
committed = replace(
credentials,
schema_version=_SCHEMA_VERSION,
generation=next_generation,
)
next_state = {
**state,
"schema_version": _SCHEMA_VERSION,
"generation": next_generation,
}
_atomic_write_json(self.state_path, next_state)
_atomic_write_json(self.credentials_path, committed.to_dict())
return committed
def clear_credentials(self, expected_generation: int | None = None) -> int:
with self._locked():
state = self._read_state_unlocked()
current_generation = int(state["generation"])
if expected_generation is not None and current_generation != expected_generation:
raise self._generation_changed()
next_generation = current_generation + 1
_atomic_write_json(
self.state_path,
{
**state,
"schema_version": _SCHEMA_VERSION,
"generation": next_generation,
},
)
for path in (self.credentials_path, self.catalog_cache_path):
_assert_safe_regular_path(path)
path.unlink(missing_ok=True)
return next_generation
def load_catalog_cache(self) -> dict[str, Any] | None:
with self._locked():
return self._read_json(
self.catalog_cache_path,
code="catalog_corrupt",
message="Stored Codex model data is invalid.",
)
def save_catalog_cache(self, payload: Mapping[str, Any]) -> None:
with self._locked():
_atomic_write_json(self.catalog_cache_path, payload)
def clear_catalog_cache(self) -> None:
with self._locked():
_assert_safe_regular_path(self.catalog_cache_path)
self.catalog_cache_path.unlink(missing_ok=True)