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 <noreply@anthropic.com>
This commit is contained in:
Charles YaoandClaude Opus 4.8 committed 2026-08-29 13:28:30 -07:00
1 parent cbb1487d6a
commit ac2523beb6
4 files changed
+73 -22

No files matched your search

+13 -16
View File
@@ -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,
+18 -4
View File
@@ -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",
@@ -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(
@@ -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"]