refactor(server): extract context.py into focused modules

Split the monolithic context.py (117 lines) into five purpose-specific
modules following single-responsibility principle:

- config.py: Server-level constants (DATA_DIR, SESSIONS_DIR, PORT,
  STALE_EVENT_AGE, _state_lock)
- agents.py: Agent-specific paths and caches (CLAUDE_PROJECTS_DIR,
  CODEX_SESSIONS_DIR, discovery caches)
- auth.py: Authentication token generation/validation for spawn endpoint
- spawn_config.py: Spawn feature configuration (PENDING_SPAWNS_DIR,
  rate limiting, projects watcher thread)
- zellij.py: Zellij binary resolution and session management constants

This refactoring improves:
- Code navigation: Find relevant constants by domain, not alphabetically
- Testing: Each module can be tested in isolation
- Import clarity: Mixins import only what they need
- Future maintenance: Changes to one domain don't risk breaking others

All mixins updated to import from new module locations. Tests updated
to use new import paths.

Includes PROPOSED_CODE_FILE_REORGANIZATION_PLAN.md documenting the
rationale and mapping from old to new locations.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
teernisse
2026-02-27 11:05:39 -05:00
parent 69175f08f9
commit 1fb4a82b39
20 changed files with 683 additions and 191 deletions

View File

@@ -1,18 +1,18 @@
import unittest
from unittest.mock import patch
from amc_server.context import _resolve_zellij_bin
from amc_server.zellij import _resolve_zellij_bin
class ContextTests(unittest.TestCase):
def test_resolve_zellij_bin_prefers_which(self):
with patch("amc_server.context.shutil.which", return_value="/custom/bin/zellij"):
with patch("amc_server.zellij.shutil.which", return_value="/custom/bin/zellij"):
self.assertEqual(_resolve_zellij_bin(), "/custom/bin/zellij")
def test_resolve_zellij_bin_falls_back_to_default_name(self):
with patch("amc_server.context.shutil.which", return_value=None), patch(
"amc_server.context.Path.exists", return_value=False
), patch("amc_server.context.Path.is_file", return_value=False):
with patch("amc_server.zellij.shutil.which", return_value=None), patch(
"amc_server.zellij.Path.exists", return_value=False
), patch("amc_server.zellij.Path.is_file", return_value=False):
self.assertEqual(_resolve_zellij_bin(), "zellij")

View File

@@ -336,7 +336,7 @@ class TestDismissSession(unittest.TestCase):
# (if it existed, it would still exist)
def test_tracks_dismissed_codex_session(self):
from amc_server.context import _dismissed_codex_ids
from amc_server.agents import _dismissed_codex_ids
_dismissed_codex_ids.clear()
with tempfile.TemporaryDirectory() as tmpdir:

View File

@@ -27,7 +27,7 @@ class TestGetCodexPaneInfo(unittest.TestCase):
def setUp(self):
self.handler = DummyDiscoveryHandler()
# Clear cache before each test
from amc_server.context import _codex_pane_cache
from amc_server.agents import _codex_pane_cache
_codex_pane_cache["expires"] = 0
_codex_pane_cache["pid_info"] = {}
_codex_pane_cache["cwd_map"] = {}
@@ -80,7 +80,7 @@ class TestGetCodexPaneInfo(unittest.TestCase):
self.assertEqual(pid_info["12345"]["zellij_session"], "myproject")
def test_cache_used_when_fresh(self):
from amc_server.context import _codex_pane_cache
from amc_server.agents import _codex_pane_cache
_codex_pane_cache["pid_info"] = {"cached": {"pane_id": "1", "zellij_session": "s"}}
_codex_pane_cache["cwd_map"] = {"/cached/path": {"session": "s", "pane_id": "1"}}
_codex_pane_cache["expires"] = time.time() + 100
@@ -203,7 +203,7 @@ class TestDiscoverActiveCodexSessions(unittest.TestCase):
def setUp(self):
self.handler = DummyDiscoveryHandler()
# Clear caches
from amc_server.context import _codex_transcript_cache, _dismissed_codex_ids
from amc_server.agents import _codex_transcript_cache, _dismissed_codex_ids
_codex_transcript_cache.clear()
_dismissed_codex_ids.clear()
@@ -236,7 +236,7 @@ class TestDiscoverActiveCodexSessions(unittest.TestCase):
def test_skips_dismissed_sessions(self):
"""Sessions in _dismissed_codex_ids should be skipped."""
from amc_server.context import _dismissed_codex_ids
from amc_server.agents import _dismissed_codex_ids
with tempfile.TemporaryDirectory() as tmpdir:
codex_dir = Path(tmpdir)

View File

@@ -228,7 +228,7 @@ class TestFindCodexTranscriptFile(unittest.TestCase):
def test_codex_sessions_dir_missing_returns_none(self):
with patch("amc_server.mixins.parsing.CODEX_SESSIONS_DIR", Path("/nonexistent")):
# Clear cache to force discovery
from amc_server.context import _codex_transcript_cache
from amc_server.agents import _codex_transcript_cache
_codex_transcript_cache.clear()
result = self.handler._find_codex_transcript_file("abc123")
self.assertIsNone(result)
@@ -238,7 +238,7 @@ class TestFindCodexTranscriptFile(unittest.TestCase):
transcript_file = Path(tmpdir) / "abc123.jsonl"
transcript_file.write_text('{"type": "session_meta"}\n')
from amc_server.context import _codex_transcript_cache
from amc_server.agents import _codex_transcript_cache
_codex_transcript_cache["abc123"] = str(transcript_file)
result = self.handler._find_codex_transcript_file("abc123")
@@ -248,7 +248,7 @@ class TestFindCodexTranscriptFile(unittest.TestCase):
_codex_transcript_cache.clear()
def test_cache_hit_with_deleted_file_returns_none(self):
from amc_server.context import _codex_transcript_cache
from amc_server.agents import _codex_transcript_cache
_codex_transcript_cache["deleted-session"] = "/nonexistent/file.jsonl"
result = self.handler._find_codex_transcript_file("deleted-session")
@@ -257,7 +257,7 @@ class TestFindCodexTranscriptFile(unittest.TestCase):
_codex_transcript_cache.clear()
def test_cache_hit_with_none_returns_none(self):
from amc_server.context import _codex_transcript_cache
from amc_server.agents import _codex_transcript_cache
_codex_transcript_cache["cached-none"] = None
result = self.handler._find_codex_transcript_file("cached-none")
@@ -557,7 +557,7 @@ class TestGetCachedContextUsage(unittest.TestCase):
def setUp(self):
self.handler = DummyParsingHandler()
# Clear cache before each test
from amc_server.context import _context_usage_cache
from amc_server.agents import _context_usage_cache
_context_usage_cache.clear()
def test_nonexistent_file_returns_none(self):

View File

@@ -290,7 +290,7 @@ class TestRateLimiting(unittest.TestCase):
def test_first_spawn_allowed(self):
"""First spawn for a project should not be rate-limited."""
from amc_server.context import _spawn_timestamps
from amc_server.spawn_config import _spawn_timestamps
_spawn_timestamps.clear()
handler = self._make_handler('fresh-project')
@@ -317,7 +317,7 @@ class TestRateLimiting(unittest.TestCase):
def test_rapid_spawn_same_project_rejected(self):
"""Spawning the same project within cooldown returns 429."""
from amc_server.context import _spawn_timestamps
from amc_server.spawn_config import _spawn_timestamps
_spawn_timestamps.clear()
# Pretend we just spawned this project
_spawn_timestamps['rapid-project'] = time.monotonic()
@@ -339,7 +339,7 @@ class TestRateLimiting(unittest.TestCase):
def test_spawn_different_project_allowed(self):
"""Spawning a different project while one is on cooldown succeeds."""
from amc_server.context import _spawn_timestamps
from amc_server.spawn_config import _spawn_timestamps
_spawn_timestamps.clear()
_spawn_timestamps['project-a'] = time.monotonic()
@@ -360,7 +360,7 @@ class TestRateLimiting(unittest.TestCase):
def test_spawn_after_cooldown_allowed(self):
"""Spawning the same project after cooldown expires succeeds."""
from amc_server.context import _spawn_timestamps, SPAWN_COOLDOWN_SEC
from amc_server.spawn_config import _spawn_timestamps, SPAWN_COOLDOWN_SEC
_spawn_timestamps.clear()
# Set timestamp far enough in the past
_spawn_timestamps['cooled-project'] = time.monotonic() - SPAWN_COOLDOWN_SEC - 1