argo-search 2.14.0 → 2.15.0
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.
- package/AGENTS.md +8 -6
- package/README.es.md +56 -824
- package/README.ja.md +55 -825
- package/README.ko.md +55 -823
- package/README.md +128 -868
- package/README.zh.md +206 -0
- package/SKILL.md +17 -18
- package/assets/readme/counts.json +1 -1
- package/assets/readme/hero.svg +23 -23
- package/backends/concept_vectors.bin +0 -0
- package/backends/domain_profiles.json +66 -0
- package/backends/engine_concept_vectors.json +1525 -1525
- package/backends/engine_registry.yaml +193 -46
- package/backends/quota_profiles.json +110 -0
- package/bin/argo +30 -17
- package/config.yaml +196 -36
- package/docs/ADDING_NEW_ENGINE.md +10 -0
- package/docs/ARGO_INTRO.md +1 -1
- package/docs/CHANGELOG.md +45 -0
- package/docs/ENGINE_CATALOG.md +128 -112
- package/docs/MCP_SETUP.md +2 -2
- package/docs/RELEASE_NOTES_v2.14.0.md +12 -1
- package/docs/RELEASE_NOTES_v2.14.1.md +80 -0
- package/docs/RELEASE_NOTES_v2.15.0.md +50 -0
- package/docs/RELEASING.md +53 -1
- package/engines/specs/ai_labs.yaml +89 -0
- package/engines/specs/artic.yaml +11 -1
- package/engines/specs/bilibili_hot.yaml +11 -1
- package/engines/specs/brave.yaml +3 -3
- package/engines/specs/cisa_alerts.yaml +40 -0
- package/engines/specs/cleveland.yaml +15 -1
- package/engines/specs/clinicaltrials.yaml +16 -1
- package/engines/specs/cn_ai_news.yaml +10 -8
- package/engines/specs/cnii.yaml +15 -4
- package/engines/specs/deezer.yaml +12 -1
- package/engines/specs/digup.yaml +11 -6
- package/engines/specs/doaj.yaml +14 -1
- package/engines/specs/douyin_hot.yaml +11 -1
- package/engines/specs/dw_news.yaml +4 -2
- package/engines/specs/endoflife.yaml +16 -1
- package/engines/specs/eng_blogs.yaml +63 -0
- package/engines/specs/eu_opendata.yaml +11 -1
- package/engines/specs/europeana.yaml +22 -5
- package/engines/specs/feed.yaml +62 -0
- package/engines/specs/felo.yaml +3 -3
- package/engines/specs/figshare.yaml +12 -1
- package/engines/specs/firecrawl.yaml +3 -0
- package/engines/specs/fourget.yaml +5 -2
- package/engines/specs/france24.yaml +6 -4
- package/engines/specs/fxtwitter.yaml +29 -6
- package/engines/specs/grepapp_mcp.yaml +62 -0
- package/engines/specs/guardian_rss.yaml +6 -4
- package/engines/specs/hackernews.yaml +17 -2
- package/engines/specs/hal.yaml +19 -1
- package/engines/specs/hf_datasets.yaml +59 -0
- package/engines/specs/hf_papers.yaml +17 -1
- package/engines/specs/itotii.yaml +13 -1
- package/engines/specs/jikan.yaml +12 -10
- package/engines/specs/kaggle.yaml +5 -3
- package/engines/specs/lobsters.yaml +41 -0
- package/engines/specs/local_seek.yaml +6 -0
- package/engines/specs/maven.yaml +61 -0
- package/engines/specs/nasa_cmr.yaml +12 -1
- package/engines/specs/nuget.yaml +57 -0
- package/engines/specs/open_meteo.yaml +1 -1
- package/engines/specs/redskill.yaml +2 -0
- package/engines/specs/searxng.yaml +4 -2
- package/engines/specs/sec_edgar.yaml +8 -3
- package/engines/specs/tatoeba.yaml +14 -1
- package/engines/specs/tinyfish.yaml +2 -0
- package/engines/specs/tinyfish_news.yaml +2 -0
- package/engines/specs/tinyfish_paper.yaml +2 -0
- package/engines/specs/toutiao_hot.yaml +11 -1
- package/engines/specs/train.yaml +3 -0
- package/engines/specs/tvmaze.yaml +5 -1
- package/engines/specs/twitter.yaml +4 -0
- package/engines/specs/unpaywall.yaml +6 -0
- package/engines/specs/usda.yaml +14 -1
- package/engines/specs/vam.yaml +17 -3
- package/engines/specs/weibo_hot.yaml +11 -1
- package/engines/specs/wolframalpha.yaml +3 -3
- package/engines/specs/xiaohongshu.yaml +2 -0
- package/engines/specs/zhihu.yaml +3 -18
- package/engines/specs/zhihu_kb.yaml +18 -0
- package/engines/specs/zhihu_questions.yaml +19 -0
- package/package.json +4 -2
- package/packages/dsh-plugin/README.md +9 -14
- package/packages/dsh-plugin/dsh/client.js +467 -1024
- package/packages/dsh-plugin/dsh/diagnostics.js +71 -20
- package/packages/dsh-plugin/dsh/native-tools.mjs +61 -113
- package/packages/dsh-plugin/dsh/runner.js +2 -0
- package/packages/dsh-plugin/dsh/wide-research.js +1 -1
- package/packages/dsh-plugin/package.json +8 -3
- package/packages/dsh-plugin/scripts/sync-local.mjs +87 -0
- package/packages/dsh-plugin/scripts/sync-local.sh +13 -1
- package/packages/dsh-plugin/test/diagnostics.test.js +38 -1
- package/packages/dsh-plugin/test/size-gate.test.js +2 -9
- package/scripts/adaptive.py +7 -1
- package/scripts/archive_run.py +112 -53
- package/scripts/argo_paths.py +40 -3
- package/scripts/badcase_ledger.py +153 -10
- package/scripts/build_concept_table.py +112 -55
- package/scripts/build_release_assets.sh +43 -15
- package/scripts/cache.py +5 -1
- package/scripts/caliber_isolation.py +60 -23
- package/scripts/circuit_breaker.py +56 -8
- package/scripts/cli_io.py +20 -2
- package/scripts/config_cache.py +31 -0
- package/scripts/daemon.py +146 -51
- package/scripts/dormant_ledger.py +239 -0
- package/scripts/engine_dispatch.py +3 -3
- package/scripts/engine_env.py +182 -1
- package/scripts/engine_families.py +50 -7
- package/scripts/engine_states.py +9 -51
- package/scripts/engine_validate.py +150 -6
- package/scripts/engines.py +15 -67
- package/scripts/engines_base.py +68 -124
- package/scripts/engines_builders.py +0 -51
- package/scripts/engines_builders_batch10.py +346 -100
- package/scripts/engines_builders_batch11.py +2 -75
- package/scripts/engines_builders_batch9.py +15 -228
- package/scripts/engines_builders_cn.py +0 -194
- package/scripts/engines_builders_feeds.py +203 -35
- package/scripts/engines_builders_intl.py +15 -443
- package/scripts/engines_builders_pixiv.py +2 -16
- package/scripts/engines_builders_science.py +0 -85
- package/scripts/engines_builders_search.py +7 -14
- package/scripts/engines_builders_tech.py +5 -112
- package/scripts/engines_builders_weather.py +236 -0
- package/scripts/engines_fields.py +91 -0
- package/scripts/engines_http.py +135 -0
- package/scripts/engines_mcp.py +383 -0
- package/scripts/engines_parsers.py +196 -27
- package/scripts/engines_schema_drift.py +260 -0
- package/scripts/evidence.py +13 -0
- package/scripts/evidence_loop.py +93 -5
- package/scripts/fetch_jsrun.py +15 -11
- package/scripts/fetch_v3.py +28 -28
- package/scripts/fulltext_store.py +42 -1
- package/scripts/fxtwitter_api.py +8 -7
- package/scripts/gen_engine_catalog.py +4 -4
- package/scripts/gen_native_tools.py +2 -1
- package/scripts/general_pool.py +15 -4
- package/scripts/health_check.py +1 -1
- package/scripts/link_source.py +65 -24
- package/scripts/local_intent.py +51 -15
- package/scripts/local_seek.py +37 -4
- package/scripts/local_seek_adapter.py +57 -17
- package/scripts/mcp_handlers.py +173 -99
- package/scripts/mcp_handlers_surface.py +6 -18
- package/scripts/mcp_server.py +1 -1
- package/scripts/mcp_tools.py +143 -61
- package/scripts/mcp_transport.py +42 -10
- package/scripts/min_version_scan.py +7 -3
- package/scripts/network_aware.py +7 -13
- package/scripts/pre-commit +57 -31
- package/scripts/query_understanding.py +30 -6
- package/scripts/quota.py +6 -1
- package/scripts/rank_signals.py +9 -0
- package/scripts/recompute.py +15 -5
- package/scripts/recovery.py +21 -15
- package/scripts/release.py +29 -32
- package/scripts/release_check.py +6 -26
- package/scripts/replay_eval.py +2 -2
- package/scripts/research_cli.py +40 -0
- package/scripts/research_notes.py +368 -0
- package/scripts/route.py +32 -70
- package/scripts/route_audit.py +259 -0
- package/scripts/route_combo.py +10 -4
- package/scripts/route_domains.py +150 -23
- package/scripts/route_lang.py +20 -7
- package/scripts/route_log.py +26 -9
- package/scripts/route_policy.py +91 -7
- package/scripts/route_telemetry_export.py +199 -0
- package/scripts/rrf_classic.py +5 -22
- package/scripts/search.py +2 -1
- package/scripts/search_attribution.py +33 -10
- package/scripts/search_cli.py +44 -7
- package/scripts/search_entry.py +5 -1
- package/scripts/search_output.py +5 -4
- package/scripts/search_pipeline.py +12 -0
- package/scripts/search_rank.py +21 -11
- package/scripts/secret_guard.py +106 -0
- package/scripts/semantic_evidence.py +539 -117
- package/scripts/settings_page.py +83 -11
- package/scripts/social_engines/xiaohongshu_engine.py +2 -1
- package/scripts/social_engines/zhihu_engine.py +251 -43
- package/scripts/sogou_weixin_browser.py +5 -6
- package/scripts/source_audit.py +77 -0
- package/scripts/source_ledger.py +2 -15
- package/scripts/source_selection.py +45 -20
- package/scripts/straggler_policy.py +1 -2
- package/scripts/sync_backends.py +24 -0
- package/scripts/tfidf_router.py +23 -11
- package/scripts/time_utils.py +54 -1
- package/scripts/train.py +68 -2
- package/scripts/usage_log.py +26 -1
- package/sub-skills/argo-wide-research/SKILL.md +68 -0
- package/sub-skills/ego-search/SKILL.md +1 -1
- package/sub-skills/local-seek/scripts/seek_fts.py +67 -4
- package/README.en.md +0 -826
- package/scripts/caliber_dedupe.py +0 -66
- package/scripts/engines_builders_batch16.py +0 -186
- package/scripts/health_probe.py +0 -253
- package/scripts/image_vision/vision_probe +0 -0
- package/scripts/social_engines/twitter_engine.py +0 -514
- package/scripts/sync_installs.py +0 -29
package/scripts/search_output.py
CHANGED
|
@@ -12,6 +12,7 @@ from search_rank import filter_results_by_domains
|
|
|
12
12
|
from search_types import slot_record as dataclass
|
|
13
13
|
from snippet_clean import clean_snippet
|
|
14
14
|
import engine_states # 状态词表唯一真源(2026-11-09 收口)
|
|
15
|
+
from except_sets import OPT_IMPORT, SHAPE_BENIGN # 收窄词汇表(2026-11-13 裸吞收口)
|
|
15
16
|
from typing import Any
|
|
16
17
|
from output_transparency import ( # noqa: F401
|
|
17
18
|
_AGGREGATOR_HOSTS,
|
|
@@ -712,7 +713,7 @@ def shape_response(ctx: _ShapeContext, result: dict[str, Any]) -> dict[str, Any]
|
|
|
712
713
|
result.setdefault("limitations", []).append(
|
|
713
714
|
f"证据门控本次跳过({type(e).__name__}):fetch_required/"
|
|
714
715
|
"evidence_loop 不可用,高后果域结论请自行核验")
|
|
715
|
-
except
|
|
716
|
+
except SHAPE_BENIGN:
|
|
716
717
|
pass
|
|
717
718
|
|
|
718
719
|
# 上游权威等级回填(DeepSeek 0-4):单引擎/对冲路径不走五维精排,
|
|
@@ -768,7 +769,7 @@ def shape_response(ctx: _ShapeContext, result: dict[str, Any]) -> dict[str, Any]
|
|
|
768
769
|
from local_seek import local_seek_last_skipped
|
|
769
770
|
if local_seek_last_skipped():
|
|
770
771
|
result["local_seek_skipped"] = True
|
|
771
|
-
except
|
|
772
|
+
except OPT_IMPORT + SHAPE_BENIGN:
|
|
772
773
|
pass
|
|
773
774
|
if local_hits:
|
|
774
775
|
_seen_urls = {r.get("url") for r in (result.get("results") or [])
|
|
@@ -848,7 +849,7 @@ def shape_response(ctx: _ShapeContext, result: dict[str, Any]) -> dict[str, Any]
|
|
|
848
849
|
sanitize_results_boundary)
|
|
849
850
|
sanitize_results_boundary(result.get("results"))
|
|
850
851
|
result["content_trust"] = CONTENT_TRUST_UNTRUSTED
|
|
851
|
-
except
|
|
852
|
+
except OPT_IMPORT + SHAPE_BENIGN:
|
|
852
853
|
pass
|
|
853
854
|
# 搜索期告警(issue #34 第 7 条):--json 下告警不能留在 stderr(调用方
|
|
854
855
|
# 2>&1 合并会破坏 JSON 解析),统一收进响应的 warnings[] 字段。住在
|
|
@@ -858,6 +859,6 @@ def shape_response(ctx: _ShapeContext, result: dict[str, Any]) -> dict[str, Any]
|
|
|
858
859
|
warns = [w.strip() for w in drain_search_warnings() if w.strip()]
|
|
859
860
|
if warns:
|
|
860
861
|
result.setdefault("warnings", []).extend(warns)
|
|
861
|
-
except
|
|
862
|
+
except OPT_IMPORT + SHAPE_BENIGN:
|
|
862
863
|
pass
|
|
863
864
|
return result
|
|
@@ -79,6 +79,7 @@ import engine_states
|
|
|
79
79
|
from stage_timing import tick as _tick, tock as _tock
|
|
80
80
|
from time_utils import apply_time_window as _apply_time_window
|
|
81
81
|
from time_utils import sort_results_by_time as _sort_results
|
|
82
|
+
from time_utils import weather_chronological
|
|
82
83
|
|
|
83
84
|
|
|
84
85
|
@dataclass(frozen=True)
|
|
@@ -641,6 +642,17 @@ def postprocess(req: _SearchRequest, run: _SearchRun, hooks: Any) -> _SearchRun:
|
|
|
641
642
|
|
|
642
643
|
if merged:
|
|
643
644
|
merged = _apply_consensus_and_sort(merged, max_results)
|
|
645
|
+
# 天气结果按时间序收口:当前天气在前,逐日预报升序。放在最终截断
|
|
646
|
+
# 之后,避免五维 rerank 的新颖性把最远一天顶到最前(见函数 docstring)。
|
|
647
|
+
# 显式 `--engine open_meteo` 时 domain 会被置成 general,所以判据同时
|
|
648
|
+
# 看域、引擎列表与结果来源,不能只认 domain。
|
|
649
|
+
_weather_scope = (
|
|
650
|
+
domain == "weather_query"
|
|
651
|
+
or "open_meteo" in (engines or [])
|
|
652
|
+
or (bool(merged) and all(r.get("source") == "open_meteo" for r in merged))
|
|
653
|
+
)
|
|
654
|
+
if _weather_scope:
|
|
655
|
+
merged = weather_chronological(merged)
|
|
644
656
|
|
|
645
657
|
_attach_selection_signals(merged, mode, depth)
|
|
646
658
|
|
package/scripts/search_rank.py
CHANGED
|
@@ -28,6 +28,7 @@ import engine_states # 状态词表唯一真源(2026-11-09 收口)
|
|
|
28
28
|
|
|
29
29
|
from engine_env import env_flag, get_env
|
|
30
30
|
from url_canon import canonical_url as _canonical_url_impl
|
|
31
|
+
from except_sets import IO_BENIGN, SHAPE_BENIGN # 收窄词汇表(2026-11-13 裸吞收口)
|
|
31
32
|
|
|
32
33
|
# cache 延迟到首次真正需要相似度时再导入(2026-09-27,方案 B3):顶层 import
|
|
33
34
|
# 会把 cache.py 连带 sqlite3/shutil/tempfile 整条拉进每次 import search 的
|
|
@@ -72,6 +73,7 @@ from rank_signals import ( # noqa: E402,F401
|
|
|
72
73
|
score_completeness as _score_completeness,
|
|
73
74
|
score_relevance as _score_relevance,
|
|
74
75
|
title_stuffing_penalty as _title_stuffing_penalty,
|
|
76
|
+
upstream_score_boost as _upstream_score_boost,
|
|
75
77
|
)
|
|
76
78
|
|
|
77
79
|
# 中文/英文混合分词用的正则,延迟编译(首次 _tokens 时建)
|
|
@@ -281,8 +283,12 @@ def _content_similarity(a: str, b: str) -> float:
|
|
|
281
283
|
if _sim_fn is not None:
|
|
282
284
|
try:
|
|
283
285
|
return float(_sim_fn(a, b))
|
|
284
|
-
except Exception:
|
|
285
|
-
|
|
286
|
+
except Exception as e:
|
|
287
|
+
# 单条相似度计算失败按「无相似度」处理,回退 Jaccard;相似度查
|
|
288
|
+
# cache 的 sqlite(OperationalError 不在词汇表),保持宽口径仅可见化。
|
|
289
|
+
import logging
|
|
290
|
+
logging.getLogger("unified_search").debug(
|
|
291
|
+
f"query 相似度计算跳过: {type(e).__name__}")
|
|
286
292
|
import re as _re
|
|
287
293
|
sa, sb = set(_re.findall(r"[\u4e00-\u9fff]|[a-zA-Z0-9]+", a.lower())), set(_re.findall(r"[\u4e00-\u9fff]|[a-zA-Z0-9]+", b.lower()))
|
|
288
294
|
if not sa or not sb:
|
|
@@ -339,7 +345,7 @@ def minhash_dedupe(
|
|
|
339
345
|
try:
|
|
340
346
|
thr = float(os.environ.get("ARGO_MINHASH_THRESHOLD", str(threshold)))
|
|
341
347
|
threshold = max(0.5, min(0.98, thr))
|
|
342
|
-
except
|
|
348
|
+
except SHAPE_BENIGN:
|
|
343
349
|
pass
|
|
344
350
|
pool = sorted(
|
|
345
351
|
results,
|
|
@@ -470,7 +476,7 @@ def _note_rerank_failure(breaker, category: str, detail: str) -> None:
|
|
|
470
476
|
attribution={"category": category,
|
|
471
477
|
"reason": "语义精排端点不可用", "detail": detail},
|
|
472
478
|
)
|
|
473
|
-
except
|
|
479
|
+
except IO_BENIGN + SHAPE_BENIGN:
|
|
474
480
|
pass
|
|
475
481
|
|
|
476
482
|
def rerank_results(query: str, results: list[dict[str, Any]],
|
|
@@ -498,8 +504,13 @@ def rerank_results(query: str, results: list[dict[str, Any]],
|
|
|
498
504
|
from quota import get_quota_manager
|
|
499
505
|
if get_quota_manager().is_remote_exhausted("bocha"):
|
|
500
506
|
return results, engine_states.RERANK_SKIPPED_QUOTA
|
|
501
|
-
except Exception:
|
|
502
|
-
|
|
507
|
+
except Exception as e:
|
|
508
|
+
# quota 联动是侧信道:fail-open 是被测契约(test_quota_gate_fails_open
|
|
509
|
+
# _when_quota_unavailable 用 RuntimeError 桩验证),不收窄——except_sets
|
|
510
|
+
# 判定规则看 try 块失败的代价,quota 层可抛任意异常。
|
|
511
|
+
import logging
|
|
512
|
+
logging.getLogger("unified_search").debug(
|
|
513
|
+
f"quota 联动检查跳过(fail-open): {type(e).__name__}")
|
|
503
514
|
|
|
504
515
|
# 端点熔断:先问「还该不该打这一枪」。
|
|
505
516
|
#
|
|
@@ -546,7 +557,7 @@ def rerank_results(query: str, results: list[dict[str, Any]],
|
|
|
546
557
|
if breaker is not None:
|
|
547
558
|
try:
|
|
548
559
|
breaker.record_success(_RERANK_BREAKER_KEY)
|
|
549
|
-
except
|
|
560
|
+
except IO_BENIGN + SHAPE_BENIGN:
|
|
550
561
|
pass
|
|
551
562
|
rerank_results_list = data.get("data", {}).get("results", [])
|
|
552
563
|
if not rerank_results_list:
|
|
@@ -758,10 +769,9 @@ def local_five_dim_rerank(query: str, results: list[dict[str, Any]],
|
|
|
758
769
|
# prior——浮点加法顺序与改造前一致,金标对拍才可能零差异。
|
|
759
770
|
pool = enriched[:]
|
|
760
771
|
for e in pool:
|
|
761
|
-
e["_static4"] = (w["relevance"] * e["relevance"]
|
|
762
|
-
+ w["
|
|
763
|
-
+
|
|
764
|
-
+ w["completeness"] * e["completeness"])
|
|
772
|
+
e["_static4"] = (w["relevance"] * e["relevance"] + w["authority"] * e["authority"]
|
|
773
|
+
+ w["freshness"] * e["freshness"] + w["completeness"] * e["completeness"]
|
|
774
|
+
+ _upstream_score_boost(e["r"]))
|
|
765
775
|
# 来源侧惩罚(域级集中 / 跨域同质)作为静态分乘子(≤1)在**贪心之前**
|
|
766
776
|
# 应用:① 无需事后重排,且惩罚表为空时本行是 ×1.0、浮点上逐位无操作,
|
|
767
777
|
# 「无惩罚时与旧实现逐位一致」的不变式不被破坏;② K 剪枝上界
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""secret_guard.py — 输出流自动脱敏拦截器(防止真实凭据反射/外泄给 Agent 与 LLM)。
|
|
3
|
+
|
|
4
|
+
第一性原理:
|
|
5
|
+
任何流向终端、JSON 或 MCP 工具返回的文本/结构化数据,都必须经过
|
|
6
|
+
脱敏拦截,杜绝三类泄露:
|
|
7
|
+
1. 已配置的真实凭据(从 os.environ 与 envfile 收集的所有有效 API Key/Token);
|
|
8
|
+
2. URL 参数中的敏感认证(?key=..., &token=..., &secret=...);
|
|
9
|
+
3. 通用高熵 API 凭据签名模式(sk-..., tvly-..., ghp_..., Bearer ...)。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
import re
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
# 通用高熵 API Key 签名正则
|
|
19
|
+
_GENERIC_KEY_PATTERNS = [
|
|
20
|
+
# OpenAI / DeepSeek / 通用 sk- 开头
|
|
21
|
+
re.compile(r"\b(sk-[a-zA-Z0-9_\-]{20,})\b"),
|
|
22
|
+
# Tavily tvly- 开头
|
|
23
|
+
re.compile(r"\b(tvly-[a-zA-Z0-9_\-]{20,})\b"),
|
|
24
|
+
# GitHub Token (ghp_, github_pat_)
|
|
25
|
+
re.compile(r"\b(ghp_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9_]{40,})\b"),
|
|
26
|
+
# HTTP Bearer 认证头残留
|
|
27
|
+
re.compile(r"(?i)\bBearer\s+([a-zA-Z0-9_\-\.]{24,})\b"),
|
|
28
|
+
# 典型键值对形式
|
|
29
|
+
re.compile(r"(?i)(?:api_key|access_token|secret_key|api_secret)\s*[:=]\s*['\"]?([a-zA-Z0-9_\-]{24,})['\"]?"),
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
# URL query 敏感参数脱敏
|
|
33
|
+
_URL_PARAM_PATTERN = re.compile(
|
|
34
|
+
r"([?&](?:api_?key|access_?token|token|key|secret|password|auth)=)[^& \t\r\n\"'>]+",
|
|
35
|
+
re.I,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# 避免误杀的非密钥常见词
|
|
39
|
+
_WHITELIST = {
|
|
40
|
+
"undefined", "null", "false", "true", "none", "password", "secret", "bearer",
|
|
41
|
+
"http_proxy", "https_proxy", "all_proxy", "no_proxy",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_active_secrets() -> set[str]:
|
|
46
|
+
"""收集当前环境中加载的真实有效长密钥字符串集合(去重、过滤短串)。"""
|
|
47
|
+
secrets: set[str] = set()
|
|
48
|
+
# 1. 进程环境变量
|
|
49
|
+
for k, v in os.environ.items():
|
|
50
|
+
if not v or len(v) < 8:
|
|
51
|
+
continue
|
|
52
|
+
k_upper = k.upper()
|
|
53
|
+
if (k_upper.startswith("ARGO_") or
|
|
54
|
+
k_upper.endswith(("_KEY", "_TOKEN", "_SECRET", "_PASSWORD", "_APIKEY")) or
|
|
55
|
+
"API_KEY" in k_upper or "ACCESS_KEY" in k_upper):
|
|
56
|
+
if v.lower() not in _WHITELIST:
|
|
57
|
+
secrets.add(v.strip())
|
|
58
|
+
|
|
59
|
+
# 2. envfile 存储的密钥
|
|
60
|
+
try:
|
|
61
|
+
from engine_env import _envfile_load
|
|
62
|
+
file_env = _envfile_load()
|
|
63
|
+
for _k, v in file_env.items():
|
|
64
|
+
if v and len(v) >= 8 and v.lower() not in _WHITELIST:
|
|
65
|
+
secrets.add(v.strip())
|
|
66
|
+
except Exception:
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
return secrets
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def redact_text(text: str, extra_secrets: set[str] | None = None) -> str:
|
|
73
|
+
"""对单段文本执行深度脱敏替换。"""
|
|
74
|
+
if not text or not isinstance(text, str):
|
|
75
|
+
return text
|
|
76
|
+
|
|
77
|
+
out = text
|
|
78
|
+
|
|
79
|
+
# 1. 替换已知真实有效凭据
|
|
80
|
+
active_secrets = get_active_secrets()
|
|
81
|
+
if extra_secrets:
|
|
82
|
+
active_secrets.update(extra_secrets)
|
|
83
|
+
|
|
84
|
+
for sec in sorted(active_secrets, key=len, reverse=True):
|
|
85
|
+
if sec in out:
|
|
86
|
+
out = out.replace(sec, "[REDACTED_API_KEY]")
|
|
87
|
+
|
|
88
|
+
# 2. 替换通用高熵 API Key 签名
|
|
89
|
+
for pat in _GENERIC_KEY_PATTERNS:
|
|
90
|
+
out = pat.sub("[REDACTED_SECRET]", out)
|
|
91
|
+
|
|
92
|
+
# 3. 替换 URL query 参数中的敏感参数
|
|
93
|
+
out = _URL_PARAM_PATTERN.sub(r"\1[REDACTED]", out)
|
|
94
|
+
|
|
95
|
+
return out
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def redact_data(data: Any, extra_secrets: set[str] | None = None) -> Any:
|
|
99
|
+
"""递归对复杂数据结构(dict / list / str)进行全量脱敏。"""
|
|
100
|
+
if isinstance(data, str):
|
|
101
|
+
return redact_text(data, extra_secrets=extra_secrets)
|
|
102
|
+
if isinstance(data, list):
|
|
103
|
+
return [redact_data(item, extra_secrets=extra_secrets) for item in data]
|
|
104
|
+
if isinstance(data, dict):
|
|
105
|
+
return {k: redact_data(v, extra_secrets=extra_secrets) for k, v in data.items()}
|
|
106
|
+
return data
|