Skip to content

The agent that grows with you

fix: context-length fallback logging, batch trajectory durability, pool cleanup

kshitijk4poora1ff62a139ac
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index e47a10eb07c..c7f16e8ea20 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -2771,7 +2771,14 @@ def get_model_context_length(
if default_model in model_lower:
return length
- # 9. Default fallback — 256K
+ # 9. Default fallback — log so small-context models (8K, 32K) don't
+ # silently get 256K and cause hard-to-debug API failures.
+ logger.warning(
+ "Could not determine context length for model %r (base_url=%s) "
+ "— falling back to %s tokens. Set model.context_length in "
+ "config.yaml to override.",
+ model, base_url or "default", f"{DEFAULT_FALLBACK_CONTEXT:,}",
+ )
return DEFAULT_FALLBACK_CONTEXT
diff --git a/batch_runner.py b/batch_runner.py
index 9e8883275a8..61014b49bd0 100644
--- a/batch_runner.py
+++ b/batch_runner.py
@@ -485,6 +485,8 @@ def _process_batch_worker(args: Tuple) -> Dict[str, Any]:
# Append to batch output file
with open(batch_output_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(trajectory_entry, ensure_ascii=False) + "\n")
+ f.flush()
+ os.fsync(f.fileno())
# Aggregate tool statistics
for tool_name, stats in result.get("tool_stats", {}).items():
@@ -978,8 +980,15 @@ class BatchRunner:
except Exception as ckpt_err:
# Don't fail the run if checkpoint write fails
print(f"⚠️ Warning: Failed to save incremental checkpoint: {ckpt_err}")
+ except KeyboardInterrupt:
+ print("\n⚠️ Interrupted — terminating batch workers...")
+ pool.terminate()
+ pool.join()
+ raise
except Exception as e:
logger.error("Batch worker failed: %s", e, exc_info=True)
+ pool.terminate()
+ pool.join()
raise
finally:
root_logger.setLevel(original_level)
diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py
index 5a3440f0401..6398383bc2a 100644
--- a/tests/agent/test_model_metadata.py
+++ b/tests/agent/test_model_metadata.py
@@ -1177,3 +1177,57 @@ class TestMoAContextLength:
assert compressor.context_length == configured_context
assert compressor.threshold_tokens == configured_context // 2
endpoint_probe.assert_not_called()
+
+
+# =========================================================================
+# Fallback diagnostic logging
+# =========================================================================
+
+class TestFallbackWarning:
+ """When all 9 detection methods fail, the 10th fallback should log a
+ warning so users with small-context models (8K, 32K) don't silently get
+ 256K and hit hard-to-debug API context-length errors.
+ """
+
+ def test_warning_emitted_on_fallback(self, caplog):
+ import logging
+
+ with patch("agent.model_metadata.get_cached_context_length", return_value=None), \
+ patch("agent.model_metadata.fetch_model_metadata", return_value={}), \
+ patch("agent.model_metadata.fetch_endpoint_model_metadata", return_value={}), \
+ patch("agent.model_metadata._query_ollama_api_show", return_value=None), \
+ patch("agent.model_metadata._query_anthropic_context_length", return_value=None), \
+ patch("agent.model_metadata._endpoint_scoped_context_length", return_value=None), \
+ patch("agent.model_metadata._resolve_endpoint_context_length", return_value=None), \
+ patch("agent.models_dev.lookup_models_dev_context", return_value=None):
+ with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
+ result = get_model_context_length(
+ "totally-unknown-model-xyz",
+ )
+
+ assert result == DEFAULT_FALLBACK_CONTEXT
+ # The warning must mention the model name and the config override hint.
+ warning_msgs = [r for r in caplog.records if r.levelno == logging.WARNING]
+ assert any("totally-unknown-model-xyz" in r.getMessage() for r in warning_msgs)
+ assert any("model.context_length" in r.getMessage() for r in warning_msgs)
+
+ def test_no_warning_when_cached(self, caplog):
+ """No fallback warning when the context length is found in the cache."""
+ import logging
+
+ with patch(
+ "agent.model_metadata.get_cached_context_length",
+ return_value=32_000,
+ ):
+ with caplog.at_level(logging.WARNING, logger="agent.model_metadata"):
+ result = get_model_context_length(
+ "some-model",
+ base_url="http://127.0.0.1:1/v1",
+ )
+
+ assert result == 32_000
+ fallback_warnings = [
+ r for r in caplog.records
+ if r.levelno == logging.WARNING and "falling back" in r.getMessage()
+ ]
+ assert len(fallback_warnings) == 0
diff --git a/tests/test_batch_runner_durability.py b/tests/test_batch_runner_durability.py
new file mode 100644
index 00000000000..bece9da7b07
--- /dev/null
+++ b/tests/test_batch_runner_durability.py
@@ -0,0 +1,187 @@
+"""Tests for batch_runner trajectory durability and pool cleanup.
+
+Verifies:
+ 1. Trajectory entries are fsync'd to disk before the checkpoint marks
+ them as completed (crash-between-write-and-sync safety).
+ 2. Pool.terminate() + pool.join() are called on KeyboardInterrupt and
+ Exception during batch execution (responsive worker shutdown).
+"""
+
+import json
+import os
+import sys
+from pathlib import Path
+from unittest.mock import MagicMock, patch, call
+
+import pytest
+
+# batch_runner uses relative imports, ensure project root is on path
+sys.path.insert(0, str(Path(__file__).parent.parent))
+
+from batch_runner import BatchRunner, _process_batch_worker
+
+
+# =========================================================================
+# Trajectory write durability (fsync)
+# =========================================================================
+
+class TestTrajectoryWriteDurability:
+ """Verify that trajectory entries are flushed and fsync'd before the
+ checkpoint marks them as completed.
+
+ Without fsync, a crash between the write and the disk sync would leave
+ the checkpoint claiming completion with no trajectory data on disk.
+ """
+
+ def test_trajectory_entry_is_synced_to_disk(self, tmp_path, monkeypatch):
+ """_process_batch_worker should flush+fsync the trajectory file."""
+ prompt_result = {
+ "success": True,
+ "trajectory": [{"role": "assistant", "content": "x"}],
+ "reasoning_stats": {"has_any_reasoning": True},
+ "tool_stats": {},
+ "metadata": {},
+ "completed": True,
+ "api_calls": 1,
+ "toolsets_used": [],
+ }
+
+ monkeypatch.setattr(
+ "batch_runner._process_single_prompt", lambda *a, **kw: prompt_result
+ )
+
+ # Intercept os.fsync to record calls
+ fsync_calls = []
+ original_fsync = os.fsync
+
+ def mock_fsync(fd):
+ fsync_calls.append(fd)
+
+ monkeypatch.setattr("os.fsync", mock_fsync)
+
+ result = _process_batch_worker(
+ (
+ 1,
+ [(0, {"prompt": "hi"})],
+ tmp_path,
+ set(),
+ {"verbose": False},
+ )
+ )
+
+ # Verify fsync was called at least once during trajectory write
+ assert len(fsync_calls) >= 1, (
+ "os.fsync was not called — trajectory writes are not durable"
+ )
+
+ # Verify the trajectory file exists and is valid
+ output_files = list(tmp_path.glob("*.jsonl"))
+ assert len(output_files) >= 1
+ for f in output_files:
+ lines = f.read_text().strip().split("\n")
+ for line in lines:
+ if line:
+ entry = json.loads(line)
+ assert "conversations" in entry
+ assert "completed" in entry
+
+
+# =========================================================================
+# Pool cleanup on interruption / exception
+# =========================================================================
+
+class TestPoolCleanupOnInterruption:
+ """Verify that pool.terminate() + pool.join() are called when a
+ KeyboardInterrupt or Exception occurs during batch execution.
+
+ CPython's multiprocessing.pool.Pool.join() does NOT accept a timeout
+ parameter — calling pool.join(timeout=10) raises TypeError. The fix
+ uses pool.terminate() followed by pool.join() (no timeout), which is
+ the correct shutdown pattern.
+ """
+
+ def test_pool_terminate_called_on_exception(self, tmp_path, monkeypatch):
+ """When pool.imap_unordered raises an exception, pool.terminate()
+ and pool.join() must be called for clean worker shutdown.
+
+ We simulate the relevant slice of run()'s try/except block with a
+ mock pool to verify the cleanup contract.
+ """
+ mock_pool = MagicMock()
+ mock_pool.imap_unordered.side_effect = RuntimeError("worker exploded")
+
+ # Reproduce the exception-handling block from batch_runner.run()
+ with pytest.raises(RuntimeError, match="worker exploded"):
+ try:
+ for result in mock_pool.imap_unordered(None, []):
+ pass
+ except KeyboardInterrupt:
+ mock_pool.terminate()
+ mock_pool.join()
+ raise
+ except Exception:
+ mock_pool.terminate()
+ mock_pool.join()
+ raise
+
+ mock_pool.terminate.assert_called_once()
+ mock_pool.join.assert_called_once_with()
+
+ def test_pool_terminate_called_on_keyboard_interrupt(self, tmp_path, monkeypatch):
+ """When pool.imap_unordered is interrupted (Ctrl+C), pool.terminate()
+ and pool.join() must be called for responsive shutdown."""
+ mock_pool = MagicMock()
+ mock_pool.imap_unordered.side_effect = KeyboardInterrupt()
+
+ with pytest.raises(KeyboardInterrupt):
+ try:
+ for result in mock_pool.imap_unordered(None, []):
+ pass
+ except KeyboardInterrupt:
+ mock_pool.terminate()
+ mock_pool.join()
+ raise
+ except Exception:
+ mock_pool.terminate()
+ mock_pool.join()
+ raise
+
+ mock_pool.terminate.assert_called_once()
+ mock_pool.join.assert_called_once_with()
+
+ def test_pool_join_called_without_timeout(self, tmp_path):
+ """Pool.join() must NOT be called with a timeout argument —
+ CPython's Pool.join signature is (self), so join(timeout=10)
+ would raise TypeError."""
+ mock_pool = MagicMock()
+ mock_pool.imap_unordered.side_effect = RuntimeError("boom")
+
+ with pytest.raises(RuntimeError):
+ try:
+ for result in mock_pool.imap_unordered(None, []):
+ pass
+ except Exception:
+ mock_pool.terminate()
+ mock_pool.join()
+ raise
+
+ # The join call must have no positional/keyword timeout argument
+ join_call = mock_pool.join.call_args
+ assert join_call == call(), (
+ f"pool.join() called with unexpected args: {join_call}"
+ )
+
+ def test_real_pool_join_accepts_no_timeout(self):
+ """Integration check: a real multiprocessing.Pool's join() must not
+ accept a timeout kwarg. This guards against re-introducing
+ pool.join(timeout=10), which raises TypeError on CPython.
+ """
+ import inspect
+ import multiprocessing.pool
+
+ sig = inspect.signature(multiprocessing.pool.Pool.join)
+ params = list(sig.parameters.keys())
+ # The only parameter should be 'self' — no 'timeout'
+ assert "timeout" not in params, (
+ f"Pool.join has unexpected parameters: {params}"
+ )