Files
dify/api/core/app/apps/workflow
Yansong Zhang 71db7d6c1c feat(api): agent backend session lifecycle for workflow agent nodes
Implements the dify-agent run lifecycle contract for workflow Agent nodes so a
single workflow run can fan out into multiple Agent backend runs that share an
Agent session via persisted snapshots, and so a HITL pause routes through a
workflow-level pause instead of being modeled as agent-run pause.

Highlights:

* New ``workflow_agent_runtime_sessions`` table (migration a7b9c8d1e2f3) keyed
  by (tenant_id, workflow_run_id, node_id, binding_id, agent_id). Stores the
  CompositorSessionSnapshot plus the replayable ``composition_layer_specs`` so
  a future cleanup-only run can match the agenton compositor's
  snapshot-vs-composition name-order validator.
* ``WorkflowAgentRuntimeRequestBuilder`` now threads the prior snapshot back
  into ``CreateRunRequest.session_snapshot`` and defaults to
  ``on_exit.default = suspend`` so layers survive between runs in the same
  Agent session. A pydantic-ai history layer is added by default to carry the
  Agent's chat history across runs.
* ``DifyAgentNode`` saves a fresh snapshot on success / paused terminals,
  reuses the prior snapshot on the next entry, and retires the local row on
  failed / cancelled terminals so a workflow loop cannot resume from stale
  state. A paused agent run is reported as a workflow-level
  ``PauseRequestedEvent`` rather than an Agent backend pause.
* New ``WorkflowAgentSessionCleanupLayer`` (GraphEngineLayer) attached on
  Workflow and AdvancedChat app runners. On terminal workflow events it
  retires every ACTIVE session row for that workflow run. The HTTP cleanup
  path is fully built (composition spec replay + snapshot filtering for
  excluded plugin layers + wait_run gating before mark_cleaned) but kept
  behind ``_HTTP_CLEANUP_SUPPORTED = False`` because the current dify-agent
  ``runner._run_agent`` always requires an LLM layer and has no
  ``purpose=cleanup`` branch yet; flipping the flag enables the contract once
  the backend supports cleanup-only runs.
* Adds a compositor integration test that runs the real
  ``agenton.Compositor.from_config`` + ``_create_run`` against the built
  cleanup request so the snapshot-vs-composition mismatch trap (which is
  invisible to FakeAgentBackendRunClient) is caught at unit-test time.

Tests: 130 passing in agent_v2 + agent_backend; verified end-to-end against
real agent backend, gunicorn + celery, and the new
``workflow_agent_runtime_sessions`` table.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 17:12:07 +08:00
..
2024-04-08 18:51:46 +08:00
2026-02-09 14:57:23 +08:00