import asyncio import uuid import httpx import pytest import pytest_asyncio from free_claude_code.application.code_sessions import CodeService from free_claude_code.application.errors import ApplicationUnavailableError from free_claude_code.runtime.code_sessions_sqlite import SQLiteCodeStore from tests.api.support import create_test_app from tests.code_sessions_support import CodexPackets, FakeHarness, close_code_database @pytest_asyncio.fixture async def code_api(database_factory, tmp_path): harness = FakeHarness() code = CodeService( SQLiteCodeStore(database_factory(tmp_path / "code.db", tmp_path / "code.lock")), harness, ) await code.start() app = create_test_app(code=code) try: async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://127.0.0.1" ) as client: yield client, code, harness, tmp_path, app finally: await code.close() await close_code_database(code) await app.state.services.admin.close() async def create_session(code_api): client, _, _, folder, _ = code_api response = await client.post( "/admin/api/code/sessions", json={"session_id": str(uuid.uuid4()), "cwd": str(folder), "harness": "codex"}, ) assert response.status_code == 201 return response.json() @pytest.mark.asyncio async def test_detail_exposes_child_review_liveness_without_native_source(code_api): client, code, harness, _, _ = code_api session = await create_session(code_api) path = f"/admin/api/code/sessions/{session['id']}" await code.send( session["id"], str(uuid.uuid4()), session["revision"], "Delegate", expected_epoch=code.epoch, ) await asyncio.wait_for(harness.started.wait(), 3) connection = harness.connections[0] packets = CodexPackets(connection) await packets.spawn() await packets.review() await connection.finish("turn-1") detail = (await client.get(path)).json() review = detail["items"][-1] assert detail.get("active_review_ids") == [review["id"]] assert review["kind"] == "subagent_auto_review" assert not {"raw", "native_turn_id", "native_item_id", "generation"} & review.keys() await connection.close() closed = (await client.get(path)).json() assert closed["active_review_ids"] == [] assert closed["items"] == detail["items"] @pytest.mark.asyncio async def test_mode_is_saved_but_native_defaults_stay_private(code_api): client, code, harness, _, _ = code_api harness.configurations["other/vendor/model"] = "other" session = await create_session(code_api) path = f"/admin/api/code/sessions/{session['id']}" changed = await client.patch( path, json={ "expected_revision": session["revision"], "mode": "auto_review", "model": "other/vendor/model", }, ) assert changed.status_code == 200 assert changed.json()["mode"] == "auto_review" assert changed.json()["provider_id"] == "other" assert changed.json()["model_name"] == "vendor/model" for mode in (None, "plan", "never"): rejected = await client.patch( path, json={"expected_revision": changed.json()["revision"], "mode": mode} ) assert rejected.status_code == 422 await code.send( session["id"], str(uuid.uuid4()), changed.json()["revision"], "hello", expected_epoch=code.epoch, ) await asyncio.wait_for(harness.started.wait(), 3) detail = (await client.get(path)).json() assert detail["run"]["mode"] == "auto_review" assert "native_permission_defaults" not in detail["session"] assert (await code.get_detail(session["id"])).session.native_permission_defaults rejected = await client.patch( path, json={ "expected_revision": detail["session"]["revision"], "native_permission_defaults": {}, }, ) assert rejected.status_code == 422 @pytest.mark.asyncio async def test_code_library_has_one_harness_and_no_native_work_on_open(code_api): client, _, harness, _, _ = code_api bootstrap = await client.get("/admin/api/code/bootstrap") assert bootstrap.json()["harnesses"] == [{"id": "codex", "name": "Codex"}] session = await create_session(code_api) detail = await client.get(f"/admin/api/code/sessions/{session['id']}") assert detail.status_code == 200 assert detail.headers["cache-control"] == "no-store" assert detail.json()["items"] == [] assert harness.connections == [] page = await client.get(f"/admin/code/{session['id']}") assert page.status_code == 200 @pytest.mark.asyncio async def test_context_usage_and_raw_provider_capacity_are_public_session_state( code_api, ): client, code, harness, _, _ = code_api harness.context_windows[harness.model] = 100_000 harness.configurations["unknown/model"] = "unknown" bootstrap = (await client.get("/admin/api/code/bootstrap")).json() models = {model["id"]: model for model in bootstrap["models"]} assert models[harness.model]["context_window_tokens"] == 100_000 assert models["unknown/model"]["context_window_tokens"] is None session = await create_session(code_api) assert session["context_used_tokens"] is None await code.send( session["id"], str(uuid.uuid4()), session["revision"], "hello", expected_epoch=code.epoch, ) await harness.started.wait() await harness.connections[0].context_usage("turn-1", 12_438) detail = (await client.get(f"/admin/api/code/sessions/{session['id']}")).json() assert detail["session"]["context_used_tokens"] == 12_438 library = (await client.get("/admin/api/code/sessions")).json() assert library["sessions"][0]["context_used_tokens"] == 12_438 @pytest.mark.asyncio async def test_http_send_competition_and_safe_item_projection(code_api): client, code, harness, _, _ = code_api session = await create_session(code_api) path = f"/admin/api/code/sessions/{session['id']}" responses = await asyncio.gather( *( client.post( path + "/turns", json={ "operation_id": str(uuid.uuid4()), "expected_revision": session["revision"], "expected_epoch": code.epoch, "text": text, }, ) for text in ("one", "two") ) ) assert sorted(response.status_code for response in responses) == [202, 409] await harness.started.wait() await harness.connections[0].text( "turn-1", "answer", "**safe** ", complete=True ) await harness.connections[0].finish("turn-1") detail = (await client.get(path)).json() item = detail["items"][-1] assert "safe" in item["html"] assert "