Files
ragflow/internal/ingestion/task/debug_result_dsl.go
Jack 8546eb62bf Feat(ingestion): add canvas pipeline debug (dry-run) mode with View result log (#17538)
## Summary
Adds a side-effect-free DataFlow canvas **debug (dry-run) mode** plus a
**debug run log with a "View result" panel**, so a canvas can be
executed synchronously and inspected end-to-end (per-component progress
and parsed chunks) without persisting anything.

### Dry-run execution (inline parsed chunks)
- `task/debug.go`: `NewDebugTaskContext` builds an in-memory
`TaskContext` with **`KB.ID == ""` — the single debug signal used across
the ingestion pipeline**. A canvas debug run has no knowledgebase, and
production ingestion always supplies one, so `kb_id == ""` occurs ONLY
in debug mode. Components gate their own side effects on this signal
without any dedicated debug vocabulary (the former `CANVAS_DEBUG_DOC_ID`
marker constant is removed).
- `task/pipeline_executor.go`: `validateTaskContext` no longer requires
a KB when `KB.ID == ""` (debug); debug runs return `collectDebugOutput`
(chunks) instead of a no-op; uploaded bytes are delivered as
`inputs['binary']` for doc-less runs; `injectDebugPageCap` caps the
parser to the first pages for a fast preview via the production
`override_params` channel (Parser cpnID + family).
- `component/tokenizer.go`: `shouldHaveEmbedding` skips embedding when
`kb_id == ""` — the embedder is configured on the knowledgebase, so a
debug run has nothing to resolve against and stays side-effect free.
- `chunker/register.go`: chunk images are uploaded to MinIO only when a
KB is present (persist run). **In debug mode the raw image bytes are
intentionally dropped (`delete(ck, "image")`)** — the debug preview does
not render chunk images, and dropping the bytes keeps them out of memory
and out of the Redis-stored debug log. This is a deliberate trade-off,
not an oversight.
- `component/file.go`: pass through in-memory binary bytes, skipping
`doc_id` -> storage resolution.
- `handler/agent.go` + `agent_webhook.go`: detect `dataflow_canvas` and
run a sync debug returning chunks inline on the existing
chat/completions endpoint; reject DataFlow canvases from webhooks (fixes
the previously dead `== "DataFlow"` check; mirrors Python
`agent_api.py`).
- `parser_dispatch.go`: export `ParserFileFamily` for the executor's
page-cap injection.

### Debug run log + "View result"
Mirrors Python's debug-log contract so the front-end can replay each
component's progress and parsed output:
- `task/debug_log_sink.go`: a `DebugLogSink` records every component's
lifecycle into a `[{component_id, trace}]` array (each trace entry
carries `message`, `progress`, `timestamp`, `elapsed_time`). `Flush`
appends a terminal `END` marker whose first trace message is non-empty
so the front-end detects completion. On failure the END marker is
prefixed `[ERROR]` yet still carries the run, so the failure timeline
renders instead of being stuck empty. Timestamps and `elapsed_time` are
in seconds (matching the rest of the app).
- `task/debug_result_dsl.go`: `BuildDebugResultDSL` builds the `dsl` the
END marker carries — the Go analogue of Python's `Graph.__str__` +
END-marker `dsl` in `rag/flow/pipeline.py`. It combines the static DSL
structure (component_name / downstream / params / graph.nodes) with the
run output map (`output["state"][<id>]`) to emit, per component,
`obj.params.outputs[<format>].value` (chunks / text / json / html /
markdown) — the exact keys the front-end `dataflow-result` page reads to
render each step's parsed chunks. Raw embedding vectors (including the
dimension-scoped `q_<dim>_vec` keys) are stripped so the stored log
stays Python-scale.
- `task/pipeline_executor.go`: after the run, attach the built `dsl` to
the END marker via the `ResultSink` capability.
- `handler/agent.go`: `runCanvasPipelineDebug` generates a stable
`message_id` up-front and always flushes the log (success or failure);
`respondWithDebugResult` returns `message_id` in **both** the success
and the error envelope so the front-end can poll the log. The debug-log
endpoint `GET /agents/:id/logs/:message_id` serves the array.
- `web/src/pages/agent/hooks/use-run-dataflow.ts`: on a run failure,
also surface `message_id` via `setMessageId` so the log sheet renders
the failure timeline (the `[ERROR]` END marker is already written).
Guarded by `if (msgId)`, so it is a safe no-op when the back-end does
not return an id.

## Behavioral notes
- Debug parses only the first pages (`debugPageCapPages`) for a fast
preview; an explicit `pages` cap already present in the ParserConfig is
respected.
- Debug mode does not keep chunk images (see above) and does not compute
embeddings — it exercises parse + chunk only.

## Test plan
- Go: `debug_test.go`, `debug_log_sink_test.go` (trace pairing, END
marker, `[ERROR]` prefix, fractional-second timestamp/elapsed_time, size
caps, and a real-pipeline test asserting the END-marker `dsl` carries
non-empty per-component `params.outputs` with chunks),
`debug_result_dsl_test.go` (flat and real nested `output["state"]`
shapes, vector stripping, format priority),
`debug_pages_integration_test.go`, `pipeline_executor_persist_test.go`,
`handler/agent_pipeline_debug_test.go`, `handler/agent_logs_test.go`
(incl. `TestRunCanvasPipelineDebug_ErrorStillExposesMessageID` /
`TestRespondWithDebugResult_ErrorCarriesMessageID` locking `message_id`
on failure), plus updates to `agent_test.go` / `agent_webhook_test.go` /
`chunker/image_upload_test.go` / `tokenizer*.go`.
- `go build ./...` and `./build.sh --test` for affected packages.

🤖 Generated with [CodeBuddy Code](https://cnb.cool/codebuddy)

---------

Co-authored-by: CodeBuddy Code <noreply@tencent.com>
2026-07-31 13:10:08 +08:00

254 lines
8.4 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is the Go analogue of Python's `Graph.__str__`
// (agent/canvas.py:119) used by the dataflow debug "View result" page.
// Python attaches `dsl = json.loads(str(self))` to the END debug-log marker
// (rag/flow/pipeline.py:98); the front-end reads `dsl.components[<id>].obj`
// to render each component's parsed output. Go has no per-component `__str__`
// serialization, so BuildDebugResultDSL combines the STATIC DSL structure
// (component_name / downstream / params / graph.nodes) with the RUN output map
// (state.go:284 `out[<componentID>] = <outputs>`) to emit the identical JSON
// shape. Result is a strict subset (UI-relevant fields only) of Python's, so
// the front-end renders chunks with parity and a smaller payload.
package task
import (
"encoding/json"
"fmt"
"strings"
)
// ResultSink is an OPTIONAL capability a ProgressSink may implement to receive
// the debug-run result DSL. The pipeline executor probes for it via a type
// assertion, so the ProgressSink contract stays unchanged and non-debug
// (DB-backed) sinks simply ignore it — keeping the coupling one-directional.
type ResultSink interface {
SetResult(dsl map[string]any)
}
// outputFormats is the priority order used to pick a component's payload key,
// mirroring NormalizeChunks (chunk_utils.go:27).
var outputFormats = []string{"chunks", "json", "text", "html", "markdown"}
// vectorKeys are dropped from payloads while copying. The front-end never
// renders raw vectors and Python's serialized component obj excludes them;
// stripping keeps the Redis-stored debug log at Python-scale size.
var vectorKeys = map[string]struct{}{
"vector": {},
"embedding": {},
"q_vec": {},
"feature": {},
}
// isVectorKey reports whether k is a raw embedding-vector key that must be
// stripped from debug payloads. It matches the fixed legacy keys
// (vector/embedding/feature) AND the dimension-scoped pattern q_<dim>_vec that
// the tokenizer actually emits (see hasEmbeddingVector, tokenizer.go:828, e.g.
// q_4_vec, q_1024_vec). The literal "q_vec" entry never matches those, so a
// bare map lookup would let real vectors leak into the Redis log.
func isVectorKey(k string) bool {
if _, ok := vectorKeys[k]; ok {
return true
}
return strings.HasPrefix(k, "q_") && strings.HasSuffix(k, "_vec")
}
// BuildDebugResultDSL builds the `dsl` object the debug-log END marker carries
// so the front-end "View result" page can render each component's output.
//
// dsl is the raw canvas DSL JSON (optionally wrapped as {"dsl": {...}}). output
// is the pipeline run output keyed by component id (output[<id>] is that
// component's outputs map, which may carry chunks/json/text/html/markdown).
func BuildDebugResultDSL(dsl string, output map[string]any) (map[string]any, error) {
var tpl map[string]any
if err := json.Unmarshal([]byte(dsl), &tpl); err != nil {
return nil, fmt.Errorf("BuildDebugResultDSL: unmarshal dsl: %w", err)
}
root := tpl
if nested, ok := tpl["dsl"].(map[string]any); ok {
root = nested
}
components, ok := root["components"].(map[string]any)
if !ok {
return nil, fmt.Errorf("BuildDebugResultDSL: dsl missing components map")
}
built := make(map[string]any, len(components))
for id, raw := range components {
comp, _ := raw.(map[string]any)
if comp == nil {
comp = map[string]any{}
}
// component_name: prefer the nested obj.component_name (Python shape),
// fall back to a top-level component_name.
name := ""
var staticParams map[string]any
if obj, _ := comp["obj"].(map[string]any); obj != nil {
name, _ = obj["component_name"].(string)
if p, _ := obj["params"].(map[string]any); p != nil {
staticParams = p
}
}
if name == "" {
name, _ = comp["component_name"].(string)
}
down := comp["downstream"] // preserve as-is (string or []any)
// Build obj.params: start from a deep copy of the static DSL params
// (setups/field_name/...), then inject the runtime outputs wrapper.
mergedParams := map[string]any{}
for k, v := range staticParams {
mergedParams[k] = deepCopy(v)
}
if format, payload := detectFormat(lookupComponentOutput(output, id)); format != "" {
mergedParams["outputs"] = map[string]any{
format: map[string]any{
"value": deepCopyStrip(payload),
"type": "",
},
"output_format": map[string]any{"value": format},
}
}
built[id] = map[string]any{
"obj": map[string]any{
"component_name": name,
"params": mergedParams,
},
"downstream": down,
"component_name": name,
}
}
result := map[string]any{
"components": built,
"graph": deepCopy(root["graph"]),
}
return result, nil
}
// lookupComponentOutput resolves a single component's runtime output map from
// the pipeline run result.
//
// The production `pipe.Run` return value nests every component's outputs under
// output["state"][<componentID>] — finalizeResult (pipeline.go:636) attaches
// runState.Snapshot() (state.go:296, map[string]map[string]any keyed by cpn
// id), and statePost (scheduler.go:229) writes each component's top-level
// output keys there via SetVar(cpnID, k, v). So the canonical lookup is
// output["state"][id].
//
// A flat keyed-by-id shape (output[id] directly) is accepted as a fallback so
// this builder stays usable for hand-built outputs in tests and any
// non-Snapshot callers; it is never produced by the real pipeline.
func lookupComponentOutput(output map[string]any, id string) any {
// The production run output nests each component under
// output["state"][<id>] (finalizeResult → runState.Snapshot(), state.go:296).
// Snapshot returns map[string]map[string]any, but some callers build a
// map[string]any-shaped state, so accept BOTH concrete types — a
// single-type assertion would silently fail the real shape and fall through
// to the (usually empty) top-level lookup.
switch state := output["state"].(type) {
case map[string]map[string]any:
if v, ok := state[id]; ok {
return v
}
case map[string]any:
if v, ok := state[id]; ok {
return v
}
}
// Fallback: flat shape (tests / non-Snapshot producers).
return output[id]
}
// detectFormat returns the output key (chunks/json/text/html/markdown) present
// in a component's output map, by priority, plus the raw payload under it.
// Returns ("", nil) when the component produced no recognized output — the
// front-end then renders that step empty (matching Python's empty obj).
func detectFormat(out any) (string, any) {
m, ok := out.(map[string]any)
if !ok || m == nil {
return "", nil
}
for _, f := range outputFormats {
if v, exists := m[f]; exists && v != nil {
return f, v
}
}
return "", nil
}
// deepCopy returns a JSON-compatible deep copy of v (maps/slices/primitives),
// preserving structure but sharing nothing mutable with the source.
func deepCopy(v any) any {
switch val := v.(type) {
case map[string]any:
cp := make(map[string]any, len(val))
for k, vv := range val {
cp[k] = deepCopy(vv)
}
return cp
case []map[string]any:
cp := make([]any, len(val))
for i, vv := range val {
cp[i] = deepCopy(vv)
}
return cp
case []any:
cp := make([]any, len(val))
for i, vv := range val {
cp[i] = deepCopy(vv)
}
return cp
default:
return v
}
}
// deepCopyStrip is deepCopy plus dropping vectorKeys from every map it visits.
// Used for component payloads so raw embedding vectors never reach the log.
func deepCopyStrip(v any) any {
switch val := v.(type) {
case map[string]any:
cp := make(map[string]any, len(val))
for k, vv := range val {
if isVectorKey(k) {
continue
}
cp[k] = deepCopyStrip(vv)
}
return cp
case []map[string]any:
cp := make([]any, len(val))
for i, vv := range val {
cp[i] = deepCopyStrip(vv)
}
return cp
case []any:
cp := make([]any, len(val))
for i, vv := range val {
cp[i] = deepCopyStrip(vv)
}
return cp
default:
return v
}
}