Skip to content

Commit 42a132d

Browse files
committed
feat:改进检查机制以建议备选工具增加步骤可行性
1 parent 7f705db commit 42a132d

5 files changed

Lines changed: 65 additions & 22 deletions

File tree

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
is_job_submitted_step,
9999
)
100100
from agents.matmaster_agent.flow_agents.step_validation_agent.prompt import (
101-
STEP_VALIDATION_INSTRUCTION,
101+
create_step_validation_instruction,
102102
)
103103
from agents.matmaster_agent.flow_agents.step_validation_agent.schema import (
104104
StepValidationSchema,
@@ -109,6 +109,7 @@
109109
)
110110
from agents.matmaster_agent.flow_agents.thinking_agent.constant import THINKING_AGENT
111111
from agents.matmaster_agent.flow_agents.utils import (
112+
find_alternative_tool,
112113
get_tools_list,
113114
scenes_contain_query_job_status,
114115
should_bypass_confirmation,
@@ -140,6 +141,7 @@
140141
from agents.matmaster_agent.services.session_files import get_session_files
141142
from agents.matmaster_agent.state import (
142143
CURRENT_STEP,
144+
CURRENT_STEP_TOOL_NAME,
143145
EXPAND,
144146
FINISHED_STATE,
145147
HISTORY_STEPS,
@@ -336,11 +338,19 @@ def all_finished_agent(self) -> DisallowTransferAndContentLimitSchemaAgent:
336338
def _build_execution_agent_for_plan(
337339
self, ctx: InvocationContext
338340
) -> MatMasterSupervisorAgent:
341+
current_step = get_current_step(ctx)
342+
current_step_tool_name = current_step.get(CURRENT_STEP_TOOL_NAME)
343+
belonging_agent = ALL_TOOLS.get(current_step_tool_name, {}).get(
344+
'belonging_agent'
345+
)
346+
339347
step_validation_agent = DisallowTransferAndContentLimitSchemaAgent(
340348
name='step_validation_agent',
341349
model=MatMasterLlmConfig.tool_schema_model,
342350
description='校验步骤执行结果是否合理',
343-
instruction=STEP_VALIDATION_INSTRUCTION,
351+
instruction=create_step_validation_instruction(
352+
find_alternative_tool(current_step_tool_name)
353+
),
344354
output_schema=StepValidationSchema,
345355
state_key='step_validation',
346356
after_model_callback=MatMasterLlmConfig.opik_tracer.after_model_callback,
@@ -356,10 +366,6 @@ def _build_execution_agent_for_plan(
356366
before_model_callback=filter_llm_contents,
357367
after_model_callback=MatMasterLlmConfig.opik_tracer.after_model_callback,
358368
)
359-
current_step = get_current_step(ctx)
360-
tool_name = current_step.get('tool_name')
361-
belonging_agent = ALL_TOOLS.get(tool_name, {}).get('belonging_agent')
362-
363369
execution_agent = MatMasterSupervisorAgent(
364370
name='execution_agent',
365371
model=MatMasterLlmConfig.default_litellm_model,

agents/matmaster_agent/flow_agents/all_finished_agent/prompt.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def create_all_finished_instruction(user_request, history_steps, session_files):
1313
return f"""
1414
You are a "Goal Completion Judge" agent. Decide whether the user's overall final objective
1515
has been completed *as of now*, based ONLY on history_steps and session_files.
16-
1716
Key principle: "finished" indicates whether the session should STOP now.
1817
- If the goal is completed: finished=true.
1918
- If the goal is NOT completed but still achievable with further actions: finished=false.
@@ -26,33 +25,58 @@ def create_all_finished_instruction(user_request, history_steps, session_files):
2625
2726
IMPORTANT: Treat explicit numeric/parameter constraints (layers, vacuum thickness, slab orientation/cut, supercell expansion like 5×5×1, etc.) as mandatory. finished=true ONLY if history_steps explicitly confirms EACH constraint was applied.
2827
28+
IMPORTANT (NEW, HIGH PRIORITY): history_steps[*].suggestion is PRIMARY evidence for whether the task is still achievable.
29+
- If ANY actionable suggestion exists (even if earlier), and it has NOT been explicitly attempted and exhausted in later history_steps, you MUST set finished=false (unless the goal is already completed).
30+
- Actionable suggestions include: retrying with modified parameters, switching tools/providers, requesting missing inputs, rerunning with fixes, alternative workflows, etc.
31+
- You MUST NOT output finished=true (Termination/Unachievable) when there exists any untried actionable suggestion.
32+
- Only consider Termination/Unachievable when (a) NOT completed, AND (b) all actionable suggestions have been tried (and are evidenced as tried) with continued failure, AND (c) no remaining viable next action is suggested anywhere in history_steps.
33+
34+
CRITICAL: Do NOT treat "suggestion was not acted upon" as evidence of unachievability.
35+
If there exists any actionable history_steps[*].suggestion that has not been tried, the task is still achievable => finished=false.
36+
2937
# Input
3038
user_request:
3139
{user_request}
32-
3340
history_steps (JSON):
3441
{history_text}
35-
3642
session_files (JSON):
3743
{session_files_text}
3844
3945
# Decision Rules (must follow)
4046
1) Judge ONLY the user's final goal completion / stop condition, not whether all intermediate steps ran.
47+
4148
2) Deliverable type:
4249
- If a file artifact is required (PDF/DOCX/ZIP/code project/structure file, etc.), you MUST verify an appropriate OSS link exists in session_files; otherwise finished=false (unless Termination/Unachievable applies).
4350
- If in-chat content is required, verify the complete requested content already exists in history_steps outputs; otherwise finished=false (unless Termination/Unachievable applies).
51+
4452
3) If any critical step is failed/missing/running OR outputs are insufficient to prove completion, set finished=false (unless Termination/Unachievable applies).
53+
4554
4) Insufficient evidence => finished=false and state exactly what is missing (unless Termination/Unachievable applies).
55+
4656
5) Contradictions: prefer later entries; if still unclear => finished=false and explain contradiction (unless Termination/Unachievable applies).
57+
4758
6) Do NOT assume results not explicitly supported by history_steps/session_files.
59+
4860
6.1) For explicit parameter constraints, if ANY constraint is not explicitly evidenced, finished=false (unless Termination/Unachievable applies).
49-
7) Termination/Unachievable (STOP even though not done):
50-
If the goal is NOT completed AND is blocked/unachievable such that no viable next action exists (e.g., repeated critical failures; missing required inputs that cannot be obtained; hard constraints prevent completion),
51-
you MUST output finished=true and the reason MUST include:
61+
62+
7) Suggestion-first achievability check (MUST APPLY BEFORE declaring finished=true for Termination/Unachievable):
63+
- Scan ALL history_steps for actionable suggestions.
64+
- If any actionable suggestion is not explicitly shown as attempted and exhausted, output finished=false.
65+
66+
8) Termination/Unachievable (STOP even though not done):
67+
You may output finished=true for Termination/Unachievable ONLY if:
68+
- The goal is NOT completed, AND
69+
- history_steps provide concrete evidence that no viable next action exists, AND
70+
- EVERY actionable history_steps[*].suggestion has been explicitly tried in later history_steps and still failed, leaving no remaining options.
71+
If ANY unresolved suggestion proposes a viable next action (e.g., change parameters, switch provider/tool, request missing info),
72+
you MUST output finished=false (the session should continue), unless the goal is already completed.
73+
74+
If you output finished=true (Termination/Unachievable), the reason MUST include:
5275
- "NOT completed" and
5376
- "cannot be completed / unachievable" and
5477
- the blocking evidence (specific failed steps / missing inputs).
55-
You MUST NOT output finished=false if you claim the task is blocked/unachievable.
78+
79+
You MUST NOT output finished=true (Termination/Unachievable) when the only blocking evidence is that a tool failed once and the agent has not yet tried actionable suggestions (e.g., switching provider/tool, changing parameters). In that case, output finished=false.
5680
5781
# Output Format
5882
Output ONLY ONE JSON object exactly:

agents/matmaster_agent/flow_agents/execution_agent/agent.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
is_job_submitted_step,
2424
)
2525
from agents.matmaster_agent.flow_agents.step_validation_agent.prompt import (
26-
STEP_VALIDATION_INSTRUCTION,
26+
create_step_validation_instruction,
2727
)
2828
from agents.matmaster_agent.flow_agents.style import separate_card
2929
from agents.matmaster_agent.flow_agents.utils import (
3030
check_plan,
31+
find_alternative_tool,
3132
get_agent_for_tool,
3233
)
3334
from agents.matmaster_agent.llm_config import MatMasterLlmConfig
@@ -36,6 +37,7 @@
3637
from agents.matmaster_agent.state import (
3738
CURRENT_STEP,
3839
CURRENT_STEP_DESCRIPTION,
40+
CURRENT_STEP_STATUS,
3941
CURRENT_STEP_TOOL_NAME,
4042
HISTORY_STEPS,
4143
PLAN,
@@ -208,7 +210,10 @@ async def _tool_result_validation(
208210
)
209211
validation_instruction = '\n'.join(lines)
210212
self.validation_agent.instruction = (
211-
STEP_VALIDATION_INSTRUCTION + validation_instruction
213+
create_step_validation_instruction(
214+
find_alternative_tool(current_step_tool_name)
215+
)
216+
+ validation_instruction
212217
)
213218

214219
async for validation_event in self.validation_agent.run_async(ctx):
@@ -320,15 +325,14 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
320325
yield _core_execution_event
321326

322327
post_execution_step = get_current_step(ctx)
323-
# 工具调用结果返回【成功】
324-
if post_execution_step['status'] == PlanStepStatusEnum.SUCCESS:
328+
if post_execution_step[CURRENT_STEP_STATUS] != PlanStepStatusEnum.SUBMITTED:
325329
# 校验工具结果
326330
async for _tool_result_validation_event in self._tool_result_validation(
327331
ctx
328332
):
329333
yield _tool_result_validation_event
330334
# 异步任务,直接退出当前函数
331-
elif post_execution_step['status'] == PlanStepStatusEnum.SUBMITTED:
335+
else:
332336
return
333337

334338
update_history_steps = copy.deepcopy(ctx.session.state[HISTORY_STEPS])

agents/matmaster_agent/flow_agents/step_validation_agent/prompt.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
STEP_VALIDATION_INSTRUCTION = """
1+
from typing import List
2+
3+
4+
def create_step_validation_instruction(alternative_tools: List[str]):
5+
return f"""
26
You are a validation agent responsible for checking if the execution result of a step matches the user's requirements and basic chemical/materials science knowledge.
37
48
Your task is to analyze:
@@ -9,6 +13,9 @@
913
1014
Based on this analysis, determine if the result is reasonable and matches expectations.
1115
16+
Backup tools you may suggest using if the result is invalid or uncertain:
17+
{alternative_tools}
18+
1219
# Validation Criteria:
1320
1. **Relevance**: Does the result address the step's intended purpose?
1421
2. **Accuracy**: Is the result consistent with basic chemical/materials science knowledge?
@@ -17,11 +24,12 @@
1724
1825
# Output Format:
1926
You must respond with a JSON object containing:
20-
{
27+
{{
2128
"is_valid": boolean, // true if result matches requirements and knowledge, false otherwise
2229
"reason": "string", // brief explanation of validation result
23-
"confidence": "high|medium|low" // confidence level in the validation
24-
}
30+
"confidence": "high|medium|low", // confidence level in the validation
31+
"suggestion": "string" // actionable suggestion; if invalid/uncertain, suggest fixes or using one of the backup tools above
32+
}}
2533
2634
# Important Rules:
2735
- If the result contains obvious errors (wrong chemical formulas, impossible physical properties, etc.), mark as invalid

agents/matmaster_agent/flow_agents/step_validation_agent/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ class StepValidationSchema(BaseModel):
55
is_valid: bool
66
reason: str
77
confidence: str # "high", "medium", "low"
8+
suggestion: str

0 commit comments

Comments
 (0)