foxclaw 0.1.2 → 0.1.3
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/index.js +1 -8
- package/package.json +1 -1
- package/python/.env.example +8 -0
- package/python/.github/workflows/ci.yml +47 -2
- package/python/CHANGELOG.md +57 -0
- package/python/CHANGELOG_CN.md +57 -0
- package/python/README.md +11 -2
- package/python/README_CN.md +32 -20
- package/python/bench/bm25_audit.py +1 -14
- package/python/bench/bm25_regression.py +0 -1
- package/python/bench/bm25_stress.py +0 -4
- package/python/bench/cd_audit.py +0 -23
- package/python/bench/chaos.py +6 -8
- package/python/bench/e2e_real_models.py +0 -1
- package/python/bench/llm_retry_test.py +2 -9
- package/python/bench/load_test.py +5 -8
- package/python/bench/vector_ivf_upgrade.py +1 -7
- package/python/docs/RELEASE.md +4 -3
- package/python/docs/RUN_CN.md +2 -2
- package/python/evals/governance/cases.py +0 -37
- package/python/evals/governance/run_evals.py +5 -37
- package/python/examples/governance_middleware_demo.py +1 -6
- package/python/examples/langgraph_governance_demo.py +7 -11
- package/python/foxclaw/__init__.py +34 -152
- package/python/foxclaw/agents/base.py +65 -136
- package/python/foxclaw/agents/impl/_helpers.py +2 -10
- package/python/foxclaw/agents/impl/finance.py +4 -21
- package/python/foxclaw/agents/impl/office.py +7 -24
- package/python/foxclaw/agents/impl/supervisor.py +12 -79
- package/python/foxclaw/agents/mixins/_context.py +0 -23
- package/python/foxclaw/agents/mixins/augment.py +10 -16
- package/python/foxclaw/agents/mixins/llm.py +7 -6
- package/python/foxclaw/agents/mixins/memory.py +7 -1
- package/python/foxclaw/agents/mixins/preference.py +58 -8
- package/python/foxclaw/agents/mixins/prompt.py +0 -6
- package/python/foxclaw/agents/mixins/skillbox.py +6 -6
- package/python/foxclaw/agents/mixins/tool_exec.py +16 -42
- package/python/foxclaw/api/__init__.py +18 -77
- package/python/foxclaw/api/_state.py +43 -0
- package/python/foxclaw/api/{app.py → bootstrap.py} +5 -32
- package/python/foxclaw/api/middleware.py +5 -28
- package/python/foxclaw/api/routes/ahs.py +60 -73
- package/python/foxclaw/api/routes/attachments.py +5 -6
- package/python/foxclaw/api/routes/browser.py +7 -11
- package/python/foxclaw/api/routes/domain.py +18 -26
- package/python/foxclaw/api/routes/knowledge.py +3 -21
- package/python/foxclaw/api/routes/llm_admin.py +21 -33
- package/python/foxclaw/api/routes/memory.py +28 -12
- package/python/foxclaw/api/routes/orchestration.py +3 -5
- package/python/foxclaw/api/routes/recovery.py +4 -3
- package/python/foxclaw/api/routes/review.py +2 -11
- package/python/foxclaw/api/routes/search.py +3 -7
- package/python/foxclaw/api/routes/system.py +33 -49
- package/python/foxclaw/api/routes/task.py +31 -49
- package/python/foxclaw/api/routes/ws.py +2 -6
- package/python/foxclaw/api/spine.py +1 -4
- package/python/foxclaw/browser/action/action_executor.py +1 -5
- package/python/foxclaw/browser/action/action_types.py +0 -1
- package/python/foxclaw/browser/action/url_policy.py +1 -5
- package/python/foxclaw/browser/adapter/web_adapter.py +1 -1
- package/python/foxclaw/browser/driver/playwright_driver.py +20 -16
- package/python/foxclaw/browser/perception/dom_extractor.py +0 -1
- package/python/foxclaw/browser/perception/element_indexer.py +1 -2
- package/python/foxclaw/browser/service.py +7 -11
- package/python/foxclaw/capability/embedding_service.py +4 -6
- package/python/foxclaw/channels/__init__.py +9 -11
- package/python/foxclaw/channels/discord.py +4 -13
- package/python/foxclaw/channels/dispatcher.py +2 -27
- package/python/foxclaw/channels/feishu.py +10 -20
- package/python/foxclaw/channels/openclaw_bridge.py +4 -9
- package/python/foxclaw/channels/signing.py +9 -9
- package/python/foxclaw/channels/telegram.py +3 -4
- package/python/foxclaw/channels/weixin.py +7 -17
- package/python/foxclaw/cli.py +42 -13
- package/python/foxclaw/cognitive/reasoner.py +9 -10
- package/python/foxclaw/cognitive/state.py +18 -24
- package/python/foxclaw/core/audit_chain.py +1 -3
- package/python/foxclaw/core/audit_sink.py +3 -3
- package/python/foxclaw/core/compaction.py +8 -26
- package/python/foxclaw/core/config.py +2 -2
- package/python/foxclaw/core/confirm_nonce.py +6 -12
- package/python/foxclaw/core/envfile.py +2 -2
- package/python/foxclaw/core/failure_policy.py +17 -26
- package/python/foxclaw/core/gateway.py +6 -10
- package/python/foxclaw/core/governance_audit.py +3 -7
- package/python/foxclaw/core/idempotency.py +15 -22
- package/python/foxclaw/core/injection_guard.py +10 -29
- package/python/foxclaw/core/memory_adapter.py +30 -57
- package/python/foxclaw/core/metrics.py +4 -5
- package/python/foxclaw/core/model_router.py +36 -148
- package/python/foxclaw/core/paths.py +1 -1
- package/python/foxclaw/core/recovery.py +21 -34
- package/python/foxclaw/core/scheduler.py +23 -26
- package/python/foxclaw/core/secrets.py +0 -3
- package/python/foxclaw/core/sessions.py +4 -6
- package/python/foxclaw/core/settings.py +16 -32
- package/python/foxclaw/core/shared_write.py +11 -23
- package/python/foxclaw/core/steering_queue.py +12 -11
- package/python/foxclaw/core/system_bus.py +1 -1
- package/python/foxclaw/core/thought_chain.py +30 -32
- package/python/foxclaw/core/token_meter.py +9 -13
- package/python/foxclaw/core/tools.py +4 -12
- package/python/foxclaw/core/tracing.py +25 -40
- package/python/foxclaw/core/truth_pipeline.py +5 -9
- package/python/foxclaw/core/update_check.py +149 -0
- package/python/foxclaw/core/vec_recall_adapter.py +11 -37
- package/python/foxclaw/core/verify.py +6 -14
- package/python/foxclaw/core/web_search.py +4 -28
- package/python/foxclaw/finance/evidence.py +14 -18
- package/python/foxclaw/finance/fx.py +1 -3
- package/python/foxclaw/finance/graph.py +9 -18
- package/python/foxclaw/finance/models.py +20 -24
- package/python/foxclaw/finance/nlparse.py +12 -72
- package/python/foxclaw/finance/policy.py +8 -36
- package/python/foxclaw/finance/reasoner.py +3 -11
- package/python/foxclaw/finance/reasoning.py +13 -34
- package/python/foxclaw/finance/reconcile.py +5 -10
- package/python/foxclaw/finance/runtime.py +19 -46
- package/python/foxclaw/governance/middleware.py +13 -21
- package/python/foxclaw/harness/_base.py +2 -8
- package/python/foxclaw/harness/_policy.py +19 -38
- package/python/foxclaw/harness/drift.py +1 -13
- package/python/foxclaw/harness/event_bus.py +2 -5
- package/python/foxclaw/harness/harness.py +9 -97
- package/python/foxclaw/harness/loop_detection.py +11 -18
- package/python/foxclaw/itinerary/amap_client.py +2 -5
- package/python/foxclaw/itinerary/models.py +7 -7
- package/python/foxclaw/itinerary/reasoner.py +1 -6
- package/python/foxclaw/itinerary/reasoning.py +15 -21
- package/python/foxclaw/itinerary/runtime.py +8 -12
- package/python/foxclaw/kernel/capabilities.py +5 -6
- package/python/foxclaw/kernel/policy_gate.py +4 -10
- package/python/foxclaw/kernel/process_manager.py +17 -36
- package/python/foxclaw/kernel/scheduler.py +3 -9
- package/python/foxclaw/kernel/sentinel.py +13 -23
- package/python/foxclaw/kernel/sentinel_client.py +12 -15
- package/python/foxclaw/kernel/spine.py +9 -9
- package/python/foxclaw/knowledge/__init__.py +4 -20
- package/python/foxclaw/knowledge/truth_crystallizer.py +5 -8
- package/python/foxclaw/learning/knowledge_graph.py +4 -9
- package/python/foxclaw/learning/models.py +13 -13
- package/python/foxclaw/learning/reasoner.py +1 -4
- package/python/foxclaw/learning/runtime.py +19 -31
- package/python/foxclaw/learning/spaced_repetition.py +2 -7
- package/python/foxclaw/mcp_server.py +2 -3
- package/python/foxclaw/orchestration/__init__.py +16 -33
- package/python/foxclaw/pipeline/cognitive_pipeline.py +45 -97
- package/python/foxclaw/pipeline/stage_meta.py +21 -0
- package/python/foxclaw/pipeline/supervisor_stages.py +13 -42
- package/python/foxclaw/planning/llm_planner.py +3 -3
- package/python/foxclaw/planning/planner.py +6 -11
- package/python/foxclaw/search/agent_port.py +7 -11
- package/python/foxclaw/search/evidence.py +0 -4
- package/python/foxclaw/search/internal_search.py +3 -5
- package/python/foxclaw/search/models.py +11 -13
- package/python/foxclaw/search/planner.py +3 -3
- package/python/foxclaw/search/retriever.py +1 -1
- package/python/foxclaw/search/runtime.py +1 -8
- package/python/foxclaw/skills/__init__.py +46 -100
- package/python/foxclaw/skills/archive.py +111 -0
- package/python/foxclaw/skills/fetch.py +226 -0
- package/python/foxclaw/skills/hooks_spec.py +21 -25
- package/python/foxclaw/skills/install_cmd.py +216 -0
- package/python/foxclaw/skills/lifecycle.py +16 -42
- package/python/foxclaw/skills/markdown_skill.py +10 -9
- package/python/foxclaw/skills/mcp_client.py +5 -11
- package/python/foxclaw/skills/mcp_store.py +0 -3
- package/python/foxclaw/skills/permission.py +13 -48
- package/python/foxclaw/skills/pipeline.py +21 -53
- package/python/foxclaw/skills/policy.py +4 -10
- package/python/foxclaw/skills/review.py +3 -3
- package/python/frontend/deck/auth.js +0 -7
- package/python/frontend/deck/chat_model_picker.js +0 -54
- package/python/frontend/deck/domain.js +0 -20
- package/python/frontend/deck/main.js +0 -144
- package/python/frontend/deck/markdown.js +0 -4
- package/python/frontend/deck/settings.js +1 -18
- package/python/frontend/deck/state.js +0 -1
- package/python/frontend/deck/util.js +0 -2
- package/python/frontend/deck/visuals.js +0 -22
- package/python/frontend/planet/planet.js +0 -201
- package/python/frontend/v3/assets/index-CQSBdxk4.js +34 -0
- package/python/frontend/v3/assets/index-D5nIsjms.css +1 -0
- package/python/frontend/v3/index.html +3 -3
- package/python/frontend/widget_runtime.js +0 -43
- package/python/frontend/widgets.js +0 -57
- package/python/frontend_src/index.html +1 -1
- package/python/frontend_src/package-lock.json +2 -2
- package/python/frontend_src/package.json +1 -1
- package/python/frontend_src/src/App.vue +3 -7
- package/python/frontend_src/src/api.js +0 -7
- package/python/frontend_src/src/components/ChatDock.vue +27 -39
- package/python/frontend_src/src/components/DocViewer.vue +0 -9
- package/python/frontend_src/src/components/Inspector.vue +0 -6
- package/python/frontend_src/src/components/LeftRail.vue +60 -39
- package/python/frontend_src/src/components/MenuPopover.vue +9 -11
- package/python/frontend_src/src/components/MessageStream.vue +18 -30
- package/python/frontend_src/src/components/ModelPicker.vue +0 -5
- package/python/frontend_src/src/components/QuickIngest.vue +0 -6
- package/python/frontend_src/src/components/Stage.vue +71 -25
- package/python/frontend_src/src/components/ToolMenu.vue +0 -3
- package/python/frontend_src/src/components/TopBar.vue +38 -10
- package/python/frontend_src/src/components/panels/ActivityPanel.vue +0 -2
- package/python/frontend_src/src/components/panels/AgentsPanel.vue +2 -5
- package/python/frontend_src/src/components/panels/KnowledgePanel.vue +0 -16
- package/python/frontend_src/src/components/panels/MemoryPanel.vue +0 -1
- package/python/frontend_src/src/components/panels/PanelBox.vue +6 -7
- package/python/frontend_src/src/components/panels/ReviewPanel.vue +0 -16
- package/python/frontend_src/src/components/panels/SettingsPanel.vue +142 -38
- package/python/frontend_src/src/components/panels/TasksPanel.vue +0 -1
- package/python/frontend_src/src/components/panels/ToolsPanel.vue +125 -19
- package/python/frontend_src/src/lib/fmt.js +0 -4
- package/python/frontend_src/src/lib/i18n.js +53 -0
- package/python/frontend_src/src/lib/locales/en.js +173 -0
- package/python/frontend_src/src/lib/locales/zh.js +174 -0
- package/python/frontend_src/src/lib/md.js +0 -14
- package/python/frontend_src/src/main.js +2 -0
- package/python/frontend_src/src/stores/app.js +17 -32
- package/python/frontend_src/src/stores/chat.js +0 -34
- package/python/frontend_src/src/stores/model.js +0 -8
- package/python/frontend_src/src/stores/panels.js +0 -41
- package/python/frontend_src/src/stores/profile.js +31 -0
- package/python/frontend_src/src/stores/system.js +25 -0
- package/python/frontend_src/vite.config.js +0 -7
- package/python/pyproject.toml +6 -3
- package/python/requirements.txt +311 -0
- package/python/tests/conftest.py +0 -22
- package/python/tests/run_all.py +3 -66
- package/python/tests/test_agent_lifecycle.py +4 -38
- package/python/tests/test_ahs_p2.py +4 -25
- package/python/tests/test_anthropic_tools.py +1 -15
- package/python/tests/test_api.py +19 -9
- package/python/tests/test_api_auth.py +1 -5
- package/python/tests/test_attachments.py +9 -25
- package/python/tests/test_audit_chain.py +2 -7
- package/python/tests/test_audit_sink.py +0 -6
- package/python/tests/test_browser.py +2 -6
- package/python/tests/test_browser_request_guard.py +0 -4
- package/python/tests/test_capabilities.py +0 -6
- package/python/tests/test_channel_signing.py +2 -9
- package/python/tests/test_channels.py +1 -11
- package/python/tests/test_config_layering.py +0 -4
- package/python/tests/test_confirm_nonce.py +1 -12
- package/python/tests/test_context_compaction_wiring.py +1 -7
- package/python/tests/test_dispatch.py +0 -6
- package/python/tests/test_domain_evals.py +11 -80
- package/python/tests/test_domain_import.py +0 -6
- package/python/tests/test_domain_reasoners.py +0 -14
- package/python/tests/test_e2e.py +0 -17
- package/python/tests/test_emb_cache.py +7 -17
- package/python/tests/test_embedding_singleton.py +1 -3
- package/python/tests/test_evals_smoke.py +0 -24
- package/python/tests/test_event_spine.py +10 -18
- package/python/tests/test_evidence_semantic.py +1 -14
- package/python/tests/test_evidence_sse.py +3 -6
- package/python/tests/test_file_ingest.py +0 -11
- package/python/tests/test_finance.py +0 -9
- package/python/tests/test_finance_ledger.py +5 -12
- package/python/tests/test_finance_persist.py +8 -12
- package/python/tests/test_gateway.py +3 -22
- package/python/tests/test_governance.py +5 -52
- package/python/tests/test_governance_audit.py +4 -39
- package/python/tests/test_governance_middleware.py +1 -7
- package/python/tests/test_hkc.py +6 -24
- package/python/tests/test_hkc_index_build_manager.py +0 -33
- package/python/tests/test_hkc_manifest_reuse.py +4 -27
- package/python/tests/test_hkc_search_event_sync.py +3 -54
- package/python/tests/test_import_path_guard.py +0 -6
- package/python/tests/test_injection_guard.py +18 -36
- package/python/tests/test_integration.py +5 -29
- package/python/tests/test_itinerary.py +0 -3
- package/python/tests/test_kee_semantic.py +0 -5
- package/python/tests/test_kernel.py +1 -11
- package/python/tests/test_learning.py +3 -12
- package/python/tests/test_llm_admin.py +4 -19
- package/python/tests/test_llm_planner.py +1 -23
- package/python/tests/test_markdown_skills.py +3 -14
- package/python/tests/test_markdown_skills_openclaw.py +4 -9
- package/python/tests/test_mcp_connectors.py +3 -15
- package/python/tests/test_mcp_guardian_discovery.py +2 -6
- package/python/tests/test_mcp_no_memory.py +3 -3
- package/python/tests/test_mcp_policy_gate.py +0 -1
- package/python/tests/test_mcp_server.py +6 -9
- package/python/tests/test_memory_recall.py +1 -4
- package/python/tests/test_metrics.py +0 -8
- package/python/tests/test_multi_chain.py +1 -13
- package/python/tests/test_multimodal.py +5 -18
- package/python/tests/test_negotiation.py +3 -13
- package/python/tests/test_orchestration.py +1 -11
- package/python/tests/test_orchestration_decoupled.py +0 -6
- package/python/tests/test_p3_p5_hardening.py +3 -17
- package/python/tests/test_parallel_dispatch.py +4 -12
- package/python/tests/test_pipeline.py +17 -58
- package/python/tests/test_plan_visible.py +4 -4
- package/python/tests/test_planner.py +0 -7
- package/python/tests/test_policy_gate.py +2 -3
- package/python/tests/test_profile.py +67 -0
- package/python/tests/test_readme_claims.py +163 -0
- package/python/tests/test_reasoner.py +2 -17
- package/python/tests/test_release_check.py +2 -13
- package/python/tests/test_resume_checkpoint.py +7 -15
- package/python/tests/test_routing.py +2 -12
- package/python/tests/test_scheduler.py +7 -10
- package/python/tests/test_search.py +1 -11
- package/python/tests/test_secrets.py +4 -10
- package/python/tests/test_semantic.py +1 -10
- package/python/tests/test_sessions.py +3 -4
- package/python/tests/test_settings.py +2 -18
- package/python/tests/test_shadow_learning.py +3 -43
- package/python/tests/test_shared_write.py +0 -7
- package/python/tests/test_skill_taint_gate.py +1 -10
- package/python/tests/test_skills.py +2 -18
- package/python/tests/test_skills_fetch.py +282 -0
- package/python/tests/test_stats_and_events.py +2 -5
- package/python/tests/test_streaming.py +2 -7
- package/python/tests/test_supervisor_pipeline.py +4 -5
- package/python/tests/test_supervisor_triage.py +4 -10
- package/python/tests/test_taint.py +2 -7
- package/python/tests/test_task_stream.py +1 -12
- package/python/tests/test_thinking_sse.py +0 -1
- package/python/tests/test_tool_center.py +1 -17
- package/python/tests/test_tool_parallel.py +2 -2
- package/python/tests/test_tool_params.py +1 -5
- package/python/tests/test_tool_use.py +0 -13
- package/python/tests/test_toplevel_concurrency.py +3 -4
- package/python/tests/test_tracing.py +7 -41
- package/python/tests/test_truth_crystallizer.py +4 -6
- package/python/tests/test_truth_flow.py +2 -7
- package/python/tests/test_update_check.py +95 -0
- package/python/tests/test_verify_evidence.py +8 -21
- package/python/tests/test_version_sync.py +35 -6
- package/python/tests/test_web_trigger.py +0 -17
- package/python/tools/check_faiss_type.py +0 -1
- package/python/tools/check_log.ps1 +0 -8
- package/python/tools/mypy_baseline.txt +12 -4
- package/python/tools/mypy_ratchet.py +52 -6
- package/python/tools/pack.py +0 -5
- package/python/tools/release_check.py +7 -7
- package/python/tools/restart_foxclaw.ps1 +5 -8
- package/python/tools/shared_write_audit.py +1 -15
- package/python/tools/size_check.py +1 -1
- package/python/tools/strip_comments.py +228 -0
- package/python/frontend/v3/assets/index-BD7VPbMV.js +0 -33
- package/python/frontend/v3/assets/index-CAvnYWGH.css +0 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""剥离代码里的解释性注释。
|
|
2
|
+
|
|
3
|
+
用法:
|
|
4
|
+
python tools/strip_comments.py --dry 只统计,不改文件
|
|
5
|
+
python tools/strip_comments.py 真正改写
|
|
6
|
+
python tools/strip_comments.py a.py b.js 只处理指定文件
|
|
7
|
+
|
|
8
|
+
不能一刀切:下面这些注释被删会改变 lint / 类型检查 / 解释器行为,必须原样保留。
|
|
9
|
+
#!... shebang
|
|
10
|
+
# -*- coding -*- 编码声明
|
|
11
|
+
# noqa ... ruff 抑制
|
|
12
|
+
# type: ... mypy 注解(含 `x = [] # type: list[int]`)
|
|
13
|
+
# pragma / fmt: / isort: / pyright: / pylint: / nosec / coverage:
|
|
14
|
+
docstring 不动 —— 它是运行时对象,不是注释。
|
|
15
|
+
|
|
16
|
+
vendor/ 是内嵌第三方,不改;frontend/v3 是构建产物,由 vite 重新生成。
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import io
|
|
22
|
+
import re
|
|
23
|
+
import sys
|
|
24
|
+
import tokenize
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
ROOT = Path(__file__).resolve().parent.parent
|
|
28
|
+
|
|
29
|
+
SKIP_PARTS = {".git", "node_modules", "__pycache__", ".venv", "venv", "vendor",
|
|
30
|
+
"foxclaw_data", "test_data", ".pytest_cache", ".ruff_cache", ".mypy_cache",
|
|
31
|
+
"build", "dist", "htmlcov"}
|
|
32
|
+
# 生成副本:npm/python 由 npm/build.cjs 从仓库根复制,frontend/v3 由 vite 构建
|
|
33
|
+
SKIP_PREFIX = {"frontend/v3/", "npm/python/"}
|
|
34
|
+
EXTS = {".py", ".js", ".vue", ".ps1"}
|
|
35
|
+
|
|
36
|
+
DIRECTIVE = re.compile(
|
|
37
|
+
r"^#\s*(?:!|-\*-|noqa|type:|pragma|fmt:|isort:|pyright:|pylint:|ruff:|nosec|"
|
|
38
|
+
r"coverage:|mypy:|noinspection|coding[:=])",
|
|
39
|
+
re.I,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
MAX_BLANK = 2
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def targets() -> list[Path]:
|
|
46
|
+
out = []
|
|
47
|
+
for p in ROOT.rglob("*"):
|
|
48
|
+
if p.suffix not in EXTS or not p.is_file():
|
|
49
|
+
continue
|
|
50
|
+
rel = p.relative_to(ROOT).as_posix()
|
|
51
|
+
if SKIP_PARTS & set(p.relative_to(ROOT).parts):
|
|
52
|
+
continue
|
|
53
|
+
if any(rel.startswith(x) for x in SKIP_PREFIX):
|
|
54
|
+
continue
|
|
55
|
+
if rel == "tools/strip_comments.py":
|
|
56
|
+
continue
|
|
57
|
+
out.append(p)
|
|
58
|
+
return sorted(out)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def collapse_blank(lines: list[str], limit: int = MAX_BLANK) -> list[str]:
|
|
62
|
+
out: list[str] = []
|
|
63
|
+
run = 0
|
|
64
|
+
for ln in lines:
|
|
65
|
+
if ln.strip() == "":
|
|
66
|
+
run += 1
|
|
67
|
+
if run > limit:
|
|
68
|
+
continue
|
|
69
|
+
else:
|
|
70
|
+
run = 0
|
|
71
|
+
out.append(ln)
|
|
72
|
+
return out
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def strip_py(text: str) -> tuple[str, int]:
|
|
76
|
+
try:
|
|
77
|
+
toks = list(tokenize.generate_tokens(io.StringIO(text).readline))
|
|
78
|
+
except (tokenize.TokenError, IndentationError, SyntaxError):
|
|
79
|
+
return text, 0
|
|
80
|
+
|
|
81
|
+
lines = text.splitlines(keepends=True)
|
|
82
|
+
drops: dict[int, int | None] = {}
|
|
83
|
+
for t in toks:
|
|
84
|
+
if t.type != tokenize.COMMENT:
|
|
85
|
+
continue
|
|
86
|
+
row, col = t.start
|
|
87
|
+
if DIRECTIVE.match(t.string):
|
|
88
|
+
continue
|
|
89
|
+
line = lines[row - 1] if row - 1 < len(lines) else ""
|
|
90
|
+
head = line[:col]
|
|
91
|
+
drops[row] = None if head.strip() == "" else col
|
|
92
|
+
|
|
93
|
+
if not drops:
|
|
94
|
+
return text, 0
|
|
95
|
+
|
|
96
|
+
out: list[str] = []
|
|
97
|
+
for idx, line in enumerate(lines, 1):
|
|
98
|
+
if idx not in drops:
|
|
99
|
+
out.append(line)
|
|
100
|
+
continue
|
|
101
|
+
col = drops[idx]
|
|
102
|
+
if col is None:
|
|
103
|
+
continue
|
|
104
|
+
body = line[:col].rstrip()
|
|
105
|
+
eol = line[len(line.rstrip("\r\n")):]
|
|
106
|
+
out.append(body + eol)
|
|
107
|
+
out = collapse_blank(out)
|
|
108
|
+
return "".join(out), len(drops)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _tpl_open(s: str) -> bool:
|
|
112
|
+
n = 0
|
|
113
|
+
i = 0
|
|
114
|
+
while i < len(s):
|
|
115
|
+
if s[i] == "\\":
|
|
116
|
+
i += 2
|
|
117
|
+
continue
|
|
118
|
+
if s[i] == "`":
|
|
119
|
+
n += 1
|
|
120
|
+
i += 1
|
|
121
|
+
return n % 2 == 1
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def strip_js(text: str) -> tuple[str, int]:
|
|
125
|
+
"""只删独占整行的注释,行尾注释一律不动。
|
|
126
|
+
|
|
127
|
+
按位置切不行:正则字面量里的 \/\/ 和字符串里的 // 都会被当成注释起点 ——
|
|
128
|
+
markdown.js 的 /^https?:\/\//i 就被切掉半个函数。整行判定是安全的:
|
|
129
|
+
行首出现 // 或 /* 时,它只能是注释。
|
|
130
|
+
"""
|
|
131
|
+
lines = text.splitlines(keepends=True)
|
|
132
|
+
out: list[str] = []
|
|
133
|
+
removed = 0
|
|
134
|
+
in_block = ""
|
|
135
|
+
in_tpl = False
|
|
136
|
+
for line in lines:
|
|
137
|
+
body = line.rstrip("\r\n")
|
|
138
|
+
s = body.strip()
|
|
139
|
+
if in_block:
|
|
140
|
+
removed += 1
|
|
141
|
+
if (in_block == "<!--" and "-->" in s) or (in_block == "/*" and "*/" in s):
|
|
142
|
+
in_block = ""
|
|
143
|
+
continue
|
|
144
|
+
if in_tpl:
|
|
145
|
+
if _tpl_open(s):
|
|
146
|
+
in_tpl = False
|
|
147
|
+
out.append(line)
|
|
148
|
+
continue
|
|
149
|
+
if s.startswith("<!--"):
|
|
150
|
+
removed += 1
|
|
151
|
+
if "-->" not in s[4:]:
|
|
152
|
+
in_block = "<!--"
|
|
153
|
+
continue
|
|
154
|
+
if s.startswith("/*"):
|
|
155
|
+
removed += 1
|
|
156
|
+
if "*/" not in s[2:]:
|
|
157
|
+
in_block = "/*"
|
|
158
|
+
continue
|
|
159
|
+
if s.startswith("//"):
|
|
160
|
+
removed += 1
|
|
161
|
+
continue
|
|
162
|
+
if _tpl_open(body):
|
|
163
|
+
in_tpl = True
|
|
164
|
+
out.append(line)
|
|
165
|
+
return "".join(collapse_blank(out, 1)), removed
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def strip_ps(text: str) -> tuple[str, int]:
|
|
169
|
+
"""PowerShell 的注释符是 # 和 <# #>。只删独占行的,行尾的不碰 —— 拆字符串不值当。"""
|
|
170
|
+
out: list[str] = []
|
|
171
|
+
removed = 0
|
|
172
|
+
in_block = False
|
|
173
|
+
for line in text.splitlines(keepends=True):
|
|
174
|
+
stripped = line.strip()
|
|
175
|
+
if in_block:
|
|
176
|
+
removed += 1
|
|
177
|
+
if "#>" in stripped:
|
|
178
|
+
in_block = False
|
|
179
|
+
continue
|
|
180
|
+
if stripped.startswith("<#"):
|
|
181
|
+
removed += 1
|
|
182
|
+
if "#>" not in stripped[2:]:
|
|
183
|
+
in_block = True
|
|
184
|
+
continue
|
|
185
|
+
if stripped.startswith("#") and not re.match(r"#\s*requires\b", stripped, re.I):
|
|
186
|
+
removed += 1
|
|
187
|
+
continue
|
|
188
|
+
out.append(line)
|
|
189
|
+
return "".join(collapse_blank(out, 1)), removed
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def main(argv: list[str]) -> int:
|
|
193
|
+
dry = "--dry" in argv
|
|
194
|
+
picked = [Path(a) for a in argv if not a.startswith("-")]
|
|
195
|
+
files = picked or targets()
|
|
196
|
+
total_files = 0
|
|
197
|
+
total_lines = 0
|
|
198
|
+
for p in files:
|
|
199
|
+
if not p.is_absolute():
|
|
200
|
+
p = ROOT / p
|
|
201
|
+
raw = p.read_bytes()
|
|
202
|
+
text = raw.decode("utf-8")
|
|
203
|
+
if p.suffix == ".py":
|
|
204
|
+
new, n = strip_py(text)
|
|
205
|
+
elif p.suffix == ".ps1":
|
|
206
|
+
new, n = strip_ps(text)
|
|
207
|
+
else:
|
|
208
|
+
new, n = strip_js(text)
|
|
209
|
+
if n == 0 or new == text:
|
|
210
|
+
continue
|
|
211
|
+
if p.suffix == ".py":
|
|
212
|
+
try:
|
|
213
|
+
compile(new, str(p), "exec")
|
|
214
|
+
except SyntaxError as e:
|
|
215
|
+
print(f"SKIP {p.relative_to(ROOT)}: 删后语法错误 {e}")
|
|
216
|
+
continue
|
|
217
|
+
total_files += 1
|
|
218
|
+
total_lines += n
|
|
219
|
+
if dry:
|
|
220
|
+
print(f"{n:5d} {p.relative_to(ROOT).as_posix()}")
|
|
221
|
+
else:
|
|
222
|
+
p.write_bytes(new.encode("utf-8"))
|
|
223
|
+
print(f"\n{'[dry] ' if dry else ''}files={total_files} comment_lines={total_lines}")
|
|
224
|
+
return 0
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
if __name__ == "__main__":
|
|
228
|
+
raise SystemExit(main(sys.argv[1:]))
|