Files
headroomlabs-ai--headroom/tests/test_provider_codex_threads.py
wehub-resource-sync 94b8d5d118
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Waiting to run
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Blocked by required conditions
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Waiting to run
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Waiting to run
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Blocked by required conditions
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Blocked by required conditions
Docker / promote-latest (push) Blocked by required conditions
Init Native E2E / init-native (macos-latest, claude) (push) Waiting to run
Init Native E2E / init-native (macos-latest, codex) (push) Waiting to run
Init Native E2E / init-native (macos-latest, copilot) (push) Waiting to run
Install Native E2E / install-native (macos-latest) (push) Waiting to run
Wrap Native E2E / wrap-native (macos-latest) (push) Waiting to run
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / changes (push) Failing after 2s
Deploy Documentation / deploy (push) Failing after 0s
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 1s
Dev Containers / validate-worktree (push) Failing after 1s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Init E2E / docker-init-e2e (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 0s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
Release Please / release-please (push) Failing after 0s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Merge Conflicts / merge-conflicts (push) Failing after 2s
OpenCode Plugin / typecheck + build + test (push) Failing after 2s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 3s
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:29 +08:00

201 lines
6.7 KiB
Python

from __future__ import annotations
import sqlite3
from pathlib import Path
import pytest
from headroom.providers.codex import threads
def _seed(path: Path, rows: list[tuple[str, str]]) -> None:
conn = sqlite3.connect(str(path))
try:
conn.execute("CREATE TABLE threads (id TEXT PRIMARY KEY, model_provider TEXT NOT NULL)")
conn.executemany("INSERT INTO threads (id, model_provider) VALUES (?, ?)", rows)
conn.commit()
finally:
conn.close()
def _count(path: Path, provider: str) -> int:
conn = sqlite3.connect(str(path))
try:
(n,) = conn.execute(
"SELECT COUNT(*) FROM threads WHERE model_provider = ?", (provider,)
).fetchone()
return n
finally:
conn.close()
def test_codex_state_db_paths_discovers_numeric_files_in_directory_order(tmp_path: Path) -> None:
sqlite_home = tmp_path / "sqlite"
sqlite_home.mkdir()
nested = sqlite_home / "nested"
nested.mkdir()
_seed(sqlite_home / "state_10.sqlite", [("a", "openai")])
_seed(sqlite_home / "state_2.sqlite", [("b", "openai")])
_seed(sqlite_home / "state_backup.sqlite", [("c", "openai")])
(sqlite_home / "state_6.sqlite-wal").write_text("wal", encoding="utf-8")
_seed(nested / "state_7.sqlite", [("d", "openai")])
_seed(tmp_path / "state_9.sqlite", [("e", "openai")])
_seed(tmp_path / "state_1.sqlite", [("f", "openai")])
(tmp_path / "other.db").write_text("other", encoding="utf-8")
assert threads._codex_state_db_paths(tmp_path) == [
sqlite_home / "state_2.sqlite",
sqlite_home / "state_10.sqlite",
tmp_path / "state_1.sqlite",
tmp_path / "state_9.sqlite",
]
def test_retag_thread_providers_discovers_later_state_store_and_skips_adjacent_files(
tmp_path: Path,
) -> None:
sqlite_home = tmp_path / "sqlite"
sqlite_home.mkdir()
nested = sqlite_home / "nested"
nested.mkdir()
later = sqlite_home / "state_6.sqlite"
legacy = tmp_path / "state_5.sqlite"
backup = sqlite_home / "state_backup.sqlite"
sidecar = sqlite_home / "state_6.sqlite-wal"
nested_store = nested / "state_7.sqlite"
_seed(later, [("a", "openai"), ("b", "openai"), ("c", "anthropic")])
_seed(legacy, [("d", "openai"), ("e", "headroom")])
_seed(backup, [("f", "openai")])
sidecar.write_text("wal", encoding="utf-8")
_seed(nested_store, [("g", "openai")])
threads.retag_to_headroom(tmp_path)
assert _count(later, "headroom") == 2
assert _count(later, "openai") == 0
assert _count(later, "anthropic") == 1
assert _count(legacy, "headroom") == 2
assert _count(legacy, "openai") == 0
assert _count(backup, "openai") == 1
assert _count(backup, "headroom") == 0
assert _count(nested_store, "openai") == 1
assert _count(nested_store, "headroom") == 0
def test_retag_one_moves_only_matching_provider(tmp_path: Path) -> None:
db = tmp_path / "state_5.sqlite"
_seed(db, [("a", "openai"), ("b", "openai"), ("c", "headroom"), ("d", "anthropic")])
moved = threads._retag_one(db, frm="openai", to="headroom")
assert moved == 2
assert _count(db, "openai") == 0
assert _count(db, "headroom") == 3
# Third-party providers are left alone.
assert _count(db, "anthropic") == 1
back = threads._retag_one(db, frm="headroom", to="openai")
assert back == 3
assert _count(db, "headroom") == 0
assert _count(db, "openai") == 3
assert _count(db, "anthropic") == 1
def test_retag_one_noop_without_threads_table(tmp_path: Path) -> None:
db = tmp_path / "state_5.sqlite"
sqlite3.connect(str(db)).close() # empty schema, no threads table
assert threads._retag_one(db, frm="openai", to="headroom") == 0
def test_retag_thread_providers_silent_when_no_store(tmp_path: Path) -> None:
# No stores exist under this codex_home: must not raise.
threads.retag_thread_providers(tmp_path, frm="openai", to="headroom")
def test_retag_thread_providers_skips_unreadable_store_directory(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
sqlite_home = tmp_path / "sqlite"
sqlite_home.mkdir()
_seed(sqlite_home / "state_6.sqlite", [("a", "openai")])
fallback = tmp_path / "state_5.sqlite"
_seed(fallback, [("b", "openai")])
original_iterdir = Path.iterdir
def fail_for_sqlite(path: Path):
if path == sqlite_home:
raise OSError("permission denied")
return original_iterdir(path)
monkeypatch.setattr(Path, "iterdir", fail_for_sqlite)
threads.retag_to_headroom(tmp_path)
assert _count(fallback, "headroom") == 1
assert _count(sqlite_home / "state_6.sqlite", "openai") == 1
def test_retag_thread_providers_best_effort_on_corrupt_store(tmp_path: Path) -> None:
bad = tmp_path / "state_5.sqlite"
bad.write_text("not a sqlite database", encoding="utf-8")
# A corrupt store is logged and skipped, never raised.
threads.retag_thread_providers(tmp_path, frm="openai", to="headroom")
def test_retag_thread_providers_skips_corrupt_discovered_store_and_continues(
tmp_path: Path,
) -> None:
sqlite_home = tmp_path / "sqlite"
sqlite_home.mkdir()
bad = sqlite_home / "state_6.sqlite"
later = sqlite_home / "state_7.sqlite"
bad.write_text("not a sqlite database", encoding="utf-8")
_seed(later, [("a", "openai"), ("b", "anthropic")])
threads.retag_to_headroom(tmp_path)
assert _count(later, "headroom") == 1
assert _count(later, "openai") == 0
assert _count(later, "anthropic") == 1
def test_retag_thread_providers_skips_os_error_store_and_continues(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
sqlite_home = tmp_path / "sqlite"
sqlite_home.mkdir()
locked = sqlite_home / "state_6.sqlite"
later = sqlite_home / "state_7.sqlite"
_seed(locked, [("a", "openai")])
_seed(later, [("b", "openai"), ("c", "anthropic")])
original_retag_one = threads._retag_one
def fail_for_locked(path: Path, *, frm: str, to: str) -> int:
if path == locked:
raise OSError("locked")
return original_retag_one(path, frm=frm, to=to)
monkeypatch.setattr(threads, "_retag_one", fail_for_locked)
threads.retag_to_headroom(tmp_path)
assert _count(locked, "openai") == 1
assert _count(later, "headroom") == 1
assert _count(later, "anthropic") == 1
def test_enable_disable_wrappers_retag_expected_direction(tmp_path: Path) -> None:
db = tmp_path / "state_5.sqlite"
_seed(db, [("a", "openai"), ("b", "headroom")])
threads.retag_to_headroom(tmp_path)
assert _count(db, "headroom") == 2
assert _count(db, "openai") == 0
threads.retag_to_native(tmp_path)
assert _count(db, "openai") == 2
assert _count(db, "headroom") == 0