mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
Parity for the two semantic guards, which Task 8 committed to for exactly this reason: a guard Edit honours and Fix merely implies is worse than no guard, because the inconsistency is invisible to whoever reads only one of them. The hole was not the one I reported at the end of round 2. Probed against the literal S6 batch, `fix.propose_repair` already refuses to hand on a batch carrying any rejection -- unlike Edit it never had a fallback to the first attempt, so the condemned batch was never written. What it did have was a BLIND surface: the engine-grounded reason was discarded and `_no_fix()` returned the generic "No safe automatic fix found", which the handler then stored on `fc.risk` and showed to nobody. The human at the gate was told a fix could not be found and given nothing to act on -- while the guard had just described the aggregator, the node and the selector that would have made a manual fix obvious. So: `_no_fix()` takes an optional reason (one dead-end path, not a second), a `would_run_wrong` verdict fills it in, and `handle_propose` renders it on an ErrorCard beside the reply line -- the same division of labour Edit's refusal uses. The verdict is read off the structured `would_run_wrong` list, never off the rejection prose, and deliberately NOT off `rejections` at large: those inline whole `intent.args` and belong in a re-prompt, not on a card, and a structural refusal says nothing about the batch that remains. A purely structural refusal therefore still ends at the generic sentence, as before. The `would_run_wrong` check is written as its own line rather than left to the broader rejection check it currently duplicates. That redundancy is a property of the line below it, not of the guard: if Fix is ever loosened the way Edit was, this is what still holds -- and it is what carries the reason either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
375 lines
17 KiB
Python
375 lines
17 KiB
Python
"""Real LLM cognition for Dify Builder's Fix mode.
|
|
|
|
Pure functions called by ``LlmBuilderAgent`` for Fix cognition.
|
|
On any dead-end (no model, unparseable output, or -- for propose_repair -- no
|
|
valid repair) they DEGRADE to an honest, surface-to-human result rather than
|
|
crashing the advance or applying a canned guess.
|
|
"""
|
|
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
from core.dify_builder.models import (
|
|
ChecklistError,
|
|
Diagnosis,
|
|
Graph,
|
|
MutationIntent,
|
|
NodeOutput,
|
|
Risk,
|
|
Run,
|
|
)
|
|
from core.model_manager import ModelInstance
|
|
from graphon.enums import BUILT_IN_NODE_TYPES, BuiltinNodeTypes
|
|
from services.dify_builder import credentials, graph_ops, preflight
|
|
from services.dify_builder.agent import graph_prompt, llm
|
|
|
|
_SEVERITIES = {"low", "medium", "high"}
|
|
|
|
# `BuiltinNodeTypes` is a plain namespace class of string ClassVars (not a StrEnum/Enum
|
|
# in this graphon version) so it isn't iterable itself; `BUILT_IN_NODE_TYPES` is the
|
|
# tuple of every member's value and is what we iterate to build the allow-list.
|
|
_ALLOWED_NODE_TYPES: set[str] = set(BUILT_IN_NODE_TYPES)
|
|
|
|
# Node types whose execution has effects OUTSIDE the workflow (network / tool calls).
|
|
# A Code node is treated as sandboxed compute (NOT external) in v1 so a config/code fix
|
|
# can auto-apply.
|
|
_EXTERNAL_SIDE_EFFECT_TYPES: set[str] = {str(BuiltinNodeTypes.HTTP_REQUEST), str(BuiltinNodeTypes.TOOL)}
|
|
|
|
# Shared with Edit (``graph_prompt``): both agents write the same five ops
|
|
# through the same ``graph_ops`` apply functions, so the rules the port refuses
|
|
# on -- a branch connect's source_handle, an indexed array path, the engine's
|
|
# comparison literals, the redaction sentinel -- are one text for both.
|
|
# Edit's "follow a new branch to the aggregator it rejoins" clause is NOT here:
|
|
# Fix repairs the one node it diagnosed, and a structural op of its own already
|
|
# forces the change to a human.
|
|
_OP_SCHEMA = graph_prompt.OP_LIST + graph_prompt.CONDITION_OPERATOR_RULES + graph_prompt.REDACTION_RULE
|
|
|
|
|
|
def _truncate(value: Any, limit: int = 300) -> str:
|
|
text = str(value)
|
|
return text if len(text) <= limit else text[:limit] + "…"
|
|
|
|
|
|
def _graph_context(graph: Graph) -> str:
|
|
"""Compact LLM-readable view: each node as 'id (type): title' plus the
|
|
source handles it declares, each edge plus a non-default source handle.
|
|
|
|
Rendered by ``graph_prompt`` so Fix and Edit show a branch node the same
|
|
way. Without the handles a repair could not see which arm to connect to,
|
|
``apply_connect`` refused every connect it proposed from an if-else /
|
|
question-classifier / human-input node, and that refusal spent the single
|
|
corrective re-prompt.
|
|
"""
|
|
lines = ["NODES:"]
|
|
lines.extend(graph_prompt.node_line(node) for node in graph.get("nodes", []))
|
|
lines.append("EDGES:")
|
|
lines.extend(graph_prompt.edge_line(edge) for edge in graph.get("edges", []))
|
|
return "\n".join(lines)
|
|
|
|
|
|
def _node_ids(graph: Graph) -> set[str]:
|
|
return {str(n.get("id")) for n in graph.get("nodes", []) if n.get("id") is not None}
|
|
|
|
|
|
def _coerce_severity(value: Any) -> str:
|
|
text = str(value or "").lower()
|
|
return text if text in _SEVERITIES else "medium"
|
|
|
|
|
|
def _failed_nodes(node_outputs: list[NodeOutput]) -> list[NodeOutput]:
|
|
return [o for o in node_outputs if o.status in ("failed", "exception")]
|
|
|
|
|
|
def _diagnosis_from_json(
|
|
data: dict[str, Any],
|
|
graph: Graph,
|
|
*,
|
|
fallback_node: str = "",
|
|
) -> Diagnosis:
|
|
culprit = str(data.get("culprit_node_id") or "")
|
|
if culprit not in _node_ids(graph):
|
|
culprit = fallback_node
|
|
root_cause = str(data.get("root_cause") or "") or "Diagnosis produced no explanation."
|
|
return Diagnosis(culprit_node_id=culprit, root_cause=root_cause, severity=_coerce_severity(data.get("severity")))
|
|
|
|
|
|
def _degraded_diagnosis(failed: list[NodeOutput], launch_error: str = "") -> Diagnosis:
|
|
if failed:
|
|
node = failed[0]
|
|
return Diagnosis(
|
|
culprit_node_id=node.node_id,
|
|
root_cause=f"Automatic diagnosis unavailable — node error: {node.error or '(none)'}",
|
|
severity="high",
|
|
)
|
|
if launch_error:
|
|
# No node executed: surface the captured launch error, not the blind message.
|
|
return Diagnosis(
|
|
culprit_node_id="",
|
|
root_cause=f"Automatic diagnosis unavailable — launch error: {launch_error}",
|
|
severity="high",
|
|
)
|
|
return Diagnosis(culprit_node_id="", root_cause="Automatic diagnosis unavailable.", severity="medium")
|
|
|
|
|
|
def _degraded_checklist(errors: list[ChecklistError]) -> Diagnosis:
|
|
for error in errors:
|
|
if error.messages:
|
|
return Diagnosis(culprit_node_id=error.node_id, root_cause=error.messages[0], severity="medium")
|
|
return Diagnosis(culprit_node_id="", root_cause="Checklist error", severity="medium")
|
|
|
|
|
|
def diagnose(
|
|
model: ModelInstance | None,
|
|
failed_run: Run,
|
|
graph: Graph,
|
|
node_outputs: list[NodeOutput],
|
|
on_reasoning: Callable[[str], None] | None = None,
|
|
) -> Diagnosis:
|
|
failed = _failed_nodes(node_outputs)
|
|
if model is None:
|
|
return _degraded_diagnosis(failed, failed_run.error)
|
|
system = (
|
|
"You are a Dify workflow debugging assistant. Given a failed workflow run, identify the "
|
|
"single node that caused the failure and the root cause. Reply with ONLY a JSON object: "
|
|
'{"culprit_node_id": "<an existing node id>", "root_cause": "<concise explanation>", '
|
|
'"severity": "low|medium|high"}.'
|
|
) + llm.json_language_instruction("root_cause")
|
|
failed_desc = (
|
|
"\n".join(
|
|
f"- {o.node_id} ({o.title}) status={o.status} error={o.error!r} "
|
|
f"inputs={_truncate(o.inputs)} outputs={_truncate(o.outputs)}"
|
|
for o in failed
|
|
)
|
|
or (
|
|
f"(no node executed; the run threw at launch: {failed_run.error!r})"
|
|
if failed_run.error
|
|
else "(no per-node failure recorded)"
|
|
)
|
|
)
|
|
user = f"FAILED NODES:\n{failed_desc}\n\nGRAPH:\n{_graph_context(graph)}"
|
|
try:
|
|
data = llm.invoke_json(model, system=system, user=user, on_reasoning=on_reasoning)
|
|
except Exception: # any LLM/provider failure degrades to a surfaced result, never crashes the advance
|
|
return _degraded_diagnosis(failed, failed_run.error)
|
|
fallback = failed[0].node_id if failed else ""
|
|
return _diagnosis_from_json(data, graph, fallback_node=fallback)
|
|
|
|
|
|
def diagnose_checklist(
|
|
model: ModelInstance | None,
|
|
errors: list[ChecklistError],
|
|
graph: Graph,
|
|
on_reasoning: Callable[[str], None] | None = None,
|
|
) -> Diagnosis:
|
|
if model is None:
|
|
return _degraded_checklist(errors)
|
|
system = (
|
|
"You are a Dify workflow pre-publish checker. Given checklist errors, identify the single "
|
|
"most important node to fix and the root cause. Reply with ONLY a JSON object: "
|
|
'{"culprit_node_id": "<an existing node id>", "root_cause": "<concise explanation>", '
|
|
'"severity": "low|medium|high"}.'
|
|
) + llm.json_language_instruction("root_cause")
|
|
errs = (
|
|
"\n".join(
|
|
f"- {e.node_id} ({e.node_type}) {e.title}: {'; '.join(e.messages)} "
|
|
f"unconnected={e.unconnected} plugin_missing={e.plugin_missing}"
|
|
for e in errors
|
|
)
|
|
or "(no checklist errors)"
|
|
)
|
|
user = f"CHECKLIST ERRORS:\n{errs}\n\nGRAPH:\n{_graph_context(graph)}"
|
|
try:
|
|
data = llm.invoke_json(model, system=system, user=user, on_reasoning=on_reasoning)
|
|
except Exception: # degrade to a surfaced result, never crash the advance
|
|
return _degraded_checklist(errors)
|
|
fallback = next((e.node_id for e in errors if e.node_id), "")
|
|
return _diagnosis_from_json(data, graph, fallback_node=fallback)
|
|
|
|
|
|
# What the gate says when Fix has nothing to stage and nothing specific to
|
|
# blame. Every other dead end in this module -- no model, unparseable output, an
|
|
# engine refusal the re-prompt could not answer -- ends here.
|
|
_NO_FIX_REASON = "No safe automatic fix found — please review the diagnosis and edit the canvas manually, or reject."
|
|
|
|
# ...and what it says when there IS something specific. A would-run-wrong
|
|
# verdict is the only refusal that describes the batch STILL ON THE TABLE rather
|
|
# than one intent that was dropped, so it is the only one worth putting in front
|
|
# of the human in place of the sentence above.
|
|
_WOULD_RUN_WRONG_REASON = "No safe automatic fix found — the repair would have applied cleanly and then not worked:\n"
|
|
|
|
|
|
def _no_fix(reason: str = "") -> tuple[list[MutationIntent], Risk]:
|
|
"""Fix's one surface-to-human result: nothing staged, high risk, and a
|
|
reason the gate shows.
|
|
|
|
``reason`` replaces the generic sentence when this module knows something
|
|
specific -- which today means a ``would_run_wrong`` verdict. Parameterised
|
|
rather than joined by a second dead-end path, so there stays exactly one
|
|
place that says "no fix" and one shape for callers to recognise.
|
|
"""
|
|
return [], Risk(level="high", reason=reason or _NO_FIX_REASON, has_external_side_effect=False)
|
|
|
|
|
|
def _judged_wrong_reason(vetted: preflight.VettedIntents) -> str:
|
|
"""The surface text for a batch the semantic guards condemned, or ``""``
|
|
when they had nothing to say (leaving ``_no_fix`` its generic sentence).
|
|
|
|
Read off ``would_run_wrong``, never off the rejection prose: the guards'
|
|
lines carry only node ids, paths, handle names and element ids, so this is
|
|
safe to show, while the structural rejections beside them inline whole
|
|
``intent.args`` and belong in a re-prompt, not on a card.
|
|
"""
|
|
if not vetted.would_run_wrong:
|
|
return ""
|
|
return _WOULD_RUN_WRONG_REASON + "\n".join(vetted.would_run_wrong)
|
|
|
|
|
|
def _culprit_config(node_id: str, graph: Graph) -> str:
|
|
"""The culprit node's own ``data``, with its secrets withheld.
|
|
|
|
A failing http-request node is one of the likeliest culprits there is, and
|
|
its ``data`` is where a live bearer token or API key sits -- so the config
|
|
that goes into the repair prompt goes through ``redact_node_config`` first.
|
|
The model still sees which header exists and how the node authenticates,
|
|
never the secret itself. ``graph_ops.validate_intent_args`` is the other
|
|
half: it refuses any repair intent that hands the placeholder back, so a
|
|
redacted value cannot be written over the real one.
|
|
|
|
Rendered by ``graph_prompt.config_block``, the same way Edit renders a
|
|
node: JSON, and an over-budget config loses its last key ENTIRELY and says
|
|
which one. It used to be ``_truncate``'d -- a Python repr cut at 1500
|
|
characters -- which the shared op schema turned into a trap: it tells the
|
|
model to re-send an array's existing elements byte-identical "as GRAPH
|
|
shows them", so a ``cases`` array cut mid-value would have come back
|
|
completed from imagination, obediently, over the case nobody could see.
|
|
"""
|
|
for node in graph.get("nodes", []):
|
|
if node.get("id") == node_id:
|
|
data = node.get("data")
|
|
config = credentials.redact_node_config(data) if isinstance(data, dict) else {}
|
|
return graph_prompt.config_block(config)
|
|
return "(culprit node not found in graph)"
|
|
|
|
|
|
def _parse_repair(data: dict[str, Any]) -> tuple[list[MutationIntent] | None, Risk]:
|
|
raw = data.get("intents")
|
|
if not isinstance(raw, list):
|
|
return None, _no_fix()[1]
|
|
intents: list[MutationIntent] = []
|
|
for item in raw:
|
|
if isinstance(item, dict) and isinstance(item.get("op"), str) and isinstance(item.get("args"), dict):
|
|
intents.append(MutationIntent(op=item["op"], args=item["args"]))
|
|
else:
|
|
return None, _no_fix()[1]
|
|
risk_raw = data.get("risk") if isinstance(data.get("risk"), dict) else {}
|
|
risk = Risk(
|
|
level="high" if str(risk_raw.get("level", "")).lower() == "high" else "low",
|
|
reason=str(risk_raw.get("reason", "")),
|
|
has_external_side_effect=bool(risk_raw.get("has_external_side_effect", False)),
|
|
)
|
|
return intents, risk
|
|
|
|
|
|
def _touched_node_types(intents: list[MutationIntent], graph: Graph) -> set[str]:
|
|
by_id = {n.get("id"): str((n.get("data") or {}).get("type", "")) for n in graph.get("nodes", [])}
|
|
types: set[str] = set()
|
|
for intent in intents:
|
|
if intent.op in ("create_node", "insert_between"):
|
|
types.add(str(intent.args.get("node_type", "")))
|
|
elif intent.op == "connect":
|
|
types.update(by_id.get(intent.args.get(key), "") for key in ("from_node", "to_node"))
|
|
else: # set_node_config / delete_node -> node_id
|
|
types.add(by_id.get(intent.args.get("node_id"), ""))
|
|
return types
|
|
|
|
|
|
def _shape_risk(intents: list[MutationIntent], graph: Graph, llm_risk: Risk) -> Risk:
|
|
structural = any(i.op in graph_ops.STRUCTURAL_OPS for i in intents)
|
|
touched_external = bool(_touched_node_types(intents, graph) & _EXTERNAL_SIDE_EFFECT_TYPES)
|
|
external = touched_external or llm_risk.has_external_side_effect
|
|
if structural or external:
|
|
return Risk(
|
|
level="high",
|
|
reason=llm_risk.reason or "Structural or side-effecting change — needs review.",
|
|
has_external_side_effect=external,
|
|
)
|
|
return Risk(
|
|
level=llm_risk.level or "low",
|
|
reason=llm_risk.reason or "Config-only fix.",
|
|
has_external_side_effect=False,
|
|
)
|
|
|
|
|
|
def _invoke_repair(
|
|
model: ModelInstance,
|
|
system: str,
|
|
user: str,
|
|
on_reasoning: Callable[[str], None] | None,
|
|
) -> tuple[list[MutationIntent] | None, Risk]:
|
|
try:
|
|
data = llm.invoke_json(model, system=system, user=user, on_reasoning=on_reasoning)
|
|
except Exception: # degrade to a surfaced result, never crash the advance
|
|
return None, _no_fix()[1]
|
|
return _parse_repair(data)
|
|
|
|
|
|
def propose_repair(
|
|
model: ModelInstance | None,
|
|
diagnosis: Diagnosis,
|
|
graph: Graph,
|
|
on_reasoning: Callable[[str], None] | None = None,
|
|
) -> tuple[list[MutationIntent], Risk]:
|
|
if model is None:
|
|
return _no_fix()
|
|
system = (
|
|
"You are a Dify workflow repair assistant. Propose the minimal set of mutations that fix "
|
|
"the diagnosed problem. Reference only node ids that exist in the graph, and use only the "
|
|
"listed node types. Reply with ONLY a JSON object: "
|
|
'{"intents": [{"op": ..., "args": {...}}], '
|
|
'"risk": {"level": "low|high", "reason": "...", "has_external_side_effect": true|false}}.\n'
|
|
+ _OP_SCHEMA
|
|
+ "Allowed node types: "
|
|
+ ", ".join(sorted(_ALLOWED_NODE_TYPES))
|
|
+ ".\n"
|
|
) + llm.json_language_instruction("risk.reason")
|
|
user = (
|
|
f"DIAGNOSIS:\n culprit={diagnosis.culprit_node_id}\n root_cause={diagnosis.root_cause}\n"
|
|
f" severity={diagnosis.severity}\n\nCULPRIT NODE CONFIG:\n{_culprit_config(diagnosis.culprit_node_id, graph)}"
|
|
f"\n\nGRAPH:\n{_graph_context(graph)}"
|
|
)
|
|
intents, risk = _invoke_repair(model, system, user, on_reasoning)
|
|
if intents is None:
|
|
return _no_fix()
|
|
# Structure AND node data, the same two checks ``apply_repair`` makes: a
|
|
# repair that applies cleanly but leaves a node ``Graph.init`` refuses is the
|
|
# worst outcome there is here -- it reaches the approval gate as a real fix,
|
|
# the human approves it, and the write dies. One corrective re-prompt, then
|
|
# surface to the human; a proposal the engine still refuses is not a fix.
|
|
vetted = preflight.vet_intents(graph, intents, _ALLOWED_NODE_TYPES)
|
|
if vetted.rejections:
|
|
reasons = "\n".join(vetted.rejections)
|
|
retry_user = f"{user}\n\nYour previous intents were invalid:\n{reasons}\nReturn corrected intents."
|
|
retried, retried_risk = _invoke_repair(model, system, retry_user, on_reasoning)
|
|
if retried is None:
|
|
# Nothing usable came back, so the verdict that stands is the one on
|
|
# the FIRST attempt -- which is also the batch the human would have
|
|
# been asked about. Say what was wrong with it.
|
|
return _no_fix(_judged_wrong_reason(vetted))
|
|
intents, risk = retried, retried_risk
|
|
vetted = preflight.vet_intents(graph, intents, _ALLOWED_NODE_TYPES)
|
|
# Parity with Edit (``agent.edit._refuse_a_batch_already_judged_wrong``), and
|
|
# written as its own check rather than left to the broader one below.
|
|
# ``would_run_wrong`` is the one refusal the ENGINE DOES NOT MAKE: the draft
|
|
# would start, run green and do the wrong thing, so ``apply_repair`` has
|
|
# nothing to veto and this is the only thing standing between the guard and
|
|
# a written batch. Fix happens to refuse ANY remaining rejection today,
|
|
# which makes this redundant -- but that is a property of the line below,
|
|
# not of the guard, and if Fix is ever loosened the way Edit was, this is
|
|
# what still holds. It also carries the reason, which the generic exit does
|
|
# not.
|
|
if vetted.would_run_wrong:
|
|
return _no_fix(_judged_wrong_reason(vetted))
|
|
if vetted.rejections or not vetted.applicable:
|
|
return _no_fix()
|
|
return vetted.applicable, _shape_risk(vetted.applicable, graph, risk)
|