@@ -1694,25 +1694,38 @@ def fake_sync(payload, on_delta, on_tool_call, usage):
16941694
16951695class TestContextWindow (unittest .TestCase ):
16961696 """Client.context_window: config-file overrides -> CONTEXT_WINDOWS
1697- patterns -> DEFAULT_CONTEXT_WINDOW. The resolved value is cached
1698- for the life of the client ."""
1697+ patterns -> DEFAULT_CONTEXT_WINDOW. Resolved on every access (no
1698+ caching), so model switches and config edits take effect at once ."""
16991699
17001700 def _client (self , model : str , config_path : str | None = None ) -> Client :
17011701 c = Client (base_url = "http://x/v1" , api_key = "k" , model = model , config_path = config_path )
17021702 self .addCleanup (c .close )
17031703 return c
17041704
1705+ def test_model_change_re_resolves_window (self ):
1706+ """Changing client.model (runtime /model switch) resolves the
1707+ new model's window on the next access."""
1708+ with tempfile .TemporaryDirectory () as d :
1709+ p = Path (d ) / "config.json"
1710+ p .write_text ('{"context_windows": {"fake*": 999999}}' , encoding = "utf-8" )
1711+ c = self ._client (model = "fake" , config_path = str (p ))
1712+ self .assertEqual (c .context_window , 999_999 )
1713+ c .model = "gpt-5-mini"
1714+ self .assertEqual (c .context_window , 128_000 )
1715+ c .model = "deepseek-v4-flash"
1716+ self .assertEqual (c .context_window , 1_000_000 )
1717+
17051718 def test_config_file_override_wins (self ):
17061719 """A context_windows entry in the config file beats the
1707- built-in table, and the resolved value is cached ."""
1720+ built-in table, and edits are picked up on the next access ."""
17081721 with tempfile .TemporaryDirectory () as d :
17091722 p = Path (d ) / "config.json"
17101723 p .write_text ('{"context_windows": {"fake*": 999999}}' , encoding = "utf-8" )
17111724 c = self ._client (model = "fake" , config_path = str (p ))
17121725 self .assertEqual (c .context_window , 999_999 )
1713- # cached : a later config change must not affect the window
1726+ # no caching : a later config change takes effect immediately
17141727 p .write_text ('{"context_windows": {"fake*": 111111}}' , encoding = "utf-8" )
1715- self .assertEqual (c .context_window , 999_999 )
1728+ self .assertEqual (c .context_window , 111_111 )
17161729
17171730 def test_config_file_wildcard_matching (self ):
17181731 """Config-file patterns support fnmatch wildcards, first match
@@ -1763,11 +1776,16 @@ def test_missing_config_file_uses_table(self):
17631776
17641777 def test_malformed_config_file_falls_back_to_default (self ):
17651778 """A broken context_windows section must not break the loop:
1766- the client caches DEFAULT_CONTEXT_WINDOW."""
1779+ the client uses DEFAULT_CONTEXT_WINDOW for that access, and
1780+ recovers once the file is fixed (failures are not cached)."""
17671781 with tempfile .TemporaryDirectory () as d :
17681782 p = Path (d ) / "config.json"
17691783 p .write_text ('{"context_windows": {"fake": "not-a-number"}}' , encoding = "utf-8" )
1770- self .assertEqual (self ._client ("fake" , str (p )).context_window , 128_000 )
1784+ c = self ._client ("fake" , str (p ))
1785+ self .assertEqual (c .context_window , 128_000 )
1786+ # no caching of the failure: a fixed file is picked up
1787+ p .write_text ('{"context_windows": {"fake*": 999999}}' , encoding = "utf-8" )
1788+ self .assertEqual (c .context_window , 999_999 )
17711789
17721790
17731791if __name__ == "__main__" :
0 commit comments