From d3d433574f462f570f21cbcf5fb04810c744e2d2 Mon Sep 17 00:00:00 2001 From: guangyang1206 Date: Tue, 5 May 2026 19:14:28 +0800 Subject: [PATCH] fix(data-analysis): capture LOCAL_STORAGE_BASE_DIR at init time (#1040) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolveFileServiceForKnowledge was reading os.Getenv("LOCAL_STORAGE_BASE_DIR") on every request. This causes a mismatch when the variable is not in the process environment at request time (e.g. it was set programmatically before app startup but not exported, or the DI-constructed fileService uses a different configured path). Fix: store the value in a new localBaseDir field at NewDataAnalysisTool construction time — the same moment the DI container assembles the local FileService — and use t.localBaseDir in resolveFileServiceForKnowledge instead. This guarantees the data-analysis tool always resolves files under the same base directory that the local FileService was configured with, eliminating the "path not found" errors when LOCAL storage is used for a knowledge base while the global storage backend is minio/oss/cos. Fixes #1040 --- internal/agent/tools/data_analysis.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/agent/tools/data_analysis.go b/internal/agent/tools/data_analysis.go index 0ca1b2ea..6bf2d25d 100644 --- a/internal/agent/tools/data_analysis.go +++ b/internal/agent/tools/data_analysis.go @@ -116,6 +116,13 @@ type DataAnalysisTool struct { db *sql.DB sessionID string createdTables []string // Track tables created in this session + // localBaseDir is the LOCAL_STORAGE_BASE_DIR value captured at construction + // time so resolveFileServiceForKnowledge uses the same base path that was + // used when the local FileService was initialised by DI. Re-reading the + // env var at request time can produce a different (or empty) value if the + // variable was not exported to the sub-process or was set programmatically + // after startup, causing GetFile to look in the wrong directory (#1040). + localBaseDir string } func NewDataAnalysisTool( @@ -134,6 +141,11 @@ func NewDataAnalysisTool( tenantService: tenantService, db: db, sessionID: sessionID, + // Capture LOCAL_STORAGE_BASE_DIR once at construction time so that every + // call to resolveFileServiceForKnowledge uses the same base path. The + // env var is guaranteed to be set (or empty == "/data/files" fallback) + // when the application starts and the DI container is assembled. + localBaseDir: strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR")), } } @@ -806,7 +818,14 @@ func (t *DataAnalysisTool) resolveFileServiceForKnowledge(ctx context.Context, k } storageConfig := tenant.StorageEngineConfig - baseDir := strings.TrimSpace(os.Getenv("LOCAL_STORAGE_BASE_DIR")) + // Use the localBaseDir captured at construction time rather than re-reading + // LOCAL_STORAGE_BASE_DIR from os.Getenv here. Reading the env var at + // request-handling time can produce an empty string (or the wrong value) + // when the variable was set programmatically before startup or is absent + // from the process environment of the DI-constructed sub-component, causing + // the newly created local FileService to use the /data/files fallback + // instead of the configured path and therefore fail to locate files (#1040). + baseDir := t.localBaseDir resolvedSvc, resolvedProvider, err := filesvc.NewFileServiceFromStorageConfig(provider, storageConfig, baseDir) if err != nil {