@louisultra/dsh-verymath 0.0.0-stage → 0.3.2
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/CHANGELOG.md +133 -0
- package/LICENSE +21 -0
- package/NOTICE.md +35 -0
- package/README.md +108 -2
- package/README_EN.md +105 -0
- package/UPSTREAM.json +106 -0
- package/cordis.patch.yml +12 -0
- package/docs/capability-examples.md +581 -0
- package/docs/examples/README.md +32 -0
- package/docs/examples/bad_scan.md +10 -0
- package/docs/examples/broken-proof.lean +9 -0
- package/docs/examples/claims.md +7 -0
- package/docs/examples/knapsack.json +10 -0
- package/docs/examples/matrix.json +1 -0
- package/docs/examples/points.csv +21 -0
- package/docs/examples/qp.json +8 -0
- package/docs/examples/research-ws/project/GOALS.yaml +4 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/report.md +13 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/reviews/r1.json +1 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/status.yaml +1 -0
- package/docs/examples/source.csv +4 -0
- package/lib/core/error.d.ts +20 -0
- package/lib/core/error.js +25 -0
- package/lib/core/gates.d.ts +42 -0
- package/lib/core/gates.js +217 -0
- package/lib/core/glossary.d.ts +101 -0
- package/lib/core/glossary.js +243 -0
- package/lib/core/graph.d.ts +118 -0
- package/lib/core/graph.js +460 -0
- package/lib/core/index.d.ts +120 -0
- package/lib/core/index.js +197 -0
- package/lib/core/paper.d.ts +163 -0
- package/lib/core/paper.js +73 -0
- package/lib/core/paths.d.ts +2 -0
- package/lib/core/paths.js +12 -0
- package/lib/core/sage.d.ts +61 -0
- package/lib/core/sage.js +212 -0
- package/lib/core/skills.d.ts +11 -0
- package/lib/core/skills.js +110 -0
- package/lib/tool/index.d.ts +21 -0
- package/lib/tool/index.js +1144 -0
- package/package.json +92 -4
- package/skills/after-ocr/SKILL.md +123 -0
- package/skills/after-ocr/references/audit-schema.md +94 -0
- package/skills/after-ocr/references/error-taxonomy.md +179 -0
- package/skills/after-ocr/scripts/merge_audit_logs.py +329 -0
- package/skills/after-ocr/scripts/scan_markdown_ocr.py +350 -0
- package/skills/after-ocr/tests/test_scripts.py +84 -0
- package/skills/discover-math-problems/SKILL.md +115 -0
- package/skills/discover-math-problems/references/artifact-contract.md +93 -0
- package/skills/discover-math-problems/references/autonomous-research-protocol.md +86 -0
- package/skills/discover-math-problems/references/conjecture-generation-protocol.md +63 -0
- package/skills/discover-math-problems/references/extremal-graph-variants.md +53 -0
- package/skills/graph-theory-paper-reading/SKILL.md +600 -0
- package/skills/invariant-computation/SKILL.md +169 -0
- package/skills/invariant-computation/examples/invariant_summary.example.json +51 -0
- package/skills/invariant-computation/manifest.yaml +46 -0
- package/skills/invariant-computation/references/INDEX.md +20 -0
- package/skills/invariant-computation/references/failure_modes.md +40 -0
- package/skills/invariant-computation/references/method_route_map.md +177 -0
- package/skills/invariant-computation/references/source_notes.md +19 -0
- package/skills/invariant-computation/references/tool_catalog.md +65 -0
- package/skills/invariant-computation/references/validation_checks.md +52 -0
- package/skills/invariant-computation/schema/invariant_summary.schema.json +260 -0
- package/skills/lean-formalization/SKILL.md +111 -0
- package/skills/lean-formalization/lean-runtime/config/env.example +5 -0
- package/skills/lean-formalization/lean-runtime/config/lean_agent.example.toml +28 -0
- package/skills/lean-formalization/lean-runtime/config/numina_runtime.example.toml +12 -0
- package/skills/lean-formalization/lean-runtime/examples/smoke/LocalLeanSmoke.lean +7 -0
- package/skills/lean-formalization/lean-runtime/prompts/complete_sorries.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/formalize_statement.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/prove_theorem.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/repair_lean_file.md +3 -0
- package/skills/lean-formalization/lean-runtime/references/backend_adapter_checklist.md +36 -0
- package/skills/lean-formalization/lean-runtime/references/direct_lean_workflow.md +48 -0
- package/skills/lean-formalization/lean-runtime/references/failure_taxonomy.md +17 -0
- package/skills/lean-formalization/lean-runtime/references/interactive_orchestration.md +83 -0
- package/skills/lean-formalization/lean-runtime/references/lean_agent_capability_map.md +116 -0
- package/skills/lean-formalization/lean-runtime/references/lean_lsp_mcp_adapter.md +219 -0
- package/skills/lean-formalization/lean-runtime/references/lean_runtime_configuration.md +92 -0
- package/skills/lean-formalization/lean-runtime/references/numina_reverse_analysis.md +80 -0
- package/skills/lean-formalization/lean-runtime/references/numina_runtime.md +41 -0
- package/skills/lean-formalization/lean-runtime/references/numina_subagent_troubleshooting.md +77 -0
- package/skills/lean-formalization/lean-runtime/references/review_checklist.md +10 -0
- package/skills/lean-formalization/lean-runtime/references/specialist_agent_patterns.md +65 -0
- package/skills/lean-formalization/lean-runtime/schemas/config.schema.json +12 -0
- package/skills/lean-formalization/lean-runtime/schemas/result.schema.json +33 -0
- package/skills/lean-formalization/lean-runtime/schemas/task.schema.json +30 -0
- package/skills/lean-formalization/lean-runtime/scripts/ai4m_lean.py +222 -0
- package/skills/lean-formalization/lean-runtime/scripts/check_lean_project.py +134 -0
- package/skills/lean-formalization/lean-runtime/scripts/common.py +296 -0
- package/skills/lean-formalization/lean-runtime/scripts/configure_lean.py +222 -0
- package/skills/lean-formalization/lean-runtime/scripts/detect_sorry.py +49 -0
- package/skills/lean-formalization/lean-runtime/scripts/direct_task.py +100 -0
- package/skills/lean-formalization/lean-runtime/scripts/extract_minimal_failure.py +74 -0
- package/skills/lean-formalization/lean-runtime/scripts/numina_runtime.py +414 -0
- package/skills/lean-formalization/lean-runtime/scripts/smoke_test.py +75 -0
- package/skills/lean-formalization/lean-runtime/scripts/tool_status.py +79 -0
- package/skills/lean-formalization/lean-runtime/scripts/validate_patch.py +81 -0
- package/skills/lean-formalization/lean-runtime/scripts/verify_delivery.py +484 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/after_bad.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/after_statement_changed.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/before.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/failure.lean +7 -0
- package/skills/lean-formalization/lean-runtime/tests/test_check_lean_project.py +59 -0
- package/skills/lean-formalization/lean-runtime/tests/test_cli.py +377 -0
- package/skills/lean-formalization/lean-runtime/tests/test_common_config.py +84 -0
- package/skills/lean-formalization/lean-runtime/tests/test_configure_lean.py +138 -0
- package/skills/lean-formalization/lean-runtime/tests/test_detect_sorry.py +24 -0
- package/skills/lean-formalization/lean-runtime/tests/test_direct_task.py +69 -0
- package/skills/lean-formalization/lean-runtime/tests/test_numina_runtime.py +168 -0
- package/skills/lean-formalization/lean-runtime/tests/test_validate_patch.py +29 -0
- package/skills/lean-setup/SKILL.md +99 -0
- package/skills/least-squares/README.md +85 -0
- package/skills/least-squares/SKILL.md +613 -0
- package/skills/least-squares/examples.md +380 -0
- package/skills/math-beamer/SKILL.md +325 -0
- package/skills/math-beamer/references/build-and-layout-audit.md +63 -0
- package/skills/math-beamer/references/math-domain-patterns.md +56 -0
- package/skills/math-beamer/references/template-policy.md +43 -0
- package/skills/math-beamer/references/template-source-index.md +51 -0
- package/skills/math-beamer/templates/catalog.yaml +235 -0
- package/skills/math-beamer/templates/packs/algebra-number-theory-talk/main.tex +45 -0
- package/skills/math-beamer/templates/packs/algebra-number-theory-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/algorithm-talk/main.tex +43 -0
- package/skills/math-beamer/templates/packs/algorithm-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/analysis-core-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/analysis-core-talk/template.yaml +19 -0
- package/skills/math-beamer/templates/packs/backup-heavy-talk/main.tex +40 -0
- package/skills/math-beamer/templates/packs/backup-heavy-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/beamerposter-a0/main.tex +45 -0
- package/skills/math-beamer/templates/packs/beamerposter-a0/template.yaml +12 -0
- package/skills/math-beamer/templates/packs/combinatorics-graph-talk/main.tex +53 -0
- package/skills/math-beamer/templates/packs/combinatorics-graph-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/control-systems-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/control-systems-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/course-lecture-zh/main.tex +43 -0
- package/skills/math-beamer/templates/packs/course-lecture-zh/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/data-experiment-results/main.tex +45 -0
- package/skills/math-beamer/templates/packs/data-experiment-results/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/dynamical-systems-talk/main.tex +42 -0
- package/skills/math-beamer/templates/packs/dynamical-systems-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/geometry-topology-talk/main.tex +43 -0
- package/skills/math-beamer/templates/packs/geometry-topology-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/information-coding-crypto-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/information-coding-crypto-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/logic-foundations-category-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/logic-foundations-category-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/numerical-analysis-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/numerical-analysis-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/optimization-talk/main.tex +58 -0
- package/skills/math-beamer/templates/packs/optimization-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/pde-analysis-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/pde-analysis-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/probability-statistics-talk/main.tex +52 -0
- package/skills/math-beamer/templates/packs/probability-statistics-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/reading-group-paper/main.tex +46 -0
- package/skills/math-beamer/templates/packs/reading-group-paper/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-en/main.tex +60 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-en/template.yaml +13 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-zh/main.tex +60 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-zh/template.yaml +13 -0
- package/skills/math-beamer/templates/packs/theorem-proof-talk/main.tex +60 -0
- package/skills/math-beamer/templates/packs/theorem-proof-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/thesis-defense/main.tex +42 -0
- package/skills/math-beamer/templates/packs/thesis-defense/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/workshop-tutorial/main.tex +32 -0
- package/skills/math-beamer/templates/packs/workshop-tutorial/template.yaml +9 -0
- package/skills/math-beamer/templates/preview-gallery.md +32 -0
- package/skills/math-beamer/templates/previews/algebra-number-theory-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/algebra-number-theory-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/algorithm-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/algorithm-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/analysis-core-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/analysis-core-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/backup-heavy-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/backup-heavy-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/beamerposter-a0-content.png +0 -0
- package/skills/math-beamer/templates/previews/beamerposter-a0-cover.png +0 -0
- package/skills/math-beamer/templates/previews/combinatorics-graph-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/combinatorics-graph-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/control-systems-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/control-systems-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/course-lecture-zh-content.png +0 -0
- package/skills/math-beamer/templates/previews/course-lecture-zh-cover.png +0 -0
- package/skills/math-beamer/templates/previews/data-experiment-results-content.png +0 -0
- package/skills/math-beamer/templates/previews/data-experiment-results-cover.png +0 -0
- package/skills/math-beamer/templates/previews/dynamical-systems-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/dynamical-systems-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/geometry-topology-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/geometry-topology-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/information-coding-crypto-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/information-coding-crypto-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/logic-foundations-category-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/logic-foundations-category-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/numerical-analysis-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/numerical-analysis-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/optimization-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/optimization-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/pde-analysis-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/pde-analysis-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/probability-statistics-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/probability-statistics-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/reading-group-paper-content.png +0 -0
- package/skills/math-beamer/templates/previews/reading-group-paper-cover.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-en-content.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-en-cover.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-zh-content.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-zh-cover.png +0 -0
- package/skills/math-beamer/templates/previews/theorem-proof-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/theorem-proof-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/thesis-defense-content.png +0 -0
- package/skills/math-beamer/templates/previews/thesis-defense-cover.png +0 -0
- package/skills/math-beamer/templates/previews/workshop-tutorial-content.png +0 -0
- package/skills/math-beamer/templates/previews/workshop-tutorial-cover.png +0 -0
- package/skills/math-beamer/templates/review-report.md +54 -0
- package/skills/math-beamer/templates/slide-source-ledger.md +22 -0
- package/skills/math-beamer/templates/source-packet.md +50 -0
- package/skills/math-glossary/SKILL.md +347 -0
- package/skills/math-glossary/references/glossary-schema.md +71 -0
- package/skills/math-glossary/references/glossary-usage-guide.md +176 -0
- package/skills/math-glossary/references/glossary-web-template.md +127 -0
- package/skills/math-glossary/references/glossary.schema.json +76 -0
- package/skills/math-glossary/references/user-manual.md +435 -0
- package/skills/math-glossary/requirements-ai.txt +3 -0
- package/skills/math-glossary/requirements.txt +3 -0
- package/skills/math-glossary/scripts/glossary_backup.py +42 -0
- package/skills/math-glossary/scripts/glossary_build.py +467 -0
- package/skills/math-glossary/scripts/glossary_enrich_examples.py +283 -0
- package/skills/math-glossary/scripts/glossary_export.py +333 -0
- package/skills/math-glossary/scripts/glossary_web.py +1265 -0
- package/skills/math-paper-reading/README.md +163 -0
- package/skills/math-paper-reading/SKILL.md +48 -0
- package/skills/math-paper-reading/agent_router.md +36 -0
- package/skills/math-paper-reading/references/proof_pattern_taxonomy.md +123 -0
- package/skills/math-paper-reading/references/quality_rubric.md +36 -0
- package/skills/math-paper-reading/skill_base.md +627 -0
- package/skills/math-paper-reading/skill_literature_search.md +866 -0
- package/skills/math-paper-reading/skill_paper_deep_read.md +1024 -0
- package/skills/math-paper-reading/skill_pathway_proof.md +718 -0
- package/skills/math-paper-reading/skill_reference_manager.md +746 -0
- package/skills/matlab-runner/SKILL.md +217 -0
- package/skills/matlab-runner/examples/README.md +33 -0
- package/skills/matlab-runner/examples/matrix_report.m +28 -0
- package/skills/matlab-runner/examples/plot_damped_sine.m +64 -0
- package/skills/matlab-runner/examples/test_vector_norm.m +12 -0
- package/skills/matlab-runner/references/client-setup.md +24 -0
- package/skills/matlab-runner/references/file-session-safety.md +86 -0
- package/skills/matlab-runner/references/numerical-safety.md +126 -0
- package/skills/matlab-runner/references/resource-safety.md +125 -0
- package/skills/mixed-integer-programming/SKILL.md +119 -0
- package/skills/mixed-integer-programming/lp-examples.md +251 -0
- package/skills/mixed-integer-programming/mip-examples-verified.md +118 -0
- package/skills/open-problem-research-pipeline/SKILL.md +76 -0
- package/skills/open-problem-research-pipeline/skill_literature_analysis.md +201 -0
- package/skills/open-problem-research-pipeline/skill_literature_proof_framework.md +206 -0
- package/skills/open-problem-research-pipeline/skill_literature_search.md +172 -0
- package/skills/open-problem-research-pipeline/skill_qskill.md +177 -0
- package/skills/osqp-solver/SKILL.md +245 -0
- package/skills/osqp-solver/references/citation.md +25 -0
- package/skills/osqp-solver/references/dual-infeasible-example.json +14 -0
- package/skills/osqp-solver/references/feasible-example.json +21 -0
- package/skills/osqp-solver/references/primal-infeasible-example.json +17 -0
- package/skills/osqp-solver/references/python-api.md +95 -0
- package/skills/osqp-solver/references/verification.md +94 -0
- package/skills/osqp-solver/scripts/check_osqp_environment.py +120 -0
- package/skills/osqp-solver/scripts/solve_qp.py +943 -0
- package/skills/paper-writing/SKILL.md +115 -0
- package/skills/paper-writing/skill_claim_evidence_ledger.md +55 -0
- package/skills/paper-writing/skill_formula_environment_and_readability.md +41 -0
- package/skills/paper-writing/skill_latex_build_and_layout_audit.md +73 -0
- package/skills/paper-writing/skill_notation_and_variable_consistency.md +42 -0
- package/skills/paper-writing/skill_paper_skeleton_and_logical_architecture.md +54 -0
- package/skills/paper-writing/skill_proof_obligation_and_assumption_audit.md +57 -0
- package/skills/paper-writing/templates/claim-evidence-ledger.md +23 -0
- package/skills/paper-writing/templates/proof-obligation-ledger.md +22 -0
- package/skills/paper-writing/templates/rapid-prototype-paper-skeleton.md +46 -0
- package/skills/paper-writing/templates/source-packet.md +37 -0
- package/skills/paper-writing/templates/submission-readiness.md +41 -0
- package/skills/proof-blueprint-review/SKILL.md +129 -0
- package/skills/proof-blueprint-review/references/agent-mediated-proof-protocol.md +57 -0
- package/skills/proof-blueprint-review/references/generation-agent.md +45 -0
- package/skills/proof-blueprint-review/references/proof-acceptance-contract.md +41 -0
- package/skills/proof-blueprint-review/references/verification-agent.md +37 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import shlex
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
import tomllib
|
|
14
|
+
except ModuleNotFoundError: # pragma: no cover
|
|
15
|
+
try:
|
|
16
|
+
import tomli as tomllib # type: ignore[no-redef]
|
|
17
|
+
except ModuleNotFoundError:
|
|
18
|
+
tomllib = None # type: ignore[assignment]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
SKILL_ROOT = Path(__file__).resolve().parents[1]
|
|
22
|
+
DEFAULT_CONFIG = SKILL_ROOT / "config" / "lean_agent.example.toml"
|
|
23
|
+
ENV_KEY_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
24
|
+
CANONICAL_LEAN_TOOLCHAIN = "leanprover/lean4:v4.28.0"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def deep_merge(base: dict[str, Any], overlay: dict[str, Any]) -> dict[str, Any]:
|
|
28
|
+
result = dict(base)
|
|
29
|
+
for key, value in overlay.items():
|
|
30
|
+
if isinstance(value, dict) and isinstance(result.get(key), dict):
|
|
31
|
+
result[key] = deep_merge(result[key], value)
|
|
32
|
+
else:
|
|
33
|
+
result[key] = value
|
|
34
|
+
return result
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _parse_simple_value(raw: str) -> Any:
|
|
38
|
+
value = raw.strip().rstrip(",")
|
|
39
|
+
if value in {"true", "false"}:
|
|
40
|
+
return value == "true"
|
|
41
|
+
if value.startswith('"') and value.endswith('"'):
|
|
42
|
+
try:
|
|
43
|
+
return json.loads(value)
|
|
44
|
+
except json.JSONDecodeError:
|
|
45
|
+
return value[1:-1]
|
|
46
|
+
try:
|
|
47
|
+
return int(value)
|
|
48
|
+
except ValueError:
|
|
49
|
+
return value
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _load_simple_toml(path: Path) -> dict[str, Any]:
|
|
53
|
+
data: dict[str, Any] = {}
|
|
54
|
+
section: dict[str, Any] = data
|
|
55
|
+
for raw_line in path.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
56
|
+
line = raw_line.strip()
|
|
57
|
+
if not line or line.startswith("#"):
|
|
58
|
+
continue
|
|
59
|
+
if line.startswith("[") and line.endswith("]"):
|
|
60
|
+
section_name = line[1:-1].strip()
|
|
61
|
+
section = data.setdefault(section_name, {})
|
|
62
|
+
continue
|
|
63
|
+
if "=" not in line:
|
|
64
|
+
continue
|
|
65
|
+
key, value = line.split("=", 1)
|
|
66
|
+
section[key.strip()] = _parse_simple_value(value)
|
|
67
|
+
return data
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def load_toml(path: Path) -> dict[str, Any]:
|
|
71
|
+
if not path.exists():
|
|
72
|
+
return {}
|
|
73
|
+
if tomllib is None:
|
|
74
|
+
return _load_simple_toml(path)
|
|
75
|
+
with path.open("rb") as handle:
|
|
76
|
+
data = tomllib.load(handle)
|
|
77
|
+
return data if isinstance(data, dict) else {}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def expand_path(value: str | None, cwd: Path) -> Path | None:
|
|
81
|
+
if not value:
|
|
82
|
+
return None
|
|
83
|
+
expanded = os.path.expandvars(os.path.expanduser(value))
|
|
84
|
+
path = Path(expanded)
|
|
85
|
+
if not path.is_absolute():
|
|
86
|
+
path = cwd / path
|
|
87
|
+
return Path(os.path.abspath(path))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def ai4math_home(cwd: str | Path = ".") -> Path:
|
|
91
|
+
cwd_path = Path(cwd).resolve()
|
|
92
|
+
override = os.environ.get("AI4MATH_HOME")
|
|
93
|
+
if override:
|
|
94
|
+
return expand_path(override, cwd_path) or (Path.home() / ".ai4math")
|
|
95
|
+
return Path.home() / ".ai4math"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def read_config(cwd: str | Path = ".", config_path: str | Path | None = None) -> dict[str, Any]:
|
|
99
|
+
cwd_path = Path(cwd).resolve()
|
|
100
|
+
config = load_toml(DEFAULT_CONFIG)
|
|
101
|
+
if config_path:
|
|
102
|
+
config = deep_merge(config, load_toml(Path(config_path).expanduser()))
|
|
103
|
+
local_config = cwd_path / ".ai4math" / "lean_agent.local.toml"
|
|
104
|
+
config = deep_merge(config, load_toml(local_config))
|
|
105
|
+
|
|
106
|
+
home = ai4math_home(cwd_path)
|
|
107
|
+
env_values = {
|
|
108
|
+
"managed_workspace_path": os.environ.get("AI4MATH_LEAN_WORKSPACE"),
|
|
109
|
+
"preferred_toolchain": os.environ.get("AI4MATH_LEAN_TOOLCHAIN"),
|
|
110
|
+
}
|
|
111
|
+
if os.environ.get("AI4MATH_HOME") and not env_values["managed_workspace_path"]:
|
|
112
|
+
env_values["managed_workspace_path"] = str(home / "lean-workspace")
|
|
113
|
+
env_values["managed_workspace_root"] = str(home / "lean-workspaces")
|
|
114
|
+
env_lean = {key: value for key, value in env_values.items() if value}
|
|
115
|
+
if env_lean:
|
|
116
|
+
config = deep_merge(config, {"lean": env_lean})
|
|
117
|
+
return config
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def ensure_ai4math_gitignore(cwd: str | Path) -> Path:
|
|
121
|
+
ai4math = Path(cwd).resolve() / ".ai4math"
|
|
122
|
+
ai4math.mkdir(parents=True, exist_ok=True)
|
|
123
|
+
gitignore = ai4math / ".gitignore"
|
|
124
|
+
entries = [
|
|
125
|
+
"lean_agent.local.toml",
|
|
126
|
+
".env.local",
|
|
127
|
+
"lean-workspace/",
|
|
128
|
+
"lean-workspaces/",
|
|
129
|
+
"numina-runtime/",
|
|
130
|
+
"logs/",
|
|
131
|
+
"failures/",
|
|
132
|
+
]
|
|
133
|
+
current = gitignore.read_text(encoding="utf-8").splitlines() if gitignore.exists() else []
|
|
134
|
+
merged = list(current)
|
|
135
|
+
for entry in entries:
|
|
136
|
+
if entry not in merged:
|
|
137
|
+
merged.append(entry)
|
|
138
|
+
gitignore.write_text("\n".join(merged).rstrip() + "\n", encoding="utf-8")
|
|
139
|
+
return gitignore
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def write_local_toml(cwd: str | Path, values: dict[str, dict[str, Any]]) -> Path:
|
|
143
|
+
ensure_ai4math_gitignore(cwd)
|
|
144
|
+
path = Path(cwd).resolve() / ".ai4math" / "lean_agent.local.toml"
|
|
145
|
+
lines: list[str] = []
|
|
146
|
+
for section, items in values.items():
|
|
147
|
+
lines.append(f"[{section}]")
|
|
148
|
+
for key, value in items.items():
|
|
149
|
+
if isinstance(value, bool):
|
|
150
|
+
rendered = "true" if value else "false"
|
|
151
|
+
elif isinstance(value, (int, float)):
|
|
152
|
+
rendered = str(value)
|
|
153
|
+
else:
|
|
154
|
+
rendered = json.dumps(str(value))
|
|
155
|
+
lines.append(f"{key} = {rendered}")
|
|
156
|
+
lines.append("")
|
|
157
|
+
path.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8")
|
|
158
|
+
return path
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def update_local_toml(cwd: str | Path, values: dict[str, dict[str, Any]]) -> Path:
|
|
162
|
+
local_config = Path(cwd).resolve() / ".ai4math" / "lean_agent.local.toml"
|
|
163
|
+
merged = deep_merge(load_toml(local_config), values)
|
|
164
|
+
return write_local_toml(cwd, merged)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def read_env_local(cwd: str | Path) -> dict[str, str]:
|
|
168
|
+
path = Path(cwd).resolve() / ".ai4math" / ".env.local"
|
|
169
|
+
if not path.exists():
|
|
170
|
+
return {}
|
|
171
|
+
values: dict[str, str] = {}
|
|
172
|
+
for raw_line in path.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
173
|
+
line = raw_line.strip()
|
|
174
|
+
if not line or line.startswith("#"):
|
|
175
|
+
continue
|
|
176
|
+
try:
|
|
177
|
+
parts = shlex.split(line, comments=True, posix=True)
|
|
178
|
+
except ValueError:
|
|
179
|
+
continue
|
|
180
|
+
if parts and parts[0] == "export":
|
|
181
|
+
parts = parts[1:]
|
|
182
|
+
for part in parts:
|
|
183
|
+
if "=" not in part:
|
|
184
|
+
continue
|
|
185
|
+
key, value = part.split("=", 1)
|
|
186
|
+
if ENV_KEY_RE.match(key):
|
|
187
|
+
values[key] = value
|
|
188
|
+
return values
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def write_env_local(cwd: str | Path, values: dict[str, str]) -> Path:
|
|
192
|
+
ensure_ai4math_gitignore(cwd)
|
|
193
|
+
path = Path(cwd).resolve() / ".ai4math" / ".env.local"
|
|
194
|
+
invalid = [key for key in values if not ENV_KEY_RE.match(key)]
|
|
195
|
+
if invalid:
|
|
196
|
+
raise ValueError(f"Invalid environment variable name(s): {', '.join(invalid)}")
|
|
197
|
+
|
|
198
|
+
current = path.read_text(encoding="utf-8").splitlines() if path.exists() else []
|
|
199
|
+
keys = set(values)
|
|
200
|
+
kept: list[str] = []
|
|
201
|
+
for raw_line in current:
|
|
202
|
+
stripped = raw_line.strip()
|
|
203
|
+
if not stripped or stripped.startswith("#"):
|
|
204
|
+
kept.append(raw_line)
|
|
205
|
+
continue
|
|
206
|
+
try:
|
|
207
|
+
parts = shlex.split(stripped, comments=True, posix=True)
|
|
208
|
+
except ValueError:
|
|
209
|
+
kept.append(raw_line)
|
|
210
|
+
continue
|
|
211
|
+
if parts and parts[0] == "export":
|
|
212
|
+
parts = parts[1:]
|
|
213
|
+
assigned_keys = {part.split("=", 1)[0] for part in parts if "=" in part}
|
|
214
|
+
if assigned_keys & keys:
|
|
215
|
+
continue
|
|
216
|
+
kept.append(raw_line)
|
|
217
|
+
|
|
218
|
+
if kept and kept[-1].strip():
|
|
219
|
+
kept.append("")
|
|
220
|
+
for key, value in values.items():
|
|
221
|
+
kept.append(f"export {key}={shlex.quote(str(value))}")
|
|
222
|
+
path.write_text("\n".join(kept).rstrip() + "\n", encoding="utf-8")
|
|
223
|
+
return path
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def run_command(
|
|
227
|
+
command: list[str],
|
|
228
|
+
cwd: str | Path | None = None,
|
|
229
|
+
timeout: int = 300,
|
|
230
|
+
env: dict[str, str] | None = None,
|
|
231
|
+
) -> dict[str, Any]:
|
|
232
|
+
def to_text(value: Any) -> str:
|
|
233
|
+
if value is None:
|
|
234
|
+
return ""
|
|
235
|
+
if isinstance(value, bytes):
|
|
236
|
+
return value.decode("utf-8", errors="replace")
|
|
237
|
+
return str(value)
|
|
238
|
+
|
|
239
|
+
try:
|
|
240
|
+
command_env = None
|
|
241
|
+
if env is not None:
|
|
242
|
+
command_env = os.environ.copy()
|
|
243
|
+
command_env.update(env)
|
|
244
|
+
completed = subprocess.run(
|
|
245
|
+
command,
|
|
246
|
+
cwd=str(cwd) if cwd else None,
|
|
247
|
+
env=command_env,
|
|
248
|
+
text=True,
|
|
249
|
+
capture_output=True,
|
|
250
|
+
timeout=timeout,
|
|
251
|
+
check=False,
|
|
252
|
+
)
|
|
253
|
+
except FileNotFoundError as exc:
|
|
254
|
+
return {
|
|
255
|
+
"ok": False,
|
|
256
|
+
"returncode": 127,
|
|
257
|
+
"stdout": "",
|
|
258
|
+
"stderr": str(exc),
|
|
259
|
+
"command": command,
|
|
260
|
+
}
|
|
261
|
+
except subprocess.TimeoutExpired as exc:
|
|
262
|
+
return {
|
|
263
|
+
"ok": False,
|
|
264
|
+
"returncode": 124,
|
|
265
|
+
"stdout": to_text(exc.stdout),
|
|
266
|
+
"stderr": to_text(exc.stderr) or f"Timed out after {timeout}s",
|
|
267
|
+
"command": command,
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
"ok": completed.returncode == 0,
|
|
271
|
+
"returncode": completed.returncode,
|
|
272
|
+
"stdout": to_text(completed.stdout),
|
|
273
|
+
"stderr": to_text(completed.stderr),
|
|
274
|
+
"command": command,
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def emit_json(result: dict[str, Any], json_output: str | None = None) -> None:
|
|
279
|
+
payload = json.dumps(result, ensure_ascii=False, indent=2)
|
|
280
|
+
if json_output:
|
|
281
|
+
Path(json_output).expanduser().write_text(payload + "\n", encoding="utf-8")
|
|
282
|
+
print(payload)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def exit_with(result: dict[str, Any], json_output: str | None = None, code: int | None = None) -> None:
|
|
286
|
+
emit_json(result, json_output=json_output)
|
|
287
|
+
if code is None:
|
|
288
|
+
code = 0 if result.get("ok") else 1
|
|
289
|
+
sys.exit(code)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def rel_or_abs(path: Path) -> str:
|
|
293
|
+
try:
|
|
294
|
+
return str(path.relative_to(Path.cwd()))
|
|
295
|
+
except ValueError:
|
|
296
|
+
return str(path)
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from check_lean_project import find_project_root, read_mathlib_revision, read_toolchain
|
|
8
|
+
from common import CANONICAL_LEAN_TOOLCHAIN, ai4math_home, ensure_ai4math_gitignore, expand_path, read_config, run_command, update_local_toml
|
|
9
|
+
from numina_runtime import execute_configure_plan, numina_readiness
|
|
10
|
+
from tool_status import find_tool
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _safe_lake_name(name: str) -> str:
|
|
14
|
+
cleaned = "".join(ch if ch.isalnum() or ch == "_" else "_" for ch in name).strip("_")
|
|
15
|
+
if not cleaned:
|
|
16
|
+
return "lean_workspace"
|
|
17
|
+
if cleaned[0].isdigit():
|
|
18
|
+
cleaned = f"lean_{cleaned}"
|
|
19
|
+
return cleaned
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _lake_command(lake: str, toolchain: str | None, args: list[str]) -> list[str]:
|
|
23
|
+
if toolchain and toolchain != "auto":
|
|
24
|
+
return [find_tool("elan") or "elan", "run", toolchain, "lake", *args]
|
|
25
|
+
return [lake, *args]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _workspace_setup_commands(lake: str, toolchain: str | None, workspace: Path) -> list[list[str]]:
|
|
29
|
+
commands: list[list[str]] = []
|
|
30
|
+
if not (workspace / "lake-manifest.json").exists():
|
|
31
|
+
commands.append(_lake_command(lake, toolchain, ["update"]))
|
|
32
|
+
commands.extend([
|
|
33
|
+
_lake_command(lake, toolchain, ["exe", "cache", "get"]),
|
|
34
|
+
_lake_command(lake, toolchain, ["build"]),
|
|
35
|
+
])
|
|
36
|
+
return commands
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _effective_managed_toolchain(config: dict[str, Any], requested_toolchain: str | None) -> str:
|
|
40
|
+
if requested_toolchain is not None:
|
|
41
|
+
return requested_toolchain.strip() or CANONICAL_LEAN_TOOLCHAIN
|
|
42
|
+
configured = str(config.get("lean", {}).get("preferred_toolchain") or "").strip()
|
|
43
|
+
if configured and configured != "auto":
|
|
44
|
+
return configured
|
|
45
|
+
return CANONICAL_LEAN_TOOLCHAIN
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _workspace_needs_setup(workspace: Path) -> bool:
|
|
49
|
+
return not ((workspace / "lake-manifest.json").exists() and (workspace / ".lake").exists())
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _action_failed(action: dict[str, Any]) -> bool:
|
|
53
|
+
return not action.get("skipped") and not action.get("recoverable") and not action.get("ok", False)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _workspace_info(path: Path) -> dict[str, Any]:
|
|
57
|
+
root = find_project_root(path)
|
|
58
|
+
return {
|
|
59
|
+
"path": str(path),
|
|
60
|
+
"exists": path.exists(),
|
|
61
|
+
"project_root": str(root) if root else None,
|
|
62
|
+
"toolchain": read_toolchain(root),
|
|
63
|
+
"mathlib_revision": read_mathlib_revision(root),
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def inspect_environment(cwd: str | Path = ".", config_path: str | Path | None = None, target: str | Path | None = None) -> dict[str, Any]:
|
|
68
|
+
cwd_path = Path(cwd).resolve()
|
|
69
|
+
config = read_config(cwd_path, config_path)
|
|
70
|
+
lean = config.get("lean", {})
|
|
71
|
+
target_path = Path(target).expanduser().resolve() if target else cwd_path
|
|
72
|
+
project_root = find_project_root(target_path)
|
|
73
|
+
workspace_path = expand_path(lean.get("managed_workspace_path"), cwd_path) or (ai4math_home(cwd_path) / "lean-workspace")
|
|
74
|
+
workspace_root = find_project_root(workspace_path) if workspace_path.exists() else None
|
|
75
|
+
|
|
76
|
+
missing: list[str] = []
|
|
77
|
+
required_inputs: list[str] = []
|
|
78
|
+
if project_root is None and workspace_root is None:
|
|
79
|
+
missing.append("lean_workspace")
|
|
80
|
+
required_inputs.append("existing Lake project or permission to create/reuse the shared Lean workspace")
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
"ok": not missing,
|
|
84
|
+
"status": "configured" if not missing else "missing_config",
|
|
85
|
+
"configuration_status": "configured" if not missing else "missing_config",
|
|
86
|
+
"cwd": str(cwd_path),
|
|
87
|
+
"agent": {
|
|
88
|
+
"mode": "coding-agent",
|
|
89
|
+
"backend": "none",
|
|
90
|
+
"numina_required": False,
|
|
91
|
+
"numina_runtime": "built_in_recipe",
|
|
92
|
+
},
|
|
93
|
+
"lean": {
|
|
94
|
+
"target": str(target_path),
|
|
95
|
+
"project_root": str(project_root) if project_root else None,
|
|
96
|
+
"toolchain": read_toolchain(project_root),
|
|
97
|
+
"mathlib_revision": read_mathlib_revision(project_root),
|
|
98
|
+
"workspace_path": str(workspace_path),
|
|
99
|
+
"workspace_project_root": str(workspace_root) if workspace_root else None,
|
|
100
|
+
"workspace_toolchain": read_toolchain(workspace_root),
|
|
101
|
+
"workspace_mathlib_revision": read_mathlib_revision(workspace_root),
|
|
102
|
+
"workspace_mode": lean.get("workspace_mode", "reuse-managed"),
|
|
103
|
+
"align_workspace_versions": bool(lean.get("align_workspace_versions", True)),
|
|
104
|
+
},
|
|
105
|
+
"numina": numina_readiness(cwd_path, target=target_path),
|
|
106
|
+
"missing_config": missing,
|
|
107
|
+
"required_inputs": required_inputs,
|
|
108
|
+
"recommended_next_action": "run configure --create-workspace or provide a Lake project" if missing else "ready",
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def configure(
|
|
113
|
+
cwd: str | Path = ".",
|
|
114
|
+
config_path: str | Path | None = None,
|
|
115
|
+
target: str | Path | None = None,
|
|
116
|
+
create_workspace: bool = False,
|
|
117
|
+
toolchain: str | None = None,
|
|
118
|
+
save_local: bool = False,
|
|
119
|
+
dry_run: bool = False,
|
|
120
|
+
setup_numina: bool = False,
|
|
121
|
+
project_name: str | None = None,
|
|
122
|
+
) -> dict[str, Any]:
|
|
123
|
+
cwd_path = Path(cwd).resolve()
|
|
124
|
+
if not dry_run:
|
|
125
|
+
ensure_ai4math_gitignore(cwd_path)
|
|
126
|
+
|
|
127
|
+
config = read_config(cwd_path, config_path)
|
|
128
|
+
lean = config.get("lean", {})
|
|
129
|
+
effective_toolchain = _effective_managed_toolchain(config, toolchain)
|
|
130
|
+
workspace = expand_path(lean.get("managed_workspace_path"), cwd_path) or (ai4math_home(cwd_path) / "lean-workspace")
|
|
131
|
+
workspace_root_config = expand_path(lean.get("managed_workspace_root"), cwd_path) or (workspace.parent / "lean-workspaces")
|
|
132
|
+
workspace_actions: list[dict[str, Any]] = []
|
|
133
|
+
if create_workspace:
|
|
134
|
+
lake = find_tool("lake") or "lake"
|
|
135
|
+
parent = workspace.parent
|
|
136
|
+
lake_project_name = _safe_lake_name(workspace.name)
|
|
137
|
+
created_path = parent / lake_project_name
|
|
138
|
+
if dry_run:
|
|
139
|
+
workspace_actions.append({
|
|
140
|
+
"command": _lake_command(lake, effective_toolchain, ["new", lake_project_name, "math"]),
|
|
141
|
+
"cwd": str(parent),
|
|
142
|
+
"skipped": True,
|
|
143
|
+
"reason": "dry_run",
|
|
144
|
+
})
|
|
145
|
+
elif not workspace.exists():
|
|
146
|
+
parent.mkdir(parents=True, exist_ok=True)
|
|
147
|
+
created = run_command(_lake_command(lake, effective_toolchain, ["new", lake_project_name, "math"]), cwd=parent)
|
|
148
|
+
if not created.get("ok") and find_project_root(created_path):
|
|
149
|
+
created["recoverable"] = True
|
|
150
|
+
created["recovered_by"] = "lake_project_files_created"
|
|
151
|
+
workspace_actions.append(created)
|
|
152
|
+
if created.get("ok") or created.get("recoverable"):
|
|
153
|
+
if created_path != workspace:
|
|
154
|
+
shutil.move(str(created_path), str(workspace))
|
|
155
|
+
workspace_actions.append({
|
|
156
|
+
"ok": True,
|
|
157
|
+
"command": ["move", str(created_path), str(workspace)],
|
|
158
|
+
"returncode": 0,
|
|
159
|
+
"stdout": "",
|
|
160
|
+
"stderr": "",
|
|
161
|
+
})
|
|
162
|
+
elif find_project_root(workspace):
|
|
163
|
+
workspace_actions.append({
|
|
164
|
+
"ok": True,
|
|
165
|
+
"command": ["reuse", str(workspace)],
|
|
166
|
+
"returncode": 0,
|
|
167
|
+
"stdout": "",
|
|
168
|
+
"stderr": "",
|
|
169
|
+
})
|
|
170
|
+
if not dry_run and find_project_root(workspace) and _workspace_needs_setup(workspace):
|
|
171
|
+
for command in _workspace_setup_commands(lake, effective_toolchain, workspace):
|
|
172
|
+
workspace_actions.append(run_command(command, cwd=workspace, timeout=1800))
|
|
173
|
+
|
|
174
|
+
workspace_info = _workspace_info(workspace)
|
|
175
|
+
workspace_failed = create_workspace and not dry_run and (
|
|
176
|
+
not workspace_info["project_root"] or any(_action_failed(action) for action in workspace_actions)
|
|
177
|
+
)
|
|
178
|
+
if (save_local or create_workspace) and not dry_run:
|
|
179
|
+
update_local_toml(cwd_path, {
|
|
180
|
+
"lean": {
|
|
181
|
+
"workspace_mode": "reuse-managed",
|
|
182
|
+
"managed_workspace_path": str(workspace),
|
|
183
|
+
"managed_workspace_root": str(workspace_root_config),
|
|
184
|
+
"reuse_managed_workspace": True,
|
|
185
|
+
"workspace_key": "lean-toolchain",
|
|
186
|
+
"align_workspace_versions": True,
|
|
187
|
+
"preferred_toolchain": effective_toolchain,
|
|
188
|
+
"preferred_mathlib_rev": "auto",
|
|
189
|
+
"allow_user_project_version_changes": False,
|
|
190
|
+
},
|
|
191
|
+
"agent": {
|
|
192
|
+
"mode": "coding-agent",
|
|
193
|
+
"backend": "none",
|
|
194
|
+
},
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
env = inspect_environment(cwd_path, config_path=config_path, target=target)
|
|
198
|
+
env["workspace"] = workspace_info
|
|
199
|
+
env["workspace_actions"] = workspace_actions
|
|
200
|
+
if setup_numina:
|
|
201
|
+
numina_result = execute_configure_plan(
|
|
202
|
+
cwd_path,
|
|
203
|
+
project_name=project_name,
|
|
204
|
+
dry_run=dry_run,
|
|
205
|
+
)
|
|
206
|
+
env["numina"] = numina_result
|
|
207
|
+
if numina_result.get("ok"):
|
|
208
|
+
env["ok"] = True
|
|
209
|
+
env["status"] = numina_result.get("status", "numina_configured")
|
|
210
|
+
env["configuration_status"] = "numina_runtime_ready" if not dry_run else "numina_runtime_dry_run"
|
|
211
|
+
env["recommended_next_action"] = numina_result.get("recommended_next_action", "ready")
|
|
212
|
+
else:
|
|
213
|
+
env["ok"] = False
|
|
214
|
+
env["status"] = "interactive_required" if numina_result.get("status") == "missing_project_name" else numina_result.get("status", "numina_setup_failed")
|
|
215
|
+
env["configuration_status"] = "numina_runtime_missing_config"
|
|
216
|
+
env["recommended_next_action"] = numina_result.get("recommended_next_action", "review Numina setup requirements")
|
|
217
|
+
if workspace_failed:
|
|
218
|
+
env["ok"] = False
|
|
219
|
+
env["status"] = "lean_workspace_setup_failed"
|
|
220
|
+
env["configuration_status"] = "missing_config"
|
|
221
|
+
env["recommended_next_action"] = "retry configure --create-workspace after Lean/mathlib download succeeds"
|
|
222
|
+
return env
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
FORBIDDEN_PATTERNS = {
|
|
9
|
+
"sorry": re.compile(r"\bsorry\b"),
|
|
10
|
+
"admit": re.compile(r"\badmit\b"),
|
|
11
|
+
"axiom": re.compile(r"^\s*axiom\b"),
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _strip_line_comment(line: str) -> str:
|
|
16
|
+
return line.split("--", 1)[0]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def scan_text(text: str, source: str = "<text>") -> dict[str, Any]:
|
|
20
|
+
findings: list[dict[str, Any]] = []
|
|
21
|
+
for line_no, line in enumerate(text.splitlines(), start=1):
|
|
22
|
+
code = _strip_line_comment(line)
|
|
23
|
+
for kind, pattern in FORBIDDEN_PATTERNS.items():
|
|
24
|
+
if pattern.search(code):
|
|
25
|
+
findings.append({
|
|
26
|
+
"kind": kind,
|
|
27
|
+
"file": source,
|
|
28
|
+
"line": line_no,
|
|
29
|
+
"text": line.rstrip(),
|
|
30
|
+
})
|
|
31
|
+
return {
|
|
32
|
+
"ok": not findings,
|
|
33
|
+
"status": "ok" if not findings else "forbidden_tokens_found",
|
|
34
|
+
"findings": findings,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def scan_file(path: str | Path) -> dict[str, Any]:
|
|
39
|
+
file_path = Path(path).expanduser().resolve()
|
|
40
|
+
if not file_path.exists():
|
|
41
|
+
return {
|
|
42
|
+
"ok": False,
|
|
43
|
+
"status": "file_not_found",
|
|
44
|
+
"findings": [],
|
|
45
|
+
"errors": [{"severity": "error", "data": f"File not found: {file_path}"}],
|
|
46
|
+
}
|
|
47
|
+
result = scan_text(file_path.read_text(encoding="utf-8", errors="replace"), source=str(file_path))
|
|
48
|
+
result["file"] = str(file_path)
|
|
49
|
+
return result
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from check_lean_project import find_project_root, read_mathlib_revision, read_toolchain
|
|
7
|
+
from common import ai4math_home, expand_path, read_config
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
TASK_TO_PROMPT = {
|
|
11
|
+
"prove": "prove_theorem.md",
|
|
12
|
+
"formalize": "formalize_statement.md",
|
|
13
|
+
"repair": "repair_lean_file.md",
|
|
14
|
+
"complete-sorries": "complete_sorries.md",
|
|
15
|
+
"batch": "prove_theorem.md",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _managed_workspace_root(config: dict[str, Any], cwd_path: Path) -> tuple[Path | None, Path]:
|
|
20
|
+
lean = config.get("lean", {})
|
|
21
|
+
workspace_path = expand_path(lean.get("managed_workspace_path"), cwd_path) or (ai4math_home(cwd_path) / "lean-workspace")
|
|
22
|
+
workspace_root = find_project_root(workspace_path) if workspace_path.exists() else None
|
|
23
|
+
return workspace_root, workspace_path
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def build_direct_task(
|
|
27
|
+
task_type: str,
|
|
28
|
+
cwd: str | Path,
|
|
29
|
+
target: str | Path,
|
|
30
|
+
max_rounds: int = 5,
|
|
31
|
+
config_path: str | Path | None = None,
|
|
32
|
+
prompt_file: str | Path | None = None,
|
|
33
|
+
result_dir: str | Path | None = None,
|
|
34
|
+
) -> dict[str, Any]:
|
|
35
|
+
cwd_path = Path(cwd).resolve()
|
|
36
|
+
config = read_config(cwd_path, config_path)
|
|
37
|
+
target_path = Path(target).expanduser().resolve()
|
|
38
|
+
target_project_root = find_project_root(target_path)
|
|
39
|
+
workspace_root, workspace_path = _managed_workspace_root(config, cwd_path)
|
|
40
|
+
project_root = target_project_root or workspace_root
|
|
41
|
+
workspace_mode = "target_project" if target_project_root else ("managed_workspace" if workspace_root else "missing")
|
|
42
|
+
skill_root = Path(__file__).resolve().parents[1]
|
|
43
|
+
prompt = Path(prompt_file).expanduser().resolve() if prompt_file else skill_root / "prompts" / TASK_TO_PROMPT.get(task_type, "prove_theorem.md")
|
|
44
|
+
|
|
45
|
+
missing = []
|
|
46
|
+
if project_root is None:
|
|
47
|
+
missing.append("lean_workspace")
|
|
48
|
+
if not target_path.exists() and task_type != "formalize":
|
|
49
|
+
missing.append("target")
|
|
50
|
+
|
|
51
|
+
next_actions = [
|
|
52
|
+
"Read the target Lean file and identify the exact declaration or error.",
|
|
53
|
+
"Run check or lake env lean after each small edit.",
|
|
54
|
+
"Keep theorem statements unchanged unless the user explicitly approves a change.",
|
|
55
|
+
"Stop when the file verifies without sorry/admit/axiom, or return minimize-failure output.",
|
|
56
|
+
]
|
|
57
|
+
if task_type == "formalize":
|
|
58
|
+
next_actions.insert(0, "Draft the Lean declaration from the source statement and ask for user confirmation before long proof work.")
|
|
59
|
+
if task_type == "complete-sorries":
|
|
60
|
+
next_actions.insert(0, "Locate each sorry/admit and solve one minimal target at a time.")
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
"ok": not missing,
|
|
64
|
+
"status": "coding_agent_task_ready" if not missing else "missing_config",
|
|
65
|
+
"agent_mode": "coding-agent",
|
|
66
|
+
"backend": "none",
|
|
67
|
+
"task_type": task_type,
|
|
68
|
+
"target": str(target_path),
|
|
69
|
+
"cwd": str(cwd_path),
|
|
70
|
+
"project_root": str(project_root) if project_root else None,
|
|
71
|
+
"target_project_root": str(target_project_root) if target_project_root else None,
|
|
72
|
+
"workspace_project_root": str(workspace_root) if workspace_root else None,
|
|
73
|
+
"workspace_path": str(workspace_path),
|
|
74
|
+
"workspace_mode": workspace_mode,
|
|
75
|
+
"toolchain": read_toolchain(project_root),
|
|
76
|
+
"mathlib_revision": read_mathlib_revision(project_root),
|
|
77
|
+
"prompt_file": str(prompt),
|
|
78
|
+
"result_dir": str(Path(result_dir).expanduser().resolve()) if result_dir else None,
|
|
79
|
+
"max_rounds": max_rounds,
|
|
80
|
+
"missing_config": missing,
|
|
81
|
+
"required_inputs": ["existing Lake project or shared reusable managed workspace"] if "lean_workspace" in missing else [],
|
|
82
|
+
"recommended_next_action": "run configure --create-workspace for the shared workspace or move target into a Lake project" if missing else "coding agent should edit/check directly; use a backend adapter only if the optional adapter path is approved",
|
|
83
|
+
"direct_workflow": next_actions,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def run_direct_task(
|
|
88
|
+
task_type: str,
|
|
89
|
+
cwd: str | Path,
|
|
90
|
+
target: str | Path,
|
|
91
|
+
max_rounds: int = 5,
|
|
92
|
+
config_path: str | Path | None = None,
|
|
93
|
+
prompt_file: str | Path | None = None,
|
|
94
|
+
result_dir: str | Path | None = None,
|
|
95
|
+
dry_run: bool = False,
|
|
96
|
+
) -> dict[str, Any]:
|
|
97
|
+
task = build_direct_task(task_type, cwd, target, max_rounds, config_path, prompt_file, result_dir)
|
|
98
|
+
if dry_run and task.get("ok"):
|
|
99
|
+
task["status"] = "dry_run"
|
|
100
|
+
return task
|