fix(copilot): make the step budget real, configurable and visible

The graph passed remaining_steps=20 (50 for injection) in the run inputs.
RemainingSteps is a LangGraph managed value derived from recursion_limit,
so the input was ignored and the real budget was 25 (100 for injection).
No one could see those numbers, and a turn that hit the cap ended on a
bare tool result with no explanation.

- Remove the dead remaining_steps input.
- Add resolve_recursion_limit(): max_steps from the LLM model config wins;
  otherwise 25 teaching_assistant, 50 lab_automation_assistant, 100
  troubleshooting_injection.
- When fewer than 4 steps remain after a tool run, route to the new
  budget_wrapup node. The next llm_call binds no tools and gets a wrap-up
  instruction, so the turn ends with a short summary of what was done and
  what remains.
- should_continue skips the title call after a wrap-up (no step left).
- budget_exhausted is reset at the start of every user turn.
- Add max_steps to the LLM config schemas. The config is a JSON column,
  so no migration is needed.
- Tests: 16 new tests cover the resolver, the routing, the tool-free
  wrap-up call and the removed input. Docs updated.
This commit is contained in:
YueGuobin committed 2026-11-03 23:16:26 +08:00
1 parent e8462507a5
commit 5a294461e8
6 files changed
+282 -37

No files matched your search

@@ -64,6 +64,7 @@ from gns3server.agent.gns3_copilot.agent.context_manager import (
create_pre_model_hook,
)
from gns3server.agent.gns3_copilot.agent.model_factory import (
create_base_model,
create_base_model_with_tools,
create_title_model,
)
@@ -187,6 +188,44 @@ TROUBLESHOOTING_INJECTION_MODE_TOOLS = [
# Default tools (legacy support - will be overridden by mode-specific tools)
tools = LAB_AUTOMATION_ASSISTANT_MODE_TOOLS
# LangGraph recursion limit per copilot mode: the hard cap on super-steps
# per user turn. One LLM call plus one tool execution costs two steps, so
# 25 is about 11 tool rounds; lab mode needs more room (topology build,
# configure, verify), fault injection more still.
MODE_RECURSION_LIMITS = {
"teaching_assistant": 25,
"lab_automation_assistant": 50,
"troubleshooting_injection": 100,
}
DEFAULT_RECURSION_LIMIT = 25
def resolve_recursion_limit(llm_config: dict | None) -> int:
"""Resolve the LangGraph recursion limit for one user turn.
``max_steps`` in the LLM model config wins when set to a positive
integer; otherwise the per-mode default applies. The value is a hard
safety cap, not a quality target: without it a tool loop or a routing
bug can spin forever and burn tokens.
"""
config = llm_config or {}
mode = (config.get("copilot_mode") or "teaching_assistant").lower()
default = MODE_RECURSION_LIMITS.get(mode, DEFAULT_RECURSION_LIMIT)
try:
configured = int(config.get("max_steps"))
except (TypeError, ValueError):
return default
return configured if configured > 0 else default
# Injected before the final LLM call when the step budget runs out. The
# model gets no tools in that call, so it can only produce text.
BUDGET_WRAPUP_INSTRUCTION = (
"The tool-step budget for this turn is exhausted. Do not call any tools. "
"Reply with a short summary: what you completed, what is still unfinished, "
"and what the user should do next (for example, ask to continue)."
)
# Create combined tool lookup for tool_node (supports all modes)
# tool_node will receive tool calls based on mode-specific tools bound to the
# model. The registry must cover every mode: a tool bound to the model but
@@ -260,7 +299,11 @@ class MessagesState(TypedDict):
operator.add
llm_calls: Counter for tracking the number of LLM invocations
remaining_steps: Is automatically managed by LangGraph's RemainingSteps
to track and limit recursion depth.
to track and limit recursion depth. The value comes from
the recursion_limit in the run config, not from inputs.
budget_exhausted: True once the step budget ran out and the graph
switched to the final summary call (no tools bound).
Reset to False at the start of every user turn.
conversation_title: Optional conversation title for session
identification and management
topology_info: Dictionary containing GNS3 project topology information
@@ -274,6 +317,9 @@ class MessagesState(TypedDict):
remaining_steps: RemainingSteps
# True when the step budget is used up and the turn ends with a summary
budget_exhausted: bool
# Optional conversation title
conversation_title: str | None
@@ -384,15 +430,26 @@ def llm_call(state: dict, config: RunnableConfig | None = None):
# token estimation
)
# Create fresh model with tools for each LLM call
logger.debug(
"Creating model with tools: provider=%s, model=%s, mode=%s, tools=%d",
llm_config.get("provider"),
llm_config.get("model"),
copilot_mode,
len(mode_tools),
)
model_with_tools = create_base_model_with_tools(mode_tools, llm_config=llm_config)
# When the step budget ran out, this is the final summary call: no tools
# are bound, so the model can only answer in text, and an instruction
# tells it to wrap up instead of planning more work.
budget_exhausted = bool(state.get("budget_exhausted"))
if budget_exhausted:
logger.warning(
"Step budget exhausted; making the final summary call without tools (provider=%s, model=%s)",
llm_config.get("provider"),
llm_config.get("model"),
)
model_to_call = create_base_model(llm_config)
else:
logger.debug(
"Creating model with tools: provider=%s, model=%s, mode=%s, tools=%d",
llm_config.get("provider"),
llm_config.get("model"),
copilot_mode,
len(mode_tools),
)
model_to_call = create_base_model_with_tools(mode_tools, llm_config=llm_config)
# Call pre_hook directly to prepare messages (topology injection +
# trimming)
@@ -403,8 +460,11 @@ def llm_call(state: dict, config: RunnableConfig | None = None):
prepared_messages = prepared_state["messages"]
logger.info("Messages prepared: %d → %d", len(messages), len(prepared_messages))
if budget_exhausted:
prepared_messages = [*prepared_messages, SystemMessage(content=BUDGET_WRAPUP_INSTRUCTION)]
# Invoke model with prepared messages
response = model_with_tools.invoke(prepared_messages)
response = model_to_call.invoke(prepared_messages)
# Add metadata with created_at timestamp to AI response
if hasattr(response, "metadata"):
@@ -640,6 +700,11 @@ def should_continue(
if last_message.tool_calls:
return "tool_node"
# Final summary after the step budget ran out: end the turn, skip the
# title call (it would need one more super-step we may not have)
if state.get("budget_exhausted"):
return END
# First full interaction completed and title not yet generated
if current_title in [None, "New Conversation"]:
return "title_generator_node"
@@ -648,23 +713,41 @@ def should_continue(
return END
def budget_wrapup(state: MessagesState) -> dict:
"""Switch the graph to the final summary call after the step budget ran out.
The next llm_call binds no tools and appends BUDGET_WRAPUP_INSTRUCTION,
so the turn ends with a text answer that tells the user where the work
stopped instead of ending silently on a tool result.
"""
logger.warning(
"Step budget nearly exhausted (remaining_steps=%s); switching to the summary call",
state.get("remaining_steps"),
)
return {"budget_exhausted": True, "session_id": state.get("session_id")}
# Routing logic after the tool node, Check remaining_steps
def recursion_limit_continue(state: MessagesState) -> Literal["llm_call", END]:
def recursion_limit_continue(state: MessagesState) -> Literal["llm_call", "budget_wrapup", END]:
"""
Routing logic after tool execution to prevent infinite recursion.
Determines whether to continue with another LLM call or end the
conversation based on remaining steps and message type.
Determines whether to continue with another LLM call, switch to the
final summary call, or end the conversation based on remaining steps and
message type.
Args:
state: Current conversation state with messages and remaining steps
Returns:
"llm_call" to continue processing, END to terminate conversation
"llm_call" to continue processing, "budget_wrapup" for the final
summary call, END to terminate the conversation.
Logic:
- If abort flag is set → end the conversation
- If the last message is ToolMessage and steps >= 4: continue to LLM
- If the last message is ToolMessage and steps < 4: wrap up with one
final summary call (no tools bound)
- Otherwise: end the conversation to prevent infinite loops
"""
# Check abort flag first
@@ -675,7 +758,7 @@ def recursion_limit_continue(state: MessagesState) -> Literal["llm_call", END]:
last_message = state["messages"][-1]
if isinstance(last_message, ToolMessage):
if state["remaining_steps"] < 4:
return END
return "budget_wrapup"
return "llm_call"
return END
@@ -688,6 +771,7 @@ agent_builder = StateGraph(MessagesState)
# Add nodes
agent_builder.add_node("llm_call", llm_call)
agent_builder.add_node("tool_node", tool_node)
agent_builder.add_node("budget_wrapup", budget_wrapup)
agent_builder.add_node("title_generator_node", generate_title)
agent_builder.add_node("abort_handler_node", abort_handler_node)
@@ -716,9 +800,11 @@ agent_builder.add_conditional_edges(
{
"llm_call": "llm_call", # Continue to LLM if tools executed and steps
# remain
"budget_wrapup": "budget_wrapup", # Final summary call, no tools
END: END, # End conversation to prevent infinite loops
},
)
agent_builder.add_edge("budget_wrapup", "llm_call")
agent_builder.add_edge("title_generator_node", END)
agent_builder.add_edge("abort_handler_node", END)
+13 -17
View File
@@ -43,7 +43,10 @@ import aiosqlite
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
from gns3server.agent.gns3_copilot.agent.gns3_copilot import agent_builder
from gns3server.agent.gns3_copilot.agent.gns3_copilot import (
agent_builder,
resolve_recursion_limit,
)
from gns3server.agent.gns3_copilot.chat_sessions_repository import (
ChatSessionsRepository,
)
@@ -282,13 +285,13 @@ class AgentService:
)
# Build config - only thread-safe identifiers
# Determine recursion_limit based on copilot_mode
# The recursion limit is the step budget of one user turn: the
# per-mode default, or max_steps from the LLM model config when set.
# RemainingSteps is a managed value derived from this limit; passing
# it in inputs has no effect.
copilot_mode = (llm_config or {}).get("copilot_mode", "teaching_assistant").lower()
if copilot_mode == "troubleshooting_injection":
recursion_limit = 100 # Need more recursion depth for fault injection workflow
log.debug("Using extended recursion_limit for troubleshooting_injection mode: 100")
else:
recursion_limit = 25 # Default recursion limit
recursion_limit = resolve_recursion_limit(llm_config)
log.debug("Using recursion_limit %d for copilot_mode %s", recursion_limit, copilot_mode)
config = {
"configurable": {
@@ -302,15 +305,8 @@ class AgentService:
"recursion_limit": recursion_limit,
}
# Build inputs
# Determine remaining_steps based on copilot_mode
# Troubleshooting injection mode needs more steps for multi-tool workflow
if copilot_mode == "troubleshooting_injection":
remaining_steps = 50 # Need more steps for fault injection workflow
log.debug("Using extended remaining_steps for troubleshooting_injection mode: 50")
else:
remaining_steps = 20 # Default for other modes
# Build inputs. budget_exhausted is reset per user turn: the
# checkpoint keeps it from a previous turn that ended on a summary.
inputs = {
"messages": [
HumanMessage(
@@ -320,7 +316,7 @@ class AgentService:
)
],
"llm_calls": 0,
"remaining_steps": remaining_steps,
"budget_exhausted": False,
"session_id": session_id,
"abort": False,
}
@@ -53,6 +53,16 @@ class LLMModelConfigData(BaseModel):
copilot_mode: Optional[str] = Field(
None, description="GNS3-Copilot mode: 'teaching_assistant' or 'lab_automation_assistant'"
)
max_steps: Optional[int] = Field(
None,
gt=0,
le=500,
description=(
"Max LangGraph super-steps per copilot turn "
"(1 LLM call + 1 tool run = 2 steps). Defaults: 25 teaching_assistant, "
"50 lab_automation_assistant, 100 troubleshooting_injection"
),
)
# Allow extra fields for extensibility
# Ensure all fields are included in serialization, even if None
@@ -82,6 +92,16 @@ class LLMModelConfigCreate(BaseModel):
copilot_mode: Optional[str] = Field(
None, description="GNS3-Copilot mode: 'teaching_assistant' or 'lab_automation_assistant'"
)
max_steps: Optional[int] = Field(
None,
gt=0,
le=500,
description=(
"Max LangGraph super-steps per copilot turn "
"(1 LLM call + 1 tool run = 2 steps). Defaults: 25 teaching_assistant, "
"50 lab_automation_assistant, 100 troubleshooting_injection"
),
)
# Allow extra config fields
model_config = ConfigDict(extra="allow")
@@ -112,6 +132,16 @@ class LLMModelConfigUpdate(BaseModel):
copilot_mode: Optional[str] = Field(
None, description="GNS3-Copilot mode: 'teaching_assistant' or 'lab_automation_assistant'"
)
max_steps: Optional[int] = Field(
None,
gt=0,
le=500,
description=(
"Max LangGraph super-steps per copilot turn "
"(1 LLM call + 1 tool run = 2 steps). Defaults: 25 teaching_assistant, "
"50 lab_automation_assistant, 100 troubleshooting_injection"
),
)
# Allow extra config fields
model_config = ConfigDict(extra="allow")
@@ -149,6 +179,16 @@ class LLMModelConfigDataWithoutSecret(BaseModel):
copilot_mode: Optional[str] = Field(
None, description="GNS3-Copilot mode: 'teaching_assistant' or 'lab_automation_assistant'"
)
max_steps: Optional[int] = Field(
None,
gt=0,
le=500,
description=(
"Max LangGraph super-steps per copilot turn "
"(1 LLM call + 1 tool run = 2 steps). Defaults: 25 teaching_assistant, "
"50 lab_automation_assistant, 100 troubleshooting_injection"
),
)
# Allow extra fields for extensibility
model_config = ConfigDict(extra="allow", populate_by_name=True)