nexusloom 3.9.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agents/__init__.py +7 -0
- agents/checker.py +179 -0
- agents/fact_checker.py +289 -0
- agents/fact_coverage.py +51 -0
- agents/fetcher.py +964 -0
- agents/http_request_agent.py +186 -0
- agents/ingest_agent.py +240 -0
- agents/keyword_extract.py +58 -0
- agents/knowledge_base_agent.py +410 -0
- agents/layout.py +110 -0
- agents/link_check.py +55 -0
- agents/quality_gate.py +708 -0
- agents/readability.py +60 -0
- agents/renderer_agent.py +144 -0
- agents/requirements_analyzer.py +387 -0
- agents/researcher.py +899 -0
- agents/safe_writer_agent.py +282 -0
- agents/tldr.py +60 -0
- agents/transform_agent.py +224 -0
- agents/writer.py +1521 -0
- artesian/__init__.py +2 -0
- artesian/cache.py +72 -0
- artesian/embeddings.py +384 -0
- artesian/env.py +58 -0
- artesian/fast_json.py +51 -0
- artesian/knowledge_base.py +417 -0
- artesian/py.typed +0 -0
- artesian/search_engines.py +1261 -0
- artesian/selectolax_compat.py +151 -0
- artesian/url_guard.py +165 -0
- docpipeline/__init__.py +23 -0
- docpipeline/degradation.py +63 -0
- docpipeline/document_enhancer.py +443 -0
- docpipeline/ingest.py +390 -0
- docpipeline/renderer.py +752 -0
- nexusloom-3.9.2.data/data/pipelines/_quality-tail.lock +35 -0
- nexusloom-3.9.2.data/data/pipelines/_quality-tail.yaml +108 -0
- nexusloom-3.9.2.data/data/pipelines/alert-runbook.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/alert-runbook.yaml +88 -0
- nexusloom-3.9.2.data/data/pipelines/api-digest.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/api-digest.yaml +88 -0
- nexusloom-3.9.2.data/data/pipelines/api-report.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/api-report.yaml +87 -0
- nexusloom-3.9.2.data/data/pipelines/data-qc.lock +29 -0
- nexusloom-3.9.2.data/data/pipelines/data-qc.yaml +113 -0
- nexusloom-3.9.2.data/data/pipelines/deck-brief.lock +29 -0
- nexusloom-3.9.2.data/data/pipelines/deck-brief.yaml +98 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-lean.lock +57 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-lean.yaml +94 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-render.lock +63 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-render.yaml +101 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-verified.lock +57 -0
- nexusloom-3.9.2.data/data/pipelines/docgen-verified.yaml +88 -0
- nexusloom-3.9.2.data/data/pipelines/docgen.lock +57 -0
- nexusloom-3.9.2.data/data/pipelines/docgen.yaml +87 -0
- nexusloom-3.9.2.data/data/pipelines/docreq.lock +63 -0
- nexusloom-3.9.2.data/data/pipelines/docreq.yaml +95 -0
- nexusloom-3.9.2.data/data/pipelines/intel-brief.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/intel-brief.yaml +80 -0
- nexusloom-3.9.2.data/data/pipelines/invoice-extract.lock +29 -0
- nexusloom-3.9.2.data/data/pipelines/invoice-extract.yaml +100 -0
- nexusloom-3.9.2.data/data/pipelines/k8s-patrol.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/k8s-patrol.yaml +88 -0
- nexusloom-3.9.2.data/data/pipelines/kb-brief.lock +29 -0
- nexusloom-3.9.2.data/data/pipelines/kb-brief.yaml +95 -0
- nexusloom-3.9.2.data/data/pipelines/kb-docgen.lock +52 -0
- nexusloom-3.9.2.data/data/pipelines/kb-docgen.yaml +81 -0
- nexusloom-3.9.2.data/data/pipelines/minutes-weekly.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/minutes-weekly.yaml +85 -0
- nexusloom-3.9.2.data/data/pipelines/prompts/default.yaml +125 -0
- nexusloom-3.9.2.data/data/pipelines/prompts/generic-tech.yaml +62 -0
- nexusloom-3.9.2.data/data/pipelines/quality/technical-doc.yaml +49 -0
- nexusloom-3.9.2.data/data/pipelines/quality/tutorial.yaml +35 -0
- nexusloom-3.9.2.data/data/pipelines/spec-cases.lock +23 -0
- nexusloom-3.9.2.data/data/pipelines/spec-cases.yaml +82 -0
- nexusloom-3.9.2.data/data/pipelines/test_pipeline.lock +47 -0
- nexusloom-3.9.2.data/data/pipelines/test_pipeline.yaml +111 -0
- nexusloom-3.9.2.data/data/pipelines/three_pass.lock +52 -0
- nexusloom-3.9.2.data/data/pipelines/three_pass.yaml +125 -0
- nexusloom-3.9.2.dist-info/METADATA +1071 -0
- nexusloom-3.9.2.dist-info/RECORD +142 -0
- nexusloom-3.9.2.dist-info/WHEEL +5 -0
- nexusloom-3.9.2.dist-info/entry_points.txt +3 -0
- nexusloom-3.9.2.dist-info/licenses/LICENSE +21 -0
- nexusloom-3.9.2.dist-info/top_level.txt +6 -0
- pipeline_core/__init__.py +87 -0
- pipeline_core/admin_api.py +1754 -0
- pipeline_core/agent_loader.py +408 -0
- pipeline_core/alert_manager.py +91 -0
- pipeline_core/artifacts.py +155 -0
- pipeline_core/audit.py +64 -0
- pipeline_core/base_agent.py +428 -0
- pipeline_core/bootstrap.py +380 -0
- pipeline_core/cache_manager.py +429 -0
- pipeline_core/checkpoint_manager.py +181 -0
- pipeline_core/circuit_breaker.py +252 -0
- pipeline_core/conditions.py +257 -0
- pipeline_core/config.py +159 -0
- pipeline_core/config_schema.py +123 -0
- pipeline_core/cost_tracker.py +258 -0
- pipeline_core/cron.py +181 -0
- pipeline_core/dag_executor.py +1466 -0
- pipeline_core/deliveries.py +116 -0
- pipeline_core/event_hook.py +429 -0
- pipeline_core/executor_factory.py +220 -0
- pipeline_core/html_export.py +192 -0
- pipeline_core/ids.py +9 -0
- pipeline_core/input_docs.py +38 -0
- pipeline_core/llm_router.py +638 -0
- pipeline_core/mcp_server.py +562 -0
- pipeline_core/message_bus_v3.py +496 -0
- pipeline_core/message_store.py +480 -0
- pipeline_core/naming.py +70 -0
- pipeline_core/observability.py +263 -0
- pipeline_core/openapi_spec.py +459 -0
- pipeline_core/pack_manifest.py +189 -0
- pipeline_core/paths.py +88 -0
- pipeline_core/pipeline.py +1468 -0
- pipeline_core/quality_feedback.py +164 -0
- pipeline_core/rate_limiter.py +178 -0
- pipeline_core/registry.py +513 -0
- pipeline_core/scheduler.py +930 -0
- pipeline_core/sqlite_util.py +69 -0
- pipeline_core/state_paths.py +39 -0
- pipeline_core/streaming.py +370 -0
- pipeline_core/task_queue.py +417 -0
- pipeline_core/telemetry.py +86 -0
- pipeline_core/triggers.py +245 -0
- pipeline_core/version_manager.py +380 -0
- pipeline_core/webhooks.py +218 -0
- pipeline_core/worker.py +208 -0
- run.py +771 -0
- scripts/__init__.py +1 -0
- scripts/checker_rules.yaml +71 -0
- scripts/cleanup_state.py +275 -0
- scripts/convert_ascii.py +335 -0
- scripts/debug_data_flow.py +41 -0
- scripts/format_converter.py +331 -0
- scripts/layout_optimizer.py +387 -0
- scripts/markdown_checker.py +645 -0
- scripts/safe_writer.py +532 -0
- scripts/verify_dist.py +232 -0
agents/__init__.py
ADDED
agents/checker.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Checker Agent - 结构检查插件(改进版)
|
|
3
|
+
=========================================
|
|
4
|
+
改进点:
|
|
5
|
+
- P0 失败不再 raise,改为返回状态码,避免破坏 checkpoint
|
|
6
|
+
- 检查结果附带修复建议
|
|
7
|
+
- 支持从消息 payload 传入 content(不必须读文件)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from pipeline_core.base_agent import AgentStatus, BaseAgent, Message
|
|
14
|
+
|
|
15
|
+
AGENT_NAME = "checker"
|
|
16
|
+
# 随产品发布的内置 Agent:显式声明信任,加载器据此跳过 AST 沙箱检查
|
|
17
|
+
# (信任来自声明本身,不再依赖 core 里写死的名单)
|
|
18
|
+
SANDBOX_TRUSTED = True
|
|
19
|
+
|
|
20
|
+
# 配置契约:类型名用字符串写,好让 Scheduler 用 AST 读取而不必执行本模块
|
|
21
|
+
# (类型名表见 pipeline_core/config_schema.py)
|
|
22
|
+
CONFIG_SCHEMA = {
|
|
23
|
+
"fail_fast": ('bool', False),
|
|
24
|
+
"block_on_p1": ('bool', False),
|
|
25
|
+
}
|
|
26
|
+
AGENT_VERSION = "1.1"
|
|
27
|
+
AGENT_DESC = "结构检查 Agent - P0/P1/P2/P3 分级质检"
|
|
28
|
+
AGENT_AUTHOR = "doc-pipeline"
|
|
29
|
+
AGENT_PRIORITY = 50
|
|
30
|
+
INPUT_TOPICS = ["writer.done", "checker.check", "checker.input"]
|
|
31
|
+
OUTPUT_TOPICS = ["checker.done", "checker.failed"]
|
|
32
|
+
# 产物契约(引擎按此声明组装下游载荷,见 pipeline_core/artifacts.py):只做结构检查,产出问题清单
|
|
33
|
+
PRODUCES: dict = {}
|
|
34
|
+
CONSUMES = ["content"]
|
|
35
|
+
DEPENDENCIES = ["quality_gate"]
|
|
36
|
+
CACHE_TTL = 0
|
|
37
|
+
RESPAWN = False
|
|
38
|
+
AGENT_TAGS = ["check", "quality"]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class CheckerAgent(BaseAgent):
|
|
42
|
+
"""
|
|
43
|
+
结构检查 Agent
|
|
44
|
+
|
|
45
|
+
P0 = 阻断(流水线终止)
|
|
46
|
+
P1 = 严重(需修复后重检)
|
|
47
|
+
P2 = 警告(记录,继续执行)
|
|
48
|
+
P3 = 建议(仅记录)
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
AGENT_NAME = AGENT_NAME
|
|
52
|
+
AGENT_VERSION = AGENT_VERSION
|
|
53
|
+
AGENT_DESC = AGENT_DESC
|
|
54
|
+
AGENT_AUTHOR = AGENT_AUTHOR
|
|
55
|
+
AGENT_PRIORITY = AGENT_PRIORITY
|
|
56
|
+
INPUT_TOPICS = INPUT_TOPICS
|
|
57
|
+
OUTPUT_TOPICS = OUTPUT_TOPICS
|
|
58
|
+
DEPENDENCIES = DEPENDENCIES
|
|
59
|
+
CACHE_TTL = CACHE_TTL
|
|
60
|
+
RESPAWN = RESPAWN
|
|
61
|
+
AGENT_TAGS = AGENT_TAGS
|
|
62
|
+
|
|
63
|
+
def __init__(self, name, meta, config, message_bus, registry):
|
|
64
|
+
super().__init__(name, meta, config, message_bus, registry)
|
|
65
|
+
# 修复 P0:移除手动重复订阅。BaseAgent.__init__ 已通过 INPUT_TOPICS
|
|
66
|
+
# 自动订阅 writer.done / checker.check / checker.input 到 _wrapped_handle。
|
|
67
|
+
# 原 __init__ 再次手动订阅导致每条消息被处理两次:
|
|
68
|
+
# - checker.check 被 handle 处理两次(一次经 _wrapped_handle,一次直接 handle)
|
|
69
|
+
# - writer.done 被 handle + _on_writer_done 各处理一次
|
|
70
|
+
# 现改为:所有 topic 统一经 _wrapped_handle -> handle 分发,
|
|
71
|
+
# handle 内部根据 msg.topic 路由到 _on_writer_done(保留 publish 协议)。
|
|
72
|
+
|
|
73
|
+
def _on_writer_done(self, msg: Message):
|
|
74
|
+
"""Writer 完成后,自动触发检查"""
|
|
75
|
+
payload = msg.payload
|
|
76
|
+
target = payload.get("target") or payload.get("output")
|
|
77
|
+
content = payload.get("content", "")
|
|
78
|
+
task_id = payload.get("task_id", "")
|
|
79
|
+
|
|
80
|
+
if not target and not content:
|
|
81
|
+
return
|
|
82
|
+
|
|
83
|
+
self.report(AgentStatus.RUNNING, f"自动检查 {Path(target).name if target else '内容'}")
|
|
84
|
+
result = self._check(target, content=content)
|
|
85
|
+
self.publish("checker.done", {**result, "task_id": task_id})
|
|
86
|
+
|
|
87
|
+
def handle(self, msg: Message) -> dict | None:
|
|
88
|
+
"""处理消息:根据 topic 分发到自动检查或主动检查"""
|
|
89
|
+
# writer.done 触发自动检查(保留原 _on_writer_done 的 publish("checker.done") 协议)
|
|
90
|
+
if getattr(msg, "topic", "") == "writer.done":
|
|
91
|
+
self._on_writer_done(msg)
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
# 主动检查请求
|
|
95
|
+
self.report(AgentStatus.RUNNING, "开始检查...")
|
|
96
|
+
payload = msg.payload
|
|
97
|
+
target = payload.get("target") or payload.get("file")
|
|
98
|
+
content = payload.get("content", "")
|
|
99
|
+
fix = payload.get("fix", False)
|
|
100
|
+
|
|
101
|
+
if not target and not content:
|
|
102
|
+
return {"status": "error", "message": "未指定文件或内容"}
|
|
103
|
+
|
|
104
|
+
result = self._check(target, content=content, fix=fix,
|
|
105
|
+
block_on_p1=self._block_on_p1(payload))
|
|
106
|
+
|
|
107
|
+
# P0 问题需要阻断,但不 raise,避免破坏 checkpoint
|
|
108
|
+
if result.get("P0", 0) > 0:
|
|
109
|
+
result["status"] = "blocked"
|
|
110
|
+
self.log_error(f"P0 阻断: {result['P0']} 个问题,返回状态码供上游处理")
|
|
111
|
+
self.publish("checker.failed", result)
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
return result
|
|
115
|
+
|
|
116
|
+
def _block_on_p1(self, payload: dict) -> bool:
|
|
117
|
+
"""节点 config 优先于构造期 config(DAG 里 YAML 写的值必须生效)。"""
|
|
118
|
+
cfg = payload.get("config") or {}
|
|
119
|
+
value = cfg.get("block_on_p1")
|
|
120
|
+
if value is None:
|
|
121
|
+
value = self.config.get("block_on_p1", False)
|
|
122
|
+
return bool(value)
|
|
123
|
+
|
|
124
|
+
def _check(self, target: str = None, content: str = "",
|
|
125
|
+
fix: bool = False, block_on_p1: bool | None = None) -> dict:
|
|
126
|
+
"""调用 markdown_checker 核心逻辑"""
|
|
127
|
+
# 读文件内容
|
|
128
|
+
if not content and target:
|
|
129
|
+
if not os.path.exists(target):
|
|
130
|
+
return {"status": "error", "message": f"文件不存在: {target}"}
|
|
131
|
+
with open(target, encoding="utf-8", errors="replace") as f:
|
|
132
|
+
content = f.read()
|
|
133
|
+
|
|
134
|
+
if not content:
|
|
135
|
+
return {"status": "error", "message": "内容为空"}
|
|
136
|
+
|
|
137
|
+
try:
|
|
138
|
+
from scripts.markdown_checker import Checker
|
|
139
|
+
|
|
140
|
+
filepath = target or "<content>"
|
|
141
|
+
checker = Checker(content, filepath, fix=fix)
|
|
142
|
+
result = checker.run()
|
|
143
|
+
|
|
144
|
+
p0 = result["summary"].get("P0_blocking", 0)
|
|
145
|
+
p1 = result["summary"].get("P1_severe", 0)
|
|
146
|
+
p2 = result["summary"].get("P2_warning", 0)
|
|
147
|
+
p3 = result["summary"].get("P3_suggestion", 0)
|
|
148
|
+
|
|
149
|
+
# 契约分层:status 表示"这次检查跑没跑成",verdict 表示"文档有没有问题"。
|
|
150
|
+
# 以前 P1>0 直接把 status 写成 fail,而引擎把 fail 当业务失败 →
|
|
151
|
+
# 下游 layout/safe_writer 全部被跳过。检查器完成了工作却因"发现了问题"
|
|
152
|
+
# 而被判死,这与 pipeline 里 `checker.fail_fast: false` 的意图相反。
|
|
153
|
+
# 需要"P1 也阻断"的场景请显式开 block_on_p1。
|
|
154
|
+
verdict = "pass" if p0 == 0 and p1 == 0 else "fail"
|
|
155
|
+
if block_on_p1 is None:
|
|
156
|
+
block_on_p1 = bool(self.config.get("block_on_p1", False))
|
|
157
|
+
status = "fail" if (verdict == "fail" and block_on_p1) else "ok"
|
|
158
|
+
|
|
159
|
+
self.log_info(f"检查结果: P0={p0} P1={p1} P2={p2} P3={p3} "
|
|
160
|
+
f"[verdict={verdict}{', block_on_p1' if block_on_p1 else ''}]")
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
"status": status,
|
|
164
|
+
"verdict": verdict,
|
|
165
|
+
"target": target or "<content>",
|
|
166
|
+
"P0": p0,
|
|
167
|
+
"P1": p1,
|
|
168
|
+
"P2": p2,
|
|
169
|
+
"P3": p3,
|
|
170
|
+
"total_issues": p0 + p1 + p2 + p3,
|
|
171
|
+
"by_level": result.get("by_level", {}),
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
except ImportError:
|
|
175
|
+
self.log_warning("markdown_checker 未找到,跳过检查")
|
|
176
|
+
return {"status": "skip", "message": "checker 模块未找到"}
|
|
177
|
+
except Exception as e:
|
|
178
|
+
self.log_error(f"检查异常: {e}")
|
|
179
|
+
return {"status": "error", "message": str(e)}
|
agents/fact_checker.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"""
|
|
2
|
+
FactChecker Agent - 轻量事实核查插件(MVP)
|
|
3
|
+
=============================================
|
|
4
|
+
职责:
|
|
5
|
+
- 从上游最终文档中提取"可验证声明"(数字/百分比/年份/版本号等客观陈述)
|
|
6
|
+
- 对照检索源内容做一致性核查:
|
|
7
|
+
· 无 LLM:归一化字符串包含匹配(零成本基线)
|
|
8
|
+
· 有 LLM:批量语义判定(supported / refuted / unverifiable),失败回退字符串匹配
|
|
9
|
+
- 在文档尾部附加「事实核查附注」,并把核查报告写入节点结果
|
|
10
|
+
|
|
11
|
+
设计边界(如实声明):
|
|
12
|
+
- 这是启发式核查而非完整性验证:只能发现"来源不支持的数字类声明",
|
|
13
|
+
不能保证文档事实全量正确。unverifiable ≠ 错误。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import re
|
|
17
|
+
|
|
18
|
+
from pipeline_core.base_agent import AgentStatus, BaseAgent, Message
|
|
19
|
+
|
|
20
|
+
AGENT_NAME = "fact_checker"
|
|
21
|
+
# 随产品发布的内置 Agent:显式声明信任,加载器据此跳过 AST 沙箱检查
|
|
22
|
+
# (信任来自声明本身,不再依赖 core 里写死的名单)
|
|
23
|
+
SANDBOX_TRUSTED = True
|
|
24
|
+
AGENT_VERSION = "1.0"
|
|
25
|
+
AGENT_DESC = "事实核查 Agent - 可验证声明提取 + 来源一致性核查"
|
|
26
|
+
AGENT_AUTHOR = "doc-pipeline"
|
|
27
|
+
AGENT_PRIORITY = 45
|
|
28
|
+
INPUT_TOPICS = ["checker.done", "fact_checker.check", "fact_checker.input"]
|
|
29
|
+
OUTPUT_TOPICS = ["fact_checker.done"]
|
|
30
|
+
# 产物契约(引擎按此声明组装下游载荷,见 pipeline_core/artifacts.py):核查后可能补注来源,重新导出正文
|
|
31
|
+
PRODUCES = {"content": "last"}
|
|
32
|
+
CONSUMES = ["content", "results"]
|
|
33
|
+
DEPENDENCIES = ["checker"]
|
|
34
|
+
CACHE_TTL = 0
|
|
35
|
+
RESPAWN = False
|
|
36
|
+
AGENT_TAGS = ["check", "facts"]
|
|
37
|
+
|
|
38
|
+
# 声明提取上限(控制 LLM 成本)
|
|
39
|
+
MAX_CLAIMS = 30
|
|
40
|
+
# 句子最小长度(过滤碎片)
|
|
41
|
+
_MIN_SENT_LEN = 12
|
|
42
|
+
|
|
43
|
+
# 数字类声明模式:百分比、带单位数值、年份、版本号、倍数/规模
|
|
44
|
+
# 版本号模式(C14 收紧):裸小数(如"延迟 0.5 秒"的 0.5、"准确率 99.5%")
|
|
45
|
+
# 不再误判为版本声明——要求 v 前缀或前置英文词(Kafka 3.5 / Redis 5.0),
|
|
46
|
+
# 中文单位数值已由第 2 条模式覆盖,不会漏提取
|
|
47
|
+
_CLAIM_PATTERNS = [
|
|
48
|
+
re.compile(r"\d+(?:\.\d+)?%"),
|
|
49
|
+
re.compile(r"\d+(?:\.\d+)?\s*(?:万|亿|千万|百万|k|K|M|GB|MB|TB|ms|秒|分钟|小时|天|年|人|台|次|元|美元|美金)"),
|
|
50
|
+
re.compile(r"\b(?:19|20)\d{2}\s*年"),
|
|
51
|
+
re.compile(r"\b[Vv]\d+\.\d+(?:\.\d+)?"),
|
|
52
|
+
# 尾部用 (?!\d) 而非 \b:CJK 字符也是 \w,"Python 3.11版本" 的 "3.11版"
|
|
53
|
+
# 无词边界,尾部 \b 会导致整句漏提取
|
|
54
|
+
re.compile(r"\b[A-Za-z]{2,}\s+\d+\.\d+(?:\.\d+)?(?!\d)"),
|
|
55
|
+
re.compile(r"\d+\s*(?:倍|个并发|万级|亿级)"),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
_SENT_SPLIT = re.compile(r"[。!?!?\n]")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _normalize(text: str) -> str:
|
|
62
|
+
"""归一化:去空白/中英文标点差异,用于包含匹配。
|
|
63
|
+
|
|
64
|
+
C14:数字间小数点必须保留——原实现把 '3.5' 去点成 '35',来源中真实的
|
|
65
|
+
'35' 会与之匹配,产生跨值误判 supported。先用占位符保护 \\d.\\d,
|
|
66
|
+
剥离标点后还原,声明与语料两侧归一化保持一致。
|
|
67
|
+
"""
|
|
68
|
+
protected = re.sub(r"(?<=\d)\.(?=\d)", "\x00", text)
|
|
69
|
+
stripped = re.sub(
|
|
70
|
+
r"[\s,,。.、;;::!!??'\"“”‘’()()\[\]【】《》<>〈〉\-—~~·…]+",
|
|
71
|
+
"", protected)
|
|
72
|
+
return stripped.replace("\x00", ".")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class FactCheckerAgent(BaseAgent):
|
|
76
|
+
"""事实核查 Agent(MVP)"""
|
|
77
|
+
|
|
78
|
+
AGENT_NAME = AGENT_NAME
|
|
79
|
+
AGENT_VERSION = AGENT_VERSION
|
|
80
|
+
AGENT_DESC = AGENT_DESC
|
|
81
|
+
AGENT_AUTHOR = AGENT_AUTHOR
|
|
82
|
+
AGENT_PRIORITY = AGENT_PRIORITY
|
|
83
|
+
INPUT_TOPICS = INPUT_TOPICS
|
|
84
|
+
OUTPUT_TOPICS = OUTPUT_TOPICS
|
|
85
|
+
DEPENDENCIES = DEPENDENCIES
|
|
86
|
+
CACHE_TTL = CACHE_TTL
|
|
87
|
+
RESPAWN = RESPAWN
|
|
88
|
+
AGENT_TAGS = AGENT_TAGS
|
|
89
|
+
|
|
90
|
+
def __init__(self, name, meta, config, message_bus, registry):
|
|
91
|
+
super().__init__(name, meta, config, message_bus, registry)
|
|
92
|
+
self._max_claims = int(config.get("max_claims", MAX_CLAIMS))
|
|
93
|
+
# LLM 核查总时长预算(秒):必须显著小于节点 timeout,
|
|
94
|
+
# 防止多供应商 failover 叠加导致 bus.request 整体超时而丢失核查结果
|
|
95
|
+
self._llm_timeout = int(config.get("llm_timeout", 30))
|
|
96
|
+
|
|
97
|
+
# ─── 主入口 ──────────────────────────────
|
|
98
|
+
|
|
99
|
+
def handle(self, msg: Message) -> dict:
|
|
100
|
+
payload = getattr(msg, "payload", {}) or {}
|
|
101
|
+
content = payload.get("content", "") or ""
|
|
102
|
+
sources = self._collect_sources(payload.get("dependencies_results") or {})
|
|
103
|
+
|
|
104
|
+
if not content:
|
|
105
|
+
return {"status": "skip", "message": "无上游内容,跳过核查"}
|
|
106
|
+
|
|
107
|
+
claims = self._extract_claims(content)
|
|
108
|
+
if not claims:
|
|
109
|
+
self.report(AgentStatus.RUNNING, "未发现可验证的数字类声明")
|
|
110
|
+
return {
|
|
111
|
+
"status": "ok",
|
|
112
|
+
"content": content,
|
|
113
|
+
"claims": [],
|
|
114
|
+
"summary": {"total": 0, "verified": 0, "unverified": 0},
|
|
115
|
+
"report_markdown": "",
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
verdicts = self._verify_claims(claims, sources)
|
|
119
|
+
|
|
120
|
+
verified = sum(1 for v in verdicts if v["verdict"] == "supported")
|
|
121
|
+
unverified = sum(1 for v in verdicts if v["verdict"] != "supported")
|
|
122
|
+
|
|
123
|
+
report_md = self._render_report(verdicts)
|
|
124
|
+
annotated = content + "\n\n## 事实核查附注\n\n" + report_md if unverified else content
|
|
125
|
+
|
|
126
|
+
self.log_info(
|
|
127
|
+
f"事实核查完成: {len(claims)} 条声明, "
|
|
128
|
+
f"{verified} 条来源支持, {unverified} 条未能核实")
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
"status": "ok",
|
|
132
|
+
"content": annotated,
|
|
133
|
+
"claims": verdicts,
|
|
134
|
+
"summary": {
|
|
135
|
+
"total": len(claims),
|
|
136
|
+
"verified": verified,
|
|
137
|
+
"unverified": unverified,
|
|
138
|
+
},
|
|
139
|
+
"report_markdown": report_md,
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
# ─── 声明提取 ──────────────────────────────
|
|
143
|
+
|
|
144
|
+
def _extract_claims(self, content: str) -> list[str]:
|
|
145
|
+
"""提取含数字特征的句子(去重、截断到 max_claims)"""
|
|
146
|
+
claims: list[str] = []
|
|
147
|
+
seen: set[str] = set()
|
|
148
|
+
# 剔除代码块与引用链接行,避免误报
|
|
149
|
+
cleaned = re.sub(r"```.*?```", "", content, flags=re.DOTALL)
|
|
150
|
+
cleaned = re.sub(r"^>.*$", "", cleaned, flags=re.MULTILINE)
|
|
151
|
+
|
|
152
|
+
for raw in _SENT_SPLIT.split(cleaned):
|
|
153
|
+
sent = raw.strip()
|
|
154
|
+
if len(sent) < _MIN_SENT_LEN or len(sent) > 300:
|
|
155
|
+
continue
|
|
156
|
+
if any(p.search(sent) for p in _CLAIM_PATTERNS):
|
|
157
|
+
key = _normalize(sent)[:80]
|
|
158
|
+
if key and key not in seen:
|
|
159
|
+
seen.add(key)
|
|
160
|
+
claims.append(sent)
|
|
161
|
+
if len(claims) >= self._max_claims:
|
|
162
|
+
break
|
|
163
|
+
return claims
|
|
164
|
+
|
|
165
|
+
# ─── 来源收集 ──────────────────────────────
|
|
166
|
+
|
|
167
|
+
def _collect_sources(self, dep_results: dict) -> list[str]:
|
|
168
|
+
"""从依赖节点结果中收集检索源文本(results/articles/text/snippet/content)"""
|
|
169
|
+
texts: list[str] = []
|
|
170
|
+
|
|
171
|
+
def _walk(obj, depth: int = 0):
|
|
172
|
+
if depth > 4 or obj is None:
|
|
173
|
+
return
|
|
174
|
+
if isinstance(obj, dict):
|
|
175
|
+
for key in ("text", "snippet", "content", "summary"):
|
|
176
|
+
val = obj.get(key)
|
|
177
|
+
if isinstance(val, str) and len(val) > 50:
|
|
178
|
+
texts.append(val)
|
|
179
|
+
for val in obj.values():
|
|
180
|
+
if isinstance(val, (list, dict)):
|
|
181
|
+
_walk(val, depth + 1)
|
|
182
|
+
elif isinstance(obj, list):
|
|
183
|
+
for item in obj:
|
|
184
|
+
_walk(item, depth + 1)
|
|
185
|
+
|
|
186
|
+
_walk(dep_results)
|
|
187
|
+
return texts
|
|
188
|
+
|
|
189
|
+
# ─── 核查 ──────────────────────────────
|
|
190
|
+
|
|
191
|
+
def _verify_claims(self, claims: list[str], sources: list[str]) -> list[dict]:
|
|
192
|
+
"""先尝试 LLM 语义判定,失败回退归一化包含匹配"""
|
|
193
|
+
if sources and self._llm_available():
|
|
194
|
+
try:
|
|
195
|
+
return self._verify_with_llm(claims, sources)
|
|
196
|
+
except Exception as e:
|
|
197
|
+
self.log_warning(f"LLM 核查失败,回退字符串匹配: {e}")
|
|
198
|
+
return self._verify_by_matching(claims, sources)
|
|
199
|
+
|
|
200
|
+
def _llm_available(self) -> bool:
|
|
201
|
+
try:
|
|
202
|
+
from pipeline_core.llm_router import get_router
|
|
203
|
+
router = get_router()
|
|
204
|
+
return bool(router and router.get_active_providers())
|
|
205
|
+
except Exception:
|
|
206
|
+
return False
|
|
207
|
+
|
|
208
|
+
def _verify_by_matching(self, claims: list[str], sources: list[str]) -> list[dict]:
|
|
209
|
+
"""零成本基线:声明归一化后在来源全文中查找数字特征串"""
|
|
210
|
+
corpus = _normalize(" ".join(sources))
|
|
211
|
+
results = []
|
|
212
|
+
for claim in claims:
|
|
213
|
+
# 取声明中的数字特征片段作为锚点
|
|
214
|
+
anchors = [m.group() for p in _CLAIM_PATTERNS for m in p.finditer(claim)]
|
|
215
|
+
supported = bool(anchors) and all(
|
|
216
|
+
_normalize(anchor) in corpus for anchor in anchors[:3])
|
|
217
|
+
results.append({
|
|
218
|
+
"claim": claim,
|
|
219
|
+
"verdict": "supported" if supported else "unverified",
|
|
220
|
+
"method": "string-match",
|
|
221
|
+
})
|
|
222
|
+
return results
|
|
223
|
+
|
|
224
|
+
def _verify_with_llm(self, claims: list[str], sources: list[str]) -> list[dict]:
|
|
225
|
+
"""LLM 批量语义判定(带总时长预算,防止多供应商 failover 超过节点 timeout)"""
|
|
226
|
+
import time
|
|
227
|
+
|
|
228
|
+
from pipeline_core.llm_router import get_router
|
|
229
|
+
router = get_router()
|
|
230
|
+
|
|
231
|
+
deadline = time.time() + self._llm_timeout
|
|
232
|
+
source_excerpt = "\n---\n".join(sources)[:12000]
|
|
233
|
+
claims_text = "\n".join(f"{i + 1}. {c}" for i, c in enumerate(claims))
|
|
234
|
+
prompt = (
|
|
235
|
+
"你是事实核查员。根据以下检索来源资料,逐条判断声明的真实性。\n"
|
|
236
|
+
f"来源资料:\n{source_excerpt}\n\n待核查声明:\n{claims_text}\n\n"
|
|
237
|
+
'严格输出 JSON(不要其他文字):\n'
|
|
238
|
+
'{"verdicts": [{"index": 1, "verdict": "supported|refuted|unverifiable", '
|
|
239
|
+
'"note": "一句话依据"}]}'
|
|
240
|
+
)
|
|
241
|
+
remaining = deadline - time.time()
|
|
242
|
+
if remaining <= 1:
|
|
243
|
+
raise ValueError("LLM 核查预算已耗尽")
|
|
244
|
+
content, _provider = router.chat(
|
|
245
|
+
[{"role": "user", "content": prompt}],
|
|
246
|
+
max_tokens=2048, temperature=0.1,
|
|
247
|
+
timeout=max(5, int(remaining)))
|
|
248
|
+
|
|
249
|
+
match = re.search(r"\{[\s\S]*\}", content)
|
|
250
|
+
if not match:
|
|
251
|
+
raise ValueError("LLM 未返回 JSON")
|
|
252
|
+
data = __import__("json").loads(match.group())
|
|
253
|
+
|
|
254
|
+
by_index = {v.get("index"): v for v in data.get("verdicts", [])}
|
|
255
|
+
# C14:索引全体无效(LLM 用 0 基编号、整体偏移或空 verdicts)时,
|
|
256
|
+
# 不能静默把全部声明降级为 unverifiable——抛错走 _verify_claims
|
|
257
|
+
# 的既有回退路径(字符串匹配),保持核查可用
|
|
258
|
+
valid = sum(1 for idx in by_index
|
|
259
|
+
if isinstance(idx, int) and 1 <= idx <= len(claims))
|
|
260
|
+
if not by_index or valid == 0:
|
|
261
|
+
raise ValueError("LLM 返回的 verdicts 索引全部无效")
|
|
262
|
+
results = []
|
|
263
|
+
for i, claim in enumerate(claims):
|
|
264
|
+
v = by_index.get(i + 1, {})
|
|
265
|
+
verdict = v.get("verdict", "unverifiable")
|
|
266
|
+
if verdict not in ("supported", "refuted", "unverifiable"):
|
|
267
|
+
verdict = "unverifiable"
|
|
268
|
+
results.append({
|
|
269
|
+
"claim": claim,
|
|
270
|
+
"verdict": verdict,
|
|
271
|
+
"note": v.get("note", ""),
|
|
272
|
+
"method": "llm",
|
|
273
|
+
})
|
|
274
|
+
return results
|
|
275
|
+
|
|
276
|
+
# ─── 报告 ──────────────────────────────
|
|
277
|
+
|
|
278
|
+
def _render_report(self, verdicts: list[dict]) -> str:
|
|
279
|
+
lines = [
|
|
280
|
+
f"> 本附注由 fact_checker 自动生成(启发式核查,unverifiable 不代表错误)。"
|
|
281
|
+
f"共核查 {len(verdicts)} 条数字类声明。",
|
|
282
|
+
"",
|
|
283
|
+
]
|
|
284
|
+
for v in verdicts:
|
|
285
|
+
icon = {"supported": "✅", "refuted": "❌", "unverifiable": "❓"}.get(
|
|
286
|
+
v["verdict"], "❓")
|
|
287
|
+
note = f" — {v['note']}" if v.get("note") else ""
|
|
288
|
+
lines.append(f"- {icon} `{v['verdict']}` {v['claim']}{note}")
|
|
289
|
+
return "\n".join(lines)
|
agents/fact_coverage.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""fact_coverage -- built-in agent (2026-10-11 folded in from examples/plugin-*).
|
|
2
|
+
|
|
3
|
+
Built-in: declares SANDBOX_TRUSTED like the other shipped agents
|
|
4
|
+
(pure stdlib, nothing on the loader AST blacklist).
|
|
5
|
+
|
|
6
|
+
Purpose: measure how much of a document is backed by concrete evidence --
|
|
7
|
+
digits, code blocks, tables, links -- versus pure prose. Quality gates can
|
|
8
|
+
cite it as a dimension: an all-prose doc reads low, which is exactly the
|
|
9
|
+
FP-1 shape this repo fought before.
|
|
10
|
+
"""
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
from pipeline_core.base_agent import AgentStatus, BaseAgent, Message
|
|
14
|
+
|
|
15
|
+
SANDBOX_TRUSTED = True
|
|
16
|
+
AGENT_NAME = "fact_coverage"
|
|
17
|
+
AGENT_VERSION = "0.1.0"
|
|
18
|
+
AGENT_DESC = "built-in agent: evidence-density dimension for quality gates"
|
|
19
|
+
AGENT_AUTHOR = "doc-pipeline-example"
|
|
20
|
+
AGENT_PRIORITY = 50
|
|
21
|
+
INPUT_TOPICS = ["fact_coverage.input", "writer.done"]
|
|
22
|
+
OUTPUT_TOPICS = ["fact_coverage.done"]
|
|
23
|
+
|
|
24
|
+
_DIGIT_RE = re.compile(r"\d+(?:\.\d+)?")
|
|
25
|
+
_CODE_FENCE_RE = re.compile(r"```")
|
|
26
|
+
_TABLE_ROW_RE = re.compile(r"^\s*\|.*\|\s*$", re.M)
|
|
27
|
+
_LINK_RE = re.compile(r"https?://[^\s)\]]+")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class FactCoverageAgent(BaseAgent):
|
|
31
|
+
"""Report evidence-density signals over payload['content']."""
|
|
32
|
+
|
|
33
|
+
def handle(self, msg: Message) -> dict | None:
|
|
34
|
+
content = msg.payload.get("content") or ""
|
|
35
|
+
chars = len(content) or 1
|
|
36
|
+
numbers = len(_DIGIT_RE.findall(content))
|
|
37
|
+
fences = len(_CODE_FENCE_RE.findall(content)) // 2
|
|
38
|
+
table_rows = len(_TABLE_ROW_RE.findall(content))
|
|
39
|
+
links = len(_LINK_RE.findall(content))
|
|
40
|
+
result = {
|
|
41
|
+
"chars": len(content),
|
|
42
|
+
"numbers": numbers,
|
|
43
|
+
"numbers_per_1kb": round(numbers / chars * 1000, 2),
|
|
44
|
+
"code_blocks": fences,
|
|
45
|
+
"table_rows": table_rows,
|
|
46
|
+
"links": links,
|
|
47
|
+
}
|
|
48
|
+
self.report(AgentStatus.RUNNING, f"{numbers} numbers, {fences} code blocks")
|
|
49
|
+
self.publish("fact_coverage.done", {
|
|
50
|
+
"task_id": msg.payload.get("task_id", ""), **result})
|
|
51
|
+
return {"status": "ok", **result}
|