From ac2523beb626ff3bb2c86d13147b244534dedbe3 Mon Sep 17 00:00:00 2001 From: Charles Yao Date: Sat, 29 Aug 2026 13:28:30 -0700 Subject: [PATCH] feat(dify-builder): goal-shaped Edit rules form from cognition output analyze_impact now returns {fields, values, target_node_ids} instead of a flat edit_rules dict, mirroring Build's goal-shaped form (A2). Rewire handle_capability_check to read fc.form_fields/edit_rules/edit_target_node_ids from that shape and render the FormCard via build_form_fields; handle_impact_analysis merges the submitted payload on the dynamic fc.form_fields keys instead of a fixed key tuple; handle_review's re_fix branch re-renders the form the same way. Delete the now-dead _EDIT_RULE_FIELDS/_EDIT_RULE_KEYS constants. PlaceholderAgent.analyze_impact returns the new shape (the old fields become canned fields/values), so both it and the LLM agent (which still delegates to canned) are shape-uniform. Co-Authored-By: Claude Opus 4.8 --- api/core/dify_builder/handlers_edit.py | 29 +++++++------- api/core/dify_builder/placeholder_agent.py | 22 +++++++++-- .../core/dify_builder/test_handlers_edit.py | 38 +++++++++++++++++++ .../test_placeholder_agent_edit.py | 6 ++- 4 files changed, 73 insertions(+), 22 deletions(-) diff --git a/api/core/dify_builder/handlers_edit.py b/api/core/dify_builder/handlers_edit.py index c0cb4486c04..43c12ab4b07 100644 --- a/api/core/dify_builder/handlers_edit.py +++ b/api/core/dify_builder/handlers_edit.py @@ -19,7 +19,6 @@ from core.dify_builder.contract import ( CheckpointCard, DecisionItem, FormCard, - FormField, PlanCard, PublishCard, SummaryCard, @@ -34,6 +33,7 @@ from core.dify_builder.handlers_fix import ( action_string, append_card, build_change_set, + build_form_fields, emit_canvas, merge_known_keys, mint_checkpoint, @@ -55,16 +55,6 @@ __all__ = [ ] -_EDIT_RULE_FIELDS = [ - FormField(key="risk_threshold", label="Risk threshold", type="text"), - FormField(key="review_team", label="Review team", type="select", options=["compliance", "legal", "engineering"]), - FormField(key="timeout_behavior", label="Timeout behavior", type="select", options=["fail_open", "fail_closed"]), - FormField(key="preserve_summary", label="Preserve summary", type="bool"), -] - -_EDIT_RULE_KEYS = ("risk_threshold", "review_team", "timeout_behavior", "preserve_summary") - - def handle_capability_check(env: Env, turn: Turn, s: Session, fc: DifyBuilderContext) -> StepResult: """(waiting) Entry state. On ``send_edit_goal`` read the existing graph, emit a read-only context summary, analyze impact into edit_rules + target @@ -84,8 +74,9 @@ def handle_capability_check(env: Env, turn: Turn, s: Session, fc: DifyBuilderCon edge_count = len(graph.get("edges", [])) impact = env.agent.analyze_impact(fc.goal_text, graph) - fc.edit_rules = dict(impact.get("edit_rules", {})) - fc.edit_target_node_ids = list(impact.get("target_node_ids", [])) + fc.form_fields = list(impact.get("fields") or []) + fc.edit_rules = dict(impact.get("values") or {}) + fc.edit_target_node_ids = list(impact.get("target_node_ids") or []) for node_id in fc.edit_target_node_ids: emit_canvas(env, "highlight_edit_target", node_id=node_id) @@ -100,7 +91,9 @@ def handle_capability_check(env: Env, turn: Turn, s: Session, fc: DifyBuilderCon ) form_items = append_card( fc, - FormCard(variant="edit_rules", fields=list(_EDIT_RULE_FIELDS), values=dict(fc.edit_rules), frozen=False), + FormCard( + variant="edit_rules", fields=build_form_fields(fc.form_fields), values=dict(fc.edit_rules), frozen=False + ), ) challenge_items = append_card( fc, @@ -148,7 +141,8 @@ def handle_impact_analysis(env: Env, turn: Turn, s: Session, fc: DifyBuilderCont fc.checkpoint_seq = fc.next_seq if turn.action is not None and isinstance(turn.action.payload, dict): - fc.edit_rules = merge_known_keys(fc.edit_rules, turn.action.payload, _EDIT_RULE_KEYS) + keys = [f["key"] for f in fc.form_fields if isinstance(f, dict) and f.get("key")] + fc.edit_rules = merge_known_keys(fc.edit_rules, turn.action.payload, keys) graph, graph_hash = env.dify.read_graph(s.app_id, turn.actor) fc.plan_items = env.agent.propose_edit_plan(dict(fc.edit_rules), graph) @@ -366,7 +360,10 @@ def handle_review(env: Env, turn: Turn, s: Session, fc: DifyBuilderContext) -> S emit_canvas(env, "highlight_edit_target", node_id=node_id) decision_items = append_card(fc, DecisionItem(text="Continue adjusting")) form_items = append_card( - fc, FormCard(variant="edit_rules", fields=list(_EDIT_RULE_FIELDS), values=dict(fc.edit_rules), frozen=False) + fc, + FormCard( + variant="edit_rules", fields=build_form_fields(fc.form_fields), values=dict(fc.edit_rules), frozen=False + ), ) challenge_items = append_card( fc, diff --git a/api/core/dify_builder/placeholder_agent.py b/api/core/dify_builder/placeholder_agent.py index fbd58902d16..4d865defa96 100644 --- a/api/core/dify_builder/placeholder_agent.py +++ b/api/core/dify_builder/placeholder_agent.py @@ -200,11 +200,25 @@ class PlaceholderAgent: # Pick affected nodes from the existing graph (LLM + knowledge), with a # canned-id fallback so the flow works on any Build-shaped draft. node_ids = {n.get("id") for n in graph.get("nodes", [])} - targets = [nid for nid in (BUILD_LLM_ID, BUILD_KNOWLEDGE_ID) if nid in node_ids] - if not targets: - targets = [BUILD_LLM_ID] + targets = [nid for nid in (BUILD_LLM_ID, BUILD_KNOWLEDGE_ID) if nid in node_ids] or [BUILD_LLM_ID] return { - "edit_rules": { + "fields": [ + {"key": "risk_threshold", "label": "Risk threshold", "type": "text"}, + { + "key": "review_team", + "label": "Review team", + "type": "select", + "options": ["compliance", "legal", "engineering"], + }, + { + "key": "timeout_behavior", + "label": "Timeout behavior", + "type": "select", + "options": ["fail_open", "fail_closed"], + }, + {"key": "preserve_summary", "label": "Preserve summary", "type": "bool"}, + ], + "values": { "risk_threshold": "medium", "review_team": "compliance", "timeout_behavior": "fail_closed", diff --git a/api/tests/unit_tests/core/dify_builder/test_handlers_edit.py b/api/tests/unit_tests/core/dify_builder/test_handlers_edit.py index 4f49b301e17..b3cb970abcb 100644 --- a/api/tests/unit_tests/core/dify_builder/test_handlers_edit.py +++ b/api/tests/unit_tests/core/dify_builder/test_handlers_edit.py @@ -33,6 +33,18 @@ def _new_env(dify=None, emit_canvas=None) -> tuple[Env, InMemoryRepository]: return env, repo +def _session(**overrides) -> Session: + fields: dict = { + "app_id": "app", + "tenant_id": "tenant-1", + "owner_account_id": "acc-1", + "entry_mode": EntryMode.EDIT, + "current_state": PcState.EDIT_CAPABILITY_CHECK, + } + fields.update(overrides) + return Session(**fields) + + def _seed_edit_session(repo: InMemoryRepository, state: PcState, **fc_kwargs) -> Session: s = Session( app_id="app", @@ -75,6 +87,28 @@ def test_capability_check_send_edit_goal_advances_to_impact_analysis(): assert any(e["event"] == "highlight_edit_target" for e in events) +def test_edit_capability_check_renders_agent_fields(): + # PlaceholderAgent.analyze_impact returns dynamic fields/values/targets; + # the form card and fc.form_fields/edit_rules/edit_target_node_ids must + # reflect them (not the old hardcoded _EDIT_RULE_FIELDS constant). + from core.dify_builder.handlers_edit import handle_capability_check + + env, _ = _new_env() + env.agent.analyze_impact = lambda _g, _graph: { + "fields": [{"key": "tone", "label": "Tone", "type": "select", "options": ["formal", "casual"]}], + "values": {"tone": "formal"}, + "target_node_ids": ["llm"], + } + s = _session(entry_mode=EntryMode.EDIT, current_state=PcState.EDIT_CAPABILITY_CHECK) + fc = DifyBuilderContext(goal_text="make it formal") + result = handle_capability_check( + env, Turn(actor=_actor(), action=Action(kind="send_edit_goal", payload={"text": "formal"})), s, fc + ) + assert result.context.form_fields[0]["key"] == "tone" + assert result.context.edit_rules == {"tone": "formal"} + assert result.context.edit_target_node_ids == ["llm"] + + def test_capability_check_ignores_non_goal_action(): from core.dify_builder.handlers_edit import handle_capability_check @@ -95,6 +129,10 @@ def test_impact_analysis_submit_rules_advances_to_plan_approval_with_checkpoint( PcState.EDIT_IMPACT_ANALYSIS, edit_rules={"risk_threshold": "medium", "review_team": "compliance"}, edit_target_node_ids=["llm"], + form_fields=[ + {"key": "risk_threshold", "label": "Risk threshold", "type": "text"}, + {"key": "review_team", "label": "Review team", "type": "text"}, + ], ) turn = Turn( action=Action( diff --git a/api/tests/unit_tests/core/dify_builder/test_placeholder_agent_edit.py b/api/tests/unit_tests/core/dify_builder/test_placeholder_agent_edit.py index 5342bea3af4..7c14d60385a 100644 --- a/api/tests/unit_tests/core/dify_builder/test_placeholder_agent_edit.py +++ b/api/tests/unit_tests/core/dify_builder/test_placeholder_agent_edit.py @@ -11,10 +11,12 @@ def _graph_with_llm(): } -def test_analyze_impact_returns_edit_rules_and_targets_from_graph(): +def test_analyze_impact_returns_fields_values_and_targets_from_graph(): agent = PlaceholderAgent() out = agent.analyze_impact("tighten risk handling", _graph_with_llm()) - assert set(out["edit_rules"]) == {"risk_threshold", "review_team", "timeout_behavior", "preserve_summary"} + expected_keys = {"risk_threshold", "review_team", "timeout_behavior", "preserve_summary"} + assert {f["key"] for f in out["fields"]} == expected_keys + assert set(out["values"]) == expected_keys assert "llm" in out["target_node_ids"]