lewtun HF Staff OpenAI Codex commited on
Commit
9dc2054
·
2 Parent(s): 479aaead32b803

Deploy 2026-06-02

Browse files

Co-authored-by: OpenAI Codex <codex@openai.com>

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. AGENTS.md +1 -1
  2. agent/__init__.py +2 -4
  3. agent/config.py +4 -5
  4. agent/context_manager/manager.py +44 -18
  5. agent/core/agent_loop.py +31 -84
  6. agent/core/effort_probe.py +9 -15
  7. agent/core/llm_params.py +46 -149
  8. agent/core/model_ids.py +41 -0
  9. agent/core/model_switcher.py +23 -28
  10. agent/core/prompt_caching.py +0 -65
  11. agent/core/redact.py +4 -6
  12. agent/core/session.py +93 -8
  13. agent/core/session_persistence.py +12 -0
  14. agent/core/session_resume.py +4 -2
  15. agent/core/session_uploader.py +1 -1
  16. agent/core/telemetry.py +5 -8
  17. agent/main.py +63 -6
  18. agent/prompts/system_prompt_v3.yaml +2 -0
  19. agent/tools/plan_tool.py +7 -0
  20. agent/tools/research_tool.py +11 -22
  21. agent/utils/terminal_display.py +2 -0
  22. backend/dependencies.py +1 -3
  23. backend/models.py +2 -0
  24. backend/routes/agent.py +85 -82
  25. backend/session_manager.py +375 -57
  26. backend/user_quotas.py +7 -6
  27. configs/cli_agent_config.json +1 -1
  28. configs/frontend_agent_config.json +1 -1
  29. frontend/src/components/Chat/ActivityStatusBar.tsx +1 -1
  30. frontend/src/components/Chat/BillingBanner.tsx +81 -0
  31. frontend/src/components/Chat/ChatErrorBanner.tsx +92 -0
  32. frontend/src/components/Chat/ChatInput.tsx +73 -107
  33. frontend/src/components/ClaudeCapDialog.tsx +0 -141
  34. frontend/src/components/SessionChat.tsx +85 -18
  35. frontend/src/hooks/useAgentChat.ts +73 -35
  36. frontend/src/hooks/useUserQuota.ts +3 -3
  37. frontend/src/lib/sse-chat-transport.ts +17 -8
  38. frontend/src/store/agentStore.ts +0 -5
  39. frontend/src/store/sessionStore.ts +27 -1
  40. frontend/src/types/agent.ts +9 -0
  41. frontend/src/utils/model.ts +11 -6
  42. scripts/build_kpis.py +2 -2
  43. scripts/build_sft.py +1 -1
  44. scripts/prioritize_backlog.py +2 -2
  45. tests/integration/test_live_thinking_models.py +15 -62
  46. tests/unit/test_agent_model_gating.py +123 -169
  47. tests/unit/test_build_kpis.py +13 -13
  48. tests/unit/test_build_sft.py +2 -2
  49. tests/unit/test_cli_local_models.py +17 -4
  50. tests/unit/test_cli_new_clear.py +147 -0
AGENTS.md CHANGED
@@ -13,7 +13,7 @@ Notes:
13
  - Vite proxies `/api` and `/auth` to `http://localhost:7860`.
14
  - If `127.0.0.1:7860` is already owned by another local process, binding the backend to `::1` lets the Vite proxy resolve `localhost` cleanly.
15
  - Prefer `npm ci` over `npm install` for setup, since `npm install` may rewrite `frontend/package-lock.json` metadata depending on npm version.
16
- - Production defaults to the Bedrock Claude model. For local development with a personal Anthropic key, set `ANTHROPIC_API_KEY` and `ML_INTERN_CLAUDE_MODEL_ID=anthropic/claude-opus-4-6` before starting the backend. Other models are selected through the app's model switcher.
17
 
18
  ## Development Checks
19
 
 
13
  - Vite proxies `/api` and `/auth` to `http://localhost:7860`.
14
  - If `127.0.0.1:7860` is already owned by another local process, binding the backend to `::1` lets the Vite proxy resolve `localhost` cleanly.
15
  - Prefer `npm ci` over `npm install` for setup, since `npm install` may rewrite `frontend/package-lock.json` metadata depending on npm version.
16
+ - Production defaults to Claude Opus 4.8 through the HF Router (`anthropic/claude-opus-4.8:fal-ai`). Non-local LLM calls use `https://router.huggingface.co/v1` with Hugging Face tokens. Subsidized premium calls use the Space/operator `INFERENCE_TOKEN` plus `X-HF-Bill-To` from `HF_BILL_TO` (default `smolagents`); after the daily allowance, premium calls bill the user's own HF token. For local development, set `HF_TOKEN` and optionally `ML_INTERN_DEFAULT_MODEL_ID`.
17
 
18
  ## Development Checks
19
 
agent/__init__.py CHANGED
@@ -8,10 +8,8 @@ import litellm
8
  # backend entries share the same config.
9
  # drop_params: quietly drop unsupported params rather than raising
10
  # suppress_debug_info: hide the noisy "Give Feedback" banner on errors
11
- # modify_params: let LiteLLM patch Anthropic's tool-call requirements
12
- # (synthesize a dummy tool spec when we call completion on a history
13
- # that contains tool_calls but aren't passing `tools=` — happens
14
- # during summarization / session seeding).
15
  litellm.drop_params = True
16
  litellm.suppress_debug_info = True
17
  litellm.modify_params = True
 
8
  # backend entries share the same config.
9
  # drop_params: quietly drop unsupported params rather than raising
10
  # suppress_debug_info: hide the noisy "Give Feedback" banner on errors
11
+ # modify_params: let LiteLLM patch provider-specific schema requirements
12
+ # for router-compatible request bodies when possible.
 
 
13
  litellm.drop_params = True
14
  litellm.suppress_debug_info = True
15
  litellm.modify_params = True
agent/config.py CHANGED
@@ -51,12 +51,11 @@ class Config(BaseModel):
51
  # Reasoning effort *preference* — the ceiling the user wants. The probe
52
  # on `/model` walks a cascade down from here (``max`` → ``xhigh`` → ``high``
53
  # → …) and caches per-model what the provider actually accepted in
54
- # ``Session.model_effective_effort``. Default ``max`` because we'd rather
55
- # burn tokens thinking than ship a wrong ML recipe; the cascade lands on
56
- # whichever level the model supports (``high`` for GPT-5 / HF router,
57
- # ``xhigh`` or ``max`` for Anthropic 4.6 / 4.7). ``None`` = thinking off.
58
  # Valid values: None | "minimal" | "low" | "medium" | "high" | "xhigh" | "max"
59
- reasoning_effort: str | None = "max"
60
  messaging: MessagingConfig = MessagingConfig()
61
 
62
 
 
51
  # Reasoning effort *preference* — the ceiling the user wants. The probe
52
  # on `/model` walks a cascade down from here (``max`` → ``xhigh`` → ``high``
53
  # → …) and caches per-model what the provider actually accepted in
54
+ # ``Session.model_effective_effort``. Default ``high`` because HF Router
55
+ # accepts low/medium/high generically and provider-specific higher levels
56
+ # should be discovered through explicit probes. ``None`` = thinking off.
 
57
  # Valid values: None | "minimal" | "low" | "medium" | "high" | "xhigh" | "max"
58
+ reasoning_effort: str | None = "high"
59
  messaging: MessagingConfig = MessagingConfig()
60
 
61
 
agent/context_manager/manager.py CHANGED
@@ -13,8 +13,6 @@ import yaml
13
  from jinja2 import Template
14
  from litellm import Message, acompletion
15
 
16
- from agent.core.prompt_caching import with_prompt_caching
17
-
18
  logger = logging.getLogger(__name__)
19
 
20
  _HF_WHOAMI_URL = "https://huggingface.co/api/whoami-v2"
@@ -91,8 +89,8 @@ class CompactionFailedError(Exception):
91
 
92
  Typically means an individual preserved message (system, first user, or
93
  untouched tail) exceeds what truncation can fix in one pass. The caller
94
- must terminate the session — retrying produces an infinite loop that
95
- burns Bedrock budget for free (~$3 per re-attempt on Opus).
96
  """
97
 
98
 
@@ -133,18 +131,19 @@ async def summarize_messages(
133
  ``session`` is optional; when provided, the call is recorded via
134
  ``telemetry.record_llm_call`` so its cost lands in the session's
135
  ``total_cost_usd``. Without it, the call still happens but is
136
- invisible in telemetry — which used to be the case for every
137
- compaction call until 2026-04-29 (~30-50% of Bedrock spend was
138
- attributed to this single source of dark cost).
139
 
140
  Returns ``(summary_text, completion_tokens)``.
141
  """
142
  from agent.core.llm_params import _resolve_llm_params
143
 
144
  prompt_messages = list(messages) + [Message(role="user", content=prompt)]
145
- llm_params = _resolve_llm_params(model_name, hf_token, reasoning_effort="high")
146
- prompt_messages, tool_specs = with_prompt_caching(
147
- prompt_messages, tool_specs, llm_params.get("model")
 
 
148
  )
149
  _t0 = time.monotonic()
150
  response = await acompletion(
@@ -184,9 +183,13 @@ class ContextManager:
184
  hf_token: str | None = None,
185
  local_mode: bool = False,
186
  ):
 
 
 
 
187
  self.system_prompt = self._load_system_prompt(
188
- tool_specs or [],
189
- prompt_file_suffix="system_prompt_v3.yaml",
190
  hf_token=hf_token,
191
  local_mode=local_mode,
192
  )
@@ -203,6 +206,30 @@ class ContextManager:
203
  self.items: list[Message] = [Message(role="system", content=self.system_prompt)]
204
  self.on_message_added = None
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  def _load_system_prompt(
207
  self,
208
  tool_specs: list[dict[str, Any]],
@@ -441,9 +468,8 @@ class ContextManager:
441
  )
442
  # Preserve all known assistant-side fields (tool_calls, thinking_blocks,
443
  # reasoning_content, provider_specific_fields) even when content is
444
- # replaced. Anthropic extended-thinking models reject the next request
445
- # with "Invalid signature in thinking block" if thinking_blocks is
446
- # dropped from a prior assistant message.
447
  kept = {
448
  k: getattr(msg, k, None)
449
  for k in (
@@ -493,7 +519,7 @@ class ContextManager:
493
  a giant tool output stuck in the untouched tail) is too large for
494
  truncation to fix. The caller must terminate the session — retrying
495
  is what caused the 2026-05-03 infinite-compaction-loop pattern that
496
- burned Bedrock budget invisibly.
497
  """
498
  if not self.needs_compaction:
499
  return
@@ -521,7 +547,7 @@ class ContextManager:
521
  # otherwise recent_messages overlaps with the messages we put in
522
  # head". The walk-back's `idx > 1` guard is necessary (no system in
523
  # recent) but insufficient (first_user is also in head and would be
524
- # duplicated). Anthropic API rejects two consecutive user messages
525
  # with a 400 — bot review on PR #213 caught this on the second clamp
526
  # iteration.
527
  if idx <= first_user_idx:
@@ -583,7 +609,7 @@ class ContextManager:
583
 
584
  # Hard verify: if compaction didn't bring us below the threshold even
585
  # after truncating oversized preserved messages, retrying just burns
586
- # Bedrock budget on the same useless compaction call. Raise so the
587
  # caller can terminate the session cleanly. Pre-2026-05-04, the
588
  # caller looped indefinitely (~$3/Opus retry) until the pod was
589
  # killed — invisible to the dataset because the session never
 
13
  from jinja2 import Template
14
  from litellm import Message, acompletion
15
 
 
 
16
  logger = logging.getLogger(__name__)
17
 
18
  _HF_WHOAMI_URL = "https://huggingface.co/api/whoami-v2"
 
89
 
90
  Typically means an individual preserved message (system, first user, or
91
  untouched tail) exceeds what truncation can fix in one pass. The caller
92
+ must terminate the session; retrying produces an infinite loop that burns
93
+ premium inference budget.
94
  """
95
 
96
 
 
131
  ``session`` is optional; when provided, the call is recorded via
132
  ``telemetry.record_llm_call`` so its cost lands in the session's
133
  ``total_cost_usd``. Without it, the call still happens but is
134
+ invisible in telemetry, which used to hide a significant share of premium
135
+ inference spend.
 
136
 
137
  Returns ``(summary_text, completion_tokens)``.
138
  """
139
  from agent.core.llm_params import _resolve_llm_params
140
 
141
  prompt_messages = list(messages) + [Message(role="user", content=prompt)]
142
+ llm_params = _resolve_llm_params(
143
+ model_name,
144
+ hf_token,
145
+ reasoning_effort="high",
146
+ bill_to_user=getattr(session, "premium_user_billed", False),
147
  )
148
  _t0 = time.monotonic()
149
  response = await acompletion(
 
183
  hf_token: str | None = None,
184
  local_mode: bool = False,
185
  ):
186
+ self.prompt_file_suffix = prompt_file_suffix
187
+ self.tool_specs = tool_specs or []
188
+ self.hf_token = hf_token
189
+ self.local_mode = local_mode
190
  self.system_prompt = self._load_system_prompt(
191
+ self.tool_specs,
192
+ prompt_file_suffix=self.prompt_file_suffix,
193
  hf_token=hf_token,
194
  local_mode=local_mode,
195
  )
 
206
  self.items: list[Message] = [Message(role="system", content=self.system_prompt)]
207
  self.on_message_added = None
208
 
209
+ def refresh_system_prompt(
210
+ self,
211
+ *,
212
+ tool_specs: list[dict[str, Any]] | None = None,
213
+ hf_token: str | None = None,
214
+ local_mode: bool | None = None,
215
+ ) -> Message:
216
+ """Re-render the system prompt and return it as a system message."""
217
+ if tool_specs is not None:
218
+ self.tool_specs = tool_specs
219
+ if hf_token is not None:
220
+ self.hf_token = hf_token
221
+ if local_mode is not None:
222
+ self.local_mode = local_mode
223
+ self.system_prompt = self._load_system_prompt(
224
+ self.tool_specs,
225
+ prompt_file_suffix=getattr(
226
+ self, "prompt_file_suffix", "system_prompt_v3.yaml"
227
+ ),
228
+ hf_token=getattr(self, "hf_token", None),
229
+ local_mode=getattr(self, "local_mode", False),
230
+ )
231
+ return Message(role="system", content=self.system_prompt)
232
+
233
  def _load_system_prompt(
234
  self,
235
  tool_specs: list[dict[str, Any]],
 
468
  )
469
  # Preserve all known assistant-side fields (tool_calls, thinking_blocks,
470
  # reasoning_content, provider_specific_fields) even when content is
471
+ # replaced. Historical traces may still contain provider reasoning
472
+ # metadata, and truncation should not silently discard it.
 
473
  kept = {
474
  k: getattr(msg, k, None)
475
  for k in (
 
519
  a giant tool output stuck in the untouched tail) is too large for
520
  truncation to fix. The caller must terminate the session — retrying
521
  is what caused the 2026-05-03 infinite-compaction-loop pattern that
522
+ burned premium inference budget invisibly.
523
  """
524
  if not self.needs_compaction:
525
  return
 
547
  # otherwise recent_messages overlaps with the messages we put in
548
  # head". The walk-back's `idx > 1` guard is necessary (no system in
549
  # recent) but insufficient (first_user is also in head and would be
550
+ # duplicated). Chat providers can reject two consecutive user messages
551
  # with a 400 — bot review on PR #213 caught this on the second clamp
552
  # iteration.
553
  if idx <= first_user_idx:
 
609
 
610
  # Hard verify: if compaction didn't bring us below the threshold even
611
  # after truncating oversized preserved messages, retrying just burns
612
+ # premium inference budget on the same useless compaction call. Raise so the
613
  # caller can terminate the session cleanly. Pre-2026-05-04, the
614
  # caller looped indefinitely (~$3/Opus retry) until the pod was
615
  # killed — invisible to the dataset because the session never
agent/core/agent_loop.py CHANGED
@@ -14,7 +14,6 @@ from litellm import (
14
  ChatCompletionMessageToolCall,
15
  Message,
16
  acompletion,
17
- stream_chunk_builder,
18
  )
19
  from litellm.exceptions import ContextWindowExceededError
20
 
@@ -28,7 +27,6 @@ from agent.messaging.gateway import NotificationGateway
28
  from agent.core import telemetry
29
  from agent.core.doom_loop import check_for_doom_loop
30
  from agent.core.llm_params import _resolve_llm_params
31
- from agent.core.prompt_caching import with_prompt_caching
32
  from agent.core.session import DEFAULT_SESSION_LOG_DIR, Event, OpType, Session
33
  from agent.core.tools import ToolRouter
34
  from agent.tools.jobs_tool import CPU_FLAVORS
@@ -405,7 +403,7 @@ async def _record_manual_approved_spend_if_needed(
405
  # -- LLM retry constants --------------------------------------------------
406
  _MAX_LLM_RETRIES = 3
407
  _LLM_RETRY_DELAYS = [5, 15, 30] # seconds between retries
408
- _LLM_RATE_LIMIT_RETRY_DELAYS = [30, 60] # exceed Bedrock's ~60s TPM bucket window
409
 
410
 
411
  def _is_rate_limit_error(error: Exception) -> bool:
@@ -541,6 +539,7 @@ async def _heal_effort_and_rebuild_params(
541
  model,
542
  session.hf_token,
543
  reasoning_effort=session.effective_effort_for(model),
 
544
  )
545
 
546
 
@@ -554,11 +553,8 @@ def _friendly_error_message(error: Exception) -> str | None:
554
  or "invalid x-api-key" in err_str
555
  ):
556
  return (
557
- "Authentication failed — your API key is missing or invalid.\n\n"
558
- "To fix this, set the API key for your model provider:\n"
559
- " • Anthropic: export ANTHROPIC_API_KEY=sk-...\n"
560
- " • OpenAI: export OPENAI_API_KEY=sk-...\n"
561
- " • HF Router: export HF_TOKEN=hf_...\n\n"
562
  "You can also add it to a .env file in the project root.\n"
563
  "To switch models, use the /model command."
564
  )
@@ -594,9 +590,8 @@ async def _compact_and_notify(session: Session) -> None:
594
 
595
  Catches ``CompactionFailedError`` and ends the session cleanly instead
596
  of letting the caller retry. Pre-2026-05-04 the caller looped on
597
- ContextWindowExceededError → compact → re-trigger, burning Bedrock
598
- budget at ~$3/Opus retry while the session never reached the upload
599
- path (so the cost was invisible in the dataset).
600
  """
601
  from agent.context_manager.manager import CompactionFailedError
602
 
@@ -698,38 +693,10 @@ class LLMResult:
698
  token_count: int
699
  finish_reason: str | None
700
  usage: dict = field(default_factory=dict)
701
- thinking_blocks: list[dict[str, Any]] | None = None
702
- reasoning_content: str | None = None
703
-
704
-
705
- def _extract_thinking_state(
706
- message: Any,
707
- ) -> tuple[list[dict[str, Any]] | None, str | None]:
708
- """Return provider reasoning fields that must be replayed after tool calls."""
709
- provider_fields = getattr(message, "provider_specific_fields", None)
710
- if not isinstance(provider_fields, dict):
711
- provider_fields = {}
712
-
713
- thinking_blocks = (
714
- getattr(message, "thinking_blocks", None)
715
- or provider_fields.get("thinking_blocks")
716
- or None
717
- )
718
- reasoning_content = (
719
- getattr(message, "reasoning_content", None)
720
- or provider_fields.get("reasoning_content")
721
- or None
722
- )
723
- return thinking_blocks, reasoning_content
724
-
725
-
726
- def _should_replay_thinking_state(model_name: str | None) -> bool:
727
- """Only Anthropic's native adapter accepts replayed thinking metadata."""
728
- return bool(model_name and model_name.startswith("anthropic/"))
729
 
730
 
731
  def _is_invalid_thinking_signature_error(exc: Exception) -> bool:
732
- """Return True when Anthropic rejected replayed extended-thinking state."""
733
  text = str(exc)
734
  return (
735
  "Invalid `signature` in `thinking` block" in text
@@ -818,7 +785,7 @@ async def _maybe_heal_invalid_thinking_signature(
818
  data={
819
  "tool": "system",
820
  "log": (
821
- "Anthropic rejected stale thinking signatures; retrying "
822
  "without replayed thinking metadata."
823
  ),
824
  },
@@ -830,21 +797,15 @@ async def _maybe_heal_invalid_thinking_signature(
830
  def _assistant_message_from_result(
831
  llm_result: LLMResult,
832
  *,
833
- model_name: str | None,
834
  tool_calls: list[ToolCall] | None = None,
835
  ) -> Message:
836
- """Build an assistant history message without dropping reasoning state."""
837
  kwargs: dict[str, Any] = {
838
  "role": "assistant",
839
  "content": llm_result.content,
840
  }
841
  if tool_calls is not None:
842
  kwargs["tool_calls"] = tool_calls
843
- if _should_replay_thinking_state(model_name):
844
- if llm_result.thinking_blocks:
845
- kwargs["thinking_blocks"] = llm_result.thinking_blocks
846
- if llm_result.reasoning_content:
847
- kwargs["reasoning_content"] = llm_result.reasoning_content
848
  return Message(**kwargs)
849
 
850
 
@@ -855,7 +816,6 @@ async def _call_llm_streaming(
855
  response = None
856
  _healed_effort = False # one-shot safety net per call
857
  _healed_thinking_signature = False
858
- messages, tools = with_prompt_caching(messages, tools, llm_params.get("model"))
859
  t_start = time.monotonic()
860
  for _llm_attempt in range(_MAX_LLM_RETRIES):
861
  try:
@@ -924,11 +884,8 @@ async def _call_llm_streaming(
924
  token_count = 0
925
  finish_reason = None
926
  final_usage_chunk = None
927
- chunks = []
928
- should_replay_thinking = _should_replay_thinking_state(llm_params.get("model"))
929
 
930
  async for chunk in response:
931
- chunks.append(chunk)
932
  if session.is_cancelled:
933
  tool_calls_acc.clear()
934
  break
@@ -982,27 +939,12 @@ async def _call_llm_streaming(
982
  latency_ms=int((time.monotonic() - t_start) * 1000),
983
  finish_reason=finish_reason,
984
  )
985
- thinking_blocks = None
986
- reasoning_content = None
987
- if chunks and should_replay_thinking:
988
- try:
989
- rebuilt = stream_chunk_builder(chunks, messages=messages)
990
- if rebuilt and getattr(rebuilt, "choices", None):
991
- rebuilt_msg = rebuilt.choices[0].message
992
- thinking_blocks, reasoning_content = _extract_thinking_state(
993
- rebuilt_msg
994
- )
995
- except Exception:
996
- logger.debug("Failed to rebuild streaming thinking state", exc_info=True)
997
-
998
  return LLMResult(
999
  content=full_content or None,
1000
  tool_calls_acc=tool_calls_acc,
1001
  token_count=token_count,
1002
  finish_reason=finish_reason,
1003
  usage=usage,
1004
- thinking_blocks=thinking_blocks,
1005
- reasoning_content=reasoning_content,
1006
  )
1007
 
1008
 
@@ -1013,7 +955,6 @@ async def _call_llm_non_streaming(
1013
  response = None
1014
  _healed_effort = False
1015
  _healed_thinking_signature = False
1016
- messages, tools = with_prompt_caching(messages, tools, llm_params.get("model"))
1017
  t_start = time.monotonic()
1018
  for _llm_attempt in range(_MAX_LLM_RETRIES):
1019
  try:
@@ -1081,7 +1022,6 @@ async def _call_llm_non_streaming(
1081
  content = message.content or None
1082
  finish_reason = choice.finish_reason
1083
  token_count = response.usage.total_tokens if response.usage else 0
1084
- thinking_blocks, reasoning_content = _extract_thinking_state(message)
1085
 
1086
  # Build tool_calls_acc in the same format as streaming
1087
  tool_calls_acc: dict[int, dict] = {}
@@ -1116,8 +1056,6 @@ async def _call_llm_non_streaming(
1116
  token_count=token_count,
1117
  finish_reason=finish_reason,
1118
  usage=usage,
1119
- thinking_blocks=thinking_blocks,
1120
- reasoning_content=reasoning_content,
1121
  )
1122
 
1123
 
@@ -1256,6 +1194,7 @@ class Handlers:
1256
  reasoning_effort=session.effective_effort_for(
1257
  session.config.model_name
1258
  ),
 
1259
  )
1260
  if session.stream:
1261
  llm_result = await _call_llm_streaming(
@@ -1296,10 +1235,7 @@ class Handlers:
1296
  " • For other tools: reduce the size of your arguments or use bash."
1297
  )
1298
  if content:
1299
- assistant_msg = _assistant_message_from_result(
1300
- llm_result,
1301
- model_name=llm_params.get("model"),
1302
- )
1303
  session.context_manager.add_message(assistant_msg, token_count)
1304
  session.context_manager.add_message(
1305
  Message(role="user", content=f"[SYSTEM: {truncation_hint}]")
@@ -1356,10 +1292,7 @@ class Handlers:
1356
  _NO_TOOL_INCOMPLETE_PLAN_RETRY_LIMIT,
1357
  )
1358
  if content:
1359
- assistant_msg = _assistant_message_from_result(
1360
- llm_result,
1361
- model_name=llm_params.get("model"),
1362
- )
1363
  session.context_manager.add_message(
1364
  assistant_msg, token_count
1365
  )
@@ -1403,10 +1336,7 @@ class Handlers:
1403
  (content or "")[:500],
1404
  )
1405
  if content:
1406
- assistant_msg = _assistant_message_from_result(
1407
- llm_result,
1408
- model_name=llm_params.get("model"),
1409
- )
1410
  session.context_manager.add_message(assistant_msg, token_count)
1411
  final_response = content
1412
  break
@@ -1433,7 +1363,6 @@ class Handlers:
1433
  # Add assistant message with all tool calls to context
1434
  assistant_msg = _assistant_message_from_result(
1435
  llm_result,
1436
- model_name=llm_params.get("model"),
1437
  tool_calls=tool_calls,
1438
  )
1439
  session.context_manager.add_message(assistant_msg, token_count)
@@ -1756,6 +1685,19 @@ class Handlers:
1756
  logger.warning("Undo: no user message found to remove")
1757
  await session.send_event(Event(event_type="undo_complete"))
1758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1759
  @staticmethod
1760
  async def resume(session: Session, path: str) -> None:
1761
  """Reload context from a saved session log into the active session."""
@@ -2058,6 +2000,11 @@ async def process_submission(session: Session, submission) -> bool:
2058
  await Handlers.undo(session)
2059
  return True
2060
 
 
 
 
 
 
2061
  if op.op_type == OpType.RESUME:
2062
  path = op.data.get("path") if op.data else None
2063
  if path:
 
14
  ChatCompletionMessageToolCall,
15
  Message,
16
  acompletion,
 
17
  )
18
  from litellm.exceptions import ContextWindowExceededError
19
 
 
27
  from agent.core import telemetry
28
  from agent.core.doom_loop import check_for_doom_loop
29
  from agent.core.llm_params import _resolve_llm_params
 
30
  from agent.core.session import DEFAULT_SESSION_LOG_DIR, Event, OpType, Session
31
  from agent.core.tools import ToolRouter
32
  from agent.tools.jobs_tool import CPU_FLAVORS
 
403
  # -- LLM retry constants --------------------------------------------------
404
  _MAX_LLM_RETRIES = 3
405
  _LLM_RETRY_DELAYS = [5, 15, 30] # seconds between retries
406
+ _LLM_RATE_LIMIT_RETRY_DELAYS = [30, 60]
407
 
408
 
409
  def _is_rate_limit_error(error: Exception) -> bool:
 
539
  model,
540
  session.hf_token,
541
  reasoning_effort=session.effective_effort_for(model),
542
+ bill_to_user=getattr(session, "premium_user_billed", False),
543
  )
544
 
545
 
 
553
  or "invalid x-api-key" in err_str
554
  ):
555
  return (
556
+ "Authentication failed - your Hugging Face token is missing or invalid.\n\n"
557
+ "To fix this, set HF_TOKEN=hf_... or run `hf auth login`.\n\n"
 
 
 
558
  "You can also add it to a .env file in the project root.\n"
559
  "To switch models, use the /model command."
560
  )
 
590
 
591
  Catches ``CompactionFailedError`` and ends the session cleanly instead
592
  of letting the caller retry. Pre-2026-05-04 the caller looped on
593
+ ContextWindowExceededError → compact → re-trigger, burning premium
594
+ inference budget while the session never reached the upload path.
 
595
  """
596
  from agent.context_manager.manager import CompactionFailedError
597
 
 
693
  token_count: int
694
  finish_reason: str | None
695
  usage: dict = field(default_factory=dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
696
 
697
 
698
  def _is_invalid_thinking_signature_error(exc: Exception) -> bool:
699
+ """Return True when a provider rejected replayed thinking metadata."""
700
  text = str(exc)
701
  return (
702
  "Invalid `signature` in `thinking` block" in text
 
785
  data={
786
  "tool": "system",
787
  "log": (
788
+ "The inference provider rejected stale thinking signatures; retrying "
789
  "without replayed thinking metadata."
790
  ),
791
  },
 
797
  def _assistant_message_from_result(
798
  llm_result: LLMResult,
799
  *,
 
800
  tool_calls: list[ToolCall] | None = None,
801
  ) -> Message:
802
+ """Build an assistant history message for HF Router-compatible replay."""
803
  kwargs: dict[str, Any] = {
804
  "role": "assistant",
805
  "content": llm_result.content,
806
  }
807
  if tool_calls is not None:
808
  kwargs["tool_calls"] = tool_calls
 
 
 
 
 
809
  return Message(**kwargs)
810
 
811
 
 
816
  response = None
817
  _healed_effort = False # one-shot safety net per call
818
  _healed_thinking_signature = False
 
819
  t_start = time.monotonic()
820
  for _llm_attempt in range(_MAX_LLM_RETRIES):
821
  try:
 
884
  token_count = 0
885
  finish_reason = None
886
  final_usage_chunk = None
 
 
887
 
888
  async for chunk in response:
 
889
  if session.is_cancelled:
890
  tool_calls_acc.clear()
891
  break
 
939
  latency_ms=int((time.monotonic() - t_start) * 1000),
940
  finish_reason=finish_reason,
941
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
942
  return LLMResult(
943
  content=full_content or None,
944
  tool_calls_acc=tool_calls_acc,
945
  token_count=token_count,
946
  finish_reason=finish_reason,
947
  usage=usage,
 
 
948
  )
949
 
950
 
 
955
  response = None
956
  _healed_effort = False
957
  _healed_thinking_signature = False
 
958
  t_start = time.monotonic()
959
  for _llm_attempt in range(_MAX_LLM_RETRIES):
960
  try:
 
1022
  content = message.content or None
1023
  finish_reason = choice.finish_reason
1024
  token_count = response.usage.total_tokens if response.usage else 0
 
1025
 
1026
  # Build tool_calls_acc in the same format as streaming
1027
  tool_calls_acc: dict[int, dict] = {}
 
1056
  token_count=token_count,
1057
  finish_reason=finish_reason,
1058
  usage=usage,
 
 
1059
  )
1060
 
1061
 
 
1194
  reasoning_effort=session.effective_effort_for(
1195
  session.config.model_name
1196
  ),
1197
+ bill_to_user=getattr(session, "premium_user_billed", False),
1198
  )
1199
  if session.stream:
1200
  llm_result = await _call_llm_streaming(
 
1235
  " • For other tools: reduce the size of your arguments or use bash."
1236
  )
1237
  if content:
1238
+ assistant_msg = _assistant_message_from_result(llm_result)
 
 
 
1239
  session.context_manager.add_message(assistant_msg, token_count)
1240
  session.context_manager.add_message(
1241
  Message(role="user", content=f"[SYSTEM: {truncation_hint}]")
 
1292
  _NO_TOOL_INCOMPLETE_PLAN_RETRY_LIMIT,
1293
  )
1294
  if content:
1295
+ assistant_msg = _assistant_message_from_result(llm_result)
 
 
 
1296
  session.context_manager.add_message(
1297
  assistant_msg, token_count
1298
  )
 
1336
  (content or "")[:500],
1337
  )
1338
  if content:
1339
+ assistant_msg = _assistant_message_from_result(llm_result)
 
 
 
1340
  session.context_manager.add_message(assistant_msg, token_count)
1341
  final_response = content
1342
  break
 
1363
  # Add assistant message with all tool calls to context
1364
  assistant_msg = _assistant_message_from_result(
1365
  llm_result,
 
1366
  tool_calls=tool_calls,
1367
  )
1368
  session.context_manager.add_message(assistant_msg, token_count)
 
1685
  logger.warning("Undo: no user message found to remove")
1686
  await session.send_event(Event(event_type="undo_complete"))
1687
 
1688
+ @staticmethod
1689
+ async def new_conversation(session: Session, *, clear_screen: bool = False) -> None:
1690
+ """Start a fresh conversation inside the active runtime."""
1691
+ try:
1692
+ result = session.start_new_conversation()
1693
+ except Exception as e:
1694
+ await session.send_event(
1695
+ Event(event_type="error", data={"error": f"New chat failed: {e}"})
1696
+ )
1697
+ return
1698
+ result["clear_screen"] = clear_screen
1699
+ await session.send_event(Event(event_type="new_complete", data=result))
1700
+
1701
  @staticmethod
1702
  async def resume(session: Session, path: str) -> None:
1703
  """Reload context from a saved session log into the active session."""
 
2000
  await Handlers.undo(session)
2001
  return True
2002
 
2003
+ if op.op_type == OpType.NEW:
2004
+ clear_screen = bool((op.data or {}).get("clear_screen"))
2005
+ await Handlers.new_conversation(session, clear_screen=clear_screen)
2006
+ return True
2007
+
2008
  if op.op_type == OpType.RESUME:
2009
  path = op.data.get("path") if op.data else None
2010
  if path:
agent/core/effort_probe.py CHANGED
@@ -34,10 +34,9 @@ logger = logging.getLogger(__name__)
34
 
35
 
36
  # Cascade: for each user-stated preference, the ordered list of levels to
37
- # try. First success wins. ``max`` is Anthropic-only; ``xhigh`` is also
38
- # supported on current OpenAI GPT-5 models. Providers that don't accept a
39
- # requested level raise ``UnsupportedEffortError`` synchronously (no wasted
40
- # network round-trip) and we advance to the next level.
41
  _EFFORT_CASCADE: dict[str, list[str]] = {
42
  "max": ["max", "xhigh", "high", "medium", "low"],
43
  "xhigh": ["xhigh", "high", "medium", "low"],
@@ -79,9 +78,7 @@ class ProbeOutcome:
79
  def _is_thinking_unsupported(e: Exception) -> bool:
80
  """Model rejected any thinking config.
81
 
82
- Matches Anthropic's 'thinking.type.enabled is not supported for this
83
- model' as well as the adaptive variant. Substring-match because the
84
- exact wording shifts across API versions.
85
  """
86
  s = str(e).lower()
87
  return "thinking" in s and "not supported" in s
@@ -90,16 +87,12 @@ def _is_thinking_unsupported(e: Exception) -> bool:
90
  def _is_invalid_effort(e: Exception) -> bool:
91
  """The requested effort level isn't accepted for this model.
92
 
93
- Covers both API responses (Anthropic/OpenAI 400 with "invalid", "must
94
- be one of", etc.) and LiteLLM's local validation that fires *before*
95
- the request (e.g. "effort='max' is only supported by Claude Opus 4.6"
96
- — LiteLLM knows max is Opus-4.6-only and raises synchronously). The
97
- cascade walks down on either.
98
 
99
  Explicitly returns False when the message is really about thinking
100
- itself (e.g. Anthropic's 4.7 error mentions ``output_config.effort``
101
- in its fix hint, but the actual failure is ``thinking.type.enabled``
102
- being unsupported). That case is caught by ``_is_thinking_unsupported``.
103
  """
104
  if _is_thinking_unsupported(e):
105
  return False
@@ -195,6 +188,7 @@ async def probe_effort(
195
  hf_token,
196
  reasoning_effort=effort,
197
  strict=True,
 
198
  )
199
  except UnsupportedEffortError:
200
  # Provider can't even accept this effort name (e.g. "max" on
 
34
 
35
 
36
  # Cascade: for each user-stated preference, the ordered list of levels to
37
+ # try. First success wins. HF Router accepts low/medium/high generically;
38
+ # higher preferences are kept in the cascade for future/provider-specific
39
+ # support and are skipped synchronously when unsupported.
 
40
  _EFFORT_CASCADE: dict[str, list[str]] = {
41
  "max": ["max", "xhigh", "high", "medium", "low"],
42
  "xhigh": ["xhigh", "high", "medium", "low"],
 
78
  def _is_thinking_unsupported(e: Exception) -> bool:
79
  """Model rejected any thinking config.
80
 
81
+ Substring-match because exact wording shifts across models and providers.
 
 
82
  """
83
  s = str(e).lower()
84
  return "thinking" in s and "not supported" in s
 
87
  def _is_invalid_effort(e: Exception) -> bool:
88
  """The requested effort level isn't accepted for this model.
89
 
90
+ Covers API responses with "invalid", "must be one of", etc. and local
91
+ validation that fires *before* the request. The cascade walks down on
92
+ either.
 
 
93
 
94
  Explicitly returns False when the message is really about thinking
95
+ itself. That case is caught by ``_is_thinking_unsupported``.
 
 
96
  """
97
  if _is_thinking_unsupported(e):
98
  return False
 
188
  hf_token,
189
  reasoning_effort=effort,
190
  strict=True,
191
+ bill_to_user=getattr(session, "premium_user_billed", False),
192
  )
193
  except UnsupportedEffortError:
194
  # Provider can't even accept this effort name (e.g. "max" on
agent/core/llm_params.py CHANGED
@@ -7,7 +7,11 @@ creating circular imports.
7
 
8
  import os
9
 
10
- from agent.core.hf_tokens import get_hf_bill_to, resolve_hf_router_token
 
 
 
 
11
  from agent.core.local_models import (
12
  LOCAL_MODEL_API_KEY_DEFAULT,
13
  LOCAL_MODEL_API_KEY_ENV,
@@ -16,6 +20,11 @@ from agent.core.local_models import (
16
  local_model_name,
17
  local_model_provider,
18
  )
 
 
 
 
 
19
 
20
 
21
  def _resolve_hf_router_token(session_hf_token: str | None = None) -> str | None:
@@ -23,81 +32,18 @@ def _resolve_hf_router_token(session_hf_token: str | None = None) -> str | None:
23
  return resolve_hf_router_token(session_hf_token)
24
 
25
 
26
- def _patch_litellm_effort_validation() -> None:
27
- """Neuter LiteLLM 1.83's hardcoded effort-level validation.
28
-
29
- Context: at ``litellm/llms/anthropic/chat/transformation.py:~1443`` the
30
- Anthropic adapter validates ``output_config.effort ∈ {high, medium,
31
- low, max}`` and gates ``max`` behind an ``_is_opus_4_6_model`` check
32
- that only matches the substring ``opus-4-6`` / ``opus_4_6``. Result:
33
-
34
- * ``xhigh`` — valid on Anthropic's real API for Claude 4.7 — is
35
- rejected pre-flight with "Invalid effort value: xhigh".
36
- * ``max`` on Opus 4.7 is rejected with "effort='max' is only supported
37
- by Claude Opus 4.6", even though Opus 4.7 accepts it in practice.
38
-
39
- We don't want to maintain a parallel model table, so we let the
40
- Anthropic API itself be the validator: widen ``_is_opus_4_6_model``
41
- to also match ``opus-4-7``+ families, and drop the valid-effort-set
42
- check entirely. If Anthropic rejects an effort level, we see a 400
43
- and the cascade walks down — exactly the behavior we want for any
44
- future model family.
45
-
46
- Removable once litellm ships 1.83.8-stable (which merges PR #25867,
47
- "Litellm day 0 opus 4.7 support") — see commit 0868a82 on their main
48
- branch. Until then, this one-time patch is the escape hatch.
49
- """
50
- try:
51
- from litellm.llms.anthropic.chat import transformation as _t
52
- except Exception:
53
- return
54
-
55
- cfg = getattr(_t, "AnthropicConfig", None)
56
- if cfg is None:
57
- return
58
-
59
- original = getattr(cfg, "_is_opus_4_6_model", None)
60
- if original is None or getattr(original, "_hf_agent_patched", False):
61
- return
62
-
63
- def _widened(model: str) -> bool:
64
- m = model.lower()
65
- # Original 4.6 match plus any future Opus >= 4.6. We only need this
66
- # to return True for families where "max" / "xhigh" are acceptable
67
- # at the API; the cascade handles the case when they're not.
68
- return any(
69
- v in m
70
- for v in (
71
- "opus-4-6",
72
- "opus_4_6",
73
- "opus-4.6",
74
- "opus_4.6",
75
- "opus-4-7",
76
- "opus_4_7",
77
- "opus-4.7",
78
- "opus_4.7",
79
- )
80
- )
81
-
82
- _widened._hf_agent_patched = True # type: ignore[attr-defined]
83
- cfg._is_opus_4_6_model = staticmethod(_widened)
84
-
85
-
86
- _patch_litellm_effort_validation()
87
-
88
-
89
  # Effort levels accepted on the wire.
90
- # Anthropic (4.6+): low | medium | high | xhigh | max (output_config.effort)
91
- # OpenAI direct: minimal | low | medium | high | xhigh (reasoning_effort top-level)
92
- # HF router: low | medium | high (extra_body.reasoning_effort)
93
- #
94
- # We validate *shape* here and let the probe cascade walk down on rejection;
95
- # we deliberately do NOT maintain a per-model capability table.
96
- _ANTHROPIC_EFFORTS = {"low", "medium", "high", "xhigh", "max"}
97
- _OPENAI_EFFORTS = {"minimal", "low", "medium", "high", "xhigh"}
98
  _HF_EFFORTS = {"low", "medium", "high"}
99
 
100
 
 
 
 
 
 
101
  class UnsupportedEffortError(ValueError):
102
  """The requested effort isn't valid for this provider's API surface.
103
 
@@ -151,40 +97,23 @@ def _resolve_llm_params(
151
  session_hf_token: str | None = None,
152
  reasoning_effort: str | None = None,
153
  strict: bool = False,
 
154
  ) -> dict:
155
  """
156
  Build LiteLLM kwargs for a given model id.
157
 
158
- • ``anthropic/<model>`` — native thinking config. We bypass LiteLLM's
159
- ``reasoning_effort`` → ``thinking`` mapping (which lags new Claude
160
- releases like 4.7 and sends the wrong API shape). Instead we pass
161
- both ``thinking={"type": "adaptive"}`` and ``output_config=
162
- {"effort": <level>}`` as top-level kwargs — LiteLLM's Anthropic
163
- adapter forwards unknown top-level kwargs into the request body
164
- verbatim (confirmed by live probe; ``extra_body`` does NOT work
165
- here because Anthropic's API rejects it as "Extra inputs are not
166
- permitted"). This is the stable API for 4.6 and 4.7. Older
167
- extended-thinking models that only accept ``thinking.type.enabled``
168
- will reject this; the probe's cascade catches that and falls back
169
- to no thinking.
170
-
171
- • ``openai/<model>`` — ``reasoning_effort`` forwarded as a top-level
172
- kwarg (GPT-5 / o-series). LiteLLM uses the user's ``OPENAI_API_KEY``.
173
-
174
  • ``ollama/<model>``, ``vllm/<model>``, ``lm_studio/<model>``, and
175
  ``llamacpp/<model>`` — local OpenAI-compatible endpoints. The id prefix
176
  selects a configurable localhost base URL, and the model suffix is sent
177
  to LiteLLM as ``openai/<model>``. These endpoints don't receive
178
  ``reasoning_effort``.
179
 
180
- • Anything else is treated as a HuggingFace router id. We hit the
181
- auto-routing OpenAI-compatible endpoint at
182
- ``https://router.huggingface.co/v1``. The id can be bare or carry an
183
- HF routing suffix (``:fastest`` / ``:cheapest`` / ``:<provider>``).
184
- A leading ``huggingface/`` is stripped. ``reasoning_effort`` is
185
- forwarded via ``extra_body`` (LiteLLM's OpenAI adapter refuses it as
186
- a top-level kwarg for non-OpenAI models). "minimal" normalizes to
187
- "low".
188
 
189
  ``strict=True`` raises ``UnsupportedEffortError`` when the requested
190
  effort isn't in the provider's accepted set, instead of silently
@@ -193,77 +122,45 @@ def _resolve_llm_params(
193
  runtime callers leave ``strict=False``, so a stale cached effort
194
  can't crash a turn — it just doesn't get sent.
195
 
196
- Token precedence (first non-empty wins):
197
  1. INFERENCE_TOKEN env — shared key on the hosted Space (inference is
198
  free for users, billed to the Space owner via ``X-HF-Bill-To``).
199
  2. session.hf_token — the user's own token (CLI / OAuth / cache file).
200
  3. huggingface_hub cache — ``HF_TOKEN`` / ``HUGGING_FACE_HUB_TOKEN`` /
201
  local ``hf auth login`` cache.
202
- """
203
- if model_name.startswith("anthropic/"):
204
- params: dict = {"model": model_name}
205
- if reasoning_effort:
206
- level = reasoning_effort
207
- if level == "minimal":
208
- level = "low"
209
- if level not in _ANTHROPIC_EFFORTS:
210
- if strict:
211
- raise UnsupportedEffortError(
212
- f"Anthropic doesn't accept effort={level!r}"
213
- )
214
- else:
215
- # Adaptive thinking + output_config.effort is the stable
216
- # Anthropic API for Claude 4.6 / 4.7. Both kwargs are
217
- # passed top-level: LiteLLM forwards unknown params into
218
- # the request body for Anthropic, so ``output_config``
219
- # reaches the API. ``extra_body`` does NOT work here —
220
- # Anthropic rejects it as "Extra inputs are not
221
- # permitted".
222
- params["thinking"] = {"type": "adaptive"}
223
- params["output_config"] = {"effort": level}
224
- return params
225
-
226
- if model_name.startswith("bedrock/"):
227
- # LiteLLM routes ``bedrock/...`` through the Converse adapter, which
228
- # picks up AWS credentials from the standard env vars
229
- # (``AWS_ACCESS_KEY_ID`` / ``AWS_SECRET_ACCESS_KEY`` / ``AWS_REGION``).
230
- # The Anthropic thinking/effort shape is not forwarded through Converse
231
- # the same way, so we leave it off for now.
232
- return {"model": model_name}
233
 
234
- if model_name.startswith("openai/"):
235
- params = {"model": model_name}
236
- if reasoning_effort:
237
- if reasoning_effort not in _OPENAI_EFFORTS:
238
- if strict:
239
- raise UnsupportedEffortError(
240
- f"OpenAI doesn't accept effort={reasoning_effort!r}"
241
- )
242
- else:
243
- params["reasoning_effort"] = reasoning_effort
244
- return params
245
 
246
- if is_reserved_local_model_id(model_name):
247
- raise ValueError(f"Unsupported local model id: {model_name}")
248
 
249
- if local_model_provider(model_name) is not None:
250
- return _resolve_local_model_params(model_name, reasoning_effort, strict)
251
 
252
- hf_model = model_name.removeprefix("huggingface/")
253
- api_key = _resolve_hf_router_token(session_hf_token)
 
 
 
 
 
254
  params = {
255
  "model": f"openai/{hf_model}",
256
- "api_base": "https://router.huggingface.co/v1",
257
  "api_key": api_key,
258
  }
259
- if bill_to := get_hf_bill_to():
260
  params["extra_headers"] = {"X-HF-Bill-To": bill_to}
261
  if reasoning_effort:
262
- hf_level = "low" if reasoning_effort == "minimal" else reasoning_effort
263
  if hf_level not in _HF_EFFORTS:
264
  if strict:
265
  raise UnsupportedEffortError(
266
- f"HF router doesn't accept effort={hf_level!r}"
267
  )
268
  else:
269
  params["extra_body"] = {"reasoning_effort": hf_level}
 
7
 
8
  import os
9
 
10
+ from agent.core.hf_tokens import (
11
+ get_hf_bill_to,
12
+ resolve_hf_router_token,
13
+ resolve_hf_token,
14
+ )
15
  from agent.core.local_models import (
16
  LOCAL_MODEL_API_KEY_DEFAULT,
17
  LOCAL_MODEL_API_KEY_ENV,
 
20
  local_model_name,
21
  local_model_provider,
22
  )
23
+ from agent.core.model_ids import (
24
+ HF_ROUTER_BASE_URL,
25
+ is_premium_model_id,
26
+ strip_huggingface_model_prefix,
27
+ )
28
 
29
 
30
  def _resolve_hf_router_token(session_hf_token: str | None = None) -> str | None:
 
32
  return resolve_hf_router_token(session_hf_token)
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Effort levels accepted on the wire.
36
+ # HF Router exposes reasoning controls through the OpenAI-compatible
37
+ # ``extra_body`` field. The probe cascade walks down when a provider rejects
38
+ # an accepted-looking value, so this stays intentionally small and generic.
 
 
 
 
 
39
  _HF_EFFORTS = {"low", "medium", "high"}
40
 
41
 
42
+ def _hf_router_effort_level(reasoning_effort: str) -> str:
43
+ level = "low" if reasoning_effort == "minimal" else reasoning_effort
44
+ return level
45
+
46
+
47
  class UnsupportedEffortError(ValueError):
48
  """The requested effort isn't valid for this provider's API surface.
49
 
 
97
  session_hf_token: str | None = None,
98
  reasoning_effort: str | None = None,
99
  strict: bool = False,
100
+ bill_to_user: bool = False,
101
  ) -> dict:
102
  """
103
  Build LiteLLM kwargs for a given model id.
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  • ``ollama/<model>``, ``vllm/<model>``, ``lm_studio/<model>``, and
106
  ``llamacpp/<model>`` — local OpenAI-compatible endpoints. The id prefix
107
  selects a configurable localhost base URL, and the model suffix is sent
108
  to LiteLLM as ``openai/<model>``. These endpoints don't receive
109
  ``reasoning_effort``.
110
 
111
+ • Anything else is treated as an HF Router id. We hit the auto-routing
112
+ OpenAI-compatible endpoint at ``https://router.huggingface.co/v1``.
113
+ The id can be bare or carry an HF routing suffix (``:fastest`` /
114
+ ``:cheapest`` / ``:<provider>``). A leading ``huggingface/`` is
115
+ stripped. ``reasoning_effort`` is forwarded via ``extra_body``.
116
+ "minimal" normalizes to "low".
 
 
117
 
118
  ``strict=True`` raises ``UnsupportedEffortError`` when the requested
119
  effort isn't in the provider's accepted set, instead of silently
 
122
  runtime callers leave ``strict=False``, so a stale cached effort
123
  can't crash a turn — it just doesn't get sent.
124
 
125
+ Token precedence for HF-router calls (first non-empty wins):
126
  1. INFERENCE_TOKEN env — shared key on the hosted Space (inference is
127
  free for users, billed to the Space owner via ``X-HF-Bill-To``).
128
  2. session.hf_token — the user's own token (CLI / OAuth / cache file).
129
  3. huggingface_hub cache — ``HF_TOKEN`` / ``HUGGING_FACE_HUB_TOKEN`` /
130
  local ``hf auth login`` cache.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ Pass ``bill_to_user=True`` only after the daily subsidized allowance is
133
+ spent. Premium router ids then use the caller's own token, skip
134
+ ``INFERENCE_TOKEN``, and omit ``X-HF-Bill-To``.
135
+ """
136
+ normalized_model = strip_huggingface_model_prefix(model_name) or model_name
 
 
 
 
 
 
137
 
138
+ if is_reserved_local_model_id(normalized_model):
139
+ raise ValueError(f"Unsupported local model id: {normalized_model}")
140
 
141
+ if local_model_provider(normalized_model) is not None:
142
+ return _resolve_local_model_params(normalized_model, reasoning_effort, strict)
143
 
144
+ hf_model = normalized_model
145
+ bill_user = bill_to_user and is_premium_model_id(hf_model)
146
+ api_key = (
147
+ resolve_hf_token(session_hf_token, include_cached=False)
148
+ if bill_user
149
+ else _resolve_hf_router_token(session_hf_token)
150
+ )
151
  params = {
152
  "model": f"openai/{hf_model}",
153
+ "api_base": HF_ROUTER_BASE_URL,
154
  "api_key": api_key,
155
  }
156
+ if not bill_user and (bill_to := get_hf_bill_to()):
157
  params["extra_headers"] = {"X-HF-Bill-To": bill_to}
158
  if reasoning_effort:
159
+ hf_level = _hf_router_effort_level(reasoning_effort)
160
  if hf_level not in _HF_EFFORTS:
161
  if strict:
162
  raise UnsupportedEffortError(
163
+ f"HF Router doesn't accept effort={hf_level!r}"
164
  )
165
  else:
166
  params["extra_body"] = {"reasoning_effort": hf_level}
agent/core/model_ids.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Canonical model ids for HF Router inference."""
2
+
3
+ HF_ROUTER_BASE_URL = "https://router.huggingface.co/v1"
4
+
5
+ CLAUDE_OPUS_48_MODEL_ID = "anthropic/claude-opus-4.8:fal-ai"
6
+ GPT_55_MODEL_ID = "openai/gpt-5.5:fal-ai"
7
+ KIMI_K26_MODEL_ID = "moonshotai/Kimi-K2.6"
8
+ MINIMAX_M27_MODEL_ID = "MiniMaxAI/MiniMax-M2.7"
9
+ GLM_51_MODEL_ID = "zai-org/GLM-5.1"
10
+ DEEPSEEK_V4_PRO_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro:deepinfra"
11
+
12
+ DEFAULT_MODEL_ID = CLAUDE_OPUS_48_MODEL_ID
13
+
14
+ PREMIUM_MODEL_IDS = {
15
+ CLAUDE_OPUS_48_MODEL_ID,
16
+ GPT_55_MODEL_ID,
17
+ }
18
+
19
+ KNOWN_ROUTER_MODEL_IDS = PREMIUM_MODEL_IDS | {
20
+ KIMI_K26_MODEL_ID,
21
+ MINIMAX_M27_MODEL_ID,
22
+ GLM_51_MODEL_ID,
23
+ DEEPSEEK_V4_PRO_MODEL_ID,
24
+ }
25
+
26
+
27
+ def strip_huggingface_model_prefix(model_id: str | None) -> str | None:
28
+ """Return model ids without LiteLLM's optional ``huggingface/`` prefix."""
29
+ if not model_id:
30
+ return model_id
31
+ return model_id.removeprefix("huggingface/")
32
+
33
+
34
+ def is_premium_model_id(model_id: str | None) -> bool:
35
+ normalized = strip_huggingface_model_prefix(model_id)
36
+ return bool(normalized and normalized in PREMIUM_MODEL_IDS)
37
+
38
+
39
+ def is_known_router_model_id(model_id: str | None) -> bool:
40
+ normalized = strip_huggingface_model_prefix(model_id)
41
+ return bool(normalized and normalized in KNOWN_ROUTER_MODEL_IDS)
agent/core/model_switcher.py CHANGED
@@ -26,31 +26,29 @@ from agent.core.local_models import (
26
  is_local_model_id,
27
  is_reserved_local_model_id,
28
  )
 
 
 
 
 
 
29
 
30
 
31
  # Suggested models shown by `/model` (not a gate). Users can paste any HF
32
- # model id (e.g. "MiniMaxAI/MiniMax-M2.7") or an `anthropic/` / `openai/`
33
- # prefix for direct API access. For HF ids, append ":fastest" /
34
- # ":cheapest" / ":preferred" / ":<provider>" to override the default
35
- # routing policy (auto = fastest with failover).
36
  SUGGESTED_MODELS = [
37
- {"id": "openai/gpt-5.5", "label": "GPT-5.5"},
38
- {"id": "openai/gpt-5.4", "label": "GPT-5.4"},
39
- {"id": "anthropic/claude-opus-4-7", "label": "Claude Opus 4.7"},
40
- {"id": "anthropic/claude-opus-4-6", "label": "Claude Opus 4.6"},
41
- {
42
- "id": "bedrock/us.anthropic.claude-opus-4-6-v1",
43
- "label": "Claude Opus 4.6 via Bedrock",
44
- },
45
  {"id": "MiniMaxAI/MiniMax-M2.7", "label": "MiniMax M2.7"},
46
- {"id": "moonshotai/Kimi-K2.6", "label": "Kimi K2.6"},
47
  {"id": "zai-org/GLM-5.1", "label": "GLM 5.1"},
48
  {"id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra", "label": "DeepSeek V4 Pro"},
49
  ]
50
 
51
 
52
  _ROUTING_POLICIES = {"fastest", "cheapest", "preferred"}
53
- _DIRECT_PREFIXES = ("anthropic/", "openai/", *LOCAL_MODEL_PREFIXES)
54
  _LOCAL_PROBE_TIMEOUT = 15.0
55
 
56
 
@@ -58,26 +56,25 @@ def is_valid_model_id(model_id: str) -> bool:
58
  """Loose format check — lets users pick any model id.
59
 
60
  Accepts:
61
- • anthropic/<model>
62
- • openai/<model>
63
  • ollama/<model>, vllm/<model>, lm_studio/<model>, llamacpp/<model>
64
  • <org>/<model>[:<tag>] (HF router; tag = provider or policy)
65
- • huggingface/<org>/<model>[:<tag>] (same, accepts legacy prefix)
66
 
67
  Actual availability is verified against the HF router catalog on
68
  switch, and by the provider on the probe's ping call.
69
  """
70
  if not model_id:
71
  return False
72
- if is_local_model_id(model_id):
 
73
  return True
74
- if is_reserved_local_model_id(model_id):
75
  return False
76
- if any(model_id.startswith(prefix) for prefix in LOCAL_MODEL_PREFIXES):
77
  return False
78
- if "/" not in model_id:
79
  return False
80
- head = model_id.split(":", 1)[0]
81
  parts = head.split("/")
82
  return len(parts) >= 2 and all(parts)
83
 
@@ -88,10 +85,11 @@ def _print_hf_routing_info(model_id: str, console) -> bool:
88
  proceed with the switch, ``False`` to indicate a hard problem the user
89
  should notice before we fire the effort probe.
90
 
91
- Anthropic / OpenAI ids return ``True`` without printing anything —
92
- the probe below covers "does this model exist".
 
93
  """
94
- if model_id.startswith(_DIRECT_PREFIXES):
95
  return True
96
 
97
  from agent.core import hf_router_catalog as cat
@@ -162,7 +160,6 @@ def print_model_listing(config, console) -> None:
162
  console.print(
163
  "\n[dim]Paste any HF model id (e.g. 'MiniMaxAI/MiniMax-M2.7').\n"
164
  "Add ':fastest', ':cheapest', ':preferred', or ':<provider>' to override routing.\n"
165
- "Use 'anthropic/<model>' or 'openai/<model>' for direct API access.\n"
166
  "Use 'ollama/<model>', 'vllm/<model>', 'lm_studio/<model>', or "
167
  "'llamacpp/<model>' for local OpenAI-compatible endpoints.[/dim]"
168
  )
@@ -173,8 +170,6 @@ def print_invalid_id(arg: str, console) -> None:
173
  console.print(
174
  "[dim]Expected:\n"
175
  " • <org>/<model>[:tag] (HF router — paste from huggingface.co)\n"
176
- " • anthropic/<model>\n"
177
- " • openai/<model>\n"
178
  " • ollama/<model> | vllm/<model> | lm_studio/<model> | llamacpp/<model>[/dim]"
179
  )
180
 
 
26
  is_local_model_id,
27
  is_reserved_local_model_id,
28
  )
29
+ from agent.core.model_ids import (
30
+ CLAUDE_OPUS_48_MODEL_ID,
31
+ GPT_55_MODEL_ID,
32
+ KIMI_K26_MODEL_ID,
33
+ strip_huggingface_model_prefix,
34
+ )
35
 
36
 
37
  # Suggested models shown by `/model` (not a gate). Users can paste any HF
38
+ # Router model id (e.g. "MiniMaxAI/MiniMax-M2.7"). Append ":fastest",
39
+ # ":cheapest", ":preferred", or ":<provider>" to override the default routing
40
+ # policy (auto = fastest with failover).
 
41
  SUGGESTED_MODELS = [
42
+ {"id": CLAUDE_OPUS_48_MODEL_ID, "label": "Claude Opus 4.8"},
43
+ {"id": GPT_55_MODEL_ID, "label": "GPT-5.5"},
 
 
 
 
 
 
44
  {"id": "MiniMaxAI/MiniMax-M2.7", "label": "MiniMax M2.7"},
45
+ {"id": KIMI_K26_MODEL_ID, "label": "Kimi K2.6"},
46
  {"id": "zai-org/GLM-5.1", "label": "GLM 5.1"},
47
  {"id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra", "label": "DeepSeek V4 Pro"},
48
  ]
49
 
50
 
51
  _ROUTING_POLICIES = {"fastest", "cheapest", "preferred"}
 
52
  _LOCAL_PROBE_TIMEOUT = 15.0
53
 
54
 
 
56
  """Loose format check — lets users pick any model id.
57
 
58
  Accepts:
 
 
59
  • ollama/<model>, vllm/<model>, lm_studio/<model>, llamacpp/<model>
60
  • <org>/<model>[:<tag>] (HF router; tag = provider or policy)
61
+ • huggingface/<org>/<model>[:<tag>] (same, optional LiteLLM prefix)
62
 
63
  Actual availability is verified against the HF router catalog on
64
  switch, and by the provider on the probe's ping call.
65
  """
66
  if not model_id:
67
  return False
68
+ normalized_model_id = strip_huggingface_model_prefix(model_id) or model_id
69
+ if is_local_model_id(normalized_model_id):
70
  return True
71
+ if is_reserved_local_model_id(normalized_model_id):
72
  return False
73
+ if any(normalized_model_id.startswith(prefix) for prefix in LOCAL_MODEL_PREFIXES):
74
  return False
75
+ if "/" not in normalized_model_id:
76
  return False
77
+ head = normalized_model_id.split(":", 1)[0]
78
  parts = head.split("/")
79
  return len(parts) >= 2 and all(parts)
80
 
 
85
  proceed with the switch, ``False`` to indicate a hard problem the user
86
  should notice before we fire the effort probe.
87
 
88
+ Local ids return ``True`` without printing anything. Router ids are checked
89
+ against the router catalog when possible; the probe below covers provider
90
+ availability for uncataloged ids.
91
  """
92
+ if is_local_model_id(model_id):
93
  return True
94
 
95
  from agent.core import hf_router_catalog as cat
 
160
  console.print(
161
  "\n[dim]Paste any HF model id (e.g. 'MiniMaxAI/MiniMax-M2.7').\n"
162
  "Add ':fastest', ':cheapest', ':preferred', or ':<provider>' to override routing.\n"
 
163
  "Use 'ollama/<model>', 'vllm/<model>', 'lm_studio/<model>', or "
164
  "'llamacpp/<model>' for local OpenAI-compatible endpoints.[/dim]"
165
  )
 
170
  console.print(
171
  "[dim]Expected:\n"
172
  " • <org>/<model>[:tag] (HF router — paste from huggingface.co)\n"
 
 
173
  " • ollama/<model> | vllm/<model> | lm_studio/<model> | llamacpp/<model>[/dim]"
174
  )
175
 
agent/core/prompt_caching.py DELETED
@@ -1,65 +0,0 @@
1
- """Anthropic prompt caching breakpoints for outgoing LLM requests.
2
-
3
- Caching is GA on Anthropic's API and natively supported by litellm >=1.83
4
- via ``cache_control`` blocks. We apply two breakpoints (out of 4 allowed):
5
-
6
- 1. The tool block — caches all tool definitions as a single prefix.
7
- 2. The system message — caches the rendered system prompt.
8
-
9
- Together these cover the ~4-5K static tokens that were being re-billed on
10
- every turn. Subsequent turns within the 5-minute TTL hit cache_read pricing
11
- (~10% of input cost) instead of full input.
12
-
13
- Non-Anthropic models (HF router, OpenAI) are passed through unchanged.
14
- """
15
-
16
- from typing import Any
17
-
18
-
19
- def with_prompt_caching(
20
- messages: list[Any],
21
- tools: list[dict] | None,
22
- model_name: str | None,
23
- ) -> tuple[list[Any], list[dict] | None]:
24
- """Return (messages, tools) with cache_control breakpoints for Anthropic.
25
-
26
- No-op for non-Anthropic models. Original objects are not mutated; a fresh
27
- list with replaced first message and last tool is returned, so callers
28
- that share the underlying ``ContextManager.items`` list don't see their
29
- persisted history rewritten.
30
- """
31
- if not model_name or "anthropic" not in model_name:
32
- return messages, tools
33
-
34
- if tools:
35
- new_tools = list(tools)
36
- last = dict(new_tools[-1])
37
- last["cache_control"] = {"type": "ephemeral"}
38
- new_tools[-1] = last
39
- tools = new_tools
40
-
41
- if messages:
42
- first = messages[0]
43
- role = (
44
- first.get("role")
45
- if isinstance(first, dict)
46
- else getattr(first, "role", None)
47
- )
48
- if role == "system":
49
- content = (
50
- first.get("content")
51
- if isinstance(first, dict)
52
- else getattr(first, "content", None)
53
- )
54
- if isinstance(content, str) and content:
55
- cached_block = [
56
- {
57
- "type": "text",
58
- "text": content,
59
- "cache_control": {"type": "ephemeral"},
60
- }
61
- ]
62
- new_first = {"role": "system", "content": cached_block}
63
- messages = [new_first] + list(messages[1:])
64
-
65
- return messages, tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
agent/core/redact.py CHANGED
@@ -18,10 +18,9 @@ from typing import Any
18
  _PATTERNS: list[tuple[re.Pattern, str]] = [
19
  # Hugging Face tokens: hf_[A-Za-z0-9]{30,}
20
  (re.compile(r"hf_[A-Za-z0-9]{30,}"), "[REDACTED_HF_TOKEN]"),
21
- # Anthropic: sk-ant-[A-Za-z0-9_\-]{20,}
22
- (re.compile(r"sk-ant-[A-Za-z0-9_\-]{20,}"), "[REDACTED_ANTHROPIC_KEY]"),
23
- # OpenAI: sk-[A-Za-z0-9]{40,} (legacy + proj keys)
24
- (re.compile(r"sk-(?!ant-)[A-Za-z0-9_\-]{40,}"), "[REDACTED_OPENAI_KEY]"),
25
  # GitHub classic PATs: ghp_, gho_, ghu_, ghs_, ghr_ followed by 36+ chars
26
  (re.compile(r"gh[pousr]_[A-Za-z0-9]{36,}"), "[REDACTED_GITHUB_TOKEN]"),
27
  # GitHub fine-grained PATs: github_pat_<alphanumeric_underscore>
@@ -36,8 +35,7 @@ _PATTERNS: list[tuple[re.Pattern, str]] = [
36
  # still see which secret was referenced. Covers `KEY=value` and `KEY: value`
37
  # when the key looks secret-y.
38
  _SECRETY_NAMES = re.compile(
39
- r"(?i)\b(HF_TOKEN|HUGGINGFACEHUB_API_TOKEN|ANTHROPIC_API_KEY|OPENAI_API_KEY|"
40
- r"GITHUB_TOKEN|AWS_SECRET_ACCESS_KEY|AWS_ACCESS_KEY_ID|PASSWORD|SECRET|API_KEY)"
41
  r"\s*[:=]\s*([^\s\"']+)"
42
  )
43
 
 
18
  _PATTERNS: list[tuple[re.Pattern, str]] = [
19
  # Hugging Face tokens: hf_[A-Za-z0-9]{30,}
20
  (re.compile(r"hf_[A-Za-z0-9]{30,}"), "[REDACTED_HF_TOKEN]"),
21
+ # Provider API keys with common sk-* prefixes.
22
+ (re.compile(r"sk-ant-[A-Za-z0-9_\-]{20,}"), "[REDACTED_PROVIDER_API_KEY]"),
23
+ (re.compile(r"sk-(?!ant-)[A-Za-z0-9_\-]{40,}"), "[REDACTED_PROVIDER_API_KEY]"),
 
24
  # GitHub classic PATs: ghp_, gho_, ghu_, ghs_, ghr_ followed by 36+ chars
25
  (re.compile(r"gh[pousr]_[A-Za-z0-9]{36,}"), "[REDACTED_GITHUB_TOKEN]"),
26
  # GitHub fine-grained PATs: github_pat_<alphanumeric_underscore>
 
35
  # still see which secret was referenced. Covers `KEY=value` and `KEY: value`
36
  # when the key looks secret-y.
37
  _SECRETY_NAMES = re.compile(
38
+ r"(?i)\b([A-Z0-9_]*(?:TOKEN|API_KEY|SECRET|PASSWORD|ACCESS_KEY_ID))"
 
39
  r"\s*[:=]\s*([^\s\"']+)"
40
  )
41
 
agent/core/session.py CHANGED
@@ -11,6 +11,8 @@ from enum import Enum
11
  from pathlib import Path
12
  from typing import Any, Optional
13
 
 
 
14
  from agent.config import Config
15
  from agent.context_manager.manager import ContextManager
16
  from agent.messaging.gateway import NotificationGateway
@@ -27,12 +29,10 @@ DEFAULT_SESSION_LOG_DIR = Path("session_logs")
27
  def _get_max_tokens_safe(model_name: str) -> int:
28
  """Return the max input-context tokens for a model.
29
 
30
- Primary source: ``litellm.get_model_info(model)['max_input_tokens']`` —
31
- LiteLLM maintains an upstream catalog that knows Claude Opus 4.6 is
32
- 1M, GPT-5 is 272k, Sonnet 4.5 is 200k, and so on. Strips any HF routing
33
- suffix / huggingface/ prefix so tagged ids ('moonshotai/Kimi-K2.6:cheapest')
34
- look up the bare model. Falls back to a conservative 200k default for
35
- models not in the catalog (typically HF-router-only models).
36
  """
37
  from litellm import get_model_info
38
 
@@ -62,6 +62,7 @@ class OpType(Enum):
62
  INTERRUPT = "interrupt"
63
  UNDO = "undo"
64
  COMPACT = "compact"
 
65
  RESUME = "resume"
66
  SHUTDOWN = "shutdown"
67
 
@@ -118,6 +119,11 @@ class Session:
118
  self.session_id = session_id or str(uuid.uuid4())
119
  self.config = config
120
  self.is_running = True
 
 
 
 
 
121
  self.current_plan: list[dict[str, str]] = []
122
  self._cancelled = asyncio.Event()
123
  self.pending_approval: Optional[dict[str, Any]] = None
@@ -322,8 +328,11 @@ class Session:
322
 
323
  def update_model(self, model_name: str) -> None:
324
  """Switch the active model and update the context window limit."""
325
- self.config.model_name = model_name
326
- self.context_manager.model_max_tokens = _get_max_tokens_safe(model_name)
 
 
 
327
 
328
  def set_auto_approval_policy(
329
  self, *, enabled: bool, cost_cap_usd: float | None
@@ -376,6 +385,82 @@ class Session:
376
  """Increment turn counter (called after each user interaction)"""
377
  self.turn_count += 1
378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
  async def auto_save_if_needed(self) -> None:
380
  """Check if auto-save should trigger and save if so (completely non-blocking)"""
381
  if not self.config.save_sessions:
 
11
  from pathlib import Path
12
  from typing import Any, Optional
13
 
14
+ from litellm import Message
15
+
16
  from agent.config import Config
17
  from agent.context_manager.manager import ContextManager
18
  from agent.messaging.gateway import NotificationGateway
 
29
  def _get_max_tokens_safe(model_name: str) -> int:
30
  """Return the max input-context tokens for a model.
31
 
32
+ Primary source: ``litellm.get_model_info(model)['max_input_tokens']``.
33
+ Strips any HF routing suffix / huggingface/ prefix so tagged ids
34
+ ('moonshotai/Kimi-K2.6:cheapest') look up the bare model. Falls back to a
35
+ conservative 200k default for models not in the catalog.
 
 
36
  """
37
  from litellm import get_model_info
38
 
 
62
  INTERRUPT = "interrupt"
63
  UNDO = "undo"
64
  COMPACT = "compact"
65
+ NEW = "new"
66
  RESUME = "resume"
67
  SHUTDOWN = "shutdown"
68
 
 
119
  self.session_id = session_id or str(uuid.uuid4())
120
  self.config = config
121
  self.is_running = True
122
+ # Billing mode for premium HF Router usage. The backend quota gate
123
+ # flips this on once the user is past their subsidized daily allowance,
124
+ # so the LLM call bills the user's own HF token instead of the Space.
125
+ # Persisted with the session so it survives idle-reclaim.
126
+ self.premium_user_billed: bool = False
127
  self.current_plan: list[dict[str, str]] = []
128
  self._cancelled = asyncio.Event()
129
  self.pending_approval: Optional[dict[str, Any]] = None
 
328
 
329
  def update_model(self, model_name: str) -> None:
330
  """Switch the active model and update the context window limit."""
331
+ from agent.core.model_ids import strip_huggingface_model_prefix
332
+
333
+ normalized = strip_huggingface_model_prefix(model_name) or model_name
334
+ self.config.model_name = normalized
335
+ self.context_manager.model_max_tokens = _get_max_tokens_safe(normalized)
336
 
337
  def set_auto_approval_policy(
338
  self, *, enabled: bool, cost_cap_usd: float | None
 
385
  """Increment turn counter (called after each user interaction)"""
386
  self.turn_count += 1
387
 
388
+ def start_new_conversation(self) -> dict[str, Any]:
389
+ """Rotate this runtime into a fresh conversation.
390
+
391
+ The tool router, model/config choices, user identity, and external
392
+ resources stay attached to the CLI process. Conversation-specific state
393
+ gets reset so later saves do not merge with the prior chat. Warm runtime
394
+ resources such as the sandbox, in-flight job tracking, and probed
395
+ model-effort cache are deliberately preserved.
396
+ """
397
+ previous_session_id = self.session_id
398
+ previous_turn_count = self.turn_count
399
+ previous_message_count = len(self.context_manager.items)
400
+ previous_non_system_count = sum(
401
+ 1
402
+ for item in self.context_manager.items
403
+ if getattr(item, "role", None) != "system"
404
+ )
405
+
406
+ saved_path: str | None = None
407
+ if self.config.save_sessions and previous_non_system_count:
408
+ saved_path = self.save_and_upload_detached(self.config.session_dataset_repo)
409
+
410
+ from agent.tools.plan_tool import reset_current_plan
411
+
412
+ self.current_plan = []
413
+ reset_current_plan()
414
+
415
+ system_msg = self._fresh_system_message()
416
+ self.context_manager.items = [system_msg] if system_msg is not None else []
417
+ self.context_manager.running_context_usage = 0
418
+
419
+ self.session_id = str(uuid.uuid4())
420
+ self.session_start_time = datetime.now().isoformat()
421
+ self.turn_count = 0
422
+ self.last_auto_save_turn = 0
423
+ self.logged_events = []
424
+ self._local_save_path = None
425
+ self._last_heartbeat_ts = None
426
+ self.pending_approval = None
427
+ self.auto_approval_estimated_spend_usd = 0.0
428
+ self.reset_cancel()
429
+
430
+ # Previous-session metadata is intentionally included for event
431
+ # consumers and telemetry, even though the CLI currently prints only
432
+ # the optional save path.
433
+ return {
434
+ "session_id": self.session_id,
435
+ "previous_session_id": previous_session_id,
436
+ "previous_turn_count": previous_turn_count,
437
+ "previous_message_count": previous_message_count,
438
+ "saved_path": saved_path,
439
+ }
440
+
441
+ def _fresh_system_message(self) -> Message | None:
442
+ existing = (
443
+ self.context_manager.items[0]
444
+ if self.context_manager.items
445
+ and getattr(self.context_manager.items[0], "role", None) == "system"
446
+ else None
447
+ )
448
+ refresh = getattr(self.context_manager, "refresh_system_prompt", None)
449
+ if refresh is None:
450
+ return existing
451
+ try:
452
+ tool_specs = (
453
+ self.tool_router.get_tool_specs_for_llm() if self.tool_router else []
454
+ )
455
+ return refresh(
456
+ tool_specs=tool_specs,
457
+ hf_token=self.hf_token,
458
+ local_mode=self.local_mode,
459
+ )
460
+ except Exception as e:
461
+ logger.warning("Failed to refresh system prompt for new chat: %s", e)
462
+ return existing
463
+
464
  async def auto_save_if_needed(self) -> None:
465
  """Check if auto-save should trigger and save if so (completely non-blocking)"""
466
  if not self.config.save_sessions:
agent/core/session_persistence.py CHANGED
@@ -175,6 +175,7 @@ class MongoSessionStore(NoopSessionStore):
175
  turn_count: int = 0,
176
  pending_approval: list[dict[str, Any]] | None = None,
177
  claude_counted: bool = False,
 
178
  notification_destinations: list[str] | None = None,
179
  auto_approval_enabled: bool = False,
180
  auto_approval_cost_cap_usd: float | None = None,
@@ -206,6 +207,7 @@ class MongoSessionStore(NoopSessionStore):
206
  "turn_count": turn_count,
207
  "pending_approval": pending_approval or [],
208
  "claude_counted": claude_counted,
 
209
  "notification_destinations": notification_destinations or [],
210
  "auto_approval_enabled": auto_approval_enabled,
211
  "auto_approval_cost_cap_usd": auto_approval_cost_cap_usd,
@@ -228,13 +230,17 @@ class MongoSessionStore(NoopSessionStore):
228
  turn_count: int = 0,
229
  pending_approval: list[dict[str, Any]] | None = None,
230
  claude_counted: bool = False,
 
231
  created_at: datetime | None = None,
232
  notification_destinations: list[str] | None = None,
233
  auto_approval_enabled: bool = False,
234
  auto_approval_cost_cap_usd: float | None = None,
235
  auto_approval_estimated_spend_usd: float = 0.0,
 
236
  ) -> None:
237
  if not self._ready():
 
 
238
  return
239
  now = _now()
240
  await self.upsert_session(
@@ -249,6 +255,7 @@ class MongoSessionStore(NoopSessionStore):
249
  turn_count=turn_count,
250
  pending_approval=pending_approval,
251
  claude_counted=claude_counted,
 
252
  notification_destinations=notification_destinations,
253
  auto_approval_enabled=auto_approval_enabled,
254
  auto_approval_cost_cap_usd=auto_approval_cost_cap_usd,
@@ -278,6 +285,11 @@ class MongoSessionStore(NoopSessionStore):
278
  if ops:
279
  await self.db.session_messages.bulk_write(ops, ordered=False)
280
  except PyMongoError as e:
 
 
 
 
 
281
  logger.warning("Failed to persist session %s snapshot: %s", session_id, e)
282
 
283
  async def load_session(
 
175
  turn_count: int = 0,
176
  pending_approval: list[dict[str, Any]] | None = None,
177
  claude_counted: bool = False,
178
+ premium_user_billed: bool = False,
179
  notification_destinations: list[str] | None = None,
180
  auto_approval_enabled: bool = False,
181
  auto_approval_cost_cap_usd: float | None = None,
 
207
  "turn_count": turn_count,
208
  "pending_approval": pending_approval or [],
209
  "claude_counted": claude_counted,
210
+ "premium_user_billed": premium_user_billed,
211
  "notification_destinations": notification_destinations or [],
212
  "auto_approval_enabled": auto_approval_enabled,
213
  "auto_approval_cost_cap_usd": auto_approval_cost_cap_usd,
 
230
  turn_count: int = 0,
231
  pending_approval: list[dict[str, Any]] | None = None,
232
  claude_counted: bool = False,
233
+ premium_user_billed: bool = False,
234
  created_at: datetime | None = None,
235
  notification_destinations: list[str] | None = None,
236
  auto_approval_enabled: bool = False,
237
  auto_approval_cost_cap_usd: float | None = None,
238
  auto_approval_estimated_spend_usd: float = 0.0,
239
+ raise_on_error: bool = False,
240
  ) -> None:
241
  if not self._ready():
242
+ if raise_on_error:
243
+ raise RuntimeError("session store not ready")
244
  return
245
  now = _now()
246
  await self.upsert_session(
 
255
  turn_count=turn_count,
256
  pending_approval=pending_approval,
257
  claude_counted=claude_counted,
258
+ premium_user_billed=premium_user_billed,
259
  notification_destinations=notification_destinations,
260
  auto_approval_enabled=auto_approval_enabled,
261
  auto_approval_cost_cap_usd=auto_approval_cost_cap_usd,
 
285
  if ops:
286
  await self.db.session_messages.bulk_write(ops, ordered=False)
287
  except PyMongoError as e:
288
+ # Best-effort by default, but the reaper passes raise_on_error so a
289
+ # silent message-write failure doesn't let it evict a session whose
290
+ # latest messages never made it to Mongo.
291
+ if raise_on_error:
292
+ raise
293
  logger.warning("Failed to persist session %s snapshot: %s", session_id, e)
294
 
295
  async def load_session(
agent/core/session_resume.py CHANGED
@@ -12,6 +12,7 @@ from typing import Any
12
 
13
  from litellm import Message
14
 
 
15
  from agent.core.model_switcher import is_valid_model_id
16
  from agent.core.session import DEFAULT_SESSION_LOG_DIR
17
 
@@ -224,8 +225,9 @@ def restore_session_from_log(session: Any, path: Path) -> dict[str, Any]:
224
  saved_model = data.get("model_name")
225
  invalid_saved_model: str | None = None
226
  if isinstance(saved_model, str) and saved_model:
227
- if is_valid_model_id(saved_model):
228
- session.update_model(saved_model)
 
229
  else:
230
  invalid_saved_model = saved_model
231
  logger.warning(
 
12
 
13
  from litellm import Message
14
 
15
+ from agent.core.model_ids import strip_huggingface_model_prefix
16
  from agent.core.model_switcher import is_valid_model_id
17
  from agent.core.session import DEFAULT_SESSION_LOG_DIR
18
 
 
225
  saved_model = data.get("model_name")
226
  invalid_saved_model: str | None = None
227
  if isinstance(saved_model, str) and saved_model:
228
+ normalized_model = strip_huggingface_model_prefix(saved_model)
229
+ if normalized_model and is_valid_model_id(normalized_model):
230
+ session.update_model(normalized_model)
231
  else:
232
  invalid_saved_model = saved_model
233
  logger.warning(
agent/core/session_uploader.py CHANGED
@@ -396,7 +396,7 @@ sessions/YYYY-MM-DD/<session_id>.jsonl
396
  **WARNING: no comprehensive redaction or human review has been performed for this dataset.**
397
 
398
  ML Intern applies automated best-effort scrubbing for common secret patterns
399
- such as Hugging Face, Anthropic, OpenAI, GitHub, and AWS tokens before upload.
400
  This is not a privacy guarantee.
401
 
402
  These traces may contain sensitive information, including prompts, code,
 
396
  **WARNING: no comprehensive redaction or human review has been performed for this dataset.**
397
 
398
  ML Intern applies automated best-effort scrubbing for common secret patterns
399
+ such as Hugging Face, GitHub, AWS, and provider API tokens before upload.
400
  This is not a privacy guarantee.
401
 
402
  These traces may contain sensitive information, including prompts, code,
agent/core/telemetry.py CHANGED
@@ -30,10 +30,8 @@ logger = logging.getLogger(__name__)
30
  def extract_usage(response_or_chunk: Any) -> dict:
31
  """Flat usage dict from a litellm response or final-chunk usage object.
32
 
33
- Normalizes across providers: Anthropic exposes cache tokens as
34
- ``cache_read_input_tokens`` / ``cache_creation_input_tokens``; OpenAI uses
35
- ``prompt_tokens_details.cached_tokens``. Exposed under the stable keys
36
- ``cache_read_tokens`` / ``cache_creation_tokens``.
37
  """
38
  u = getattr(response_or_chunk, "usage", None)
39
  if u is None and isinstance(response_or_chunk, dict):
@@ -97,11 +95,10 @@ async def record_llm_call(
97
  Pre-2026-04-29 only ``main`` calls were instrumented; observed gap on
98
  Cost Explorer was ~67%, with the other 5 call sites accounting for
99
  the rest. Tagging lets us split the dataset's ``total_cost_usd`` by
100
- category and validate against AWS billing.
101
 
102
- The ``/title`` (HF Router, not Bedrock) and ``/health/llm`` (diagnostic
103
- endpoint, no session context) call sites are intentionally not
104
- instrumented — together they're <1% of spend.
105
  """
106
  usage = extract_usage(response) if response is not None else {}
107
  cost_usd = 0.0
 
30
  def extract_usage(response_or_chunk: Any) -> dict:
31
  """Flat usage dict from a litellm response or final-chunk usage object.
32
 
33
+ Normalizes cache-token details across provider response shapes. Exposed
34
+ under the stable keys ``cache_read_tokens`` / ``cache_creation_tokens``.
 
 
35
  """
36
  u = getattr(response_or_chunk, "usage", None)
37
  if u is None and isinstance(response_or_chunk, dict):
 
95
  Pre-2026-04-29 only ``main`` calls were instrumented; observed gap on
96
  Cost Explorer was ~67%, with the other 5 call sites accounting for
97
  the rest. Tagging lets us split the dataset's ``total_cost_usd`` by
98
+ category and validate against billing data.
99
 
100
+ The ``/title`` and ``/health/llm`` diagnostic call sites are intentionally
101
+ not instrumented because they have no session context and are tiny.
 
102
  """
103
  usage = extract_usage(response) if response is not None else {}
104
  cost_usd = 0.0
agent/main.py CHANGED
@@ -12,6 +12,7 @@ import json
12
  import logging
13
  import os
14
  import signal
 
15
  import sys
16
  import time
17
  from dataclasses import dataclass
@@ -27,6 +28,7 @@ from agent.core.agent_loop import submission_loop
27
  from agent.core import model_switcher
28
  from agent.core.hf_tokens import resolve_hf_token
29
  from agent.core.local_models import is_local_model_id
 
30
  from agent.core.session import OpType
31
  from agent.core.tools import ToolRouter
32
  from agent.messaging.gateway import NotificationGateway
@@ -73,6 +75,21 @@ def _tool_runtime_label(local_mode: bool) -> str:
73
  return "local filesystem" if local_mode else "HF sandbox"
74
 
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  async def _wait_for_initial_sandbox_preload(session_holder: list | None) -> None:
77
  session = session_holder[0] if session_holder else None
78
  task = getattr(session, "sandbox_preload_task", None)
@@ -195,6 +212,14 @@ def _create_rich_console():
195
  return get_console()
196
 
197
 
 
 
 
 
 
 
 
 
198
  class _ThinkingShimmer:
199
  """Animated shiny/shimmer thinking indicator — a bright gradient sweeps across the text."""
200
 
@@ -398,6 +423,18 @@ async def event_listener(
398
  elif event.event_type == "undo_complete":
399
  console.print("[dim]Undone.[/dim]")
400
  turn_complete_event.set()
 
 
 
 
 
 
 
 
 
 
 
 
401
  elif event.event_type == "resume_complete":
402
  data = event.data or {}
403
  path = data.get("path", "?")
@@ -902,6 +939,20 @@ async def _handle_slash_command(
902
  operation=Operation(op_type=OpType.COMPACT),
903
  )
904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905
  if command == "/resume":
906
  session = session_holder[0] if session_holder else None
907
  if session is None:
@@ -958,9 +1009,9 @@ async def _handle_slash_command(
958
  console.print(f" [dim]{m}: {eff or 'off'}[/dim]")
959
  console.print(
960
  "[dim]Set with '/effort minimal|low|medium|high|xhigh|max|off'. "
961
- "'max' is Anthropic-only; 'xhigh' is also supported by current "
962
- "OpenAI GPT-5 models. The cascade falls back to whatever the "
963
- "model actually accepts.[/dim]"
964
  )
965
  return None
966
  level = arg.lower()
@@ -1109,14 +1160,15 @@ async def main(model: str | None = None, sandbox_tools: bool = False):
1109
  """Interactive chat with the agent"""
1110
 
1111
  # Clear screen
1112
- os.system("clear" if os.name != "nt" else "cls")
1113
 
1114
  # Create prompt session for input (needed early for token prompt)
1115
  prompt_session = PromptSession()
1116
 
1117
  config = load_config(CLI_CONFIG_PATH, include_user_defaults=True)
 
1118
  if model:
1119
- config.model_name = model
1120
  _apply_tool_runtime_override(config, sandbox_tools=sandbox_tools)
1121
  local_mode = _is_local_tool_runtime(config)
1122
 
@@ -1359,10 +1411,15 @@ async def headless_main(
1359
  _configure_runtime_logging()
1360
 
1361
  config = load_config(CLI_CONFIG_PATH, include_user_defaults=True)
 
1362
  config.yolo_mode = True # Auto-approve everything in headless mode
1363
 
1364
  if model:
1365
- config.model_name = model
 
 
 
 
1366
  _apply_tool_runtime_override(config, sandbox_tools=sandbox_tools)
1367
  local_mode = _is_local_tool_runtime(config)
1368
 
 
12
  import logging
13
  import os
14
  import signal
15
+ import subprocess
16
  import sys
17
  import time
18
  from dataclasses import dataclass
 
28
  from agent.core import model_switcher
29
  from agent.core.hf_tokens import resolve_hf_token
30
  from agent.core.local_models import is_local_model_id
31
+ from agent.core.model_ids import strip_huggingface_model_prefix
32
  from agent.core.session import OpType
33
  from agent.core.tools import ToolRouter
34
  from agent.messaging.gateway import NotificationGateway
 
75
  return "local filesystem" if local_mode else "HF sandbox"
76
 
77
 
78
+ def _normalize_config_model(config: Any) -> None:
79
+ normalized = strip_huggingface_model_prefix(getattr(config, "model_name", None))
80
+ if normalized:
81
+ config.model_name = normalized
82
+
83
+
84
+ def _validate_cli_model_override(model: str) -> str:
85
+ if not model_switcher.is_valid_model_id(model):
86
+ raise ValueError(
87
+ "Invalid model id. Use an HF Router id like "
88
+ "'anthropic/claude-opus-4.8:fal-ai' or a supported local prefix."
89
+ )
90
+ return model.removeprefix("huggingface/")
91
+
92
+
93
  async def _wait_for_initial_sandbox_preload(session_holder: list | None) -> None:
94
  session = session_holder[0] if session_holder else None
95
  task = getattr(session, "sandbox_preload_task", None)
 
212
  return get_console()
213
 
214
 
215
+ def _clear_terminal() -> None:
216
+ command = ["cmd", "/c", "cls"] if os.name == "nt" else ["clear"]
217
+ try:
218
+ subprocess.run(command, check=False)
219
+ except OSError:
220
+ pass
221
+
222
+
223
  class _ThinkingShimmer:
224
  """Animated shiny/shimmer thinking indicator — a bright gradient sweeps across the text."""
225
 
 
423
  elif event.event_type == "undo_complete":
424
  console.print("[dim]Undone.[/dim]")
425
  turn_complete_event.set()
426
+ elif event.event_type == "new_complete":
427
+ data = event.data or {}
428
+ if data.get("clear_screen"):
429
+ _clear_terminal()
430
+ saved_path = data.get("saved_path")
431
+ if saved_path:
432
+ console.print(
433
+ f"[dim]Started new chat. Prior chat saved to {saved_path}.[/dim]"
434
+ )
435
+ else:
436
+ console.print("[dim]Started new chat.[/dim]")
437
+ turn_complete_event.set()
438
  elif event.event_type == "resume_complete":
439
  data = event.data or {}
440
  path = data.get("path", "?")
 
939
  operation=Operation(op_type=OpType.COMPACT),
940
  )
941
 
942
+ if command in {"/new", "/clear"}:
943
+ session = session_holder[0] if session_holder else None
944
+ if session is None:
945
+ get_console().print("[bold red]No active session to reset.[/bold red]")
946
+ return None
947
+ submission_id[0] += 1
948
+ return Submission(
949
+ id=f"sub_{submission_id[0]}",
950
+ operation=Operation(
951
+ op_type=OpType.NEW,
952
+ data={"clear_screen": command == "/clear"},
953
+ ),
954
+ )
955
+
956
  if command == "/resume":
957
  session = session_holder[0] if session_holder else None
958
  if session is None:
 
1009
  console.print(f" [dim]{m}: {eff or 'off'}[/dim]")
1010
  console.print(
1011
  "[dim]Set with '/effort minimal|low|medium|high|xhigh|max|off'. "
1012
+ "HF Router accepts low|medium|high generically; higher preferences "
1013
+ "are probed and the cascade falls back to whatever the selected "
1014
+ "provider accepts.[/dim]"
1015
  )
1016
  return None
1017
  level = arg.lower()
 
1160
  """Interactive chat with the agent"""
1161
 
1162
  # Clear screen
1163
+ _clear_terminal()
1164
 
1165
  # Create prompt session for input (needed early for token prompt)
1166
  prompt_session = PromptSession()
1167
 
1168
  config = load_config(CLI_CONFIG_PATH, include_user_defaults=True)
1169
+ _normalize_config_model(config)
1170
  if model:
1171
+ config.model_name = _validate_cli_model_override(model)
1172
  _apply_tool_runtime_override(config, sandbox_tools=sandbox_tools)
1173
  local_mode = _is_local_tool_runtime(config)
1174
 
 
1411
  _configure_runtime_logging()
1412
 
1413
  config = load_config(CLI_CONFIG_PATH, include_user_defaults=True)
1414
+ _normalize_config_model(config)
1415
  config.yolo_mode = True # Auto-approve everything in headless mode
1416
 
1417
  if model:
1418
+ try:
1419
+ config.model_name = _validate_cli_model_override(model)
1420
+ except ValueError as e:
1421
+ print(f"ERROR: {e}", file=sys.stderr)
1422
+ sys.exit(1)
1423
  _apply_tool_runtime_override(config, sandbox_tools=sandbox_tools)
1424
  local_mode = _is_local_tool_runtime(config)
1425
 
agent/prompts/system_prompt_v3.yaml CHANGED
@@ -136,6 +136,8 @@ system_prompt: |
136
 
137
  Do NOT call sandbox_create before normal CPU work. Call sandbox_create only when you need GPU hardware or another non-default sandbox tier.
138
 
 
 
139
  Use a GPU sandbox (t4-small minimum) when testing code that uses CUDA, bf16/fp16, quantization, flash attention, torch.compile, or model loading. CPU sandboxes cannot test GPU code paths. If the available sandbox tiers cannot fit the full model path, test the largest useful smoke path, state what was not covered, and submit one HF job first.
140
 
141
 
 
136
 
137
  Do NOT call sandbox_create before normal CPU work. Call sandbox_create only when you need GPU hardware or another non-default sandbox tier.
138
 
139
+ The sandbox filesystem does not survive session resumption. If a session is resumed, any files, installed packages, or running processes from earlier are gone — recreate what you need before relying on the sandbox.
140
+
141
  Use a GPU sandbox (t4-small minimum) when testing code that uses CUDA, bf16/fp16, quantization, flash attention, torch.compile, or model loading. CPU sandboxes cannot test GPU code paths. If the available sandbox tiers cannot fit the full model path, test the largest useful smoke path, state what was not covered, and submit one HF job first.
142
 
143
 
agent/tools/plan_tool.py CHANGED
@@ -9,6 +9,13 @@ from .types import ToolResult
9
  _current_plan: List[Dict[str, str]] = []
10
 
11
 
 
 
 
 
 
 
 
12
  class PlanTool:
13
  """Tool for managing a list of todos with status tracking."""
14
 
 
9
  _current_plan: List[Dict[str, str]] = []
10
 
11
 
12
+ def reset_current_plan() -> None:
13
+ """Clear the CLI-visible in-memory plan."""
14
+ global _current_plan
15
+
16
+ _current_plan = []
17
+
18
+
19
  class PlanTool:
20
  """Tool for managing a list of todos with status tracking."""
21
 
agent/tools/research_tool.py CHANGED
@@ -17,7 +17,7 @@ from litellm import Message, acompletion
17
  from agent.core import telemetry
18
  from agent.core.doom_loop import check_for_doom_loop
19
  from agent.core.llm_params import _resolve_llm_params
20
- from agent.core.prompt_caching import with_prompt_caching
21
  from agent.core.session import Event
22
 
23
  logger = logging.getLogger(__name__)
@@ -220,13 +220,8 @@ RESEARCH_TOOL_SPEC = {
220
 
221
 
222
  def _get_research_model(main_model: str) -> str:
223
- """Pick a cheaper model for research based on the main model."""
224
- if main_model.startswith("anthropic/"):
225
- return "anthropic/claude-sonnet-4-6"
226
- if main_model.startswith("bedrock/") and "anthropic" in main_model:
227
- return "bedrock/us.anthropic.claude-sonnet-4-6"
228
- # For non-Anthropic models (HF router etc.), use the same model
229
- return main_model
230
 
231
 
232
  async def research_handler(
@@ -251,19 +246,18 @@ async def research_handler(
251
  user_content = f"Context: {context}\n\n{user_content}"
252
  messages.append(Message(role="user", content=user_content))
253
 
254
- # Use a cheaper/faster model for research
255
  main_model = session.config.model_name
256
  research_model = _get_research_model(main_model)
257
- # Research is a cheap sub-call — cap the main session's effort at "high"
258
- # so a user preference of ``max``/``xhigh`` (valid for Opus 4.6/4.7) doesn't
259
- # propagate to a Sonnet research model that may not accept those levels.
260
- # We also haven't probed this sub-model so we don't know its ceiling.
261
  _pref = getattr(session.config, "reasoning_effort", None)
262
  _capped = "high" if _pref in ("max", "xhigh") else _pref
263
  llm_params = _resolve_llm_params(
264
  research_model,
265
  getattr(session, "hf_token", None),
266
  reasoning_effort=_capped,
 
267
  )
268
 
269
  # Get read-only tool specs from the session's tool router
@@ -341,10 +335,9 @@ async def research_handler(
341
  )
342
  )
343
  try:
344
- _msgs, _ = with_prompt_caching(messages, None, llm_params.get("model"))
345
  _t0 = time.monotonic()
346
  response = await acompletion(
347
- messages=_msgs,
348
  tools=None, # no tools — force text response
349
  stream=False,
350
  timeout=120,
@@ -389,13 +382,10 @@ async def research_handler(
389
  )
390
 
391
  try:
392
- _msgs, _tools = with_prompt_caching(
393
- messages, tool_specs if tool_specs else None, llm_params.get("model")
394
- )
395
  _t0 = time.monotonic()
396
  response = await acompletion(
397
- messages=_msgs,
398
- tools=_tools,
399
  tool_choice="auto",
400
  stream=False,
401
  timeout=120,
@@ -508,10 +498,9 @@ async def research_handler(
508
  )
509
  )
510
  try:
511
- _msgs, _ = with_prompt_caching(messages, None, llm_params.get("model"))
512
  _t0 = time.monotonic()
513
  response = await acompletion(
514
- messages=_msgs,
515
  tools=None,
516
  stream=False,
517
  timeout=120,
 
17
  from agent.core import telemetry
18
  from agent.core.doom_loop import check_for_doom_loop
19
  from agent.core.llm_params import _resolve_llm_params
20
+ from agent.core.model_ids import strip_huggingface_model_prefix
21
  from agent.core.session import Event
22
 
23
  logger = logging.getLogger(__name__)
 
220
 
221
 
222
  def _get_research_model(main_model: str) -> str:
223
+ """Normalize the main model id for the research sub-call."""
224
+ return strip_huggingface_model_prefix(main_model) or main_model
 
 
 
 
 
225
 
226
 
227
  async def research_handler(
 
246
  user_content = f"Context: {context}\n\n{user_content}"
247
  messages.append(Message(role="user", content=user_content))
248
 
249
+ # Use the normalized router model for research
250
  main_model = session.config.model_name
251
  research_model = _get_research_model(main_model)
252
+ # Research is a cheap sub-call — cap the main session's effort at "high".
253
+ # We also haven't probed this sub-call's model so we don't know its ceiling.
 
 
254
  _pref = getattr(session.config, "reasoning_effort", None)
255
  _capped = "high" if _pref in ("max", "xhigh") else _pref
256
  llm_params = _resolve_llm_params(
257
  research_model,
258
  getattr(session, "hf_token", None),
259
  reasoning_effort=_capped,
260
+ bill_to_user=getattr(session, "premium_user_billed", False),
261
  )
262
 
263
  # Get read-only tool specs from the session's tool router
 
335
  )
336
  )
337
  try:
 
338
  _t0 = time.monotonic()
339
  response = await acompletion(
340
+ messages=messages,
341
  tools=None, # no tools — force text response
342
  stream=False,
343
  timeout=120,
 
382
  )
383
 
384
  try:
 
 
 
385
  _t0 = time.monotonic()
386
  response = await acompletion(
387
+ messages=messages,
388
+ tools=tool_specs if tool_specs else None,
389
  tool_choice="auto",
390
  stream=False,
391
  timeout=120,
 
498
  )
499
  )
500
  try:
 
501
  _t0 = time.monotonic()
502
  response = await acompletion(
503
+ messages=messages,
504
  tools=None,
505
  stream=False,
506
  timeout=120,
agent/utils/terminal_display.py CHANGED
@@ -454,6 +454,8 @@ def print_yolo_approve(count: int) -> None:
454
 
455
  HELP_ROWS: tuple[tuple[str, str, str], ...] = (
456
  ("/help", "", "Show this help"),
 
 
457
  ("/undo", "", "Undo last turn"),
458
  ("/compact", "", "Compact context window"),
459
  ("/resume", "[index|id|path]", "Pick up from ./session_logs"),
 
454
 
455
  HELP_ROWS: tuple[tuple[str, str, str], ...] = (
456
  ("/help", "", "Show this help"),
457
+ ("/new", "", "Start a fresh chat"),
458
+ ("/clear", "", "Clear terminal and start fresh"),
459
  ("/undo", "", "Undo last turn"),
460
  ("/compact", "", "Compact context window"),
461
  ("/resume", "[index|id|path]", "Pick up from ./session_logs"),
backend/dependencies.py CHANGED
@@ -298,9 +298,7 @@ def _extract_token(request: Request) -> str | None:
298
  async def require_huggingface_org_member(request: Request) -> bool:
299
  """Return True if the caller is a member of the ``huggingface`` org.
300
 
301
- Used to gate endpoints that can push a session onto an Anthropic model
302
- billed to the Space's ``ANTHROPIC_API_KEY``. Returns True unconditionally
303
- in dev mode so local testing isn't blocked.
304
  """
305
  if not AUTH_ENABLED:
306
  return True
 
298
  async def require_huggingface_org_member(request: Request) -> bool:
299
  """Return True if the caller is a member of the ``huggingface`` org.
300
 
301
+ Returns True unconditionally in dev mode so local testing isn't blocked.
 
 
302
  """
303
  if not AUTH_ENABLED:
304
  return True
backend/models.py CHANGED
@@ -105,6 +105,8 @@ class SessionInfo(BaseModel):
105
  auto_approval: SessionAutoApprovalInfo = Field(
106
  default_factory=SessionAutoApprovalInfo
107
  )
 
 
108
 
109
 
110
  class SessionNotificationsRequest(BaseModel):
 
105
  auto_approval: SessionAutoApprovalInfo = Field(
106
  default_factory=SessionAutoApprovalInfo
107
  )
108
+ premium_user_billed: bool = False
109
+ premium_quota_counted: bool = False
110
 
111
 
112
  class SessionNotificationsRequest(BaseModel):
backend/routes/agent.py CHANGED
@@ -52,71 +52,64 @@ from session_manager import (
52
  import user_quotas
53
 
54
  from agent.core.hf_access import get_jobs_access
55
- from agent.core.hf_tokens import resolve_hf_request_token, resolve_hf_router_token
56
  from agent.core.llm_params import _resolve_llm_params
 
 
 
 
 
 
 
 
 
57
 
58
  logger = logging.getLogger(__name__)
59
 
60
  router = APIRouter(prefix="/api", tags=["agent"])
61
  _background_teardown_tasks: set[asyncio.Task] = set()
62
 
63
- DEFAULT_CLAUDE_MODEL_ID = "bedrock/us.anthropic.claude-opus-4-6-v1"
64
- DEFAULT_FREE_MODEL_ID = "moonshotai/Kimi-K2.6"
65
- PREMIUM_MODEL_IDS = {
66
- DEFAULT_CLAUDE_MODEL_ID,
67
- "openai/gpt-5.5",
68
- }
69
  DATASET_UPLOAD_MULTIPART_SLACK_BYTES = 1024 * 1024
70
 
71
 
72
- def _claude_picker_model_id() -> str:
73
- """Return the model ID used by the Claude option in the UI.
74
-
75
- The frontend config sets ``session_manager.config.model_name`` from
76
- ``ML_INTERN_CLAUDE_MODEL_ID`` when that env var is present, otherwise it
77
- falls back to the production Bedrock Claude model. This function only
78
- exposes that resolved config value for the Claude picker; non-Claude models
79
- are listed separately in the model switcher.
80
- """
81
- return session_manager.config.model_name
82
-
83
-
84
  def _available_models() -> list[dict[str, Any]]:
85
  models = [
86
  {
87
- "id": "moonshotai/Kimi-K2.6",
88
- "label": "Kimi K2.6",
89
  "provider": "huggingface",
90
- "tier": "free",
91
- "recommended": True,
92
- },
93
- {
94
- "id": _claude_picker_model_id(),
95
- "label": "Claude Opus 4.6",
96
- "provider": "anthropic",
97
  "tier": "pro",
98
  "recommended": True,
99
  },
100
  {
101
- "id": "openai/gpt-5.5",
102
  "label": "GPT-5.5",
103
- "provider": "openai",
104
  "tier": "pro",
105
  },
106
  {
107
- "id": "MiniMaxAI/MiniMax-M2.7",
 
 
 
 
 
 
108
  "label": "MiniMax M2.7",
109
  "provider": "huggingface",
110
  "tier": "free",
111
  },
112
  {
113
- "id": "zai-org/GLM-5.1",
114
  "label": "GLM 5.1",
115
  "provider": "huggingface",
116
  "tier": "free",
117
  },
118
  {
119
- "id": "deepseek-ai/DeepSeek-V4-Pro:deepinfra",
120
  "label": "DeepSeek V4 Pro",
121
  "provider": "huggingface",
122
  "tier": "free",
@@ -129,7 +122,11 @@ AVAILABLE_MODELS = _available_models()
129
 
130
 
131
  def _is_premium_model(model_id: str) -> bool:
132
- return model_id in PREMIUM_MODEL_IDS
 
 
 
 
133
 
134
 
135
  async def _model_override_for_new_session(
@@ -138,24 +135,26 @@ async def _model_override_for_new_session(
138
  ) -> str | None:
139
  """Return the model override to use when creating a new session.
140
 
141
- Explicit premium model requests are allowed and charged at message-submit
142
- time. Implicit default sessions are more forgiving: when the configured
143
- default is premium, start them on the first free model instead of spending
144
- premium quota accidentally.
145
  """
146
- resolved_model = requested_model or session_manager.config.model_name
147
- if not _is_premium_model(resolved_model):
148
- return requested_model
149
- if requested_model:
150
- return requested_model
151
 
152
- logger.info(
153
- "Default premium model %s would spend quota; "
154
- "creating session with free fallback %s",
155
- resolved_model,
156
- DEFAULT_FREE_MODEL_ID,
 
 
 
 
 
 
 
 
 
157
  )
158
- return DEFAULT_FREE_MODEL_ID
159
 
160
 
161
  async def _enforce_premium_model_quota(
@@ -169,9 +168,11 @@ async def _enforce_premium_model_quota(
169
  ``claude_counted`` flag on ``AgentSession`` guards against re-counting the
170
  same session; the stored field name is kept for persistence compatibility.
171
 
172
- No-ops when the session's current model isn't premium, or when this
173
- session has already been charged. Raises 429 when the user has hit
174
- their daily cap.
 
 
175
  """
176
  if agent_session.claude_counted:
177
  return
@@ -181,27 +182,21 @@ async def _enforce_premium_model_quota(
181
  user_id = user["user_id"]
182
  plan = user.get("plan", "free")
183
  cap = user_quotas.daily_cap_for(plan)
184
- new_count = await user_quotas.try_increment_claude(user_id, cap)
185
- if new_count is None:
186
- if plan == "pro":
187
- message = (
188
- "Daily premium model limit reached. Use a free model and try "
189
- "premium models again tomorrow."
190
- )
191
- else:
192
- message = (
193
- "Daily premium model limit reached. Upgrade to HF Pro for "
194
- f"{user_quotas.CLAUDE_PRO_DAILY}/day or use a free model."
195
  )
196
- raise HTTPException(
197
- status_code=429,
198
- detail={
199
- "error": "premium_model_daily_cap",
200
- "plan": plan,
201
- "cap": cap,
202
- "message": message,
203
- },
204
- )
205
  agent_session.claude_counted = True
206
  await session_manager.persist_session_snapshot(agent_session)
207
 
@@ -385,14 +380,13 @@ async def generate_title(
385
  reasoning model — reasoning_effort=low keeps the reasoning budget small
386
  so the 60-token output budget isn't consumed before the title is written.
387
  """
388
- api_key = resolve_hf_router_token(_user_hf_token(user))
389
  try:
 
 
 
 
 
390
  response = await acompletion(
391
- # Double openai/ prefix: LiteLLM strips the first as its provider
392
- # prefix, leaving the HF model id on the wire for the router.
393
- model="openai/openai/gpt-oss-120b:cerebras",
394
- api_base="https://router.huggingface.co/v1",
395
- api_key=api_key,
396
  messages=[
397
  {
398
  "role": "system",
@@ -409,7 +403,7 @@ async def generate_title(
409
  max_tokens=60,
410
  temperature=0.3,
411
  timeout=10,
412
- reasoning_effort="low",
413
  )
414
  title = response.choices[0].message.content.strip().strip('"').strip("'")
415
  title = title.translate(_TITLE_STRIP_CHARS).strip()
@@ -470,8 +464,7 @@ async def create_session(
470
  if model and model not in valid_ids:
471
  raise HTTPException(status_code=400, detail=f"Unknown model: {model}")
472
 
473
- # Explicit premium selections are allowed. If the implicit configured
474
- # default is premium, start the session on a free model instead.
475
  model = await _model_override_for_new_session(request, model)
476
 
477
  try:
@@ -502,7 +495,8 @@ async def restore_session_summary(
502
  session's context as a user-role system note.
503
 
504
  Optional ``"model"`` in the body overrides the session's LLM. The
505
- premium-model quota runs at message-submit time, not here.
 
506
  """
507
  messages = body.get("messages")
508
  if not isinstance(messages, list) or not messages:
@@ -528,6 +522,14 @@ async def restore_session_summary(
528
  except SessionCapacityError as e:
529
  raise HTTPException(status_code=503, detail=str(e))
530
 
 
 
 
 
 
 
 
 
531
  try:
532
  summarized = await session_manager.seed_from_summary(session_id, messages)
533
  except ValueError as e:
@@ -659,6 +661,7 @@ async def upload_session_dataset(
659
  agent_session.session.context_manager.add_message(
660
  Message(role="user", content=dataset_context_note(uploaded))
661
  )
 
662
  await session_manager.persist_session_snapshot(agent_session)
663
  logger.info(
664
  "Uploaded dataset file %s to %s for session %s",
 
52
  import user_quotas
53
 
54
  from agent.core.hf_access import get_jobs_access
55
+ from agent.core.hf_tokens import resolve_hf_request_token
56
  from agent.core.llm_params import _resolve_llm_params
57
+ from agent.core.model_ids import (
58
+ DEEPSEEK_V4_PRO_MODEL_ID,
59
+ DEFAULT_MODEL_ID,
60
+ GLM_51_MODEL_ID,
61
+ GPT_55_MODEL_ID,
62
+ KIMI_K26_MODEL_ID,
63
+ MINIMAX_M27_MODEL_ID,
64
+ is_premium_model_id,
65
+ )
66
 
67
  logger = logging.getLogger(__name__)
68
 
69
  router = APIRouter(prefix="/api", tags=["agent"])
70
  _background_teardown_tasks: set[asyncio.Task] = set()
71
 
72
+ DEFAULT_PREMIUM_MODEL_ID = DEFAULT_MODEL_ID
73
+ DEFAULT_GPT_MODEL_ID = GPT_55_MODEL_ID
74
+ DEFAULT_FREE_MODEL_ID = KIMI_K26_MODEL_ID
 
 
 
75
  DATASET_UPLOAD_MULTIPART_SLACK_BYTES = 1024 * 1024
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def _available_models() -> list[dict[str, Any]]:
79
  models = [
80
  {
81
+ "id": DEFAULT_PREMIUM_MODEL_ID,
82
+ "label": "Claude Opus 4.8",
83
  "provider": "huggingface",
 
 
 
 
 
 
 
84
  "tier": "pro",
85
  "recommended": True,
86
  },
87
  {
88
+ "id": DEFAULT_GPT_MODEL_ID,
89
  "label": "GPT-5.5",
90
+ "provider": "huggingface",
91
  "tier": "pro",
92
  },
93
  {
94
+ "id": DEFAULT_FREE_MODEL_ID,
95
+ "label": "Kimi K2.6",
96
+ "provider": "huggingface",
97
+ "tier": "free",
98
+ },
99
+ {
100
+ "id": MINIMAX_M27_MODEL_ID,
101
  "label": "MiniMax M2.7",
102
  "provider": "huggingface",
103
  "tier": "free",
104
  },
105
  {
106
+ "id": GLM_51_MODEL_ID,
107
  "label": "GLM 5.1",
108
  "provider": "huggingface",
109
  "tier": "free",
110
  },
111
  {
112
+ "id": DEEPSEEK_V4_PRO_MODEL_ID,
113
  "label": "DeepSeek V4 Pro",
114
  "provider": "huggingface",
115
  "tier": "free",
 
122
 
123
 
124
  def _is_premium_model(model_id: str) -> bool:
125
+ return is_premium_model_id(model_id)
126
+
127
+
128
+ def _is_user_billed(model_id: str) -> bool:
129
+ return _is_premium_model(model_id)
130
 
131
 
132
  async def _model_override_for_new_session(
 
135
  ) -> str | None:
136
  """Return the model override to use when creating a new session.
137
 
138
+ Explicit model requests are allowed and charged at message-submit time
139
+ when premium. Empty requests use the configured default model.
 
 
140
  """
141
+ return requested_model
 
 
 
 
142
 
143
+
144
+ def _premium_cap_message(plan: str) -> str:
145
+ """Over-allowance message for a premium model that can't fall back to user
146
+ billing. Defensive: every current premium model is user-billable and
147
+ overflows to the user's own HF account instead of reaching this.
148
+ """
149
+ if plan == "pro":
150
+ return (
151
+ "Daily premium model limit reached. Use a free model and try premium "
152
+ "models again tomorrow."
153
+ )
154
+ return (
155
+ "Daily premium model limit reached. Upgrade to HF Pro for "
156
+ f"{user_quotas.CLAUDE_PRO_DAILY}/day or use a free model."
157
  )
 
158
 
159
 
160
  async def _enforce_premium_model_quota(
 
168
  ``claude_counted`` flag on ``AgentSession`` guards against re-counting the
169
  same session; the stored field name is kept for persistence compatibility.
170
 
171
+ Subsidizes the daily allowance (free = 2, pro = 20), organization-billed
172
+ through the HF Router. Past the allowance, premium router models flip the
173
+ session to ``premium_user_billed`` so the call bills the user's own HF
174
+ token instead of blocking. No-ops when the model isn't premium or when this
175
+ session's billing has already been decided.
176
  """
177
  if agent_session.claude_counted:
178
  return
 
182
  user_id = user["user_id"]
183
  plan = user.get("plan", "free")
184
  cap = user_quotas.daily_cap_for(plan)
185
+ within_allowance = await user_quotas.try_increment_claude(user_id, cap) is not None
186
+ if not within_allowance:
187
+ if not _is_user_billed(model_name):
188
+ raise HTTPException(
189
+ status_code=429,
190
+ detail={
191
+ "error": "premium_model_daily_cap",
192
+ "plan": plan,
193
+ "cap": cap,
194
+ "message": _premium_cap_message(plan),
195
+ },
196
  )
197
+ # Past the subsidized allowance on a user-billable model: bill the
198
+ # user's own HF (OAuth) token for this session instead of blocking.
199
+ agent_session.session.premium_user_billed = True
 
 
 
 
 
 
200
  agent_session.claude_counted = True
201
  await session_manager.persist_session_snapshot(agent_session)
202
 
 
380
  reasoning model — reasoning_effort=low keeps the reasoning budget small
381
  so the 60-token output budget isn't consumed before the title is written.
382
  """
 
383
  try:
384
+ llm_params = _resolve_llm_params(
385
+ "openai/gpt-oss-120b:cerebras",
386
+ _user_hf_token(user),
387
+ reasoning_effort="low",
388
+ )
389
  response = await acompletion(
 
 
 
 
 
390
  messages=[
391
  {
392
  "role": "system",
 
403
  max_tokens=60,
404
  temperature=0.3,
405
  timeout=10,
406
+ **llm_params,
407
  )
408
  title = response.choices[0].message.content.strip().strip('"').strip("'")
409
  title = title.translate(_TITLE_STRIP_CHARS).strip()
 
464
  if model and model not in valid_ids:
465
  raise HTTPException(status_code=400, detail=f"Unknown model: {model}")
466
 
467
+ # Empty requests use the configured default, which may be premium.
 
468
  model = await _model_override_for_new_session(request, model)
469
 
470
  try:
 
495
  session's context as a user-role system note.
496
 
497
  Optional ``"model"`` in the body overrides the session's LLM. The
498
+ premium-model quota runs before summarization because that call uses the
499
+ session model immediately.
500
  """
501
  messages = body.get("messages")
502
  if not isinstance(messages, list) or not messages:
 
522
  except SessionCapacityError as e:
523
  raise HTTPException(status_code=503, detail=str(e))
524
 
525
+ agent_session = await _check_session_access(
526
+ session_id,
527
+ user,
528
+ request,
529
+ preload_sandbox=False,
530
+ )
531
+ await _enforce_premium_model_quota(user, agent_session)
532
+
533
  try:
534
  summarized = await session_manager.seed_from_summary(session_id, messages)
535
  except ValueError as e:
 
661
  agent_session.session.context_manager.add_message(
662
  Message(role="user", content=dataset_context_note(uploaded))
663
  )
664
+ session_manager._touch(agent_session)
665
  await session_manager.persist_session_snapshot(agent_session)
666
  logger.info(
667
  "Uploaded dataset file %s to %s for session %s",
backend/session_manager.py CHANGED
@@ -6,12 +6,18 @@ import logging
6
  import os
7
  import uuid
8
  from dataclasses import dataclass, field
9
- from datetime import datetime
10
  from pathlib import Path
11
  from typing import Any, Optional
12
 
13
  from agent.config import load_config
14
  from agent.core.agent_loop import process_submission
 
 
 
 
 
 
15
  from agent.core.session import Event, OpType, Session
16
  from agent.core.session_persistence import get_session_store
17
  from agent.core.tools import ToolRouter
@@ -96,13 +102,19 @@ class AgentSession:
96
  hf_token: str | None = None # User's HF OAuth token for tool execution
97
  task: asyncio.Task | None = None
98
  created_at: datetime = field(default_factory=datetime.utcnow)
 
 
 
 
99
  is_active: bool = True
100
  is_processing: bool = False # True while a submission is being executed
 
 
 
101
  broadcaster: Any = None
102
  title: str | None = None
103
- # True once this session has been counted against the user's daily
104
- # Claude quota. Guards double-counting when the user re-selects an
105
- # Anthropic model mid-session.
106
  claude_counted: bool = False
107
 
108
 
@@ -125,25 +137,52 @@ DEFAULT_YOLO_COST_CAP_USD: float = 5.0
125
  SANDBOX_SHUTDOWN_CLEANUP_CONCURRENCY: int = 10
126
  SANDBOX_SHUTDOWN_CLEANUP_TIMEOUT_S: float = 60.0
127
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  class SessionManager:
130
  """Manages multiple concurrent agent sessions."""
131
 
132
  def __init__(self, config_path: str | None = None) -> None:
133
  self.config = load_config(config_path or DEFAULT_CONFIG_PATH)
 
 
 
134
  self.messaging_gateway = NotificationGateway(self.config.messaging)
135
  self.sessions: dict[str, AgentSession] = {}
136
  self._lock = asyncio.Lock()
137
  self.persistence_store = None
 
 
 
 
 
 
138
 
139
  async def start(self) -> None:
140
  """Start shared background resources."""
141
  self.persistence_store = get_session_store()
142
  await self.persistence_store.init()
143
  await self.messaging_gateway.start()
 
144
 
145
  async def close(self) -> None:
146
  """Flush and close shared background resources."""
 
 
 
 
 
 
 
147
  await self._cleanup_all_sandboxes_on_close()
148
  await self.messaging_gateway.close()
149
  if self.persistence_store is not None:
@@ -160,6 +199,37 @@ class SessionManager:
160
  1 for s in self.sessions.values() if s.user_id == user_id and s.is_active
161
  )
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  def _create_session_sync(
164
  self,
165
  *,
@@ -177,10 +247,11 @@ class SessionManager:
177
  t0 = _time.monotonic()
178
  tool_router = ToolRouter(self.config.mcpServers, hf_token=hf_token)
179
  # Deep-copy config so each session's model switches independently —
180
- # tab A picking GLM doesn't flip tab B off Claude.
181
  session_config = self.config.model_copy(deep=True)
182
- if model:
183
- session_config.model_name = model
 
184
  session = Session(
185
  event_queue=event_queue,
186
  config=session_config,
@@ -500,10 +571,20 @@ class SessionManager:
500
  *,
501
  runtime_state: str | None = None,
502
  status: str = "active",
 
503
  ) -> None:
504
- """Persist the current runtime context snapshot."""
 
 
 
 
 
 
 
505
  store = self._store()
506
  if not getattr(store, "enabled", False):
 
 
507
  return
508
  try:
509
  await store.save_snapshot(
@@ -519,6 +600,9 @@ class SessionManager:
519
  agent_session.session
520
  ),
521
  claude_counted=agent_session.claude_counted,
 
 
 
522
  created_at=agent_session.created_at,
523
  notification_destinations=list(
524
  agent_session.session.notification_destinations
@@ -537,8 +621,11 @@ class SessionManager:
537
  )
538
  or 0.0
539
  ),
 
540
  )
541
  except Exception as e:
 
 
542
  logger.warning(
543
  "Failed to persist snapshot for %s: %s",
544
  agent_session.session_id,
@@ -604,7 +691,11 @@ class SessionManager:
604
 
605
  from litellm import Message
606
 
607
- model = meta.get("model") or self.config.model_name
 
 
 
 
608
  event_queue: asyncio.Queue = asyncio.Queue()
609
  submission_queue: asyncio.Queue = asyncio.Queue()
610
  tool_router, session = await asyncio.to_thread(
@@ -634,9 +725,36 @@ class SessionManager:
634
  *restored_messages,
635
  ]
636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637
  self._restore_pending_approval(session, meta.get("pending_approval") or [])
638
  session.turn_count = int(meta.get("turn_count") or 0)
639
  session.auto_approval_enabled = bool(meta.get("auto_approval_enabled", False))
 
640
  raw_cap = meta.get("auto_approval_cost_cap_usd")
641
  session.auto_approval_cost_cap_usd = (
642
  float(raw_cap) if isinstance(raw_cap, int | float) else None
@@ -660,7 +778,7 @@ class SessionManager:
660
  created_at=created_at,
661
  is_active=True,
662
  is_processing=False,
663
- claude_counted=bool(meta.get("claude_counted")),
664
  title=meta.get("title"),
665
  )
666
  started = await self._start_agent_session(
@@ -707,11 +825,17 @@ class SessionManager:
707
  maximum number of concurrent sessions.
708
  """
709
  # ── Capacity checks ──────────────────────────────────────────
 
 
 
 
 
710
  async with self._lock:
711
  active_count = self.active_session_count
712
- if active_count >= MAX_SESSIONS:
 
713
  raise SessionCapacityError(
714
- f"Server is at capacity ({active_count}/{MAX_SESSIONS} sessions). "
715
  "Please try again later.",
716
  error_type="global",
717
  )
@@ -723,6 +847,7 @@ class SessionManager:
723
  "concurrent sessions. Please close an existing session first.",
724
  error_type="per_user",
725
  )
 
726
 
727
  session_id = str(uuid.uuid4())
728
 
@@ -730,41 +855,57 @@ class SessionManager:
730
  submission_queue: asyncio.Queue = asyncio.Queue()
731
  event_queue: asyncio.Queue = asyncio.Queue()
732
 
733
- # Run blocking constructors in a thread to keep the event loop responsive.
734
- tool_router, session = await asyncio.to_thread(
735
- self._create_session_sync,
736
- session_id=session_id,
737
- user_id=user_id,
738
- hf_username=hf_username,
739
- hf_token=hf_token,
740
- model=model,
741
- event_queue=event_queue,
742
- )
 
 
743
 
744
- # Create wrapper
745
- agent_session = AgentSession(
746
- session_id=session_id,
747
- session=session,
748
- tool_router=tool_router,
749
- submission_queue=submission_queue,
750
- user_id=user_id,
751
- hf_username=hf_username,
752
- hf_token=hf_token,
753
- )
754
 
755
- await self._start_agent_session(
756
- agent_session=agent_session,
757
- event_queue=event_queue,
758
- tool_router=tool_router,
759
- )
760
- await self.persist_session_snapshot(agent_session, runtime_state="idle")
761
- self._start_cpu_sandbox_preload(agent_session)
 
 
 
 
762
 
763
- if is_pro is not None and user_id and user_id != "dev":
764
- await self._track_pro_status(agent_session, is_pro=is_pro)
 
 
 
765
 
766
- logger.info(f"Created session {session_id} for user {user_id}")
767
- return session_id
 
 
 
 
 
 
 
768
 
769
  async def _track_pro_status(
770
  self, agent_session: AgentSession, *, is_pro: bool
@@ -824,9 +965,8 @@ class SessionManager:
824
 
825
  session = agent_session.session
826
  # Pass the real tool specs so the summarizer sees what the agent
827
- # actually has — otherwise Anthropic's modify_params injects a
828
- # dummy tool and the summarizer editorializes that the original
829
- # tool calls were fabricated.
830
  tool_specs = None
831
  try:
832
  tool_specs = agent_session.tool_router.get_tool_specs_for_llm()
@@ -856,6 +996,7 @@ class SessionManager:
856
  ),
857
  )
858
  session.context_manager.items.append(seed)
 
859
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
860
  return len(parsed)
861
 
@@ -907,6 +1048,156 @@ class SessionManager:
907
  SANDBOX_SHUTDOWN_CLEANUP_TIMEOUT_S,
908
  )
909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
910
  async def _run_session(
911
  self,
912
  session_id: str,
@@ -941,12 +1232,17 @@ class SessionManager:
941
  submission_queue.get(), timeout=1.0
942
  )
943
  agent_session.is_processing = True
 
944
  try:
945
  should_continue = await process_submission(
946
  session, submission
947
  )
948
  finally:
949
  agent_session.is_processing = False
 
 
 
 
950
  await self.persist_session_snapshot(agent_session)
951
  if not should_continue:
952
  break
@@ -982,10 +1278,10 @@ class SessionManager:
982
  logger.warning(f"Final-flush failed for {session_id}: {e}")
983
 
984
  async with self._lock:
985
- if session_id in self.sessions:
986
- self.sessions[session_id].is_active = False
987
  await self.persist_session_snapshot(
988
- self.sessions[session_id],
989
  runtime_state="ended",
990
  status="ended",
991
  )
@@ -993,16 +1289,26 @@ class SessionManager:
993
  logger.info(f"Session {session_id} ended")
994
 
995
  async def submit(self, session_id: str, operation: Operation) -> bool:
996
- """Submit an operation to a session."""
997
- async with self._lock:
998
- agent_session = self.sessions.get(session_id)
999
-
1000
- if not agent_session or not agent_session.is_active:
1001
- logger.warning(f"Session {session_id} not found or inactive")
1002
- return False
1003
 
 
 
 
 
 
 
1004
  submission = Submission(id=f"sub_{uuid.uuid4().hex[:8]}", operation=operation)
1005
- await agent_session.submission_queue.put(submission)
 
 
 
 
 
 
 
 
 
 
1006
  return True
1007
 
1008
  async def submit_user_input(self, session_id: str, text: str) -> bool:
@@ -1042,6 +1348,7 @@ class SessionManager:
1042
  user_message_index
1043
  )
1044
  if success:
 
1045
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
1046
  return success
1047
 
@@ -1115,6 +1422,7 @@ class SessionManager:
1115
  if not agent_session or not agent_session.is_active:
1116
  return False
1117
  agent_session.session.update_model(model_id)
 
1118
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
1119
  return True
1120
 
@@ -1150,6 +1458,7 @@ class SessionManager:
1150
  else:
1151
  session.auto_approval_enabled = bool(enabled)
1152
  session.auto_approval_cost_cap_usd = cost_cap_usd
 
1153
  await self.persist_session_snapshot(agent_session)
1154
  return self._auto_approval_summary(session)
1155
 
@@ -1196,6 +1505,10 @@ class SessionManager:
1196
  agent_session.session.notification_destinations
1197
  ),
1198
  "auto_approval": self._auto_approval_summary(agent_session.session),
 
 
 
 
1199
  }
1200
 
1201
  def set_notification_destinations(
@@ -1222,6 +1535,7 @@ class SessionManager:
1222
  seen.add(name)
1223
 
1224
  agent_session.session.set_notification_destinations(normalized)
 
1225
  return normalized
1226
 
1227
  async def list_sessions(self, user_id: str | None = None) -> list[dict[str, Any]]:
@@ -1259,6 +1573,10 @@ class SessionManager:
1259
  "pending_approval": pending or None,
1260
  "model": row.get("model"),
1261
  "title": row.get("title"),
 
 
 
 
1262
  "notification_destinations": row.get(
1263
  "notification_destinations"
1264
  )
 
6
  import os
7
  import uuid
8
  from dataclasses import dataclass, field
9
+ from datetime import datetime, timedelta
10
  from pathlib import Path
11
  from typing import Any, Optional
12
 
13
  from agent.config import load_config
14
  from agent.core.agent_loop import process_submission
15
+ from agent.core.model_ids import (
16
+ DEFAULT_MODEL_ID,
17
+ KIMI_K26_MODEL_ID,
18
+ is_known_router_model_id,
19
+ strip_huggingface_model_prefix,
20
+ )
21
  from agent.core.session import Event, OpType, Session
22
  from agent.core.session_persistence import get_session_store
23
  from agent.core.tools import ToolRouter
 
102
  hf_token: str | None = None # User's HF OAuth token for tool execution
103
  task: asyncio.Task | None = None
104
  created_at: datetime = field(default_factory=datetime.utcnow)
105
+ # Last genuine activity (submit/turn-start/turn-finish/direct user write).
106
+ # Drives the idle reaper. Defaults to load time so a freshly-restored but
107
+ # untouched session isn't reaped for a full idle window.
108
+ last_active_at: datetime = field(default_factory=datetime.utcnow)
109
  is_active: bool = True
110
  is_processing: bool = False # True while a submission is being executed
111
+ # Set under the lock by the reaper while tearing this session down. Blocks
112
+ # submit() from enqueueing onto a session that's being evicted.
113
+ is_reaping: bool = False
114
  broadcaster: Any = None
115
  title: str | None = None
116
+ # True once this session has been counted against the user's daily premium
117
+ # quota. The field name is kept for persistence compatibility.
 
118
  claude_counted: bool = False
119
 
120
 
 
137
  SANDBOX_SHUTDOWN_CLEANUP_CONCURRENCY: int = 10
138
  SANDBOX_SHUTDOWN_CLEANUP_TIMEOUT_S: float = 60.0
139
 
140
+ # ── Idle-session reaper ─────────────────────────────────────────────
141
+ # A live session idle ≥ REAPER_IDLE_HOURS with no in-flight work has its
142
+ # sandbox + RAM released and is evicted from the live pool, while staying
143
+ # fully resumable from Mongo (it reappears as a normal chat, never "ended").
144
+ # This frees both the global pool and the user's concurrent slots.
145
+ REAPER_IDLE_HOURS: float = float(os.environ.get("REAPER_IDLE_HOURS", "2"))
146
+ REAPER_INTERVAL_S: float = float(os.environ.get("REAPER_INTERVAL_S", "300"))
147
+ REAP_TEARDOWN_TIMEOUT_S: float = float(os.environ.get("REAP_TEARDOWN_TIMEOUT_S", "30"))
148
+ REAPER_IDLE = timedelta(hours=REAPER_IDLE_HOURS)
149
+
150
 
151
  class SessionManager:
152
  """Manages multiple concurrent agent sessions."""
153
 
154
  def __init__(self, config_path: str | None = None) -> None:
155
  self.config = load_config(config_path or DEFAULT_CONFIG_PATH)
156
+ normalized_default = strip_huggingface_model_prefix(self.config.model_name)
157
+ if normalized_default:
158
+ self.config.model_name = normalized_default
159
  self.messaging_gateway = NotificationGateway(self.config.messaging)
160
  self.sessions: dict[str, AgentSession] = {}
161
  self._lock = asyncio.Lock()
162
  self.persistence_store = None
163
+ # In-flight create_session calls that have passed the capacity check
164
+ # but not yet inserted their session. Counted alongside
165
+ # active_session_count to hard-cap the global pool against the
166
+ # check-then-create race (see create_session).
167
+ self._pending_creates: int = 0
168
+ self._reaper_task: asyncio.Task | None = None
169
 
170
  async def start(self) -> None:
171
  """Start shared background resources."""
172
  self.persistence_store = get_session_store()
173
  await self.persistence_store.init()
174
  await self.messaging_gateway.start()
175
+ self._reaper_task = asyncio.create_task(self._reaper_loop())
176
 
177
  async def close(self) -> None:
178
  """Flush and close shared background resources."""
179
+ if self._reaper_task is not None:
180
+ self._reaper_task.cancel()
181
+ try:
182
+ await self._reaper_task
183
+ except asyncio.CancelledError:
184
+ pass
185
+ self._reaper_task = None
186
  await self._cleanup_all_sandboxes_on_close()
187
  await self.messaging_gateway.close()
188
  if self.persistence_store is not None:
 
199
  1 for s in self.sessions.values() if s.user_id == user_id and s.is_active
200
  )
201
 
202
+ @staticmethod
203
+ def _touch(agent_session: "AgentSession") -> None:
204
+ """Stamp genuine activity so the idle reaper's clock resets.
205
+
206
+ Call on real user/agent activity (submit, turn start/finish, direct
207
+ user-initiated writes) — never on passive reads or hydration, which
208
+ would keep an otherwise-idle session alive forever.
209
+ """
210
+ agent_session.last_active_at = datetime.utcnow()
211
+
212
+ @staticmethod
213
+ def _model_from_saved_metadata(
214
+ model: str | None,
215
+ *,
216
+ premium_user_billed: bool,
217
+ claude_counted: bool,
218
+ ) -> tuple[str, bool, bool]:
219
+ normalized = strip_huggingface_model_prefix(model)
220
+ if normalized and is_known_router_model_id(normalized):
221
+ return normalized, premium_user_billed, claude_counted
222
+
223
+ fallback_model = KIMI_K26_MODEL_ID if premium_user_billed else DEFAULT_MODEL_ID
224
+ logger.warning(
225
+ "Saved session model %r failed validation; using %r",
226
+ model,
227
+ fallback_model,
228
+ )
229
+ if fallback_model == KIMI_K26_MODEL_ID:
230
+ return fallback_model, False, False
231
+ return fallback_model, premium_user_billed, claude_counted
232
+
233
  def _create_session_sync(
234
  self,
235
  *,
 
247
  t0 = _time.monotonic()
248
  tool_router = ToolRouter(self.config.mcpServers, hf_token=hf_token)
249
  # Deep-copy config so each session's model switches independently —
250
+ # tab A picking GLM doesn't flip tab B off the default model.
251
  session_config = self.config.model_copy(deep=True)
252
+ normalized_model = strip_huggingface_model_prefix(model)
253
+ if normalized_model:
254
+ session_config.model_name = normalized_model
255
  session = Session(
256
  event_queue=event_queue,
257
  config=session_config,
 
571
  *,
572
  runtime_state: str | None = None,
573
  status: str = "active",
574
+ raise_on_error: bool = False,
575
  ) -> None:
576
+ """Persist the current runtime context snapshot.
577
+
578
+ Best-effort by default: a disabled store is a no-op and write failures
579
+ are swallowed. Pass ``raise_on_error=True`` when the caller must know
580
+ the snapshot was durably written (e.g. the reaper, which only evicts a
581
+ session after confirming it stayed resumable) — then a disabled store
582
+ or a write failure raises instead of silently dropping the snapshot.
583
+ """
584
  store = self._store()
585
  if not getattr(store, "enabled", False):
586
+ if raise_on_error:
587
+ raise RuntimeError("persistence store is disabled")
588
  return
589
  try:
590
  await store.save_snapshot(
 
600
  agent_session.session
601
  ),
602
  claude_counted=agent_session.claude_counted,
603
+ premium_user_billed=getattr(
604
+ agent_session.session, "premium_user_billed", False
605
+ ),
606
  created_at=agent_session.created_at,
607
  notification_destinations=list(
608
  agent_session.session.notification_destinations
 
621
  )
622
  or 0.0
623
  ),
624
+ raise_on_error=raise_on_error,
625
  )
626
  except Exception as e:
627
+ if raise_on_error:
628
+ raise
629
  logger.warning(
630
  "Failed to persist snapshot for %s: %s",
631
  agent_session.session_id,
 
691
 
692
  from litellm import Message
693
 
694
+ model, premium_user_billed, claude_counted = self._model_from_saved_metadata(
695
+ meta.get("model") or self.config.model_name,
696
+ premium_user_billed=bool(meta.get("premium_user_billed", False)),
697
+ claude_counted=bool(meta.get("claude_counted")),
698
+ )
699
  event_queue: asyncio.Queue = asyncio.Queue()
700
  submission_queue: asyncio.Queue = asyncio.Queue()
701
  tool_router, session = await asyncio.to_thread(
 
725
  *restored_messages,
726
  ]
727
 
728
+ # If this session ever had a sandbox, its container did not survive the
729
+ # resume (a fresh, empty one is lazily recreated). Tell the agent so it
730
+ # recreates files/packages instead of assuming /app/train.py et al. still
731
+ # exist. Gated on sandbox_status so pure Q&A chats get no note. Mirrors
732
+ # the seed_from_summary note convention.
733
+ #
734
+ # Skip it when an approval is pending: the restored context ends with an
735
+ # assistant tool-call message awaiting results, so injecting a user
736
+ # message here would sit between the tool_calls and their results. On
737
+ # approval the real results get appended after the note, leaving them
738
+ # orphaned (the context manager stubs the "missing" result right after
739
+ # the assistant message) — which the provider rejects. The agent still
740
+ # learns the sandbox is empty when the approved tool runs against it.
741
+ if meta.get("sandbox_status") and not meta.get("pending_approval"):
742
+ session.context_manager.items.append(
743
+ Message(
744
+ role="user",
745
+ content=(
746
+ "[SYSTEM: This session was resumed and its sandbox was "
747
+ "reset. Any files, installed packages, or running "
748
+ "processes from earlier are gone — recreate what you "
749
+ "need before using the sandbox.]"
750
+ ),
751
+ )
752
+ )
753
+
754
  self._restore_pending_approval(session, meta.get("pending_approval") or [])
755
  session.turn_count = int(meta.get("turn_count") or 0)
756
  session.auto_approval_enabled = bool(meta.get("auto_approval_enabled", False))
757
+ session.premium_user_billed = premium_user_billed
758
  raw_cap = meta.get("auto_approval_cost_cap_usd")
759
  session.auto_approval_cost_cap_usd = (
760
  float(raw_cap) if isinstance(raw_cap, int | float) else None
 
778
  created_at=created_at,
779
  is_active=True,
780
  is_processing=False,
781
+ claude_counted=claude_counted,
782
  title=meta.get("title"),
783
  )
784
  started = await self._start_agent_session(
 
825
  maximum number of concurrent sessions.
826
  """
827
  # ── Capacity checks ──────────────────────────────────────────
828
+ # Reserve a global slot under the lock so concurrent creates can't all
829
+ # pass the check then over-admit past MAX_SESSIONS (the build + insert
830
+ # happen later, outside the lock). active_session_count won't reflect
831
+ # this session until _start_agent_session inserts it, so we count
832
+ # _pending_creates alongside it to close that gap.
833
  async with self._lock:
834
  active_count = self.active_session_count
835
+ projected = active_count + self._pending_creates
836
+ if projected >= MAX_SESSIONS:
837
  raise SessionCapacityError(
838
+ f"Server is at capacity ({projected}/{MAX_SESSIONS} sessions). "
839
  "Please try again later.",
840
  error_type="global",
841
  )
 
847
  "concurrent sessions. Please close an existing session first.",
848
  error_type="per_user",
849
  )
850
+ self._pending_creates += 1
851
 
852
  session_id = str(uuid.uuid4())
853
 
 
855
  submission_queue: asyncio.Queue = asyncio.Queue()
856
  event_queue: asyncio.Queue = asyncio.Queue()
857
 
858
+ reserved = True
859
+ try:
860
+ # Run blocking constructors in a thread to keep the event loop responsive.
861
+ tool_router, session = await asyncio.to_thread(
862
+ self._create_session_sync,
863
+ session_id=session_id,
864
+ user_id=user_id,
865
+ hf_username=hf_username,
866
+ hf_token=hf_token,
867
+ model=model,
868
+ event_queue=event_queue,
869
+ )
870
 
871
+ # Create wrapper
872
+ agent_session = AgentSession(
873
+ session_id=session_id,
874
+ session=session,
875
+ tool_router=tool_router,
876
+ submission_queue=submission_queue,
877
+ user_id=user_id,
878
+ hf_username=hf_username,
879
+ hf_token=hf_token,
880
+ )
881
 
882
+ await self._start_agent_session(
883
+ agent_session=agent_session,
884
+ event_queue=event_queue,
885
+ tool_router=tool_router,
886
+ )
887
+ # The session is now in self.sessions, so active_session_count
888
+ # reflects it — release the reservation before the slower (and
889
+ # non-capacity) persistence + preload work.
890
+ async with self._lock:
891
+ self._pending_creates -= 1
892
+ reserved = False
893
 
894
+ await self.persist_session_snapshot(agent_session, runtime_state="idle")
895
+ self._start_cpu_sandbox_preload(agent_session)
896
+
897
+ if is_pro is not None and user_id and user_id != "dev":
898
+ await self._track_pro_status(agent_session, is_pro=is_pro)
899
 
900
+ logger.info(f"Created session {session_id} for user {user_id}")
901
+ return session_id
902
+ finally:
903
+ # Build/start failed before the session was inserted — always
904
+ # release the reservation so a failed create can't permanently
905
+ # shrink the pool.
906
+ if reserved:
907
+ async with self._lock:
908
+ self._pending_creates -= 1
909
 
910
  async def _track_pro_status(
911
  self, agent_session: AgentSession, *, is_pro: bool
 
965
 
966
  session = agent_session.session
967
  # Pass the real tool specs so the summarizer sees what the agent
968
+ # actually has. Without them, the summarizer can editorialize that
969
+ # original tool calls were fabricated.
 
970
  tool_specs = None
971
  try:
972
  tool_specs = agent_session.tool_router.get_tool_specs_for_llm()
 
996
  ),
997
  )
998
  session.context_manager.items.append(seed)
999
+ self._touch(agent_session)
1000
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
1001
  return len(parsed)
1002
 
 
1048
  SANDBOX_SHUTDOWN_CLEANUP_TIMEOUT_S,
1049
  )
1050
 
1051
+ async def _reaper_loop(self) -> None:
1052
+ """Periodically release resources held by idle sessions.
1053
+
1054
+ Modeled on EventBroadcaster.run: a long-lived task started in start()
1055
+ and cancelled in close(). Per-sweep exceptions are swallowed so one bad
1056
+ sweep never kills the loop.
1057
+ """
1058
+ while True:
1059
+ try:
1060
+ await asyncio.sleep(REAPER_INTERVAL_S)
1061
+ await self._reap_idle_sessions()
1062
+ except asyncio.CancelledError:
1063
+ break
1064
+ except Exception as e:
1065
+ logger.error("Idle-session reaper sweep failed: %s", e)
1066
+
1067
+ async def _reap_idle_sessions(self) -> None:
1068
+ """Select idle candidates under the lock, then tear each down.
1069
+
1070
+ Candidates are non-dev sessions that are live, not processing, not
1071
+ awaiting tool approval (those are "approve later", not idle — reaping
1072
+ would destroy the sandbox the approved tool needs), and untouched for
1073
+ the idle window. We only snapshot IDs under the lock; the actual
1074
+ teardown in _reap_one re-acquires it, because tearing a session down
1075
+ while holding the lock would deadlock (the lock is non-reentrant).
1076
+ """
1077
+ # Reaping is only safe when sessions stay resumable from Mongo. With no
1078
+ # store, eviction would destroy non-dev chats outright, so don't reap.
1079
+ if not getattr(self._store(), "enabled", False):
1080
+ return
1081
+
1082
+ cutoff = datetime.utcnow() - REAPER_IDLE
1083
+ async with self._lock:
1084
+ candidates = [
1085
+ agent_session.session_id
1086
+ for agent_session in self.sessions.values()
1087
+ if agent_session.is_active
1088
+ and not agent_session.is_processing
1089
+ and not agent_session.is_reaping
1090
+ and agent_session.user_id != "dev"
1091
+ and not agent_session.session.pending_approval
1092
+ and agent_session.last_active_at <= cutoff
1093
+ ]
1094
+ if not candidates:
1095
+ return
1096
+
1097
+ reaped = 0
1098
+ for session_id in candidates:
1099
+ try:
1100
+ if await self._reap_one(session_id, cutoff):
1101
+ reaped += 1
1102
+ except Exception as e:
1103
+ logger.warning("Failed to reap idle session %s: %s", session_id, e)
1104
+ if reaped:
1105
+ logger.info("Reaped %d idle session(s)", reaped)
1106
+
1107
+ async def _reap_one(self, session_id: str, cutoff: datetime) -> bool:
1108
+ """Tear down one idle session, leaving it resumable from Mongo.
1109
+
1110
+ Re-checks every idle condition under the lock (a user may have become
1111
+ active in the gap since selection), marks the session reaping, persists
1112
+ a resumable snapshot outside the lock, then does one final locked
1113
+ re-check before eviction. The runtime task is cancelled *outside* the
1114
+ lock: its own ``finally`` frees the sandbox, and its identity-gated
1115
+ persist no-ops because the session is already popped — so it can't
1116
+ overwrite our resumable snapshot with ``"ended"`` and there's no
1117
+ deadlock. Returns True if the session was reaped.
1118
+ """
1119
+ async with self._lock:
1120
+ agent_session = self.sessions.get(session_id)
1121
+ if (
1122
+ agent_session is None
1123
+ or not agent_session.is_active
1124
+ or agent_session.is_processing
1125
+ or agent_session.is_reaping
1126
+ or agent_session.session.pending_approval
1127
+ or agent_session.last_active_at > cutoff
1128
+ or not agent_session.submission_queue.empty()
1129
+ ):
1130
+ return False
1131
+ agent_session.is_reaping = True
1132
+
1133
+ # Persist a resumable snapshot *before* eviction so a concurrent reopen
1134
+ # reloads clean state. status="active" (never "ended") keeps it a normal
1135
+ # chat in the sidebar. Do this outside the manager lock: Mongo writes can
1136
+ # take network round trips, and is_reaping=True is enough to block submit
1137
+ # from enqueueing while the snapshot is in flight.
1138
+ try:
1139
+ await self.persist_session_snapshot(
1140
+ agent_session,
1141
+ runtime_state="idle",
1142
+ status="active",
1143
+ raise_on_error=True,
1144
+ )
1145
+ except Exception as e:
1146
+ async with self._lock:
1147
+ if self.sessions.get(session_id) is agent_session:
1148
+ agent_session.is_reaping = False
1149
+ logger.warning(
1150
+ "Skipping reap of %s: could not persist resumable snapshot: %s",
1151
+ session_id,
1152
+ e,
1153
+ )
1154
+ return False
1155
+
1156
+ async with self._lock:
1157
+ current = self.sessions.get(session_id)
1158
+ if current is not agent_session:
1159
+ return False
1160
+ if (
1161
+ not agent_session.is_active
1162
+ or agent_session.is_processing
1163
+ or agent_session.session.pending_approval
1164
+ or agent_session.last_active_at > cutoff
1165
+ or not agent_session.submission_queue.empty()
1166
+ ):
1167
+ agent_session.is_reaping = False
1168
+ return False
1169
+ self.sessions.pop(session_id, None)
1170
+ task = agent_session.task
1171
+ session = agent_session.session
1172
+
1173
+ if task is not None and not task.done():
1174
+ task.cancel()
1175
+ # Use asyncio.wait, not wait_for: wait_for re-raises the cancelled
1176
+ # task's CancelledError, which we'd have to swallow — and that same
1177
+ # bare except would also eat an *outer* cancel aimed at the reaper
1178
+ # itself (close() cancelling _reaper_task), hanging shutdown.
1179
+ # asyncio.wait returns the cancelled task in `done` and lets an
1180
+ # outer cancel propagate cleanly.
1181
+ done, _pending = await asyncio.wait({task}, timeout=REAP_TEARDOWN_TIMEOUT_S)
1182
+ if not done:
1183
+ logger.warning(
1184
+ "Reaper teardown timed out after %.0fs for %s; orphan "
1185
+ "sweeper will handle any sandbox straggler",
1186
+ REAP_TEARDOWN_TIMEOUT_S,
1187
+ session_id,
1188
+ )
1189
+ elif not task.cancelled():
1190
+ # Surface (and retrieve, to avoid "exception never retrieved")
1191
+ # any non-cancellation teardown error.
1192
+ exc = task.exception()
1193
+ if exc is not None:
1194
+ logger.warning("Reaper teardown error for %s: %s", session_id, exc)
1195
+ else:
1196
+ # No live task to run the cleanup finally — free the sandbox here so
1197
+ # a reaped session never leaves an orphaned Space behind.
1198
+ await self._cleanup_sandbox(session)
1199
+ return True
1200
+
1201
  async def _run_session(
1202
  self,
1203
  session_id: str,
 
1232
  submission_queue.get(), timeout=1.0
1233
  )
1234
  agent_session.is_processing = True
1235
+ self._touch(agent_session)
1236
  try:
1237
  should_continue = await process_submission(
1238
  session, submission
1239
  )
1240
  finally:
1241
  agent_session.is_processing = False
1242
+ # Stamp on turn finish too: a turn that ran longer
1243
+ # than the idle window would otherwise be reaped the
1244
+ # instant it completes.
1245
+ self._touch(agent_session)
1246
  await self.persist_session_snapshot(agent_session)
1247
  if not should_continue:
1248
  break
 
1278
  logger.warning(f"Final-flush failed for {session_id}: {e}")
1279
 
1280
  async with self._lock:
1281
+ if self.sessions.get(session_id) is agent_session:
1282
+ agent_session.is_active = False
1283
  await self.persist_session_snapshot(
1284
+ agent_session,
1285
  runtime_state="ended",
1286
  status="ended",
1287
  )
 
1289
  logger.info(f"Session {session_id} ended")
1290
 
1291
  async def submit(self, session_id: str, operation: Operation) -> bool:
1292
+ """Submit an operation to a session.
 
 
 
 
 
 
1293
 
1294
+ Enqueues under the lock and rejects sessions being reaped, so submit
1295
+ and reap can't interleave: either the message is enqueued before the
1296
+ reaper's empty() re-check (which then aborts the reap), or the session
1297
+ is already popped (we return False and the caller reloads a fresh
1298
+ runtime from Mongo). The queue is unbounded, so put_nowait never blocks.
1299
+ """
1300
  submission = Submission(id=f"sub_{uuid.uuid4().hex[:8]}", operation=operation)
1301
+ async with self._lock:
1302
+ agent_session = self.sessions.get(session_id)
1303
+ if (
1304
+ not agent_session
1305
+ or not agent_session.is_active
1306
+ or agent_session.is_reaping
1307
+ ):
1308
+ logger.warning(f"Session {session_id} not found or inactive")
1309
+ return False
1310
+ agent_session.submission_queue.put_nowait(submission)
1311
+ self._touch(agent_session)
1312
  return True
1313
 
1314
  async def submit_user_input(self, session_id: str, text: str) -> bool:
 
1348
  user_message_index
1349
  )
1350
  if success:
1351
+ self._touch(agent_session)
1352
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
1353
  return success
1354
 
 
1422
  if not agent_session or not agent_session.is_active:
1423
  return False
1424
  agent_session.session.update_model(model_id)
1425
+ self._touch(agent_session)
1426
  await self.persist_session_snapshot(agent_session, runtime_state="idle")
1427
  return True
1428
 
 
1458
  else:
1459
  session.auto_approval_enabled = bool(enabled)
1460
  session.auto_approval_cost_cap_usd = cost_cap_usd
1461
+ self._touch(agent_session)
1462
  await self.persist_session_snapshot(agent_session)
1463
  return self._auto_approval_summary(session)
1464
 
 
1505
  agent_session.session.notification_destinations
1506
  ),
1507
  "auto_approval": self._auto_approval_summary(agent_session.session),
1508
+ "premium_user_billed": getattr(
1509
+ agent_session.session, "premium_user_billed", False
1510
+ ),
1511
+ "premium_quota_counted": agent_session.claude_counted,
1512
  }
1513
 
1514
  def set_notification_destinations(
 
1535
  seen.add(name)
1536
 
1537
  agent_session.session.set_notification_destinations(normalized)
1538
+ self._touch(agent_session)
1539
  return normalized
1540
 
1541
  async def list_sessions(self, user_id: str | None = None) -> list[dict[str, Any]]:
 
1573
  "pending_approval": pending or None,
1574
  "model": row.get("model"),
1575
  "title": row.get("title"),
1576
+ "premium_user_billed": bool(
1577
+ row.get("premium_user_billed", False)
1578
+ ),
1579
+ "premium_quota_counted": bool(row.get("claude_counted", False)),
1580
  "notification_destinations": row.get(
1581
  "notification_destinations"
1582
  )
backend/user_quotas.py CHANGED
@@ -1,4 +1,4 @@
1
- """Daily quota for premium model session creations.
2
 
3
  Tracks per-user premium model session starts against a daily cap derived from
4
  the user's HF plan. MongoDB is the source of truth when configured; the
@@ -7,12 +7,13 @@ in-process dict remains the fallback for local/dev/test runs.
7
  The public names still say ``claude`` because this quota bucket originally
8
  only covered Claude and the persisted session field uses that name.
9
 
10
- Unit: session *creations*, not messages. A user who sends with a premium model
11
- in a new session consumes one quota point; switching an already-counted session
12
- back to a premium model doesn't (`AgentSession.claude_counted` guards that).
 
13
 
14
  Cap tiers:
15
- free user → CLAUDE_FREE_DAILY (1)
16
  pro user → CLAUDE_PRO_DAILY (20)
17
  """
18
 
@@ -26,7 +27,7 @@ from agent.core.session_persistence import (
26
  _reset_store_for_tests,
27
  )
28
 
29
- CLAUDE_FREE_DAILY: int = int(os.environ.get("CLAUDE_FREE_DAILY", "1"))
30
  CLAUDE_PRO_DAILY: int = int(os.environ.get("CLAUDE_PRO_DAILY", "20"))
31
 
32
  # user_id -> (day_utc_iso, count_for_that_day)
 
1
+ """Daily quota for subsidized premium model sessions.
2
 
3
  Tracks per-user premium model session starts against a daily cap derived from
4
  the user's HF plan. MongoDB is the source of truth when configured; the
 
7
  The public names still say ``claude`` because this quota bucket originally
8
  only covered Claude and the persisted session field uses that name.
9
 
10
+ Unit: first premium-model submit in a session, not raw messages. A user who
11
+ sends with a premium model in a new session consumes one quota point; switching
12
+ an already-counted session back to a premium model doesn't
13
+ (`AgentSession.claude_counted` guards that).
14
 
15
  Cap tiers:
16
+ free user → CLAUDE_FREE_DAILY (2)
17
  pro user → CLAUDE_PRO_DAILY (20)
18
  """
19
 
 
27
  _reset_store_for_tests,
28
  )
29
 
30
+ CLAUDE_FREE_DAILY: int = int(os.environ.get("CLAUDE_FREE_DAILY", "2"))
31
  CLAUDE_PRO_DAILY: int = int(os.environ.get("CLAUDE_PRO_DAILY", "20"))
32
 
33
  # user_id -> (day_utc_iso, count_for_that_day)
configs/cli_agent_config.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "model_name": "anthropic/claude-opus-4-6",
3
  "save_sessions": true,
4
  "session_dataset_repo": "smolagents/ml-intern-sessions",
5
  "share_traces": true,
 
1
  {
2
+ "model_name": "anthropic/claude-opus-4.8:fal-ai",
3
  "save_sessions": true,
4
  "session_dataset_repo": "smolagents/ml-intern-sessions",
5
  "share_traces": true,
configs/frontend_agent_config.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "model_name": "${ML_INTERN_CLAUDE_MODEL_ID:-bedrock/us.anthropic.claude-opus-4-6-v1}",
3
  "save_sessions": true,
4
  "session_dataset_repo": "smolagents/ml-intern-sessions",
5
  "share_traces": true,
 
1
  {
2
+ "model_name": "${ML_INTERN_DEFAULT_MODEL_ID:-anthropic/claude-opus-4.8:fal-ai}",
3
  "save_sessions": true,
4
  "session_dataset_repo": "smolagents/ml-intern-sessions",
5
  "share_traces": true,
frontend/src/components/Chat/ActivityStatusBar.tsx CHANGED
@@ -25,7 +25,7 @@ function formatResearchStatus(raw: string): string {
25
  const s = raw.replace(/^▸\s*/, '');
26
  const jsonStart = s.indexOf('{');
27
  const toolName = jsonStart > 0 ? s.slice(0, jsonStart).trim() : s.trim();
28
- let args: Record<string, string> = {};
29
  if (jsonStart > 0) {
30
  const jsonStr = s.slice(jsonStart);
31
  try {
 
25
  const s = raw.replace(/^▸\s*/, '');
26
  const jsonStart = s.indexOf('{');
27
  const toolName = jsonStart > 0 ? s.slice(0, jsonStart).trim() : s.trim();
28
+ const args: Record<string, string> = {};
29
  if (jsonStart > 0) {
30
  const jsonStr = s.slice(jsonStart);
31
  try {
frontend/src/components/Chat/BillingBanner.tsx ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Shown above the composer when the active session's premium usage is being
3
+ * billed to the user's own HF account — i.e. they're past the subsidized daily
4
+ * allowance and the session flipped to `premium_user_billed`. Dismissible once
5
+ * per day so we stay transparent about billing without nagging every session.
6
+ */
7
+ import { useState } from 'react';
8
+ import { Box, Link, Typography } from '@mui/material';
9
+
10
+ const DISMISS_KEY = 'ml-intern:billing-banner-dismissed';
11
+ const today = () => new Date().toISOString().slice(0, 10);
12
+
13
+ export default function BillingBanner() {
14
+ const [dismissed, setDismissed] = useState(() => {
15
+ try {
16
+ return localStorage.getItem(DISMISS_KEY) === today();
17
+ } catch {
18
+ return false;
19
+ }
20
+ });
21
+
22
+ if (dismissed) return null;
23
+
24
+ const dismiss = () => {
25
+ try {
26
+ localStorage.setItem(DISMISS_KEY, today());
27
+ } catch {
28
+ /* ignore storage failures */
29
+ }
30
+ setDismissed(true);
31
+ };
32
+
33
+ return (
34
+ <Box sx={{ maxWidth: '880px', mx: 'auto', width: '100%', px: { xs: 0, sm: 1, md: 2 }, mb: 1 }}>
35
+ <Box
36
+ sx={{
37
+ display: 'flex',
38
+ alignItems: 'center',
39
+ gap: 1.5,
40
+ p: '8px 12px',
41
+ borderRadius: 'var(--radius-md)',
42
+ bgcolor: 'var(--accent-yellow-weak)',
43
+ border: '1px solid var(--border)',
44
+ }}
45
+ >
46
+ <Typography
47
+ variant="caption"
48
+ sx={{ flex: 1, color: 'var(--text)', fontSize: '0.78rem', lineHeight: 1.5 }}
49
+ >
50
+ You've used today's subsidized premium sessions — this session's usage is billed to your{' '}
51
+ <Link
52
+ href="https://huggingface.co/settings/billing"
53
+ target="_blank"
54
+ rel="noopener noreferrer"
55
+ sx={{ color: 'inherit', textDecoration: 'underline' }}
56
+ >
57
+ Hugging Face account
58
+ </Link>{' '}
59
+ through Hugging Face Inference Providers.
60
+ </Typography>
61
+ <Box
62
+ component="button"
63
+ onClick={dismiss}
64
+ aria-label="Dismiss billing notice"
65
+ sx={{
66
+ border: 'none',
67
+ background: 'none',
68
+ cursor: 'pointer',
69
+ color: 'var(--muted-text)',
70
+ fontSize: '0.95rem',
71
+ lineHeight: 1,
72
+ p: 0.5,
73
+ '&:hover': { color: 'var(--text)' },
74
+ }}
75
+ >
76
+ ✕
77
+ </Box>
78
+ </Box>
79
+ </Box>
80
+ );
81
+ }
frontend/src/components/Chat/ChatErrorBanner.tsx ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import { Alert, AlertTitle, Box, Button, Typography } from '@mui/material';
3
+
4
+ interface ChatErrorBannerProps {
5
+ error: string;
6
+ sessionId: string;
7
+ model?: string | null;
8
+ onDismiss: () => void;
9
+ }
10
+
11
+ export default function ChatErrorBanner({ error, sessionId, model, onDismiss }: ChatErrorBannerProps) {
12
+ const [copied, setCopied] = useState(false);
13
+ const [reportedAt, setReportedAt] = useState(() => new Date().toISOString());
14
+
15
+ useEffect(() => {
16
+ setReportedAt(new Date().toISOString());
17
+ setCopied(false);
18
+ }, [error]);
19
+
20
+ const details = useMemo(
21
+ () => [
22
+ 'ML Intern message failure',
23
+ `time: ${reportedAt}`,
24
+ `session: ${sessionId}`,
25
+ `model: ${model || 'unknown'}`,
26
+ `error: ${error}`,
27
+ ].join('\n'),
28
+ [error, model, reportedAt, sessionId],
29
+ );
30
+
31
+ const copyDetails = async () => {
32
+ try {
33
+ await navigator.clipboard.writeText(details);
34
+ setCopied(true);
35
+ } catch {
36
+ setCopied(false);
37
+ }
38
+ };
39
+
40
+ return (
41
+ <Box sx={{ maxWidth: 880, mx: 'auto', width: '100%', px: { xs: 0, sm: 1, md: 2 }, mb: 1 }}>
42
+ <Alert
43
+ severity="error"
44
+ variant="outlined"
45
+ onClose={onDismiss}
46
+ sx={{
47
+ bgcolor: 'rgba(224, 90, 79, 0.08)',
48
+ borderColor: 'rgba(224, 90, 79, 0.4)',
49
+ color: 'var(--text)',
50
+ '& .MuiAlert-icon': { color: 'var(--accent-red)' },
51
+ }}
52
+ action={
53
+ <Box sx={{ display: 'flex', gap: 0.5 }}>
54
+ <Button color="inherit" size="small" onClick={copyDetails} sx={{ textTransform: 'none' }}>
55
+ {copied ? 'Copied' : 'Copy details'}
56
+ </Button>
57
+ <Button color="inherit" size="small" onClick={onDismiss} sx={{ textTransform: 'none' }}>
58
+ Dismiss
59
+ </Button>
60
+ </Box>
61
+ }
62
+ >
63
+ <AlertTitle sx={{ fontWeight: 700, fontSize: '0.86rem' }}>
64
+ Message failed
65
+ </AlertTitle>
66
+ <Typography variant="body2" sx={{ fontSize: '0.8rem', lineHeight: 1.5 }}>
67
+ The backend could not process the last message. Retry after a moment. If it keeps
68
+ happening, raise an issue with the copied details.
69
+ </Typography>
70
+ <Typography
71
+ variant="caption"
72
+ component="pre"
73
+ sx={{
74
+ display: 'block',
75
+ mt: 1,
76
+ mb: 0,
77
+ p: 1,
78
+ maxHeight: 96,
79
+ overflow: 'auto',
80
+ bgcolor: 'var(--code-bg)',
81
+ borderRadius: '6px',
82
+ whiteSpace: 'pre-wrap',
83
+ wordBreak: 'break-word',
84
+ fontSize: '0.72rem',
85
+ }}
86
+ >
87
+ {error}
88
+ </Typography>
89
+ </Alert>
90
+ </Box>
91
+ );
92
+ }
frontend/src/components/Chat/ChatInput.tsx CHANGED
@@ -20,14 +20,12 @@ import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
20
  import StopIcon from '@mui/icons-material/Stop';
21
  import AddIcon from '@mui/icons-material/Add';
22
  import { apiFetch, apiUpload } from '@/utils/api';
23
- import { useUserQuota } from '@/hooks/useUserQuota';
24
- import ClaudeCapDialog from '@/components/ClaudeCapDialog';
25
  import JobsUpgradeDialog from '@/components/JobsUpgradeDialog';
26
  import { useAgentStore } from '@/store/agentStore';
27
  import { useSessionStore } from '@/store/sessionStore';
28
  import {
29
  CLAUDE_MODEL_PATH,
30
- FIRST_FREE_MODEL_PATH,
31
  GPT_55_MODEL_PATH,
32
  isClaudePath,
33
  isPremiumPath,
@@ -50,57 +48,97 @@ const getHfAvatarUrl = (modelId: string) => {
50
 
51
  const DEFAULT_MODEL_OPTIONS: ModelOption[] = [
52
  {
53
- id: 'kimi-k2.6',
54
- name: 'Kimi K2.6',
55
- description: 'Novita',
56
- modelPath: 'moonshotai/Kimi-K2.6',
57
- avatarUrl: getHfAvatarUrl('moonshotai/Kimi-K2.6'),
58
- recommended: true,
59
- },
60
- {
61
- id: 'claude-opus',
62
- name: 'Claude Opus 4.6',
63
- description: 'Anthropic',
64
  modelPath: CLAUDE_MODEL_PATH,
65
- avatarUrl: 'https://huggingface.co/api/avatars/Anthropic',
66
  recommended: true,
67
  },
68
  {
69
  id: 'gpt-5.5',
70
  name: 'GPT-5.5',
71
- description: 'OpenAI',
72
  modelPath: GPT_55_MODEL_PATH,
73
- avatarUrl: 'https://huggingface.co/api/avatars/openai',
 
 
 
 
 
 
 
74
  },
75
  {
76
  id: 'minimax-m2.7',
77
  name: 'MiniMax M2.7',
78
- description: 'Novita',
79
  modelPath: 'MiniMaxAI/MiniMax-M2.7',
80
  avatarUrl: getHfAvatarUrl('MiniMaxAI/MiniMax-M2.7'),
81
  },
82
  {
83
  id: 'glm-5.1',
84
  name: 'GLM 5.1',
85
- description: 'Together',
86
  modelPath: 'zai-org/GLM-5.1',
87
  avatarUrl: getHfAvatarUrl('zai-org/GLM-5.1'),
88
  },
89
  {
90
  id: 'deepseek-v4-pro',
91
  name: 'DeepSeek V4 Pro',
92
- description: 'DeepInfra',
93
  modelPath: 'deepseek-ai/DeepSeek-V4-Pro:deepinfra',
94
  avatarUrl: getHfAvatarUrl('deepseek-ai/DeepSeek-V4-Pro'),
95
  },
96
  ];
97
 
 
 
 
 
 
 
 
98
  const findModelByPath = (path: string, options: ModelOption[]): ModelOption | undefined => {
 
 
 
 
 
 
 
 
 
 
 
99
  if (isClaudePath(path)) {
100
  const claude = options.find(isClaudeModel);
101
  if (claude) return claude;
102
  }
103
- return options.find(m => m.modelPath === path || path?.includes(m.id));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  };
105
 
106
  const readApiErrorMessage = async (res: Response, fallback: string): Promise<string> => {
@@ -125,6 +163,8 @@ interface ChatInputProps {
125
  isProcessing?: boolean;
126
  disabled?: boolean;
127
  placeholder?: string;
 
 
128
  }
129
 
130
  interface DatasetUploadResponse {
@@ -148,7 +188,6 @@ const DATASET_UPLOAD_EXTENSIONS = new Set(['csv', 'json', 'jsonl']);
148
 
149
  const isClaudeModel = (m: ModelOption) => isClaudePath(m.modelPath);
150
  const isPremiumModel = (m: ModelOption) => isPremiumPath(m.modelPath);
151
- const firstFreeModel = (options: ModelOption[]) => options.find(m => !isPremiumModel(m)) ?? options[0];
152
 
153
  const formatBytes = (bytes: number) => {
154
  if (bytes < 1024) return `${bytes} B`;
@@ -160,7 +199,7 @@ const datasetRepoUrl = (repoId: string) => (
160
  `https://huggingface.co/datasets/${repoId.split('/').map(encodeURIComponent).join('/')}`
161
  );
162
 
163
- export default function ChatInput({ sessionId, initialModelPath, onSend, onStop, onDatasetUploaded, isProcessing = false, disabled = false, placeholder = 'Ask anything...' }: ChatInputProps) {
164
  const [input, setInput] = useState('');
165
  const inputRef = useRef<HTMLTextAreaElement>(null);
166
  const fileInputRef = useRef<HTMLInputElement>(null);
@@ -171,14 +210,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
171
  () => findModelByPath(initialModelPath ?? '', DEFAULT_MODEL_OPTIONS)?.id ?? DEFAULT_MODEL_OPTIONS[0].id,
172
  );
173
  const [modelAnchorEl, setModelAnchorEl] = useState<null | HTMLElement>(null);
174
- const { quota, refresh: refreshQuota } = useUserQuota();
175
- // The daily-cap dialog is triggered from two places: (a) a 429 returned
176
- // from the chat transport when the user tries to send on a premium model over cap —
177
- // surfaced via the agent-store flag — and (b) nothing else right now
178
- // (switching models is free). Keeping the open state in the store means
179
- // the hook layer can flip it without threading props through.
180
- const claudeQuotaExhausted = useAgentStore((s) => s.claudeQuotaExhausted);
181
- const setClaudeQuotaExhausted = useAgentStore((s) => s.setClaudeQuotaExhausted);
182
  const jobsUpgradeRequired = useAgentStore((s) => s.jobsUpgradeRequired);
183
  const setJobsUpgradeRequired = useAgentStore((s) => s.setJobsUpgradeRequired);
184
  const updateSessionModel = useSessionStore((s) => s.updateSessionModel);
@@ -189,7 +220,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
189
  const [uploadedDatasets, setUploadedDatasets] = useState<DatasetUploadResponse[]>([]);
190
  const [isUploadingDataset, setIsUploadingDataset] = useState(false);
191
  const [datasetUploadProgress, setDatasetUploadProgress] = useState<number | null>(null);
192
- const lastSentRef = useRef<string>('');
193
 
194
  useEffect(() => {
195
  modelOptionsRef.current = modelOptions;
@@ -205,16 +235,10 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
205
  .then((res) => (res.ok ? res.json() : null))
206
  .then((data) => {
207
  if (cancelled || !data?.available) return;
208
- const claude = data.available.find((m: { provider?: string; id?: string }) => (
209
- m.provider === 'anthropic' && m.id
210
- ));
211
- if (!claude?.id) return;
212
-
213
- const next = DEFAULT_MODEL_OPTIONS.map((option) => (
214
- isClaudeModel(option)
215
- ? { ...option, modelPath: claude.id, name: claude.label ?? option.name }
216
- : option
217
- ));
218
  modelOptionsRef.current = next;
219
  setModelOptions(next);
220
  if (!sessionIdRef.current) {
@@ -256,7 +280,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
256
 
257
  const handleSend = useCallback(() => {
258
  if (input.trim() && !disabled && !isUploadingDataset) {
259
- lastSentRef.current = input;
260
  onSend(input);
261
  setInput('');
262
  }
@@ -325,14 +348,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
325
  [sessionId, onDatasetUploaded],
326
  );
327
 
328
- // When the chat transport reports a premium-model quota 429, restore the typed
329
- // text so the user doesn't lose their message.
330
- useEffect(() => {
331
- if (claudeQuotaExhausted && lastSentRef.current) {
332
- setInput(lastSentRef.current);
333
- }
334
- }, [claudeQuotaExhausted]);
335
-
336
  useEffect(() => {
337
  if (!datasetUploadError) return;
338
  const timeout = window.setTimeout(() => setDatasetUploadError(null), 7000);
@@ -364,6 +379,7 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
364
 
365
  const handleModelClick = (event: React.MouseEvent<HTMLElement>) => {
366
  setModelAnchorEl(event.currentTarget);
 
367
  };
368
 
369
  const handleModelClose = () => {
@@ -390,48 +406,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
390
  }
391
  };
392
 
393
- // Dialog close: just clear the flag. The typed text is already restored.
394
- const handleCapDialogClose = useCallback(() => {
395
- setClaudeQuotaExhausted(false);
396
- }, [setClaudeQuotaExhausted]);
397
-
398
- // "Use a free model" — switch the current session to Kimi (or the first
399
- // non-premium option) and auto-retry the send that tripped the cap.
400
- const handleUseFreeModel = useCallback(async () => {
401
- setClaudeQuotaExhausted(false);
402
- if (!sessionId) return;
403
- const free = modelOptions.find(m => m.modelPath === FIRST_FREE_MODEL_PATH)
404
- ?? firstFreeModel(modelOptions);
405
- try {
406
- const res = await apiFetch(`/api/session/${sessionId}/model`, {
407
- method: 'POST',
408
- body: JSON.stringify({ model: free.modelPath }),
409
- });
410
- if (res.ok) {
411
- setSelectedModelId(free.id);
412
- updateSessionModel(sessionId, free.modelPath);
413
- const retryText = lastSentRef.current;
414
- if (retryText) {
415
- onSend(retryText);
416
- setInput('');
417
- lastSentRef.current = '';
418
- }
419
- }
420
- } catch { /* ignore */ }
421
- }, [sessionId, onSend, setClaudeQuotaExhausted, modelOptions, updateSessionModel]);
422
-
423
- const handlePremiumUpgradeClick = useCallback(async () => {
424
- if (!sessionId) return;
425
- try {
426
- await apiFetch(`/api/pro-click/${sessionId}`, {
427
- method: 'POST',
428
- body: JSON.stringify({ source: 'premium_cap_dialog', target: 'pro_pricing' }),
429
- });
430
- } catch {
431
- /* tracking is best-effort */
432
- }
433
- }, [sessionId]);
434
-
435
  const handleJobsUpgradeClose = useCallback(() => {
436
  setJobsUpgradeRequired(null);
437
  setAwaitingTopUp(false);
@@ -473,14 +447,14 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
473
  return () => document.removeEventListener('visibilitychange', onVisible);
474
  }, [awaitingTopUp, jobsUpgradeRequired, handleJobsRetry]);
475
 
476
- // Hide the chip until the user has actually burned quota; opening a
477
- // premium-model session without sending should not populate a counter.
478
  const premiumChip = (() => {
479
- if (!quota || quota.premiumUsedToday === 0) return null;
480
- if (quota.plan === 'free') {
481
- return quota.premiumRemaining > 0 ? 'Free today' : 'Pro only';
 
482
  }
483
- return `${quota.premiumUsedToday}/${quota.premiumDailyCap} today`;
484
  })();
485
 
486
  return (
@@ -804,14 +778,6 @@ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop,
804
  ))}
805
  </Menu>
806
 
807
- <ClaudeCapDialog
808
- open={claudeQuotaExhausted}
809
- plan={quota?.plan ?? 'free'}
810
- cap={quota?.premiumDailyCap ?? 1}
811
- onClose={handleCapDialogClose}
812
- onUseFreeModel={handleUseFreeModel}
813
- onUpgrade={handlePremiumUpgradeClick}
814
- />
815
  <JobsUpgradeDialog
816
  open={!!jobsUpgradeRequired}
817
  message={jobsUpgradeRequired?.message || ''}
 
20
  import StopIcon from '@mui/icons-material/Stop';
21
  import AddIcon from '@mui/icons-material/Add';
22
  import { apiFetch, apiUpload } from '@/utils/api';
23
+ import type { UserQuota } from '@/hooks/useUserQuota';
 
24
  import JobsUpgradeDialog from '@/components/JobsUpgradeDialog';
25
  import { useAgentStore } from '@/store/agentStore';
26
  import { useSessionStore } from '@/store/sessionStore';
27
  import {
28
  CLAUDE_MODEL_PATH,
 
29
  GPT_55_MODEL_PATH,
30
  isClaudePath,
31
  isPremiumPath,
 
48
 
49
  const DEFAULT_MODEL_OPTIONS: ModelOption[] = [
50
  {
51
+ id: 'claude-opus-4-8',
52
+ name: 'Claude Opus 4.8',
53
+ description: 'Hugging Face',
 
 
 
 
 
 
 
 
54
  modelPath: CLAUDE_MODEL_PATH,
55
+ avatarUrl: getHfAvatarUrl(CLAUDE_MODEL_PATH),
56
  recommended: true,
57
  },
58
  {
59
  id: 'gpt-5.5',
60
  name: 'GPT-5.5',
61
+ description: 'Hugging Face',
62
  modelPath: GPT_55_MODEL_PATH,
63
+ avatarUrl: getHfAvatarUrl(GPT_55_MODEL_PATH),
64
+ },
65
+ {
66
+ id: 'kimi-k2.6',
67
+ name: 'Kimi K2.6',
68
+ description: 'Hugging Face',
69
+ modelPath: 'moonshotai/Kimi-K2.6',
70
+ avatarUrl: getHfAvatarUrl('moonshotai/Kimi-K2.6'),
71
  },
72
  {
73
  id: 'minimax-m2.7',
74
  name: 'MiniMax M2.7',
75
+ description: 'Hugging Face',
76
  modelPath: 'MiniMaxAI/MiniMax-M2.7',
77
  avatarUrl: getHfAvatarUrl('MiniMaxAI/MiniMax-M2.7'),
78
  },
79
  {
80
  id: 'glm-5.1',
81
  name: 'GLM 5.1',
82
+ description: 'Hugging Face',
83
  modelPath: 'zai-org/GLM-5.1',
84
  avatarUrl: getHfAvatarUrl('zai-org/GLM-5.1'),
85
  },
86
  {
87
  id: 'deepseek-v4-pro',
88
  name: 'DeepSeek V4 Pro',
89
+ description: 'Hugging Face',
90
  modelPath: 'deepseek-ai/DeepSeek-V4-Pro:deepinfra',
91
  avatarUrl: getHfAvatarUrl('deepseek-ai/DeepSeek-V4-Pro'),
92
  },
93
  ];
94
 
95
+ const normalizeModelPath = (path: string | undefined) => (
96
+ (path ?? '')
97
+ .toLowerCase()
98
+ .replace(/^huggingface\//, '')
99
+ .replace(/claude-opus-4\.(\d)/g, 'claude-opus-4-$1')
100
+ );
101
+
102
  const findModelByPath = (path: string, options: ModelOption[]): ModelOption | undefined => {
103
+ const normalizedPath = normalizeModelPath(path);
104
+ const matched = options.find((m) => {
105
+ const normalizedModelPath = normalizeModelPath(m.modelPath);
106
+ const normalizedId = normalizeModelPath(m.id);
107
+ return (
108
+ m.modelPath === path ||
109
+ normalizedModelPath === normalizedPath ||
110
+ normalizedPath.includes(normalizedId)
111
+ );
112
+ });
113
+ if (matched) return matched;
114
  if (isClaudePath(path)) {
115
  const claude = options.find(isClaudeModel);
116
  if (claude) return claude;
117
  }
118
+ return undefined;
119
+ };
120
+
121
+ const modelOptionId = (modelPath: string) => (
122
+ normalizeModelPath(modelPath)
123
+ .replace(/[^a-z0-9]+/g, '-')
124
+ .replace(/^-|-$/g, '')
125
+ );
126
+
127
+ const modelOptionFromApi = (model: {
128
+ id?: string;
129
+ label?: string;
130
+ provider?: string;
131
+ recommended?: boolean;
132
+ }): ModelOption | null => {
133
+ if (!model.id) return null;
134
+ return {
135
+ id: modelOptionId(model.id),
136
+ name: model.label ?? model.id,
137
+ description: 'Hugging Face',
138
+ modelPath: model.id,
139
+ avatarUrl: getHfAvatarUrl(model.id.replace(/^huggingface\//, '')),
140
+ recommended: Boolean(model.recommended),
141
+ };
142
  };
143
 
144
  const readApiErrorMessage = async (res: Response, fallback: string): Promise<string> => {
 
163
  isProcessing?: boolean;
164
  disabled?: boolean;
165
  placeholder?: string;
166
+ quota: UserQuota | null;
167
+ refreshQuota: () => Promise<void> | void;
168
  }
169
 
170
  interface DatasetUploadResponse {
 
188
 
189
  const isClaudeModel = (m: ModelOption) => isClaudePath(m.modelPath);
190
  const isPremiumModel = (m: ModelOption) => isPremiumPath(m.modelPath);
 
191
 
192
  const formatBytes = (bytes: number) => {
193
  if (bytes < 1024) return `${bytes} B`;
 
199
  `https://huggingface.co/datasets/${repoId.split('/').map(encodeURIComponent).join('/')}`
200
  );
201
 
202
+ export default function ChatInput({ sessionId, initialModelPath, onSend, onStop, onDatasetUploaded, isProcessing = false, disabled = false, placeholder = 'Ask anything...', quota, refreshQuota }: ChatInputProps) {
203
  const [input, setInput] = useState('');
204
  const inputRef = useRef<HTMLTextAreaElement>(null);
205
  const fileInputRef = useRef<HTMLInputElement>(null);
 
210
  () => findModelByPath(initialModelPath ?? '', DEFAULT_MODEL_OPTIONS)?.id ?? DEFAULT_MODEL_OPTIONS[0].id,
211
  );
212
  const [modelAnchorEl, setModelAnchorEl] = useState<null | HTMLElement>(null);
 
 
 
 
 
 
 
 
213
  const jobsUpgradeRequired = useAgentStore((s) => s.jobsUpgradeRequired);
214
  const setJobsUpgradeRequired = useAgentStore((s) => s.setJobsUpgradeRequired);
215
  const updateSessionModel = useSessionStore((s) => s.updateSessionModel);
 
220
  const [uploadedDatasets, setUploadedDatasets] = useState<DatasetUploadResponse[]>([]);
221
  const [isUploadingDataset, setIsUploadingDataset] = useState(false);
222
  const [datasetUploadProgress, setDatasetUploadProgress] = useState<number | null>(null);
 
223
 
224
  useEffect(() => {
225
  modelOptionsRef.current = modelOptions;
 
235
  .then((res) => (res.ok ? res.json() : null))
236
  .then((data) => {
237
  if (cancelled || !data?.available) return;
238
+ const next = data.available
239
+ .map(modelOptionFromApi)
240
+ .filter((model: ModelOption | null): model is ModelOption => model !== null);
241
+ if (!next.length) return;
 
 
 
 
 
 
242
  modelOptionsRef.current = next;
243
  setModelOptions(next);
244
  if (!sessionIdRef.current) {
 
280
 
281
  const handleSend = useCallback(() => {
282
  if (input.trim() && !disabled && !isUploadingDataset) {
 
283
  onSend(input);
284
  setInput('');
285
  }
 
348
  [sessionId, onDatasetUploaded],
349
  );
350
 
 
 
 
 
 
 
 
 
351
  useEffect(() => {
352
  if (!datasetUploadError) return;
353
  const timeout = window.setTimeout(() => setDatasetUploadError(null), 7000);
 
379
 
380
  const handleModelClick = (event: React.MouseEvent<HTMLElement>) => {
381
  setModelAnchorEl(event.currentTarget);
382
+ void refreshQuota();
383
  };
384
 
385
  const handleModelClose = () => {
 
406
  }
407
  };
408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  const handleJobsUpgradeClose = useCallback(() => {
410
  setJobsUpgradeRequired(null);
411
  setAwaitingTopUp(false);
 
447
  return () => document.removeEventListener('visibilitychange', onVisible);
448
  }, [awaitingTopUp, jobsUpgradeRequired, handleJobsRetry]);
449
 
450
+ // Show the remaining subsidized premium-session allowance for today.
 
451
  const premiumChip = (() => {
452
+ if (!quota) return null;
453
+ const remaining = Math.max(0, quota.premiumRemaining);
454
+ if (remaining === 0) {
455
+ return quota.plan === 'pro' ? '0 left – using HF billing' : '0 left – enable billing';
456
  }
457
+ return `${remaining} left today`;
458
  })();
459
 
460
  return (
 
778
  ))}
779
  </Menu>
780
 
 
 
 
 
 
 
 
 
781
  <JobsUpgradeDialog
782
  open={!!jobsUpgradeRequired}
783
  message={jobsUpgradeRequired?.message || ''}
frontend/src/components/ClaudeCapDialog.tsx DELETED
@@ -1,141 +0,0 @@
1
- import {
2
- Box,
3
- Button,
4
- Dialog,
5
- DialogActions,
6
- DialogContent,
7
- DialogContentText,
8
- DialogTitle,
9
- Typography,
10
- } from '@mui/material';
11
- import type { PlanTier } from '@/hooks/useUserQuota';
12
-
13
- const HF_PRICING_URL = 'https://huggingface.co/pricing';
14
- const PRO_CAP = 20;
15
-
16
- interface ClaudeCapDialogProps {
17
- open: boolean;
18
- plan: PlanTier;
19
- cap: number;
20
- onClose: () => void;
21
- onUseFreeModel: () => void;
22
- onUpgrade: () => void;
23
- }
24
-
25
- export default function ClaudeCapDialog({
26
- open,
27
- plan,
28
- cap,
29
- onClose,
30
- onUseFreeModel,
31
- onUpgrade,
32
- }: ClaudeCapDialogProps) {
33
- const isFreePlan = plan === 'free';
34
-
35
- return (
36
- <Dialog
37
- open={open}
38
- onClose={onClose}
39
- slotProps={{
40
- backdrop: { sx: { backgroundColor: 'rgba(0,0,0,0.5)', backdropFilter: 'blur(4px)' } },
41
- }}
42
- PaperProps={{
43
- sx: {
44
- bgcolor: 'var(--panel)',
45
- border: '1px solid var(--border)',
46
- borderRadius: 'var(--radius-md)',
47
- boxShadow: 'var(--shadow-1)',
48
- maxWidth: 460,
49
- mx: 2,
50
- },
51
- }}
52
- >
53
- <DialogTitle
54
- sx={{ color: 'var(--text)', fontWeight: 700, fontSize: '1rem', pt: 2.5, pb: 0, px: 3 }}
55
- >
56
- You've hit your premium model limit
57
- </DialogTitle>
58
- <DialogContent sx={{ px: 3, pt: 1.25, pb: 0 }}>
59
- <DialogContentText
60
- sx={{ color: 'var(--muted-text)', fontSize: '0.85rem', lineHeight: 1.6 }}
61
- >
62
- Opus and GPT-5.5 are expensive to run, so we cap premium models at {cap}{' '}
63
- {cap === 1 ? 'session' : 'sessions'} a day. {isFreePlan
64
- ? 'HF Pro raises the daily premium-model limit.'
65
- : 'Your plan has used today’s premium-model allowance.'}{' '}
66
- Give Kimi, MiniMax, GLM, or DeepSeek a spin instead.
67
- </DialogContentText>
68
- {isFreePlan && (
69
- <Box
70
- sx={{
71
- mt: 2,
72
- p: 1.5,
73
- borderRadius: '8px',
74
- bgcolor: 'var(--accent-yellow-weak)',
75
- border: '1px solid var(--border)',
76
- }}
77
- >
78
- <Typography
79
- variant="caption"
80
- sx={{
81
- display: 'block',
82
- fontWeight: 700,
83
- color: 'var(--text)',
84
- fontSize: '0.78rem',
85
- mb: 0.5,
86
- letterSpacing: '0.02em',
87
- }}
88
- >
89
- HF Pro ($9/mo) — more premium model sessions
90
- </Typography>
91
- <Typography
92
- variant="caption"
93
- sx={{ display: 'block', color: 'var(--muted-text)', fontSize: '0.78rem', lineHeight: 1.55 }}
94
- >
95
- {PRO_CAP} premium model sessions/day here, 20× HF Inference credits,
96
- ZeroGPU access, and priority on Spaces hardware.
97
- </Typography>
98
- </Box>
99
- )}
100
- </DialogContent>
101
- <DialogActions sx={{ px: 3, pb: 2.5, pt: 2, gap: 1 }}>
102
- {isFreePlan && (
103
- <Button
104
- component="a"
105
- href={HF_PRICING_URL}
106
- target="_blank"
107
- rel="noopener noreferrer"
108
- onClick={onUpgrade}
109
- variant="contained"
110
- size="small"
111
- sx={{
112
- fontSize: '0.82rem',
113
- px: 2.5,
114
- bgcolor: 'var(--accent-yellow)',
115
- color: '#000',
116
- textTransform: 'none',
117
- fontWeight: 700,
118
- boxShadow: 'none',
119
- '&:hover': { bgcolor: '#FFB340', boxShadow: 'none' },
120
- }}
121
- >
122
- Upgrade to Pro
123
- </Button>
124
- )}
125
- <Button
126
- onClick={onUseFreeModel}
127
- size="small"
128
- sx={{
129
- color: 'var(--muted-text)',
130
- fontSize: '0.82rem',
131
- px: 2,
132
- textTransform: 'none',
133
- '&:hover': { bgcolor: 'var(--hover-bg)' },
134
- }}
135
- >
136
- Use a free model
137
- </Button>
138
- </DialogActions>
139
- </Dialog>
140
- );
141
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
frontend/src/components/SessionChat.tsx CHANGED
@@ -5,13 +5,17 @@
5
  * runs — processing events — but only the active session renders visible
6
  * UI (MessageList + ChatInput).
7
  */
8
- import { useCallback, useEffect } from 'react';
9
  import { useAgentChat } from '@/hooks/useAgentChat';
10
  import { useAgentStore } from '@/store/agentStore';
11
  import { useSessionStore } from '@/store/sessionStore';
12
  import MessageList from '@/components/Chat/MessageList';
13
  import ChatInput from '@/components/Chat/ChatInput';
14
  import ExpiredBanner from '@/components/Chat/ExpiredBanner';
 
 
 
 
15
  import { apiFetch } from '@/utils/api';
16
  import { logger } from '@/utils/logger';
17
 
@@ -23,9 +27,10 @@ interface SessionChatProps {
23
 
24
  export default function SessionChat({ sessionId, isActive, onSessionDead }: SessionChatProps) {
25
  const { isConnected, isProcessing, activityStatus, updateSession } = useAgentStore();
26
- const { updateSessionTitle, sessions } = useSessionStore();
27
  const sessionMeta = sessions.find((s) => s.id === sessionId);
28
  const isExpired = sessionMeta?.expired === true;
 
29
 
30
  const {
31
  messages,
@@ -39,8 +44,15 @@ export default function SessionChat({ sessionId, isActive, onSessionDead }: Sess
39
  } = useAgentChat({
40
  sessionId,
41
  isActive,
 
 
 
 
42
  onReady: () => logger.log(`Session ${sessionId} ready`),
43
- onError: (error) => logger.error(`Session ${sessionId} error:`, error),
 
 
 
44
  onSessionDead,
45
  });
46
 
@@ -76,12 +88,54 @@ export default function SessionChat({ sessionId, isActive, onSessionDead }: Sess
76
  // SDK status is the ground truth — if it's streaming/submitted, agent is busy
77
  const sdkBusy = status === 'streaming' || status === 'submitted';
78
  const busy = isProcessing || sdkBusy;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  const handleSendMessage = useCallback(
81
  async (text: string) => {
82
  if (!text.trim() || busy) return;
83
 
 
84
  updateSession(sessionId, { isProcessing: true, activityStatus: { type: 'thinking' } });
 
85
  sendMessage({ text: text.trim(), metadata: { createdAt: new Date().toISOString() } });
86
 
87
  // Auto-title the session from the first user message
@@ -101,7 +155,7 @@ export default function SessionChat({ sessionId, isActive, onSessionDead }: Sess
101
  });
102
  }
103
  },
104
- [sessionId, sendMessage, messages, updateSessionTitle, busy, updateSession],
105
  );
106
 
107
  // Don't render UI for background sessions — hooks still run
@@ -120,20 +174,33 @@ export default function SessionChat({ sessionId, isActive, onSessionDead }: Sess
120
  {isExpired ? (
121
  <ExpiredBanner sessionId={sessionId} />
122
  ) : (
123
- <ChatInput
124
- sessionId={sessionId}
125
- initialModelPath={sessionMeta?.model}
126
- onSend={handleSendMessage}
127
- onStop={handleStop}
128
- onDatasetUploaded={refreshMessages}
129
- isProcessing={busy}
130
- disabled={!isConnected || activityStatus.type === 'waiting-approval'}
131
- placeholder={
132
- activityStatus.type === 'waiting-approval'
133
- ? 'Approve or reject pending tools first...'
134
- : undefined
135
- }
136
- />
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  )}
138
  </>
139
  );
 
5
  * runs — processing events — but only the active session renders visible
6
  * UI (MessageList + ChatInput).
7
  */
8
+ import { useCallback, useEffect, useState } from 'react';
9
  import { useAgentChat } from '@/hooks/useAgentChat';
10
  import { useAgentStore } from '@/store/agentStore';
11
  import { useSessionStore } from '@/store/sessionStore';
12
  import MessageList from '@/components/Chat/MessageList';
13
  import ChatInput from '@/components/Chat/ChatInput';
14
  import ExpiredBanner from '@/components/Chat/ExpiredBanner';
15
+ import BillingBanner from '@/components/Chat/BillingBanner';
16
+ import ChatErrorBanner from '@/components/Chat/ChatErrorBanner';
17
+ import { useUserQuota } from '@/hooks/useUserQuota';
18
+ import { isPremiumPath } from '@/utils/model';
19
  import { apiFetch } from '@/utils/api';
20
  import { logger } from '@/utils/logger';
21
 
 
27
 
28
  export default function SessionChat({ sessionId, isActive, onSessionDead }: SessionChatProps) {
29
  const { isConnected, isProcessing, activityStatus, updateSession } = useAgentStore();
30
+ const { updateSessionTitle, sessions, setSessionProcessing } = useSessionStore();
31
  const sessionMeta = sessions.find((s) => s.id === sessionId);
32
  const isExpired = sessionMeta?.expired === true;
33
+ const [chatError, setChatError] = useState<string | null>(null);
34
 
35
  const {
36
  messages,
 
44
  } = useAgentChat({
45
  sessionId,
46
  isActive,
47
+ // A backgrounded session that the backend reports mid-turn still needs to
48
+ // mount its live subscription; idle backgrounded sessions do not (that's
49
+ // what stops app load from reactivating every historical runtime).
50
+ isProcessing: sessionMeta?.isProcessing ?? false,
51
  onReady: () => logger.log(`Session ${sessionId} ready`),
52
+ onError: (error) => {
53
+ logger.error(`Session ${sessionId} error:`, error);
54
+ setChatError(error);
55
+ },
56
  onSessionDead,
57
  });
58
 
 
88
  // SDK status is the ground truth — if it's streaming/submitted, agent is busy
89
  const sdkBusy = status === 'streaming' || status === 'submitted';
90
  const busy = isProcessing || sdkBusy;
91
+ const { quota, refresh: refreshQuota } = useUserQuota({ enabled: isActive });
92
+
93
+ // Whether this session's premium usage is being billed to the user's own HF
94
+ // account (past the subsidized daily allowance). Re-read after each turn,
95
+ // since the backend flips it at submit time. Only premium-model sessions can
96
+ // ever be user-billed, so skip the fetch for free models.
97
+ const [premiumBilled, setPremiumBilled] = useState(false);
98
+ const [premiumQuotaCounted, setPremiumQuotaCounted] = useState(false);
99
+ const onPremiumModel = isPremiumPath(sessionMeta?.model ?? undefined);
100
+ useEffect(() => {
101
+ if (!isActive || !onPremiumModel) {
102
+ setPremiumBilled(false);
103
+ setPremiumQuotaCounted(false);
104
+ return;
105
+ }
106
+ if (busy) return;
107
+ let cancelled = false;
108
+ apiFetch(`/api/session/${sessionId}`)
109
+ .then((r) => (r.ok ? r.json() : null))
110
+ .then((d) => {
111
+ if (!cancelled && d) {
112
+ setPremiumBilled(Boolean(d.premium_user_billed));
113
+ setPremiumQuotaCounted(Boolean(d.premium_quota_counted));
114
+ }
115
+ })
116
+ .catch(() => {});
117
+ return () => {
118
+ cancelled = true;
119
+ };
120
+ }, [busy, isActive, onPremiumModel, sessionId]);
121
+
122
+ const sessionPremiumBilled = premiumBilled || Boolean(sessionMeta?.premiumUserBilled);
123
+ const sessionPremiumQuotaCounted =
124
+ premiumQuotaCounted || Boolean(sessionMeta?.premiumQuotaCounted);
125
+ const premiumBillingNotice =
126
+ sessionPremiumBilled ||
127
+ (isActive &&
128
+ onPremiumModel &&
129
+ quota?.premiumRemaining === 0 &&
130
+ !sessionPremiumQuotaCounted);
131
 
132
  const handleSendMessage = useCallback(
133
  async (text: string) => {
134
  if (!text.trim() || busy) return;
135
 
136
+ setChatError(null);
137
  updateSession(sessionId, { isProcessing: true, activityStatus: { type: 'thinking' } });
138
+ setSessionProcessing(sessionId, true);
139
  sendMessage({ text: text.trim(), metadata: { createdAt: new Date().toISOString() } });
140
 
141
  // Auto-title the session from the first user message
 
155
  });
156
  }
157
  },
158
+ [sessionId, sendMessage, messages, updateSessionTitle, busy, updateSession, setSessionProcessing],
159
  );
160
 
161
  // Don't render UI for background sessions — hooks still run
 
174
  {isExpired ? (
175
  <ExpiredBanner sessionId={sessionId} />
176
  ) : (
177
+ <>
178
+ {premiumBillingNotice && <BillingBanner />}
179
+ {chatError && (
180
+ <ChatErrorBanner
181
+ error={chatError}
182
+ sessionId={sessionId}
183
+ model={sessionMeta?.model}
184
+ onDismiss={() => setChatError(null)}
185
+ />
186
+ )}
187
+ <ChatInput
188
+ sessionId={sessionId}
189
+ initialModelPath={sessionMeta?.model}
190
+ onSend={handleSendMessage}
191
+ onStop={handleStop}
192
+ onDatasetUploaded={refreshMessages}
193
+ isProcessing={busy}
194
+ disabled={!isConnected || activityStatus.type === 'waiting-approval'}
195
+ quota={quota}
196
+ refreshQuota={refreshQuota}
197
+ placeholder={
198
+ activityStatus.type === 'waiting-approval'
199
+ ? 'Approve or reject pending tools first...'
200
+ : undefined
201
+ }
202
+ />
203
+ </>
204
  )}
205
  </>
206
  );
frontend/src/hooks/useAgentChat.ts CHANGED
@@ -24,28 +24,55 @@ import { logger } from '@/utils/logger';
24
  interface UseAgentChatOptions {
25
  sessionId: string;
26
  isActive: boolean;
 
 
27
  onReady?: () => void;
28
  onError?: (error: string) => void;
29
  onSessionDead?: (sessionId: string) => void;
30
  }
31
 
32
- export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionDead }: UseAgentChatOptions) {
33
  const callbacksRef = useRef({ onReady, onError, onSessionDead });
34
  callbacksRef.current = { onReady, onError, onSessionDead };
35
 
36
  const isActiveRef = useRef(isActive);
37
  isActiveRef.current = isActive;
38
 
 
 
 
 
 
 
 
 
 
 
 
39
  const { setNeedsAttention, updateSessionYolo } = useSessionStore();
40
 
41
  // Helper: update this session's state (mirrors to globals if active)
42
  const updateSession = useAgentStore.getState().updateSession;
 
 
 
 
 
 
 
 
 
 
43
 
44
  // -- Build side-channel callbacks (stable ref) --------------------------
45
  const sideChannel = useMemo<SideChannelCallbacks>(
46
  () => ({
47
  onReady: () => {
48
- updateSession(sessionId, { isProcessing: false });
 
 
 
 
49
  if (isActiveRef.current) {
50
  useAgentStore.getState().setConnected(true);
51
  }
@@ -53,26 +80,25 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
53
  callbacksRef.current.onReady?.();
54
  },
55
  onShutdown: () => {
56
- updateSession(sessionId, { isProcessing: false });
57
  if (isActiveRef.current) {
58
  useAgentStore.getState().setConnected(false);
59
  }
60
  },
61
  onError: (error: string) => {
62
- updateSession(sessionId, { isProcessing: false });
63
  callbacksRef.current.onError?.(error);
64
  },
65
  onProcessing: () => {
66
- updateSession(sessionId, {
67
- isProcessing: true,
68
  activityStatus: { type: 'thinking' },
69
  });
70
  },
71
  onProcessingDone: () => {
72
- updateSession(sessionId, { isProcessing: false });
73
  },
74
  onUndoComplete: () => {
75
- updateSession(sessionId, { isProcessing: false });
76
  },
77
  onCompacted: (oldTokens: number, newTokens: number) => {
78
  logger.log(`Context compacted: ${oldTokens} -> ${newTokens} tokens`);
@@ -197,7 +223,13 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
197
  );
198
  }
199
 
200
- updateSession(sessionId, { activityStatus: { type: 'waiting-approval' } });
 
 
 
 
 
 
201
 
202
  // Build panel data for this session's pending approval
203
  const firstTool = tools[0];
@@ -307,7 +339,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
307
  onInterrupted: () => { /* no-op — handled by stop() caller */ },
308
  }),
309
  // eslint-disable-next-line react-hooks/exhaustive-deps
310
- [sessionId],
311
  );
312
 
313
  // -- Create transport (one per session, stable for lifetime) ------------
@@ -349,23 +381,20 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
349
  experimental_throttle: 80,
350
  // On mount, the SDK calls transport.reconnectToStream() which checks
351
  // is_processing and subscribes to the live event stream if the agent
352
- // is mid-turn. Without this, page refresh kills live updates.
353
- resume: true,
 
 
354
  // After all approval responses are set, auto-send to continue the agent loop.
355
  // Without this, addToolApprovalResponse only updates the UI — it won't trigger
356
  // sendMessages on the transport.
357
  sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
358
  onError: (error) => {
359
- updateSession(sessionId, { isProcessing: false });
360
- // Premium-model daily cap: open the cap dialog instead of the generic error
361
- // banner. Transport marks the error with this sentinel.
362
- if (error.message === 'CLAUDE_QUOTA_EXHAUSTED') {
363
- if (isActiveRef.current) {
364
- useAgentStore.getState().setClaudeQuotaExhausted(true);
365
- }
366
- return;
367
- }
368
  logger.error('useChat error:', error);
 
 
 
369
  },
370
  });
371
 
@@ -374,7 +403,13 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
374
  chatActionsRef.current.messages = chat.messages;
375
 
376
  // -- Hydrate from backend on mount (page refresh recovery) --------------
 
 
 
 
 
377
  useEffect(() => {
 
378
  let cancelled = false;
379
  (async () => {
380
  try {
@@ -429,8 +464,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
429
  // atomic update so the UI never sees isProcessing=false with stale
430
  // tool states (which would coerce them to 'output-available').
431
  const savedResearch = loadResearch(sessionId);
432
- updateSession(sessionId, {
433
- isProcessing: true,
434
  activityStatus: savedResearch?.stats.startedAt
435
  ? { type: 'tool', toolName: 'research', description: 'Resuming research...' }
436
  : { type: 'thinking' },
@@ -440,9 +474,10 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
440
  }),
441
  });
442
  } else if (pendingIds && pendingIds.size > 0) {
443
- updateSession(sessionId, { activityStatus: { type: 'waiting-approval' } });
444
  clearResearch(sessionId);
445
  } else {
 
446
  clearResearch(sessionId);
447
  }
448
  } catch {
@@ -450,7 +485,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
450
  }
451
  })();
452
  return () => { cancelled = true; };
453
- }, [sessionId]); // eslint-disable-line react-hooks/exhaustive-deps
454
 
455
  // -- Re-hydrate + reconnect on wake from sleep ----------------------------
456
  // The Vercel AI SDK only calls reconnectToStream() on mount, NOT on
@@ -627,6 +662,10 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
627
 
628
  const onVisible = async () => {
629
  if (document.visibilityState !== 'visible') return;
 
 
 
 
630
 
631
  // Always re-hydrate messages on wake
632
  const result = await hydrateMessages();
@@ -641,7 +680,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
641
 
642
  // If the backend is still processing, reconnect to the live event stream
643
  if (info?.is_processing) {
644
- updateSession(sessionId, { isProcessing: true, activityStatus: { type: 'thinking' } });
645
 
646
  // Stop any previous reconnection
647
  stopReconnect();
@@ -666,7 +705,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
666
 
667
  // If backend stopped processing, clean up
668
  if (fresh.info && !fresh.info.is_processing) {
669
- updateSession(sessionId, { isProcessing: false });
670
  stopReconnect();
671
  }
672
  }, 3000);
@@ -678,7 +717,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
678
  document.removeEventListener('visibilitychange', onVisible);
679
  stopReconnect();
680
  };
681
- }, [sessionId]); // eslint-disable-line react-hooks/exhaustive-deps
682
 
683
  // -- Persist messages ---------------------------------------------------
684
  const prevLenRef = useRef(initialMessages.length);
@@ -713,11 +752,11 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
713
  setMsgs(updated);
714
  saveMessages(sessionId, updated);
715
  }
716
- updateSession(sessionId, { isProcessing: false });
717
  } catch (e) {
718
  logger.error('Undo failed:', e);
719
  }
720
- }, [sessionId, updateSession]);
721
 
722
  // -- Approve tools ------------------------------------------------------
723
  const approveTools = useCallback(
@@ -741,8 +780,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
741
  setNeedsAttention(sessionId, false);
742
  const hasApproved = approvals.some(a => a.approved);
743
  if (hasApproved) {
744
- updateSession(sessionId, {
745
- isProcessing: true,
746
  activityStatus: { type: 'thinking' },
747
  });
748
  }
@@ -753,7 +791,7 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
753
 
754
  return true;
755
  },
756
- [sessionId, chat, updateSession, setNeedsAttention],
757
  );
758
 
759
  // -- Stop (interrupt backend agent loop, keep SSE open for events) --------
@@ -761,9 +799,9 @@ export function useAgentChat({ sessionId, isActive, onReady, onError, onSessionD
761
  // Don't call chat.stop() — keep the SSE stream open so the backend's
762
  // tool_state_change(cancelled) and interrupted events reach the frontend.
763
  // The stream closes naturally when the backend sends finish events.
764
- updateSession(sessionId, { isProcessing: false });
765
  apiFetch(`/api/interrupt/${sessionId}`, { method: 'POST' }).catch(() => {});
766
- }, [sessionId, updateSession]);
767
 
768
  // -- Edit message + regenerate from that point ----------------------------
769
  const editAndRegenerate = useCallback(async (messageId: string, newText: string) => {
 
24
  interface UseAgentChatOptions {
25
  sessionId: string;
26
  isActive: boolean;
27
+ /** Backend reports this session is mid-turn (from the GET /sessions list). */
28
+ isProcessing?: boolean;
29
  onReady?: () => void;
30
  onError?: (error: string) => void;
31
  onSessionDead?: (sessionId: string) => void;
32
  }
33
 
34
+ export function useAgentChat({ sessionId, isActive, isProcessing = false, onReady, onError, onSessionDead }: UseAgentChatOptions) {
35
  const callbacksRef = useRef({ onReady, onError, onSessionDead });
36
  callbacksRef.current = { onReady, onError, onSessionDead };
37
 
38
  const isActiveRef = useRef(isActive);
39
  isActiveRef.current = isActive;
40
 
41
+ // Only the active tab — or a session the backend says is mid-turn — gets a
42
+ // reactivating hydration (the /messages + /session fetch and the SDK's
43
+ // resume reconnect both call ensure_session_loaded, which spins a runtime +
44
+ // sandbox back up for any not-currently-live session). Gating on this keeps
45
+ // app load from reactivating every historical session and refilling the
46
+ // global active-session pool. A processing session is already live, so
47
+ // hydrating it returns the existing object without inflating the pool.
48
+ const shouldReactivate = isActive || isProcessing;
49
+ const shouldReactivateRef = useRef(shouldReactivate);
50
+ shouldReactivateRef.current = shouldReactivate;
51
+
52
  const { setNeedsAttention, updateSessionYolo } = useSessionStore();
53
 
54
  // Helper: update this session's state (mirrors to globals if active)
55
  const updateSession = useAgentStore.getState().updateSession;
56
+ const setProcessingState = useCallback(
57
+ (
58
+ next: boolean,
59
+ updates: Partial<import('@/store/agentStore').PerSessionState> = {},
60
+ ) => {
61
+ updateSession(sessionId, { ...updates, isProcessing: next });
62
+ useSessionStore.getState().setSessionProcessing(sessionId, next);
63
+ },
64
+ [sessionId, updateSession],
65
+ );
66
 
67
  // -- Build side-channel callbacks (stable ref) --------------------------
68
  const sideChannel = useMemo<SideChannelCallbacks>(
69
  () => ({
70
  onReady: () => {
71
+ // Mirror to the sidebar store too: agentStore drives the live UI, but
72
+ // SessionMeta.isProcessing (sessionStore) is what feeds shouldReactivate.
73
+ // Only mergeServerSessions sets it, so without clearing it here a
74
+ // finished background task keeps reactivating until the next list fetch.
75
+ setProcessingState(false);
76
  if (isActiveRef.current) {
77
  useAgentStore.getState().setConnected(true);
78
  }
 
80
  callbacksRef.current.onReady?.();
81
  },
82
  onShutdown: () => {
83
+ setProcessingState(false);
84
  if (isActiveRef.current) {
85
  useAgentStore.getState().setConnected(false);
86
  }
87
  },
88
  onError: (error: string) => {
89
+ setProcessingState(false);
90
  callbacksRef.current.onError?.(error);
91
  },
92
  onProcessing: () => {
93
+ setProcessingState(true, {
 
94
  activityStatus: { type: 'thinking' },
95
  });
96
  },
97
  onProcessingDone: () => {
98
+ setProcessingState(false);
99
  },
100
  onUndoComplete: () => {
101
+ setProcessingState(false);
102
  },
103
  onCompacted: (oldTokens: number, newTokens: number) => {
104
  logger.log(`Context compacted: ${oldTokens} -> ${newTokens} tokens`);
 
223
  );
224
  }
225
 
226
+ // Approval pauses the turn: the backend has returned and set
227
+ // is_processing=False, and no turn_complete/onProcessingDone fires on
228
+ // this path. Clear processing in both stores (sessionStore.isProcessing
229
+ // feeds shouldReactivate) so a backgrounded waiting-approval session
230
+ // doesn't stay "processing" until the next /sessions merge —
231
+ // activityStatus still surfaces the waiting-approval state.
232
+ setProcessingState(false, { activityStatus: { type: 'waiting-approval' } });
233
 
234
  // Build panel data for this session's pending approval
235
  const firstTool = tools[0];
 
339
  onInterrupted: () => { /* no-op — handled by stop() caller */ },
340
  }),
341
  // eslint-disable-next-line react-hooks/exhaustive-deps
342
+ [sessionId, setProcessingState],
343
  );
344
 
345
  // -- Create transport (one per session, stable for lifetime) ------------
 
381
  experimental_throttle: 80,
382
  // On mount, the SDK calls transport.reconnectToStream() which checks
383
  // is_processing and subscribes to the live event stream if the agent
384
+ // is mid-turn. Without this, page refresh kills live updates. Gated on
385
+ // shouldReactivate so an idle backgrounded session isn't reactivated on
386
+ // app load just to find there's nothing to resume.
387
+ resume: shouldReactivate,
388
  // After all approval responses are set, auto-send to continue the agent loop.
389
  // Without this, addToolApprovalResponse only updates the UI — it won't trigger
390
  // sendMessages on the transport.
391
  sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
392
  onError: (error) => {
393
+ setProcessingState(false);
 
 
 
 
 
 
 
 
394
  logger.error('useChat error:', error);
395
+ callbacksRef.current.onError?.(
396
+ error instanceof Error ? error.message : String(error),
397
+ );
398
  },
399
  });
400
 
 
403
  chatActionsRef.current.messages = chat.messages;
404
 
405
  // -- Hydrate from backend on mount (page refresh recovery) --------------
406
+ // Gated on shouldReactivate: an idle backgrounded session renders from its
407
+ // localStorage message cache + the sidebar list payload, with no per-session
408
+ // fetch that would reactivate its runtime/sandbox. Re-runs when a session
409
+ // becomes active (user selects it) or the list reports it processing, which
410
+ // is exactly when reactivation is wanted.
411
  useEffect(() => {
412
+ if (!shouldReactivate) return;
413
  let cancelled = false;
414
  (async () => {
415
  try {
 
464
  // atomic update so the UI never sees isProcessing=false with stale
465
  // tool states (which would coerce them to 'output-available').
466
  const savedResearch = loadResearch(sessionId);
467
+ setProcessingState(true, {
 
468
  activityStatus: savedResearch?.stats.startedAt
469
  ? { type: 'tool', toolName: 'research', description: 'Resuming research...' }
470
  : { type: 'thinking' },
 
474
  }),
475
  });
476
  } else if (pendingIds && pendingIds.size > 0) {
477
+ setProcessingState(false, { activityStatus: { type: 'waiting-approval' } });
478
  clearResearch(sessionId);
479
  } else {
480
+ setProcessingState(false);
481
  clearResearch(sessionId);
482
  }
483
  } catch {
 
485
  }
486
  })();
487
  return () => { cancelled = true; };
488
+ }, [sessionId, shouldReactivate, setProcessingState]); // eslint-disable-line react-hooks/exhaustive-deps
489
 
490
  // -- Re-hydrate + reconnect on wake from sleep ----------------------------
491
  // The Vercel AI SDK only calls reconnectToStream() on mount, NOT on
 
662
 
663
  const onVisible = async () => {
664
  if (document.visibilityState !== 'visible') return;
665
+ // Idle backgrounded sessions stay dormant on tab refocus too — otherwise
666
+ // every refocus would re-hydrate (and reactivate) every session, refilling
667
+ // the pool the reaper just drained. Only the active/processing session wakes.
668
+ if (!shouldReactivateRef.current) return;
669
 
670
  // Always re-hydrate messages on wake
671
  const result = await hydrateMessages();
 
680
 
681
  // If the backend is still processing, reconnect to the live event stream
682
  if (info?.is_processing) {
683
+ setProcessingState(true, { activityStatus: { type: 'thinking' } });
684
 
685
  // Stop any previous reconnection
686
  stopReconnect();
 
705
 
706
  // If backend stopped processing, clean up
707
  if (fresh.info && !fresh.info.is_processing) {
708
+ setProcessingState(false);
709
  stopReconnect();
710
  }
711
  }, 3000);
 
717
  document.removeEventListener('visibilitychange', onVisible);
718
  stopReconnect();
719
  };
720
+ }, [sessionId, setProcessingState]); // eslint-disable-line react-hooks/exhaustive-deps
721
 
722
  // -- Persist messages ---------------------------------------------------
723
  const prevLenRef = useRef(initialMessages.length);
 
752
  setMsgs(updated);
753
  saveMessages(sessionId, updated);
754
  }
755
+ setProcessingState(false);
756
  } catch (e) {
757
  logger.error('Undo failed:', e);
758
  }
759
+ }, [sessionId, setProcessingState]);
760
 
761
  // -- Approve tools ------------------------------------------------------
762
  const approveTools = useCallback(
 
780
  setNeedsAttention(sessionId, false);
781
  const hasApproved = approvals.some(a => a.approved);
782
  if (hasApproved) {
783
+ setProcessingState(true, {
 
784
  activityStatus: { type: 'thinking' },
785
  });
786
  }
 
791
 
792
  return true;
793
  },
794
+ [sessionId, chat, setProcessingState, setNeedsAttention],
795
  );
796
 
797
  // -- Stop (interrupt backend agent loop, keep SSE open for events) --------
 
799
  // Don't call chat.stop() — keep the SSE stream open so the backend's
800
  // tool_state_change(cancelled) and interrupted events reach the frontend.
801
  // The stream closes naturally when the backend sends finish events.
802
+ setProcessingState(false);
803
  apiFetch(`/api/interrupt/${sessionId}`, { method: 'POST' }).catch(() => {});
804
+ }, [sessionId, setProcessingState]);
805
 
806
  // -- Edit message + regenerate from that point ----------------------------
807
  const editAndRegenerate = useCallback(async (messageId: string, newText: string) => {
frontend/src/hooks/useUserQuota.ts CHANGED
@@ -18,13 +18,13 @@ export interface UserQuota {
18
  premiumRemaining: number;
19
  }
20
 
21
- export function useUserQuota() {
22
  const user = useAgentStore((s) => s.user);
23
  const [quota, setQuota] = useState<UserQuota | null>(null);
24
  const [loading, setLoading] = useState(false);
25
 
26
  const refresh = useCallback(async () => {
27
- if (!user?.authenticated) return;
28
  setLoading(true);
29
  try {
30
  const res = await apiFetch('/api/user/quota');
@@ -41,7 +41,7 @@ export function useUserQuota() {
41
  } finally {
42
  setLoading(false);
43
  }
44
- }, [user?.authenticated]);
45
 
46
  useEffect(() => {
47
  refresh();
 
18
  premiumRemaining: number;
19
  }
20
 
21
+ export function useUserQuota({ enabled = true }: { enabled?: boolean } = {}) {
22
  const user = useAgentStore((s) => s.user);
23
  const [quota, setQuota] = useState<UserQuota | null>(null);
24
  const [loading, setLoading] = useState(false);
25
 
26
  const refresh = useCallback(async () => {
27
+ if (!enabled || !user?.authenticated) return;
28
  setLoading(true);
29
  try {
30
  const res = await apiFetch('/api/user/quota');
 
41
  } finally {
42
  setLoading(false);
43
  }
44
+ }, [enabled, user?.authenticated]);
45
 
46
  useEffect(() => {
47
  refresh();
frontend/src/lib/sse-chat-transport.ts CHANGED
@@ -54,6 +54,21 @@ function lastEventKey(sessionId: string): string {
54
  return `hf-agent-last-event:${sessionId}`;
55
  }
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  /** Parse an SSE text stream into AgentEvent objects. */
58
  function createSSEParserStream(sessionId: string): TransformStream<string, AgentEvent> {
59
  let buffer = '';
@@ -413,15 +428,9 @@ export class SSEChatTransport implements ChatTransport<UIMessage> {
413
  // it can flag the session for the catch-up banner.
414
  this.sideChannel.onSessionDead(sessionId);
415
  }
416
- if (response.status === 429) {
417
- // Premium-model daily quota gate tripped. The prefix is the detection marker
418
- // for useAgentChat's onError handler, which surfaces the cap dialog
419
- // instead of a generic error banner.
420
- throw new Error('CLAUDE_QUOTA_EXHAUSTED');
421
- }
422
  if (!response.ok) {
423
- const errorText = await response.text().catch(() => 'Request failed');
424
- throw new Error(`Chat request failed: ${response.status} ${errorText}`);
425
  }
426
 
427
  if (!response.body) {
 
54
  return `hf-agent-last-event:${sessionId}`;
55
  }
56
 
57
+ async function readErrorResponse(response: Response): Promise<string> {
58
+ const raw = await response.text().catch(() => '');
59
+ if (!raw) return response.statusText || 'Request failed';
60
+ try {
61
+ const data = JSON.parse(raw);
62
+ const detail = data?.detail;
63
+ if (typeof detail === 'string') return detail;
64
+ if (detail?.message && typeof detail.message === 'string') return detail.message;
65
+ if (detail?.error && typeof detail.error === 'string') return detail.error;
66
+ return JSON.stringify(detail ?? data);
67
+ } catch {
68
+ return raw;
69
+ }
70
+ }
71
+
72
  /** Parse an SSE text stream into AgentEvent objects. */
73
  function createSSEParserStream(sessionId: string): TransformStream<string, AgentEvent> {
74
  let buffer = '';
 
428
  // it can flag the session for the catch-up banner.
429
  this.sideChannel.onSessionDead(sessionId);
430
  }
 
 
 
 
 
 
431
  if (!response.ok) {
432
+ const errorText = await readErrorResponse(response);
433
+ throw new Error(`Chat request failed (${response.status}): ${errorText}`);
434
  }
435
 
436
  if (!response.body) {
frontend/src/store/agentStore.ts CHANGED
@@ -118,8 +118,6 @@ interface AgentStore {
118
  activityStatus: ActivityStatus;
119
  user: User | null;
120
  llmHealthError: LLMHealthError | null;
121
- /** Set when a premium-model send hits the daily quota; ChatInput opens the cap dialog. */
122
- claudeQuotaExhausted: boolean;
123
  jobsUpgradeRequired: JobsUpgradeState | null;
124
 
125
  // Right panel (single-artifact pattern)
@@ -173,7 +171,6 @@ interface AgentStore {
173
  setActivityStatus: (status: ActivityStatus) => void;
174
  setUser: (user: User | null) => void;
175
  setLlmHealthError: (error: LLMHealthError | null) => void;
176
- setClaudeQuotaExhausted: (exhausted: boolean) => void;
177
  setJobsUpgradeRequired: (state: JobsUpgradeState | null) => void;
178
 
179
  setPanel: (data: PanelData, view?: PanelView, editable?: boolean) => void;
@@ -294,7 +291,6 @@ export const useAgentStore = create<AgentStore>()((set, get) => ({
294
  activityStatus: { type: 'idle' },
295
  user: null,
296
  llmHealthError: null,
297
- claudeQuotaExhausted: false,
298
  jobsUpgradeRequired: null,
299
 
300
  panelData: null,
@@ -407,7 +403,6 @@ export const useAgentStore = create<AgentStore>()((set, get) => ({
407
  setActivityStatus: (status) => set({ activityStatus: status }),
408
  setUser: (user) => set({ user }),
409
  setLlmHealthError: (error) => set({ llmHealthError: error }),
410
- setClaudeQuotaExhausted: (exhausted) => set({ claudeQuotaExhausted: exhausted }),
411
  setJobsUpgradeRequired: (state) => set({ jobsUpgradeRequired: state }),
412
 
413
  // ── Panel (single-artifact) ───────────────────────────────────────
 
118
  activityStatus: ActivityStatus;
119
  user: User | null;
120
  llmHealthError: LLMHealthError | null;
 
 
121
  jobsUpgradeRequired: JobsUpgradeState | null;
122
 
123
  // Right panel (single-artifact pattern)
 
171
  setActivityStatus: (status: ActivityStatus) => void;
172
  setUser: (user: User | null) => void;
173
  setLlmHealthError: (error: LLMHealthError | null) => void;
 
174
  setJobsUpgradeRequired: (state: JobsUpgradeState | null) => void;
175
 
176
  setPanel: (data: PanelData, view?: PanelView, editable?: boolean) => void;
 
291
  activityStatus: { type: 'idle' },
292
  user: null,
293
  llmHealthError: null,
 
294
  jobsUpgradeRequired: null,
295
 
296
  panelData: null,
 
403
  setActivityStatus: (status) => set({ activityStatus: status }),
404
  setUser: (user) => set({ user }),
405
  setLlmHealthError: (error) => set({ llmHealthError: error }),
 
406
  setJobsUpgradeRequired: (state) => set({ jobsUpgradeRequired: state }),
407
 
408
  // ── Panel (single-artifact) ───────────────────────────────────────
frontend/src/store/sessionStore.ts CHANGED
@@ -13,6 +13,10 @@ interface SessionStore {
13
  deleteSession: (id: string) => void;
14
  switchSession: (id: string) => void;
15
  setSessionActive: (id: string, isActive: boolean) => void;
 
 
 
 
16
  updateSessionTitle: (id: string, title: string) => void;
17
  updateSessionModel: (id: string, model: string | null) => void;
18
  setNeedsAttention: (id: string, needs: boolean) => void;
@@ -27,7 +31,10 @@ interface SessionStore {
27
  title?: string | null;
28
  created_at: string;
29
  is_active?: boolean;
 
30
  model?: string | null;
 
 
31
  pending_approval?: unknown[] | null;
32
  auto_approval?: {
33
  enabled?: boolean;
@@ -66,6 +73,8 @@ export const useSessionStore = create<SessionStore>()(
66
  autoApprovalCostCapUsd: null,
67
  autoApprovalEstimatedSpendUsd: 0,
68
  autoApprovalRemainingUsd: null,
 
 
69
  };
70
  set((state) => ({
71
  sessions: [...state.sessions, newSession],
@@ -117,7 +126,10 @@ export const useSessionStore = create<SessionStore>()(
117
  ...existing,
118
  title: server.title || existing.title,
119
  isActive: server.is_active ?? existing.isActive,
 
120
  model: server.model ?? existing.model ?? null,
 
 
121
  needsAttention: Boolean(server.pending_approval?.length) || existing.needsAttention,
122
  expired: false,
123
  ...(auto
@@ -139,6 +151,7 @@ export const useSessionStore = create<SessionStore>()(
139
  title: server.title || `Chat ${merged.length + 1}`,
140
  createdAt: server.created_at || new Date().toISOString(),
141
  isActive: server.is_active ?? true,
 
142
  needsAttention: Boolean(server.pending_approval?.length),
143
  model: server.model ?? null,
144
  expired: false,
@@ -146,6 +159,8 @@ export const useSessionStore = create<SessionStore>()(
146
  autoApprovalCostCapUsd: server.auto_approval?.cost_cap_usd ?? null,
147
  autoApprovalEstimatedSpendUsd: server.auto_approval?.estimated_spend_usd ?? 0,
148
  autoApprovalRemainingUsd: server.auto_approval?.remaining_usd ?? null,
 
 
149
  };
150
  merged.push(newSession);
151
  byId.set(id, newSession);
@@ -202,6 +217,14 @@ export const useSessionStore = create<SessionStore>()(
202
  }));
203
  },
204
 
 
 
 
 
 
 
 
 
205
  updateSessionTitle: (id: string, title: string) => {
206
  set((state) => ({
207
  sessions: state.sessions.map((s) =>
@@ -229,7 +252,10 @@ export const useSessionStore = create<SessionStore>()(
229
  {
230
  name: 'hf-agent-sessions',
231
  partialize: (state) => ({
232
- sessions: state.sessions,
 
 
 
233
  activeSessionId: state.activeSessionId,
234
  }),
235
  }
 
13
  deleteSession: (id: string) => void;
14
  switchSession: (id: string) => void;
15
  setSessionActive: (id: string, isActive: boolean) => void;
16
+ /** Track whether this session is mid-turn. Set false on terminal events so a
17
+ * finished background task stops looking "processing" (which would otherwise
18
+ * keep reactivating it until the next GET /sessions merge). */
19
+ setSessionProcessing: (id: string, isProcessing: boolean) => void;
20
  updateSessionTitle: (id: string, title: string) => void;
21
  updateSessionModel: (id: string, model: string | null) => void;
22
  setNeedsAttention: (id: string, needs: boolean) => void;
 
31
  title?: string | null;
32
  created_at: string;
33
  is_active?: boolean;
34
+ is_processing?: boolean;
35
  model?: string | null;
36
+ premium_user_billed?: boolean;
37
+ premium_quota_counted?: boolean;
38
  pending_approval?: unknown[] | null;
39
  auto_approval?: {
40
  enabled?: boolean;
 
73
  autoApprovalCostCapUsd: null,
74
  autoApprovalEstimatedSpendUsd: 0,
75
  autoApprovalRemainingUsd: null,
76
+ premiumUserBilled: false,
77
+ premiumQuotaCounted: false,
78
  };
79
  set((state) => ({
80
  sessions: [...state.sessions, newSession],
 
126
  ...existing,
127
  title: server.title || existing.title,
128
  isActive: server.is_active ?? existing.isActive,
129
+ isProcessing: Boolean(server.is_processing),
130
  model: server.model ?? existing.model ?? null,
131
+ premiumUserBilled: Boolean(server.premium_user_billed),
132
+ premiumQuotaCounted: Boolean(server.premium_quota_counted),
133
  needsAttention: Boolean(server.pending_approval?.length) || existing.needsAttention,
134
  expired: false,
135
  ...(auto
 
151
  title: server.title || `Chat ${merged.length + 1}`,
152
  createdAt: server.created_at || new Date().toISOString(),
153
  isActive: server.is_active ?? true,
154
+ isProcessing: Boolean(server.is_processing),
155
  needsAttention: Boolean(server.pending_approval?.length),
156
  model: server.model ?? null,
157
  expired: false,
 
159
  autoApprovalCostCapUsd: server.auto_approval?.cost_cap_usd ?? null,
160
  autoApprovalEstimatedSpendUsd: server.auto_approval?.estimated_spend_usd ?? 0,
161
  autoApprovalRemainingUsd: server.auto_approval?.remaining_usd ?? null,
162
+ premiumUserBilled: Boolean(server.premium_user_billed),
163
+ premiumQuotaCounted: Boolean(server.premium_quota_counted),
164
  };
165
  merged.push(newSession);
166
  byId.set(id, newSession);
 
217
  }));
218
  },
219
 
220
+ setSessionProcessing: (id: string, isProcessing: boolean) => {
221
+ set((state) => ({
222
+ sessions: state.sessions.map((s) =>
223
+ s.id === id ? { ...s, isProcessing } : s
224
+ ),
225
+ }));
226
+ },
227
+
228
  updateSessionTitle: (id: string, title: string) => {
229
  set((state) => ({
230
  sessions: state.sessions.map((s) =>
 
252
  {
253
  name: 'hf-agent-sessions',
254
  partialize: (state) => ({
255
+ // Reset the transient isProcessing flag so a stale `true` from a
256
+ // previous session can't trigger a reactivating hydration on the next
257
+ // cold load — it's always re-derived from the live GET /sessions list.
258
+ sessions: state.sessions.map((s) => ({ ...s, isProcessing: false })),
259
  activeSessionId: state.activeSessionId,
260
  }),
261
  }
frontend/src/types/agent.ts CHANGED
@@ -17,6 +17,13 @@ export interface SessionMeta {
17
  isActive: boolean;
18
  needsAttention: boolean;
19
  model?: string | null;
 
 
 
 
 
 
 
20
  /** True when the backend no longer recognizes this session id (e.g.
21
  * after a backend restart). The UI shows a recovery banner and
22
  * disables input until the user chooses to restore-with-summary or
@@ -26,6 +33,8 @@ export interface SessionMeta {
26
  autoApprovalCostCapUsd?: number | null;
27
  autoApprovalEstimatedSpendUsd?: number;
28
  autoApprovalRemainingUsd?: number | null;
 
 
29
  }
30
 
31
  export interface ToolApproval {
 
17
  isActive: boolean;
18
  needsAttention: boolean;
19
  model?: string | null;
20
+ /** True when the backend reports this session is mid-turn (from
21
+ * GET /sessions). A processing session is already live in memory, so it
22
+ * keeps streaming in the background; idle sessions are NOT hydrated on app
23
+ * load, which is what keeps them from refilling the active-session pool.
24
+ * Transient — never persisted to localStorage (always re-derived from the
25
+ * server list). */
26
+ isProcessing?: boolean;
27
  /** True when the backend no longer recognizes this session id (e.g.
28
  * after a backend restart). The UI shows a recovery banner and
29
  * disables input until the user chooses to restore-with-summary or
 
33
  autoApprovalCostCapUsd?: number | null;
34
  autoApprovalEstimatedSpendUsd?: number;
35
  autoApprovalRemainingUsd?: number | null;
36
+ premiumUserBilled?: boolean;
37
+ premiumQuotaCounted?: boolean;
38
  }
39
 
40
  export interface ToolApproval {
frontend/src/utils/model.ts CHANGED
@@ -1,19 +1,24 @@
1
  /**
2
- * Shared model-id constants used by session-create call sites and the
3
- * premium-model cap dialog "Use a free model" escape hatch.
4
  *
5
  * Keep in sync with MODEL_OPTIONS in components/Chat/ChatInput.tsx and
6
  * AVAILABLE_MODELS in backend/routes/agent.py.
7
  */
8
 
9
- export const CLAUDE_MODEL_PATH = 'bedrock/us.anthropic.claude-opus-4-6-v1';
10
- export const GPT_55_MODEL_PATH = 'openai/gpt-5.5';
11
- export const FIRST_FREE_MODEL_PATH = 'moonshotai/Kimi-K2.6';
 
 
 
 
 
12
 
13
  export function isClaudePath(modelPath: string | undefined): boolean {
14
  return !!modelPath && modelPath.includes('anthropic');
15
  }
16
 
17
  export function isPremiumPath(modelPath: string | undefined): boolean {
18
- return modelPath === CLAUDE_MODEL_PATH || modelPath === GPT_55_MODEL_PATH;
19
  }
 
1
  /**
2
+ * Shared model-id constants used by session-create call sites and the model
3
+ * picker.
4
  *
5
  * Keep in sync with MODEL_OPTIONS in components/Chat/ChatInput.tsx and
6
  * AVAILABLE_MODELS in backend/routes/agent.py.
7
  */
8
 
9
+ export const CLAUDE_OPUS_48_MODEL_PATH = 'anthropic/claude-opus-4.8:fal-ai';
10
+ export const CLAUDE_MODEL_PATH = CLAUDE_OPUS_48_MODEL_PATH;
11
+ export const GPT_55_MODEL_PATH = 'openai/gpt-5.5:fal-ai';
12
+
13
+ const PREMIUM_MODEL_PATHS = new Set([
14
+ CLAUDE_OPUS_48_MODEL_PATH,
15
+ GPT_55_MODEL_PATH,
16
+ ]);
17
 
18
  export function isClaudePath(modelPath: string | undefined): boolean {
19
  return !!modelPath && modelPath.includes('anthropic');
20
  }
21
 
22
  export function isPremiumPath(modelPath: string | undefined): boolean {
23
+ return !!modelPath && PREMIUM_MODEL_PATHS.has(modelPath);
24
  }
scripts/build_kpis.py CHANGED
@@ -58,7 +58,7 @@ re-running the same hour overwrites.
58
  tool_calls_per_turn_p50 / _p95 — calls / turns, among sessions with turns>0
59
  tool_calls_by_name_json — JSON {tool: total_calls} (all tools seen)
60
  sessions_using_tool_json — JSON {tool: distinct_sessions_using}
61
- sessions_by_model_json — JSON {model_name: count} (CLI vs Bedrock split)
62
 
63
  ================================================================================
64
  Usage
@@ -560,7 +560,7 @@ def _aggregate(per_session: list[dict]) -> dict:
560
  "sessions_using_tool_json": json.dumps(
561
  dict(sessions_using_tool), sort_keys=True
562
  ),
563
- # Surface split — answers "is research dropping on Bedrock specifically?".
564
  "sessions_by_model_json": json.dumps(dict(sessions_by_model), sort_keys=True),
565
  }
566
 
 
58
  tool_calls_per_turn_p50 / _p95 — calls / turns, among sessions with turns>0
59
  tool_calls_by_name_json — JSON {tool: total_calls} (all tools seen)
60
  sessions_using_tool_json — JSON {tool: distinct_sessions_using}
61
+ sessions_by_model_json — JSON {model_name: count} (router/local split)
62
 
63
  ================================================================================
64
  Usage
 
560
  "sessions_using_tool_json": json.dumps(
561
  dict(sessions_using_tool), sort_keys=True
562
  ),
563
+ # Surface split by selected model for dashboard drilldowns.
564
  "sessions_by_model_json": json.dumps(dict(sessions_by_model), sort_keys=True),
565
  }
566
 
scripts/build_sft.py CHANGED
@@ -13,7 +13,7 @@ Output row schema::
13
 
14
  {
15
  "session_id": "...",
16
- "model": "claude-opus-4-6",
17
  "timestamp": "2026-04-24T...",
18
  "tags": ["tool:hf_jobs", "gpu:a100", "hf_job:succeeded", ...],
19
  "messages": [...], # OpenAI / TRL SFTTrainer format
 
13
 
14
  {
15
  "session_id": "...",
16
+ "model": "anthropic/claude-opus-4.8:fal-ai",
17
  "timestamp": "2026-04-24T...",
18
  "tags": ["tool:hf_jobs", "gpu:a100", "hf_job:succeeded", ...],
19
  "messages": [...], # OpenAI / TRL SFTTrainer format
scripts/prioritize_backlog.py CHANGED
@@ -7,7 +7,7 @@ likely product impact.
7
 
8
  Usage:
9
  uv run python scripts/prioritize_backlog.py
10
- uv run python scripts/prioritize_backlog.py --model openai/gpt-5.5
11
 
12
  Outputs:
13
  scratch/backlog-prioritization/<timestamp>/sources.json
@@ -1277,7 +1277,7 @@ def _response_content(response: Any) -> str:
1277
 
1278
 
1279
  def _temperature_for_params(llm_params: dict[str, Any]) -> float:
1280
- # Anthropic requires temperature=1 when adaptive/extended thinking is active.
1281
  if llm_params.get("thinking") or llm_params.get("output_config"):
1282
  return 1.0
1283
  return 0.2
 
7
 
8
  Usage:
9
  uv run python scripts/prioritize_backlog.py
10
+ uv run python scripts/prioritize_backlog.py --model openai/gpt-5.5:fal-ai
11
 
12
  Outputs:
13
  scratch/backlog-prioritization/<timestamp>/sources.json
 
1277
 
1278
 
1279
  def _temperature_for_params(llm_params: dict[str, Any]) -> float:
1280
+ # Some providers require temperature=1 when adaptive reasoning is active.
1281
  if llm_params.get("thinking") or llm_params.get("output_config"):
1282
  return 1.0
1283
  return 0.2
tests/integration/test_live_thinking_models.py CHANGED
@@ -1,9 +1,8 @@
1
- """Opt-in live provider checks for thinking metadata replay.
2
 
3
  These tests intentionally call paid model APIs and are skipped unless
4
- ``ML_INTERN_LIVE_LLM_TESTS=1`` plus the relevant provider key are set.
5
- They cover the concrete model families involved in #87 without making
6
- default CI depend on external credentials or provider availability.
7
  """
8
 
9
  from __future__ import annotations
@@ -21,14 +20,13 @@ from agent.core.agent_loop import (
21
  _call_llm_streaming,
22
  )
23
  from agent.core.llm_params import _resolve_llm_params
 
24
 
25
 
26
  if env_file := os.environ.get("ML_INTERN_LIVE_ENV_FILE"):
27
  load_dotenv(Path(env_file))
28
 
29
  LIVE_TESTS_ENABLED = os.environ.get("ML_INTERN_LIVE_LLM_TESTS") == "1"
30
- OPUS_47_MODEL = "anthropic/claude-opus-4-7"
31
- LATEST_GPT_MODEL = "openai/gpt-5.2"
32
  REPORT_RESULT_TOOL = [
33
  {
34
  "type": "function",
@@ -55,9 +53,9 @@ def _skip_without_live_flag() -> None:
55
  pytest.skip("set ML_INTERN_LIVE_LLM_TESTS=1 to run paid live LLM tests")
56
 
57
 
58
- def _skip_without_env(name: str) -> None:
59
- if not os.environ.get(name):
60
- pytest.skip(f"set {name} to run this live provider test")
61
 
62
 
63
  def _session(model_name: str):
@@ -75,54 +73,16 @@ def _session(model_name: str):
75
 
76
 
77
  @pytest.mark.asyncio
78
- async def test_live_opus_47_preserves_thinking_metadata_for_replay():
79
  _skip_without_live_flag()
80
- _skip_without_env("ANTHROPIC_API_KEY")
81
 
82
- session = _session(OPUS_47_MODEL)
83
  llm_params = _resolve_llm_params(
84
- OPUS_47_MODEL,
85
- reasoning_effort="high",
86
- )
87
-
88
- result = await _call_llm_streaming(
89
- session,
90
- messages=[
91
- Message(
92
- role="user",
93
- content=(
94
- "Use careful reasoning for this small check. "
95
- "If 17 * 19 = 323, call report_result with answer OPUS_OK."
96
- ),
97
- )
98
- ],
99
- tools=REPORT_RESULT_TOOL,
100
- llm_params=llm_params,
101
- )
102
-
103
- replay = _assistant_message_from_result(
104
- result,
105
- model_name=OPUS_47_MODEL,
106
- )
107
-
108
- assert result.content or result.tool_calls_acc
109
- assert result.thinking_blocks, (
110
- "Opus returned no thinking_blocks with reasoning_effort='high' - "
111
- "check that adaptive thinking params are being forwarded correctly"
112
- )
113
- assert getattr(replay, "thinking_blocks", None) == result.thinking_blocks
114
- assert getattr(replay, "reasoning_content", None) == result.reasoning_content
115
-
116
-
117
- @pytest.mark.asyncio
118
- async def test_live_latest_gpt_does_not_replay_reasoning_metadata():
119
- _skip_without_live_flag()
120
- _skip_without_env("OPENAI_API_KEY")
121
-
122
- session = _session(LATEST_GPT_MODEL)
123
- llm_params = _resolve_llm_params(
124
- LATEST_GPT_MODEL,
125
  reasoning_effort="low",
 
126
  )
127
 
128
  result = await _call_llm_streaming(
@@ -130,21 +90,14 @@ async def test_live_latest_gpt_does_not_replay_reasoning_metadata():
130
  messages=[
131
  Message(
132
  role="user",
133
- content="Call report_result with answer GPT_OK.",
134
  )
135
  ],
136
  tools=REPORT_RESULT_TOOL,
137
  llm_params=llm_params,
138
  )
139
 
140
- # Even if a GPT-family response carries provider reasoning internally,
141
- # OpenAI-compatible history must not echo it back on the next tool turn.
142
- # Force the non-None strip path when the live model omits reasoning details.
143
- result.reasoning_content = result.reasoning_content or "synthetic-reasoning"
144
- replay = _assistant_message_from_result(
145
- result,
146
- model_name=LATEST_GPT_MODEL,
147
- )
148
 
149
  assert result.content or result.tool_calls_acc
150
  assert getattr(replay, "thinking_blocks", None) is None
 
1
+ """Opt-in live HF Router checks.
2
 
3
  These tests intentionally call paid model APIs and are skipped unless
4
+ ``ML_INTERN_LIVE_LLM_TESTS=1`` plus ``HF_TOKEN`` are set. They verify the
5
+ router-only request path without requiring provider-specific API keys.
 
6
  """
7
 
8
  from __future__ import annotations
 
20
  _call_llm_streaming,
21
  )
22
  from agent.core.llm_params import _resolve_llm_params
23
+ from agent.core.model_ids import DEFAULT_MODEL_ID
24
 
25
 
26
  if env_file := os.environ.get("ML_INTERN_LIVE_ENV_FILE"):
27
  load_dotenv(Path(env_file))
28
 
29
  LIVE_TESTS_ENABLED = os.environ.get("ML_INTERN_LIVE_LLM_TESTS") == "1"
 
 
30
  REPORT_RESULT_TOOL = [
31
  {
32
  "type": "function",
 
53
  pytest.skip("set ML_INTERN_LIVE_LLM_TESTS=1 to run paid live LLM tests")
54
 
55
 
56
+ def _skip_without_hf_token() -> None:
57
+ if not os.environ.get("HF_TOKEN"):
58
+ pytest.skip("set HF_TOKEN to run this live router test")
59
 
60
 
61
  def _session(model_name: str):
 
73
 
74
 
75
  @pytest.mark.asyncio
76
+ async def test_live_default_router_model_does_not_replay_reasoning_metadata():
77
  _skip_without_live_flag()
78
+ _skip_without_hf_token()
79
 
80
+ session = _session(DEFAULT_MODEL_ID)
81
  llm_params = _resolve_llm_params(
82
+ DEFAULT_MODEL_ID,
83
+ os.environ["HF_TOKEN"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  reasoning_effort="low",
85
+ bill_to_user=True,
86
  )
87
 
88
  result = await _call_llm_streaming(
 
90
  messages=[
91
  Message(
92
  role="user",
93
+ content="Call report_result with answer ROUTER_OK.",
94
  )
95
  ],
96
  tools=REPORT_RESULT_TOOL,
97
  llm_params=llm_params,
98
  )
99
 
100
+ replay = _assistant_message_from_result(result)
 
 
 
 
 
 
 
101
 
102
  assert result.content or result.tool_calls_acc
103
  assert getattr(replay, "thinking_blocks", None) is None
tests/unit/test_agent_model_gating.py CHANGED
@@ -1,6 +1,5 @@
1
  """Tests for premium model handling in backend/routes/agent.py."""
2
 
3
- import asyncio
4
  import sys
5
  from pathlib import Path
6
  from types import SimpleNamespace
@@ -22,34 +21,26 @@ def _reset_quota_store():
22
  agent.user_quotas._reset_for_tests()
23
 
24
 
25
- def test_premium_model_predicate_includes_bedrock_claude_and_gpt55_only():
26
- assert agent._is_premium_model("bedrock/us.anthropic.claude-opus-4-6-v1")
27
- assert agent._is_premium_model("openai/gpt-5.5")
28
- assert not agent._is_premium_model("anthropic/claude-opus-4-6")
29
- assert not agent._is_premium_model("moonshotai/Kimi-K2.6")
30
-
31
-
32
- @pytest.mark.asyncio
33
- async def test_default_premium_session_falls_back_to_free_model(monkeypatch):
34
- monkeypatch.setattr(
35
- agent.session_manager.config,
36
- "model_name",
37
- agent.DEFAULT_CLAUDE_MODEL_ID,
38
  )
39
 
40
- model = await agent._model_override_for_new_session(None, None)
41
 
42
- assert model == agent.DEFAULT_FREE_MODEL_ID
 
 
 
 
 
43
 
44
 
45
  @pytest.mark.asyncio
46
- async def test_default_free_session_keeps_config_default(monkeypatch):
47
- monkeypatch.setattr(
48
- agent.session_manager.config,
49
- "model_name",
50
- agent.DEFAULT_FREE_MODEL_ID,
51
- )
52
-
53
  model = await agent._model_override_for_new_session(None, None)
54
 
55
  assert model is None
@@ -59,10 +50,10 @@ async def test_default_free_session_keeps_config_default(monkeypatch):
59
  async def test_explicit_premium_session_allowed_for_authenticated_user():
60
  model = await agent._model_override_for_new_session(
61
  None,
62
- agent.DEFAULT_CLAUDE_MODEL_ID,
63
  )
64
 
65
- assert model == agent.DEFAULT_CLAUDE_MODEL_ID
66
 
67
 
68
  @pytest.mark.asyncio
@@ -88,17 +79,36 @@ async def test_switching_to_premium_model_is_allowed_for_authenticated_user(
88
 
89
  response = await agent.set_session_model(
90
  "s1",
91
- {"model": "openai/gpt-5.5"},
92
  request=None,
93
  user={"user_id": "u1", "plan": "free"},
94
  )
95
 
96
- assert response == {"session_id": "s1", "model": "openai/gpt-5.5"}
97
- assert updated == [("s1", "openai/gpt-5.5")]
98
 
99
 
100
  @pytest.mark.asyncio
101
- async def test_premium_quota_charges_gpt55(monkeypatch):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  persisted = []
103
 
104
  async def fake_persist_session_snapshot(agent_session):
@@ -110,12 +120,7 @@ async def test_premium_quota_charges_gpt55(monkeypatch):
110
  fake_persist_session_snapshot,
111
  )
112
 
113
- agent_session = SimpleNamespace(
114
- claude_counted=False,
115
- session=SimpleNamespace(
116
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
117
- ),
118
- )
119
 
120
  await agent._enforce_premium_model_quota(
121
  {"user_id": "u1", "plan": "free"},
@@ -123,12 +128,15 @@ async def test_premium_quota_charges_gpt55(monkeypatch):
123
  )
124
 
125
  assert agent_session.claude_counted is True
 
126
  assert persisted == [agent_session]
127
  assert await agent.user_quotas.get_claude_used_today("u1") == 1
128
 
129
 
130
  @pytest.mark.asyncio
131
- async def test_free_user_premium_quota_rejects_second_session(monkeypatch):
 
 
132
  async def fake_persist_session_snapshot(_agent_session):
133
  return None
134
 
@@ -138,32 +146,40 @@ async def test_free_user_premium_quota_rejects_second_session(monkeypatch):
138
  fake_persist_session_snapshot,
139
  )
140
 
141
- first_session = SimpleNamespace(
142
- claude_counted=False,
143
- session=SimpleNamespace(
144
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
145
- ),
146
- )
147
- second_session = SimpleNamespace(
148
- claude_counted=False,
149
- session=SimpleNamespace(
150
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
151
- ),
 
 
 
 
 
 
 
 
 
 
 
 
152
  )
153
 
 
 
154
  await agent._enforce_premium_model_quota(
155
- {"user_id": "free-user", "plan": "free"},
156
- first_session,
157
  )
158
- with pytest.raises(HTTPException) as exc_info:
159
- await agent._enforce_premium_model_quota(
160
- {"user_id": "free-user", "plan": "free"},
161
- second_session,
162
- )
163
 
164
- assert exc_info.value.status_code == 429
165
- assert exc_info.value.detail["error"] == "premium_model_daily_cap"
166
- assert exc_info.value.detail["plan"] == "free"
167
 
168
 
169
  @pytest.mark.asyncio
@@ -178,84 +194,79 @@ async def test_pro_user_uses_pro_premium_quota(monkeypatch):
178
  )
179
 
180
  for index in range(2):
181
- agent_session = SimpleNamespace(
182
- claude_counted=False,
183
- session=SimpleNamespace(
184
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
185
- ),
186
- )
187
  await agent._enforce_premium_model_quota(
188
  {"user_id": "pro-user", "plan": "pro"},
189
  agent_session,
190
  )
191
  assert agent_session.claude_counted is True
 
192
  assert await agent.user_quotas.get_claude_used_today("pro-user") == index + 1
193
 
194
 
195
  @pytest.mark.asyncio
196
- async def test_org_plan_uses_free_premium_quota(monkeypatch):
197
- async def fake_persist_session_snapshot(_agent_session):
198
  return None
199
 
200
- monkeypatch.setattr(
201
- agent.session_manager,
202
- "persist_session_snapshot",
203
- fake_persist_session_snapshot,
204
- )
205
-
206
- first_session = SimpleNamespace(
207
- claude_counted=False,
208
- session=SimpleNamespace(
209
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
210
- ),
211
- )
212
- second_session = SimpleNamespace(
213
- claude_counted=False,
214
- session=SimpleNamespace(
215
- config=SimpleNamespace(model_name="openai/gpt-5.5"),
216
- ),
217
- )
218
 
219
  await agent._enforce_premium_model_quota(
220
- {"user_id": "org-user", "plan": "org"},
221
- first_session,
222
  )
223
- with pytest.raises(HTTPException) as exc_info:
224
- await agent._enforce_premium_model_quota(
225
- {"user_id": "org-user", "plan": "org"},
226
- second_session,
227
- )
228
-
229
- assert exc_info.value.status_code == 429
230
- assert exc_info.value.detail["plan"] == "org"
231
- assert "Upgrade to HF Pro" in exc_info.value.detail["message"]
232
 
233
 
234
  @pytest.mark.asyncio
235
- async def test_premium_quota_skips_direct_anthropic(monkeypatch):
236
- async def fail_if_persisted(_agent_session):
237
- raise AssertionError("direct Anthropic should not consume premium quota")
238
 
239
- monkeypatch.setattr(
240
- agent.session_manager,
241
- "persist_session_snapshot",
242
- fail_if_persisted,
243
- )
244
 
245
- agent_session = SimpleNamespace(
246
- claude_counted=False,
247
- session=SimpleNamespace(
248
- config=SimpleNamespace(model_name="anthropic/claude-opus-4-6"),
249
- ),
250
- )
251
 
252
- await agent._enforce_premium_model_quota(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  {"user_id": "u1", "plan": "free"},
254
- agent_session,
255
  )
256
 
257
- assert agent_session.claude_counted is False
258
- assert await agent.user_quotas.get_claude_used_today("u1") == 0
 
 
 
 
 
259
 
260
 
261
  @pytest.mark.asyncio
@@ -320,60 +331,3 @@ async def test_set_session_yolo_calls_manager_with_cap_presence(monkeypatch):
320
  },
321
  )
322
  ]
323
-
324
-
325
- @pytest.mark.asyncio
326
- async def test_delete_session_access_check_skips_sandbox_preload(monkeypatch):
327
- ensure_calls = []
328
- delete_calls = []
329
-
330
- async def fake_ensure_session_loaded(session_id, user_id, **kwargs):
331
- ensure_calls.append((session_id, user_id, kwargs))
332
- return SimpleNamespace(user_id=user_id)
333
-
334
- async def fake_delete_session(session_id):
335
- delete_calls.append(session_id)
336
- return True
337
-
338
- monkeypatch.setattr(
339
- agent.session_manager,
340
- "ensure_session_loaded",
341
- fake_ensure_session_loaded,
342
- )
343
- monkeypatch.setattr(agent.session_manager, "delete_session", fake_delete_session)
344
-
345
- response = await agent.delete_session("s1", {"user_id": "u1"})
346
-
347
- assert response == {"status": "deleted", "session_id": "s1"}
348
- assert delete_calls == ["s1"]
349
- assert ensure_calls[0][2]["preload_sandbox"] is False
350
-
351
-
352
- @pytest.mark.asyncio
353
- async def test_teardown_session_access_check_skips_sandbox_preload(monkeypatch):
354
- ensure_calls = []
355
- teardown_calls = []
356
-
357
- async def fake_ensure_session_loaded(session_id, user_id, **kwargs):
358
- ensure_calls.append((session_id, user_id, kwargs))
359
- return SimpleNamespace(user_id=user_id)
360
-
361
- async def fake_teardown_sandbox(session_id):
362
- teardown_calls.append(session_id)
363
- return True
364
-
365
- monkeypatch.setattr(
366
- agent.session_manager,
367
- "ensure_session_loaded",
368
- fake_ensure_session_loaded,
369
- )
370
- monkeypatch.setattr(
371
- agent.session_manager, "teardown_sandbox", fake_teardown_sandbox
372
- )
373
-
374
- response = await agent.teardown_session_sandbox("s1", {"user_id": "u1"})
375
- await asyncio.sleep(0)
376
-
377
- assert response == {"status": "teardown_requested", "session_id": "s1"}
378
- assert teardown_calls == ["s1"]
379
- assert ensure_calls[0][2]["preload_sandbox"] is False
 
1
  """Tests for premium model handling in backend/routes/agent.py."""
2
 
 
3
  import sys
4
  from pathlib import Path
5
  from types import SimpleNamespace
 
21
  agent.user_quotas._reset_for_tests()
22
 
23
 
24
+ def _premium_session(model: str = agent.DEFAULT_PREMIUM_MODEL_ID):
25
+ return SimpleNamespace(
26
+ claude_counted=False,
27
+ session=SimpleNamespace(
28
+ config=SimpleNamespace(model_name=model),
29
+ premium_user_billed=False,
30
+ ),
 
 
 
 
 
 
31
  )
32
 
 
33
 
34
+ def test_premium_model_predicate_uses_router_ids_only():
35
+ assert agent._is_premium_model(agent.DEFAULT_PREMIUM_MODEL_ID)
36
+ assert agent._is_premium_model(agent.DEFAULT_GPT_MODEL_ID)
37
+ assert agent._is_user_billed(agent.DEFAULT_PREMIUM_MODEL_ID)
38
+ assert not agent._is_premium_model("moonshotai/Kimi-K2.6")
39
+ assert not agent._is_premium_model("unsupported/model")
40
 
41
 
42
  @pytest.mark.asyncio
43
+ async def test_default_session_uses_configured_default_model():
 
 
 
 
 
 
44
  model = await agent._model_override_for_new_session(None, None)
45
 
46
  assert model is None
 
50
  async def test_explicit_premium_session_allowed_for_authenticated_user():
51
  model = await agent._model_override_for_new_session(
52
  None,
53
+ agent.DEFAULT_PREMIUM_MODEL_ID,
54
  )
55
 
56
+ assert model == agent.DEFAULT_PREMIUM_MODEL_ID
57
 
58
 
59
  @pytest.mark.asyncio
 
79
 
80
  response = await agent.set_session_model(
81
  "s1",
82
+ {"model": agent.DEFAULT_GPT_MODEL_ID},
83
  request=None,
84
  user={"user_id": "u1", "plan": "free"},
85
  )
86
 
87
+ assert response == {"session_id": "s1", "model": agent.DEFAULT_GPT_MODEL_ID}
88
+ assert updated == [("s1", agent.DEFAULT_GPT_MODEL_ID)]
89
 
90
 
91
  @pytest.mark.asyncio
92
+ async def test_switching_to_unknown_model_id_is_rejected(monkeypatch):
93
+ async def fake_check_session_access(session_id, user, request=None):
94
+ return SimpleNamespace(user_id=user["user_id"])
95
+
96
+ monkeypatch.setattr(agent, "_check_session_access", fake_check_session_access)
97
+
98
+ with pytest.raises(HTTPException) as exc_info:
99
+ await agent.set_session_model(
100
+ "s1",
101
+ {"model": "unsupported/model"},
102
+ request=None,
103
+ user={"user_id": "u1", "plan": "free"},
104
+ )
105
+
106
+ assert exc_info.value.status_code == 400
107
+ assert "Unknown model" in exc_info.value.detail
108
+
109
+
110
+ @pytest.mark.asyncio
111
+ async def test_premium_quota_charges_without_user_billing_inside_allowance(monkeypatch):
112
  persisted = []
113
 
114
  async def fake_persist_session_snapshot(agent_session):
 
120
  fake_persist_session_snapshot,
121
  )
122
 
123
+ agent_session = _premium_session()
 
 
 
 
 
124
 
125
  await agent._enforce_premium_model_quota(
126
  {"user_id": "u1", "plan": "free"},
 
128
  )
129
 
130
  assert agent_session.claude_counted is True
131
+ assert agent_session.session.premium_user_billed is False
132
  assert persisted == [agent_session]
133
  assert await agent.user_quotas.get_claude_used_today("u1") == 1
134
 
135
 
136
  @pytest.mark.asyncio
137
+ async def test_free_user_gets_two_subsidized_premium_sessions_then_user_billing(
138
+ monkeypatch,
139
+ ):
140
  async def fake_persist_session_snapshot(_agent_session):
141
  return None
142
 
 
146
  fake_persist_session_snapshot,
147
  )
148
 
149
+ first = _premium_session()
150
+ await agent._enforce_premium_model_quota({"user_id": "g1", "plan": "free"}, first)
151
+ assert first.session.premium_user_billed is False
152
+
153
+ second = _premium_session()
154
+ await agent._enforce_premium_model_quota({"user_id": "g1", "plan": "free"}, second)
155
+ assert second.session.premium_user_billed is False
156
+
157
+ third = _premium_session()
158
+ await agent._enforce_premium_model_quota({"user_id": "g1", "plan": "free"}, third)
159
+ assert third.session.premium_user_billed is True
160
+ assert await agent.user_quotas.get_claude_used_today("g1") == 2
161
+
162
+
163
+ @pytest.mark.asyncio
164
+ async def test_free_model_does_not_consume_premium_quota(monkeypatch):
165
+ async def fail_if_persisted(_agent_session):
166
+ raise AssertionError("free model should not consume premium quota")
167
+
168
+ monkeypatch.setattr(
169
+ agent.session_manager,
170
+ "persist_session_snapshot",
171
+ fail_if_persisted,
172
  )
173
 
174
+ agent_session = _premium_session("moonshotai/Kimi-K2.6")
175
+
176
  await agent._enforce_premium_model_quota(
177
+ {"user_id": "u1", "plan": "free"},
178
+ agent_session,
179
  )
 
 
 
 
 
180
 
181
+ assert agent_session.claude_counted is False
182
+ assert await agent.user_quotas.get_claude_used_today("u1") == 0
 
183
 
184
 
185
  @pytest.mark.asyncio
 
194
  )
195
 
196
  for index in range(2):
197
+ agent_session = _premium_session()
 
 
 
 
 
198
  await agent._enforce_premium_model_quota(
199
  {"user_id": "pro-user", "plan": "pro"},
200
  agent_session,
201
  )
202
  assert agent_session.claude_counted is True
203
+ assert agent_session.session.premium_user_billed is False
204
  assert await agent.user_quotas.get_claude_used_today("pro-user") == index + 1
205
 
206
 
207
  @pytest.mark.asyncio
208
+ async def test_pro_user_billable_overflow_also_bills_user(monkeypatch):
209
+ async def fake_persist(_agent_session):
210
  return None
211
 
212
+ monkeypatch.setattr(agent.session_manager, "persist_session_snapshot", fake_persist)
213
+ monkeypatch.setattr(agent.user_quotas, "daily_cap_for", lambda plan: 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  await agent._enforce_premium_model_quota(
216
+ {"user_id": "p1", "plan": "pro"}, _premium_session()
 
217
  )
218
+ over = _premium_session()
219
+ await agent._enforce_premium_model_quota({"user_id": "p1", "plan": "pro"}, over)
220
+ assert over.session.premium_user_billed is True
 
 
 
 
 
 
221
 
222
 
223
  @pytest.mark.asyncio
224
+ async def test_restore_summary_enforces_premium_quota_before_seed(monkeypatch):
225
+ events = []
226
+ agent_session = _premium_session()
227
 
228
+ class Request:
229
+ headers = {}
230
+ cookies = {}
 
 
231
 
232
+ async def fake_create_session(**kwargs):
233
+ events.append(("create", kwargs["model"]))
234
+ return "s1"
 
 
 
235
 
236
+ async def fake_check_session_access(
237
+ session_id, user, request, preload_sandbox=True
238
+ ):
239
+ events.append(("check", session_id, preload_sandbox))
240
+ return agent_session
241
+
242
+ async def fake_enforce_quota(user, session):
243
+ assert user["user_id"] == "u1"
244
+ assert session is agent_session
245
+ session.session.premium_user_billed = True
246
+ events.append(("quota", session.session.config.model_name))
247
+
248
+ async def fake_seed(session_id, messages):
249
+ events.append(("seed", session_id, agent_session.session.premium_user_billed))
250
+ return len(messages)
251
+
252
+ monkeypatch.setattr(agent.session_manager, "create_session", fake_create_session)
253
+ monkeypatch.setattr(agent, "_check_session_access", fake_check_session_access)
254
+ monkeypatch.setattr(agent, "_enforce_premium_model_quota", fake_enforce_quota)
255
+ monkeypatch.setattr(agent.session_manager, "seed_from_summary", fake_seed)
256
+
257
+ response = await agent.restore_session_summary(
258
+ Request(),
259
+ {"messages": [{"role": "user", "content": "resume this"}]},
260
  {"user_id": "u1", "plan": "free"},
 
261
  )
262
 
263
+ assert response.session_id == "s1"
264
+ assert events == [
265
+ ("create", None),
266
+ ("check", "s1", False),
267
+ ("quota", agent.DEFAULT_PREMIUM_MODEL_ID),
268
+ ("seed", "s1", True),
269
+ ]
270
 
271
 
272
  @pytest.mark.asyncio
 
331
  },
332
  )
333
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/unit/test_build_kpis.py CHANGED
@@ -28,7 +28,7 @@ def _session(events, user_id="u1", start="2026-04-24T09:59:00"):
28
  "session_id": "sess-" + user_id,
29
  "session_start_time": start,
30
  "session_end_time": "2026-04-24T10:05:00",
31
- "model_name": "claude-opus-4-6",
32
  "messages": [{"role": "user", "content": "hi"}],
33
  "events": events,
34
  "user_id": user_id,
@@ -213,7 +213,7 @@ def test_per_tool_counts_in_session_metrics():
213
  assert m["_research_calls"] == 1
214
  assert m["_distinct_tools_used"] == 3
215
  assert m["_total_named_tool_calls"] == 4
216
- assert m["_model_name"] == "claude-opus-4-6"
217
 
218
 
219
  def test_aggregate_research_kpis_only_count_doer_sessions():
@@ -366,22 +366,22 @@ def test_aggregate_sessions_by_model_split():
366
  import json as _json
367
 
368
  mod = _load()
369
- s_anthropic = _session([], user_id="a")
370
- s_anthropic["model_name"] = "anthropic/claude-opus-4-6"
371
- s_bedrock = _session([], user_id="b")
372
- s_bedrock["model_name"] = "bedrock/us.anthropic.claude-opus-4-6-v1"
373
- s_bedrock2 = _session([], user_id="c")
374
- s_bedrock2["model_name"] = "bedrock/us.anthropic.claude-opus-4-6-v1"
375
  row = mod._aggregate(
376
  [
377
- mod._session_metrics(s_anthropic),
378
- mod._session_metrics(s_bedrock),
379
- mod._session_metrics(s_bedrock2),
380
  ]
381
  )
382
  assert _json.loads(row["sessions_by_model_json"]) == {
383
- "anthropic/claude-opus-4-6": 1,
384
- "bedrock/us.anthropic.claude-opus-4-6-v1": 2,
385
  }
386
 
387
 
 
28
  "session_id": "sess-" + user_id,
29
  "session_start_time": start,
30
  "session_end_time": "2026-04-24T10:05:00",
31
+ "model_name": "anthropic/claude-opus-4.8:fal-ai",
32
  "messages": [{"role": "user", "content": "hi"}],
33
  "events": events,
34
  "user_id": user_id,
 
213
  assert m["_research_calls"] == 1
214
  assert m["_distinct_tools_used"] == 3
215
  assert m["_total_named_tool_calls"] == 4
216
+ assert m["_model_name"] == "anthropic/claude-opus-4.8:fal-ai"
217
 
218
 
219
  def test_aggregate_research_kpis_only_count_doer_sessions():
 
366
  import json as _json
367
 
368
  mod = _load()
369
+ s_claude = _session([], user_id="a")
370
+ s_claude["model_name"] = "anthropic/claude-opus-4.8:fal-ai"
371
+ s_kimi = _session([], user_id="b")
372
+ s_kimi["model_name"] = "moonshotai/Kimi-K2.6"
373
+ s_kimi2 = _session([], user_id="c")
374
+ s_kimi2["model_name"] = "moonshotai/Kimi-K2.6"
375
  row = mod._aggregate(
376
  [
377
+ mod._session_metrics(s_claude),
378
+ mod._session_metrics(s_kimi),
379
+ mod._session_metrics(s_kimi2),
380
  ]
381
  )
382
  assert _json.loads(row["sessions_by_model_json"]) == {
383
+ "anthropic/claude-opus-4.8:fal-ai": 1,
384
+ "moonshotai/Kimi-K2.6": 2,
385
  }
386
 
387
 
tests/unit/test_build_sft.py CHANGED
@@ -19,7 +19,7 @@ def _session_row():
19
  "session_id": "abc",
20
  "session_start_time": "2026-04-24T10:00:00",
21
  "session_end_time": "2026-04-24T10:05:00",
22
- "model_name": "claude-opus-4-6",
23
  "messages": [
24
  {"role": "system", "content": "You are an agent"},
25
  {"role": "user", "content": "fine-tune llama"},
@@ -77,7 +77,7 @@ def test_reshape_preserves_messages_and_tools_and_adds_tags():
77
  mod = _load()
78
  row = mod._reshape_to_sft(_session_row())
79
  assert row["session_id"] == "abc"
80
- assert row["model"] == "claude-opus-4-6"
81
  assert row["timestamp"] == "2026-04-24T10:00:00"
82
  # Messages preserved verbatim, in order, with tool_calls + tool role rows.
83
  assert len(row["messages"]) == 5
 
19
  "session_id": "abc",
20
  "session_start_time": "2026-04-24T10:00:00",
21
  "session_end_time": "2026-04-24T10:05:00",
22
+ "model_name": "anthropic/claude-opus-4.8:fal-ai",
23
  "messages": [
24
  {"role": "system", "content": "You are an agent"},
25
  {"role": "user", "content": "fine-tune llama"},
 
77
  mod = _load()
78
  row = mod._reshape_to_sft(_session_row())
79
  assert row["session_id"] == "abc"
80
+ assert row["model"] == "anthropic/claude-opus-4.8:fal-ai"
81
  assert row["timestamp"] == "2026-04-24T10:00:00"
82
  # Messages preserved verbatim, in order, with tool_calls + tool role rows.
83
  assert len(row["messages"]) == 5
tests/unit/test_cli_local_models.py CHANGED
@@ -30,6 +30,19 @@ def test_openai_compat_prefix_is_not_supported():
30
  assert not model_switcher.is_valid_model_id("openai-compat/custom-model")
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def test_local_models_skip_hf_router_catalog_output():
34
  class NoPrintConsole:
35
  def print(self, *args, **kwargs):
@@ -52,8 +65,8 @@ async def test_probe_and_switch_local_model_uses_no_effort(monkeypatch):
52
  monkeypatch.setattr(model_switcher, "acompletion", fake_acompletion)
53
 
54
  class Config:
55
- model_name = "openai/gpt-5.5"
56
- reasoning_effort = "max"
57
 
58
  class Session:
59
  def __init__(self):
@@ -91,7 +104,7 @@ async def test_probe_and_switch_local_model_rejects_probe_errors(monkeypatch):
91
  monkeypatch.setattr(model_switcher, "acompletion", failing_acompletion)
92
 
93
  class Config:
94
- model_name = "openai/gpt-5.5"
95
  reasoning_effort = None
96
 
97
  class Session:
@@ -116,6 +129,6 @@ async def test_probe_and_switch_local_model_rejects_probe_errors(monkeypatch):
116
  hf_token=None,
117
  )
118
 
119
- assert config.model_name == "openai/gpt-5.5"
120
  assert session.model_id is None
121
  assert "ollama/llama3.1:8b" not in session.model_effective_effort
 
30
  assert not model_switcher.is_valid_model_id("openai-compat/custom-model")
31
 
32
 
33
+ def test_suggested_models_include_router_opus48_and_no_native_ids():
34
+ ids = {m["id"] for m in model_switcher.SUGGESTED_MODELS}
35
+
36
+ assert "anthropic/claude-opus-4.8:fal-ai" in ids
37
+ assert all(model_id.count("/") >= 1 for model_id in ids)
38
+
39
+
40
+ def test_model_switcher_accepts_router_model_ids():
41
+ assert model_switcher.is_valid_model_id("openai/gpt-5.5:fal-ai")
42
+ assert model_switcher.is_valid_model_id("openai/gpt-oss-120b")
43
+ assert model_switcher.is_valid_model_id("moonshotai/Kimi-K2.6")
44
+
45
+
46
  def test_local_models_skip_hf_router_catalog_output():
47
  class NoPrintConsole:
48
  def print(self, *args, **kwargs):
 
65
  monkeypatch.setattr(model_switcher, "acompletion", fake_acompletion)
66
 
67
  class Config:
68
+ model_name = "anthropic/claude-opus-4.8:fal-ai"
69
+ reasoning_effort = "high"
70
 
71
  class Session:
72
  def __init__(self):
 
104
  monkeypatch.setattr(model_switcher, "acompletion", failing_acompletion)
105
 
106
  class Config:
107
+ model_name = "anthropic/claude-opus-4.8:fal-ai"
108
  reasoning_effort = None
109
 
110
  class Session:
 
129
  hf_token=None,
130
  )
131
 
132
+ assert config.model_name == "anthropic/claude-opus-4.8:fal-ai"
133
  assert session.model_id is None
134
  assert "ollama/llama3.1:8b" not in session.model_effective_effort
tests/unit/test_cli_new_clear.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from types import SimpleNamespace
3
+
4
+ import pytest
5
+ from litellm import Message
6
+
7
+ import agent.main as main_mod
8
+ from agent.core.agent_loop import process_submission
9
+ from agent.core.session import Event, OpType, Session
10
+ from agent.tools import plan_tool
11
+
12
+
13
+ class _FakeConfig:
14
+ model_name = "openai/gpt-5.5:fal-ai"
15
+ save_sessions = False
16
+ session_dataset_repo = "fake/repo"
17
+ auto_save_interval = 1
18
+ heartbeat_interval_s = 60
19
+ max_iterations = 10
20
+ yolo_mode = False
21
+ confirm_cpu_jobs = False
22
+ auto_file_upload = False
23
+ reasoning_effort = None
24
+ share_traces = False
25
+ personal_trace_repo_template = None
26
+ mcpServers: dict = {}
27
+
28
+
29
+ class _FakeContext:
30
+ def __init__(self) -> None:
31
+ self.items = [
32
+ Message(role="system", content="system prompt"),
33
+ Message(role="user", content="old task"),
34
+ Message(role="assistant", content="old answer"),
35
+ ]
36
+ self.model_max_tokens = 200_000
37
+ self.running_context_usage = 123
38
+ self.on_message_added = None
39
+
40
+
41
+ def _make_session() -> Session:
42
+ return Session(
43
+ event_queue=asyncio.Queue(),
44
+ config=_FakeConfig(),
45
+ tool_router=None,
46
+ context_manager=_FakeContext(),
47
+ hf_token=None,
48
+ user_id="user-a",
49
+ local_mode=True,
50
+ )
51
+
52
+
53
+ def test_start_new_conversation_rotates_session_state(monkeypatch):
54
+ plan_tool.reset_current_plan()
55
+ session = _make_session()
56
+ session.config.save_sessions = True
57
+ session.turn_count = 2
58
+ session.last_auto_save_turn = 2
59
+ session.logged_events = [{"event_type": "turn_complete", "data": {}}]
60
+ session._local_save_path = "session_logs/old.json"
61
+ session.pending_approval = {"tool_calls": ["pending"]}
62
+ session.auto_approval_estimated_spend_usd = 1.25
63
+ session.current_plan = [
64
+ {"id": "old-session", "content": "old session item", "status": "pending"}
65
+ ]
66
+ monkeypatch.setattr(
67
+ plan_tool,
68
+ "_current_plan",
69
+ [{"id": "global", "content": "global item", "status": "in_progress"}],
70
+ )
71
+ old_session_id = session.session_id
72
+ uploads: list[str] = []
73
+
74
+ def fake_upload(repo_id: str) -> str:
75
+ uploads.append(repo_id)
76
+ return "session_logs/saved.json"
77
+
78
+ monkeypatch.setattr(session, "save_and_upload_detached", fake_upload)
79
+
80
+ result = session.start_new_conversation()
81
+
82
+ assert uploads == ["fake/repo"]
83
+ assert result["previous_session_id"] == old_session_id
84
+ assert result["previous_turn_count"] == 2
85
+ assert result["saved_path"] == "session_logs/saved.json"
86
+ assert session.session_id != old_session_id
87
+ assert [msg.role for msg in session.context_manager.items] == ["system"]
88
+ assert session.context_manager.items[0].content == "system prompt"
89
+ assert session.context_manager.running_context_usage == 0
90
+ assert session.turn_count == 0
91
+ assert session.last_auto_save_turn == 0
92
+ assert session.logged_events == []
93
+ assert session._local_save_path is None
94
+ assert session.pending_approval is None
95
+ assert session.auto_approval_estimated_spend_usd == 0.0
96
+ assert session.current_plan == []
97
+ assert plan_tool.get_current_plan() == []
98
+
99
+
100
+ @pytest.mark.asyncio
101
+ async def test_new_submission_resets_context_and_reports_clear_flag():
102
+ session = _make_session()
103
+ submission = SimpleNamespace(
104
+ operation=SimpleNamespace(
105
+ op_type=OpType.NEW,
106
+ data={"clear_screen": True},
107
+ )
108
+ )
109
+
110
+ should_continue = await process_submission(session, submission)
111
+
112
+ event: Event = await session.event_queue.get()
113
+ assert should_continue is True
114
+ assert event.event_type == "new_complete"
115
+ assert event.data["clear_screen"] is True
116
+ assert event.data["session_id"] == session.session_id
117
+ assert [msg.role for msg in session.context_manager.items] == ["system"]
118
+ assert [e["event_type"] for e in session.logged_events] == ["new_complete"]
119
+
120
+
121
+ @pytest.mark.asyncio
122
+ async def test_new_and_clear_slash_commands_share_operation_with_distinct_clear_flags():
123
+ submission_id = [0]
124
+ holder = [object()]
125
+
126
+ new_submission = await main_mod._handle_slash_command(
127
+ "/new",
128
+ _FakeConfig(),
129
+ holder,
130
+ asyncio.Queue(),
131
+ submission_id,
132
+ )
133
+ clear_submission = await main_mod._handle_slash_command(
134
+ "/clear",
135
+ _FakeConfig(),
136
+ holder,
137
+ asyncio.Queue(),
138
+ submission_id,
139
+ )
140
+
141
+ assert new_submission is not None
142
+ assert new_submission.operation.op_type == OpType.NEW
143
+ assert new_submission.operation.data == {"clear_screen": False}
144
+ assert clear_submission is not None
145
+ assert clear_submission.operation.op_type == OpType.NEW
146
+ assert clear_submission.operation.data == {"clear_screen": True}
147
+ assert submission_id == [2]