Skip to content

Commit 0763699

Browse files
committed
refactor:用CURRENT_STEP_DESCRIPTION替换STEP_DESCRIPTION并添加step验证相关的逻辑与状态定义
1 parent ca63406 commit 0763699

7 files changed

Lines changed: 53 additions & 32 deletions

File tree

agents/matmaster_agent/core_agents/base_agents/mcp_agent.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
store_tool_result_in_memory,
5151
)
5252
from agents.matmaster_agent.model import CostFuncType
53-
from agents.matmaster_agent.state import CURRENT_STEP
53+
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_RESULT
5454
from agents.matmaster_agent.style import tool_response_failed_card
5555
from agents.matmaster_agent.utils.event_utils import (
5656
all_text_event,
@@ -240,8 +240,10 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
240240
raise
241241

242242
parsed_tool_result = await parse_result(ctx, dict_result)
243-
logger.info(
244-
f'{ctx.session.id} parsed_tool_result = {parsed_tool_result}'
243+
post_execution_step = copy.deepcopy(ctx.session.state[CURRENT_STEP])
244+
post_execution_step[CURRENT_STEP_RESULT] = parsed_tool_result
245+
yield update_state_event(
246+
ctx, state_delta={CURRENT_STEP: post_execution_step}
245247
)
246248
for _frontend_render_event in frontend_render_event(
247249
ctx,

agents/matmaster_agent/core_agents/comp_agents/recommend_summary_agent/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
)
6565
from agents.matmaster_agent.state import (
6666
CURRENT_STEP,
67+
CURRENT_STEP_DESCRIPTION,
6768
RECOMMEND_PARAMS,
68-
STEP_DESCRIPTION,
6969
)
7070
from agents.matmaster_agent.sub_agents.tools import ALL_TOOLS
7171
from agents.matmaster_agent.utils.event_utils import (
@@ -215,7 +215,7 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
215215
)
216216

217217
self.tool_call_info_agent.instruction = gen_tool_call_info_instruction(
218-
user_prompt=current_step[STEP_DESCRIPTION],
218+
user_prompt=current_step[CURRENT_STEP_DESCRIPTION],
219219
agent_prompt=self.instruction,
220220
tool_doc=tool_doc,
221221
tool_schema=tool_schema,

agents/matmaster_agent/flow_agents/all_finished_agent/prompt.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def create_all_finished_instruction(user_request, history_steps, session_files):
1515
You are a "Goal Completion Judge" agent. Your task is to determine whether the user's
1616
overall final objective/task has been completed *as of now*, based solely on the provided
1717
tool-call history: history_steps and the provided session_files list.
18+
19+
IMPORTANT: The user's goal may be "content in chat" (e.g., a researched tutorial/summary),
20+
not necessarily a file deliverable. Only require session_files evidence when the user
21+
explicitly asked for a file or a file is clearly the expected final deliverable.
22+
1823
# Input
1924
history_steps is a list. Each element is a past tool invocation record, typically including
2025
(but not limited to):
@@ -25,7 +30,7 @@ def create_all_finished_instruction(user_request, history_steps, session_files):
2530
2631
session_files is a list of file links (OSS URLs). Only files that were actually generated
2732
and persisted for this session will appear here. Use session_files as verifiable evidence
28-
that a file deliverable truly exists.
33+
that a file deliverable truly exists (only when a file deliverable is required).
2934
3035
Below in the raw user_request:
3136
{user_request}
@@ -38,21 +43,26 @@ def create_all_finished_instruction(user_request, history_steps, session_files):
3843
3944
# Decision Rules (must follow)
4045
1) Use "whether the user's final goal is achieved" as the ONLY criterion, not whether all steps were executed.
41-
2) If there is clear evidence that the final deliverable/final outcome has been produced and is usable, set finished=true.
42-
- For file deliverables, you MUST verify the file exists by checking that an appropriate OSS link is present in session_files.
46+
2) Consider the expected deliverable type based on user_request:
47+
- If the user asked for a file/output artifact (e.g., PDF/DOCX/ZIP/code project), you MUST verify the file exists by checking
48+
that an appropriate OSS link is present in session_files; otherwise finished=false.
49+
- If the user asked for "in-chat content" (e.g., search + summarize + tutorial), you should judge completion by whether the final
50+
requested content is already present/produced in history_steps outputs (e.g., the assistant/tool produced a complete tutorial/summary).
4351
3) If any critical step failed, is missing, is still running, or the outputs are insufficient to prove goal completion, set finished=false.
44-
4) If the information in history_steps and session_files is insufficient to confirm completion (e.g., no final output, only partial logs,
45-
or expected output file link is not present in session_files),
52+
4) If the information in history_steps and session_files is insufficient to confirm completion (e.g., no final summary/tutorial text,
53+
only partial logs; or a required output file link is not present in session_files),
4654
you MUST return finished=false and explain what information is missing in reason.
4755
5) If there are contradictions in history_steps, prefer the later entries. If you still cannot decide, return finished=false
4856
and explain the contradiction in reason.
4957
6) Do NOT assume results that are not explicitly supported by history_steps or session_files. Judge only from verifiable evidence.
58+
5059
# Output Format (very important)
5160
You must output ONLY ONE JSON object that strictly matches this schema:
5261
{{
5362
"finished": true|false,
54-
"reason": "A brief, specific explanation in English that cites key evidence from history_steps and/or session_files (e.g., a tool_name status/output or the presence/absence of an OSS link). If not finished, state the critical blocking reason(s) or missing info."
63+
"reason": "A brief, specific explanation in English that cites key evidence from history_steps and/or session_files (e.g., a tool_name status/output; or the presence/absence of an OSS link when a file is required). If not finished, state the critical blocking reason(s) or missing info."
5564
}}
65+
5666
# Output Constraints
5767
- Output ONLY valid JSON (no Markdown, no code fences, no extra commentary).
5868
- reason must be an English string and should reference concrete evidence from history_steps and/or session_files.

agents/matmaster_agent/flow_agents/execution_agent/agent.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
2020
from agents.matmaster_agent.flow_agents.step_utils import (
2121
get_current_step,
22+
get_current_step_validation,
2223
is_job_submitted_step,
2324
)
2425
from agents.matmaster_agent.flow_agents.step_validation_agent.prompt import (
@@ -28,16 +29,16 @@
2829
from agents.matmaster_agent.flow_agents.utils import (
2930
check_plan,
3031
get_agent_for_tool,
31-
has_self_check,
3232
)
3333
from agents.matmaster_agent.llm_config import MatMasterLlmConfig
3434
from agents.matmaster_agent.logger import PrefixFilter
3535
from agents.matmaster_agent.prompt import MatMasterCheckTransferPrompt
3636
from agents.matmaster_agent.state import (
3737
CURRENT_STEP,
38+
CURRENT_STEP_DESCRIPTION,
39+
CURRENT_STEP_TOOL_NAME,
3840
HISTORY_STEPS,
3941
PLAN,
40-
STEP_DESCRIPTION,
4142
)
4243
from agents.matmaster_agent.sub_agents.mapping import (
4344
MatMasterSubAgentsEnum,
@@ -97,7 +98,7 @@ async def _construct_function_call_ctx(
9798
) -> AsyncGenerator[Event, None]:
9899
current_step = copy.deepcopy(ctx.session.state[CURRENT_STEP])
99100
current_step_tool_name = current_step['tool_name']
100-
current_step_tool_description = current_step[STEP_DESCRIPTION]
101+
current_step_tool_description = current_step[CURRENT_STEP_DESCRIPTION]
101102
current_step['status'] = PlanStepStatusEnum.PROCESS
102103
yield update_state_event(
103104
ctx,
@@ -186,9 +187,9 @@ async def _core_execution_agent(
186187
async def _tool_result_validation(
187188
self, ctx: InvocationContext
188189
) -> AsyncGenerator[Event, None]:
189-
current_step_tool_name = ctx.session.state[CURRENT_STEP]['tool_name']
190+
current_step_tool_name = ctx.session.state[CURRENT_STEP][CURRENT_STEP_TOOL_NAME]
190191
current_step_tool_description = ctx.session.state[CURRENT_STEP][
191-
STEP_DESCRIPTION
192+
CURRENT_STEP_DESCRIPTION
192193
]
193194
user_text = (
194195
ctx.user_content.parts[0].text
@@ -292,11 +293,11 @@ async def _prepare_retry_other_tool(
292293
update_plan['steps'][index]['tool_name'] = next_tool
293294
update_plan['steps'][index]['status'] = PlanStepStatusEnum.PROCESS
294295
original_description = ctx.session.state[PLAN]['steps'][index][
295-
STEP_DESCRIPTION
296+
CURRENT_STEP_DESCRIPTION
296297
].split('\n\n注意:')[
297298
0
298299
] # 移除之前的失败原因
299-
update_plan['steps'][index][STEP_DESCRIPTION] = original_description
300+
update_plan['steps'][index][CURRENT_STEP_DESCRIPTION] = original_description
300301
yield update_state_event(
301302
ctx,
302303
state_delta={
@@ -321,17 +322,17 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
321322
post_execution_step = get_current_step(ctx)
322323
# 工具调用结果返回【成功】
323324
if post_execution_step['status'] == PlanStepStatusEnum.SUCCESS:
324-
# 对成功的工具调用结果进行校验
325-
if has_self_check(post_execution_step['tool_name']):
326-
# 校验工具结果
327-
async for _tool_result_validation_event in self._tool_result_validation(
328-
ctx
329-
):
330-
yield _tool_result_validation_event
325+
# 校验工具结果
326+
async for _tool_result_validation_event in self._tool_result_validation(
327+
ctx
328+
):
329+
yield _tool_result_validation_event
331330
# 异步任务,直接退出当前函数
332331
elif post_execution_step['status'] == PlanStepStatusEnum.SUBMITTED:
333332
return
334333

335334
update_history_steps = copy.deepcopy(ctx.session.state[HISTORY_STEPS])
336-
update_history_steps.append(post_execution_step)
335+
update_history_steps.append(
336+
{**post_execution_step, **get_current_step_validation(ctx)}
337+
)
337338
yield update_state_event(ctx, state_delta={HISTORY_STEPS: update_history_steps})

agents/matmaster_agent/flow_agents/step_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
from google.adk.agents import InvocationContext
22

33
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
4-
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_STATUS
4+
from agents.matmaster_agent.state import (
5+
CURRENT_STEP,
6+
CURRENT_STEP_STATUS,
7+
CURRENT_STEP_VALIDATION,
8+
)
59

610

711
def get_current_step(ctx: InvocationContext):
812
return ctx.session.state.get(CURRENT_STEP, {})
913

1014

15+
def get_current_step_validation(ctx: InvocationContext):
16+
return ctx.session.state.get(CURRENT_STEP_VALIDATION, {})
17+
18+
1119
def is_job_submitted_step(ctx: InvocationContext) -> bool:
1220
return (
1321
get_current_step(ctx).get(CURRENT_STEP_STATUS) == PlanStepStatusEnum.SUBMITTED

agents/matmaster_agent/memory/inject_memory_callback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from agents.matmaster_agent.constant import MATMASTER_AGENT_NAME
1717
from agents.matmaster_agent.logger import PrefixFilter
1818
from agents.matmaster_agent.services.memory import format_short_term_memory
19-
from agents.matmaster_agent.state import CURRENT_STEP, STEP_DESCRIPTION
19+
from agents.matmaster_agent.state import CURRENT_STEP, CURRENT_STEP_DESCRIPTION
2020

2121
logger = logging.getLogger(__name__)
2222
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
@@ -31,7 +31,7 @@ def _query_from_request_and_state(
3131
state = getattr(callback_context, 'state', None)
3232
if state:
3333
current_step = state.get(CURRENT_STEP, {})
34-
desc = current_step.get(STEP_DESCRIPTION, '')
34+
desc = current_step.get(CURRENT_STEP_DESCRIPTION, '')
3535
if desc:
3636
return desc.strip()
3737
# Fallback: last text from contents

agents/matmaster_agent/state.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
CURRENT_STEP = 'current_step'
1515
CURRENT_STEP_STATUS = 'status'
1616
CURRENT_STEP_TOOL_NAME = 'tool_name'
17+
CURRENT_STEP_DESCRIPTION = 'step_description'
18+
CURRENT_STEP_RESULT = 'step_result'
19+
CURRENT_STEP_VALIDATION = 'step_validation'
1720

1821
HISTORY_STEPS = 'history_steps'
1922
FINISHED_STATE = 'finished_state'
20-
21-
# Other Key
22-
STEP_DESCRIPTION = 'step_description'

0 commit comments

Comments
 (0)