Skip to content

fix(L2-B): knowledge_base _is_postgres psycopg ImportError → graceful False#53

Merged
Wool-xing merged 2 commits into
mainfrom
bundle3-L2-B-psycopg-graceful
May 13, 2026
Merged

fix(L2-B): knowledge_base _is_postgres psycopg ImportError → graceful False#53
Wool-xing merged 2 commits into
mainfrom
bundle3-L2-B-psycopg-graceful

Conversation

@Wool-xing
Copy link
Copy Markdown
Owner

范围

L2 baseline CI fail 子项 B: runtime/mcp/knowledge_base/server.py _is_postgres()get_engine(), postgres db_url 配但 psycopg 未装时 ImportError → MCP tool search_similar stack trace。1 文件 +14/-1。

L2 三子项总览

子项 失败 PR
L2-A generate_report.py docx ImportError n7 fail #51 (开)
L2-B _is_postgres psycopg ImportError MCP tool crash 本 PR
L2-C env-manager rollout n2 router 未跳过 n2 fail #52 (开)

三子项合后, CI L2 ≥80% 达标。

修法 (Surgical)

def _is_postgres() -> bool:
    try:
        return get_engine().dialect.name == "postgresql"
    except (ImportError, ModuleNotFoundError):
        # psycopg / asyncpg 未装 → 视为非 postgres, 走 sqlite fallback
        return False
    except Exception:
        # 其他异常 (连接失败 / dialect 加载失败 等) 也 fallback
        return False

server.py L83+ 已有 sqlite fallback 分支自然走 (return {"indexed": True, ..., "backend": "sqlite-no-vec"})。tool_search_similar L127-128 已正确处理 not _is_postgres() → return error dict (而非 raise)。

不在范围

  • requirements 加 psycopg (依赖膨胀, CI infra 决策待用户决, 与 L2-A 不静默装 docx 一致)
  • db.py get_engine 加 try/except (公共函数, 影响面大, 由调用方各自 handle)

本地测试 (3/3 关键 case 过)

OK ImportError → False  (psycopg 缺 → 走 sqlite fallback)
OK ConnectionError → False  (其他异常兜底)
OK postgres engine → True  (正常路径不破)

协作宪章 §1.3 六道闸自检 (f1-f6)

  • a 静态: ✓ pre-commit 全过
  • b 副作用: ✓ _is_postgres 是 server.py 内部函数, 仅 server.py 内部调用 (3 处: tool_index_case / tool_index_defect / tool_search_similar) 全走 if/else 分支不依赖 raise
  • c 跨层契约: ✓ 不破 MCP tool schema (返 error dict 是合规 response)
  • d 实测: ✓ 3 关键 case 通过
  • e 回归: ✓ 正常 postgres 路径不变
  • f 诚实自检:
    • f1: 1 文件 +14/-1 — H
    • f2: ✓
    • f3: ✓ docstring 明示"视为非 postgres, 走 sqlite fallback"
    • f4: ✓
    • f5: 见下
    • f6: 见下

置信度自检 (f5)

声明 证据 f5
psycopg ImportError 是 L2 root cause gh run view 25815979436 含明文 ModuleNotFoundError: No module named 'psycopg' H
try/except 修法 git diff H
3/3 关键 case 单测 输出贴出 H
pre-commit 全过 工具输出 H
L2-A+B+C 合后 ≥80% 达标 推断 (3 子项独立修, 各自消除一类 fail) M

假阳性过滤 (f6)

唯一 finding = "_is_postgres 在 psycopg 缺时 crash":

#
1 真问题 / 误报? 。CI log stack trace 实证
2 反例? "应该装 psycopg 而不是 graceful skip" → 反驳: CI 是 selftest 不强依赖 postgres, sqlite fallback 设计已存在 (server.py L83+), 让 graceful 工作正是利用既有 fallback。不证伪
3 修后实质变好? 。MCP tool 不再 crash, sqlite fallback 路径生效

假阳性候选数: 0

关联

xiaoxing0135 and others added 2 commits May 14, 2026 01:56
… False

L2 baseline CI fail 子项 B: db_url 配 postgres 但 psycopg / asyncpg 未装,
get_engine 调 create_engine ImportError → _is_postgres crash → MCP tool
search_similar stack trace ("No module named 'psycopg'")。

修法 (Surgical, 单函数 try/except):
- _is_postgres() 加 try/except (ImportError / ModuleNotFoundError / Exception)
- 缺 driver 时返 False → server.py 已有 sqlite fallback 分支自然走 (L83+)
- search_similar 走 `return {"error": "...requires Postgres + pgvector..."}`
  正常 MCP error response, 装饰器不再标 "tool failed"

不在范围:
- requirements 加 psycopg (依赖膨胀, CI infra 决策待用户决)
- db.py get_engine 加 try/except (公共函数, 影响面大, 由调用方各自 handle)

实测 (本地 mock get_engine ImportError):
- ImportError → False ✓ (走 sqlite fallback)
- ConnectionError → False ✓ (其他异常兜底)
- 正常 postgres → True ✓

L2 baseline 3 子项 (A docx / B psycopg / C rollout) 合后, CI L2 应通过率
≥80% 达标。
@Wool-xing Wool-xing merged commit aa25303 into main May 13, 2026
11 checks passed
@Wool-xing Wool-xing deleted the bundle3-L2-B-psycopg-graceful branch May 13, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant