@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,1144 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { isAbsolute, resolve } from 'node:path';
|
|
5
|
+
import { VeryMathError, isVeryMathError } from '@louisultra/dsh-verymath/core';
|
|
6
|
+
import { bundledSkillsDir, checkGate, computeTopology, dependencySummary, findExcerpt, focusClosure, loadManifest, mergeBatches, parsePaperStructure, readCorpusDocWindow, renderMermaid, resolveCorpusRoot, searchSageRefs, termsFor, upsertTerms } from '@louisultra/dsh-verymath/core';
|
|
7
|
+
export const name = 'tool-verymath';
|
|
8
|
+
export const inject = ['tools', 'verymath', 'fs'];
|
|
9
|
+
export const Config = z.object({
|
|
10
|
+
glossaryHome: z.string(),
|
|
11
|
+
});
|
|
12
|
+
/** Render a glossary query result as a readable bilingual list. */
|
|
13
|
+
function renderGlossaryQuery(_args, value) {
|
|
14
|
+
const result = value;
|
|
15
|
+
if (result.terms.length === 0) {
|
|
16
|
+
return [{ type: 'text', text: `No glossary terms matched ${JSON.stringify(result.query)}.` }];
|
|
17
|
+
}
|
|
18
|
+
const lines = result.terms.map((term) => {
|
|
19
|
+
const extras = [
|
|
20
|
+
term.pos !== undefined ? `pos: ${term.pos}` : undefined,
|
|
21
|
+
term.field !== undefined && term.field.length > 0 ? `field: ${term.field.join(', ')}` : undefined,
|
|
22
|
+
term.source !== undefined ? `source: ${term.source}` : undefined,
|
|
23
|
+
].filter((part) => part !== undefined);
|
|
24
|
+
const examples = term.examples !== undefined && term.examples.length > 0
|
|
25
|
+
? term.examples.slice(0, 2).map(example => ` ex: ${example.en} — ${example.cn}`)
|
|
26
|
+
: [];
|
|
27
|
+
return [
|
|
28
|
+
`- ${term.en} | ${term.cn} (${term.matchedBy}${extras.length > 0 ? `; ${extras.join('; ')}` : ''})`,
|
|
29
|
+
...examples,
|
|
30
|
+
].join('\n');
|
|
31
|
+
});
|
|
32
|
+
const note = result.total > result.terms.length ? `\n(showing ${result.terms.length} of ${result.total})` : '';
|
|
33
|
+
return [{ type: 'text', text: lines.join('\n') + note }];
|
|
34
|
+
}
|
|
35
|
+
/** Render a build-script run as its command trace plus captured output. */
|
|
36
|
+
function renderGlossaryBuild(_args, value) {
|
|
37
|
+
const result = value;
|
|
38
|
+
const parts = [
|
|
39
|
+
`python ${result.script} ${result.args.join(' ')}`,
|
|
40
|
+
`exit code: ${result.exitCode} (${result.status})`,
|
|
41
|
+
];
|
|
42
|
+
if (result.output.length > 0)
|
|
43
|
+
parts.push('', result.output);
|
|
44
|
+
return [{ type: 'text', text: parts.join('\n') }];
|
|
45
|
+
}
|
|
46
|
+
/** Render the Python availability check. */
|
|
47
|
+
function renderPythonCheck(_args, value) {
|
|
48
|
+
const result = value;
|
|
49
|
+
return [{
|
|
50
|
+
type: 'text',
|
|
51
|
+
text: result.available
|
|
52
|
+
? `Python available: ${result.executable}`
|
|
53
|
+
: `Python unavailable. ${result.hint ?? 'Install Python 3 to use the math-glossary scripts.'}`,
|
|
54
|
+
}];
|
|
55
|
+
}
|
|
56
|
+
/** Render a paper-structure validation as a compact structural digest. */
|
|
57
|
+
function renderPaperValidate(_args, value) {
|
|
58
|
+
const result = value;
|
|
59
|
+
return [{
|
|
60
|
+
type: 'text',
|
|
61
|
+
text: [
|
|
62
|
+
`Valid paper structure: ${result.title}`,
|
|
63
|
+
`entities: ${result.entityCount}, main theorems: ${result.mainTheoremCount}, sections: ${result.sectionCount}`,
|
|
64
|
+
`dependency roots: ${result.roots.length > 0 ? result.roots.join(', ') : '(none)'}`,
|
|
65
|
+
`self_containment: ${result.completeness}`,
|
|
66
|
+
].join('\n'),
|
|
67
|
+
}];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Resolve a model-supplied path against the calling workspace when relative.
|
|
71
|
+
* @param path - model-supplied path, possibly workspace-relative.
|
|
72
|
+
* @param cwd - the calling agent's session cwd, when known.
|
|
73
|
+
*/
|
|
74
|
+
function toWorkspacePath(path, cwd) {
|
|
75
|
+
return isAbsolute(path) || cwd === undefined ? path : resolve(cwd, path);
|
|
76
|
+
}
|
|
77
|
+
/** Map an unknown thrown value onto the tool error taxonomy (route on `code`). */
|
|
78
|
+
function toToolError(error) {
|
|
79
|
+
if (isVeryMathError(error))
|
|
80
|
+
return error;
|
|
81
|
+
if (error instanceof Error)
|
|
82
|
+
return error;
|
|
83
|
+
return new Error(String(error));
|
|
84
|
+
}
|
|
85
|
+
/** Spawn one short command and resolve to (exitCode, stdout) or undefined on timeout/launch failure. */
|
|
86
|
+
function probeCommand(executable, args, timeoutMs = 5_000) {
|
|
87
|
+
return new Promise((resolvePromise) => {
|
|
88
|
+
let child;
|
|
89
|
+
try {
|
|
90
|
+
child = spawn(executable, [...args], { stdio: ['ignore', 'pipe', 'ignore'] });
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
resolvePromise(undefined);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const stdout = [];
|
|
97
|
+
let settled = false;
|
|
98
|
+
const settle = (value) => {
|
|
99
|
+
if (!settled) {
|
|
100
|
+
settled = true;
|
|
101
|
+
resolvePromise(value);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
child.stdout?.on('data', (chunk) => stdout.push(chunk));
|
|
105
|
+
child.on('error', () => settle(undefined));
|
|
106
|
+
child.on('close', (code) => settle({ exitCode: code ?? -1, stdout: Buffer.concat(stdout).toString() }));
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
child.kill();
|
|
109
|
+
settle(undefined);
|
|
110
|
+
}, timeoutMs);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Probe the math-beamer TeX toolchain: latexmk and xelatex binaries (PATH +
|
|
115
|
+
* `--version` smoke) and the forest/ctex LaTeX packages (`kpsewhich <name>.sty`).
|
|
116
|
+
* Nothing is installed, compiled, or downloaded; each probe is independent so a
|
|
117
|
+
* partial toolchain reports per-piece detail (mirrors the osqp/sage probe pattern).
|
|
118
|
+
*/
|
|
119
|
+
async function probeBeamerToolchain() {
|
|
120
|
+
const binaries = [
|
|
121
|
+
{ name: 'latexmk' },
|
|
122
|
+
{ name: 'xelatex' },
|
|
123
|
+
];
|
|
124
|
+
const styles = [
|
|
125
|
+
{ name: 'forest' },
|
|
126
|
+
{ name: 'ctex', note: 'required by Chinese/bilingual templates only' },
|
|
127
|
+
];
|
|
128
|
+
const binaryProbes = await Promise.all(binaries.map(async ({ name }) => {
|
|
129
|
+
const which = await probeCommand('/usr/bin/which', [name]).catch(() => undefined);
|
|
130
|
+
const resolved = which !== undefined && which.exitCode === 0 ? which.stdout.trim().split('\n')[0] : undefined;
|
|
131
|
+
const executable = resolved !== undefined && resolved.length > 0 ? resolved : undefined;
|
|
132
|
+
if (executable === undefined)
|
|
133
|
+
return { name, kind: 'binary', available: false };
|
|
134
|
+
const smoke = await probeCommand(executable, ['--version']).catch(() => undefined);
|
|
135
|
+
return smoke !== undefined && smoke.exitCode === 0
|
|
136
|
+
? { name, kind: 'binary', available: true, executable }
|
|
137
|
+
: { name, kind: 'binary', available: false, executable };
|
|
138
|
+
}));
|
|
139
|
+
const styleProbes = await Promise.all(styles.map(async ({ name }) => {
|
|
140
|
+
const probe = await probeCommand('/Library/TeX/texbin/kpsewhich', [`${name}.sty`]).catch(() => undefined);
|
|
141
|
+
const resolved = probe !== undefined && probe.exitCode === 0 ? probe.stdout.trim().split('\n')[0] : undefined;
|
|
142
|
+
return resolved !== undefined && resolved.length > 0
|
|
143
|
+
? { name, kind: 'style', available: true, executable: resolved }
|
|
144
|
+
: { name, kind: 'style', available: false };
|
|
145
|
+
}));
|
|
146
|
+
return [...binaryProbes, ...styleProbes];
|
|
147
|
+
}
|
|
148
|
+
/** Register the VeryMath glossary and paper tools. */
|
|
149
|
+
export const apply = (ctx, config = {}) => {
|
|
150
|
+
const glossaryHome = config.glossaryHome ?? 'glossary/glossary.json';
|
|
151
|
+
ctx.tools.register(defineTool({
|
|
152
|
+
name: 'verymath_glossary_query',
|
|
153
|
+
description: 'Query a VeryMath bilingual (English-Chinese) mathematical glossary JSON document for terms. Matches by exact English/Chinese term, term id, English prefix, Chinese substring, or subfield name; returns examples when entries carry them (use them to disambiguate polysemous terms per the math-glossary usage guide). Use the field parameter to restrict matches to one subdomain when a term has different translations in different fields. The glossary follows the VeryMath math-glossary contract (sorted entries, unique term_id).',
|
|
154
|
+
parameters: {
|
|
155
|
+
query: { type: 'string', required: true, description: 'English term, Chinese term, term_id, or subfield name' },
|
|
156
|
+
glossaryPath: { type: 'string', description: 'Path of the glossary.json document; defaults to the workspace default glossary' },
|
|
157
|
+
field: { type: 'string', description: 'Restrict matches to terms whose subfield list contains exactly this value, e.g. 代数拓扑' },
|
|
158
|
+
},
|
|
159
|
+
output: {
|
|
160
|
+
schema: {
|
|
161
|
+
type: 'object',
|
|
162
|
+
additionalProperties: false,
|
|
163
|
+
properties: {
|
|
164
|
+
query: { type: 'string' },
|
|
165
|
+
glossary: { type: 'string' },
|
|
166
|
+
total: { type: 'number' },
|
|
167
|
+
terms: {
|
|
168
|
+
type: 'array',
|
|
169
|
+
items: {
|
|
170
|
+
type: 'object',
|
|
171
|
+
additionalProperties: false,
|
|
172
|
+
properties: {
|
|
173
|
+
term_id: { type: 'string' },
|
|
174
|
+
en: { type: 'string' },
|
|
175
|
+
cn: { type: 'string' },
|
|
176
|
+
matchedBy: { type: 'string' },
|
|
177
|
+
pos: { type: 'string' },
|
|
178
|
+
field: { type: 'array', items: { type: 'string' } },
|
|
179
|
+
source: { type: 'string' },
|
|
180
|
+
examples: {
|
|
181
|
+
type: 'array',
|
|
182
|
+
items: {
|
|
183
|
+
type: 'object',
|
|
184
|
+
additionalProperties: false,
|
|
185
|
+
properties: {
|
|
186
|
+
en: { type: 'string' },
|
|
187
|
+
cn: { type: 'string' },
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
render: renderGlossaryQuery,
|
|
197
|
+
},
|
|
198
|
+
async execute(args, exec) {
|
|
199
|
+
try {
|
|
200
|
+
const path = toWorkspacePath(args.glossaryPath ?? glossaryHome, exec.agent?.session.header.cwd);
|
|
201
|
+
const library = await ctx.verymath.loadGlossary(path, exec.signal);
|
|
202
|
+
const { hits, total } = await ctx.verymath.queryGlossaryFile(path, args.query, {
|
|
203
|
+
...(args.field !== undefined ? { field: args.field } : {}),
|
|
204
|
+
signal: exec.signal,
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
query: args.query,
|
|
208
|
+
glossary: library.path,
|
|
209
|
+
total,
|
|
210
|
+
terms: hits.map(hit => ({
|
|
211
|
+
term_id: hit.term.term_id,
|
|
212
|
+
en: hit.term.en,
|
|
213
|
+
cn: hit.term.cn,
|
|
214
|
+
matchedBy: hit.matchedBy,
|
|
215
|
+
...(hit.term.pos !== undefined ? { pos: hit.term.pos } : {}),
|
|
216
|
+
...(hit.term.field !== undefined ? { field: [...hit.term.field] } : {}),
|
|
217
|
+
...(hit.term.source !== undefined ? { source: hit.term.source } : {}),
|
|
218
|
+
...(hit.term.examples !== undefined ? { examples: hit.term.examples.map(example => ({ ...example })) } : {}),
|
|
219
|
+
})),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
throw toToolError(error);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
}));
|
|
227
|
+
ctx.tools.register(defineTool({
|
|
228
|
+
name: 'verymath_glossary_build',
|
|
229
|
+
description: 'Run one VeryMath math-glossary Python script (glossary_build.py, glossary_export.py, glossary_backup.py, glossary_enrich_examples.py) to construct or maintain a bilingual glossary from structured sources (CSV/TSV/JSON/YAML). Requires a local Python 3; use verymath_python_check first to verify availability. OCR, PDF, and free-text sources are handled by the math-glossary skill workflow, not this tool.',
|
|
230
|
+
parameters: {
|
|
231
|
+
script: { type: 'string', required: true, description: 'Script filename, e.g. glossary_build.py' },
|
|
232
|
+
args: { type: 'array', items: { type: 'string' }, description: 'Arguments passed to the script, e.g. ["source.csv", "--output", "glossary/glossary.json", "--no-excel"]' },
|
|
233
|
+
},
|
|
234
|
+
output: {
|
|
235
|
+
schema: {
|
|
236
|
+
type: 'object',
|
|
237
|
+
additionalProperties: false,
|
|
238
|
+
properties: {
|
|
239
|
+
script: { type: 'string' },
|
|
240
|
+
args: { type: 'array', items: { type: 'string' } },
|
|
241
|
+
exitCode: { type: 'number' },
|
|
242
|
+
output: { type: 'string' },
|
|
243
|
+
pythonExecutable: { type: 'string' },
|
|
244
|
+
status: { type: 'string' },
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
render: renderGlossaryBuild,
|
|
248
|
+
},
|
|
249
|
+
async execute(args) {
|
|
250
|
+
const result = await ctx.verymath.runGlossaryScript(args.script, args.args ?? []);
|
|
251
|
+
const output = [result.stderr.trim(), result.stdout.trim()].filter(part => part.length > 0).join('\n');
|
|
252
|
+
// Upstream contract: exit 3 = partial success (JSON written, Excel pending because
|
|
253
|
+
// openpyxl is missing — report honestly, never claim full completion); exit 2 = the
|
|
254
|
+
// output already exists and --force is required. Both are structured outcomes.
|
|
255
|
+
if (result.exitCode !== 0 && result.exitCode !== 2 && result.exitCode !== 3) {
|
|
256
|
+
throw new VeryMathError(`glossary script ${args.script} exited ${result.exitCode}: ${output.slice(0, 2000) || '(no output)'}`, 'VERYMATH_IO');
|
|
257
|
+
}
|
|
258
|
+
// A successful build may have rewritten the glossary on disk; drop stale
|
|
259
|
+
// cached entries so follow-up queries observe the new document.
|
|
260
|
+
const scriptArgs = args.args ?? [];
|
|
261
|
+
const outputFlagIndex = scriptArgs.indexOf('--output');
|
|
262
|
+
const outputPath = outputFlagIndex >= 0 ? scriptArgs[outputFlagIndex + 1] : undefined;
|
|
263
|
+
if (outputPath !== undefined)
|
|
264
|
+
ctx.verymath.invalidateGlossary(outputPath);
|
|
265
|
+
else
|
|
266
|
+
ctx.verymath.invalidateAllGlossaries();
|
|
267
|
+
return {
|
|
268
|
+
script: args.script,
|
|
269
|
+
args: scriptArgs,
|
|
270
|
+
exitCode: result.exitCode,
|
|
271
|
+
output: output.slice(0, 8000),
|
|
272
|
+
pythonExecutable: (await ctx.verymath.resolvePython()) ?? '(unknown)',
|
|
273
|
+
status: result.exitCode === 3 ? 'partial' : result.exitCode === 2 ? 'needs-force' : 'completed',
|
|
274
|
+
};
|
|
275
|
+
},
|
|
276
|
+
}));
|
|
277
|
+
ctx.tools.register(defineTool({
|
|
278
|
+
name: 'verymath_glossary_upsert',
|
|
279
|
+
description: 'Add or update terms in a VeryMath bilingual glossary JSON document, preserving the contract invariants automatically: term_id derives from the lowercased English term, the document is re-sorted by (en, cn) with contiguous idx, and updated terms get today\'s date stamps. Use this when reading papers or translating and you encounter a term worth saving — do not hand-edit glossary.json. Existing entries are updated when the term_id matches; unspecified optional fields are preserved.',
|
|
280
|
+
parameters: {
|
|
281
|
+
terms: {
|
|
282
|
+
type: 'array',
|
|
283
|
+
required: true,
|
|
284
|
+
items: {
|
|
285
|
+
type: 'object',
|
|
286
|
+
additionalProperties: false,
|
|
287
|
+
properties: {
|
|
288
|
+
en: { type: 'string', required: true },
|
|
289
|
+
cn: { type: 'string', required: true },
|
|
290
|
+
pos: { type: 'string' },
|
|
291
|
+
field: { type: 'array', items: { type: 'string' } },
|
|
292
|
+
source: { type: 'string' },
|
|
293
|
+
example: {
|
|
294
|
+
type: 'object',
|
|
295
|
+
additionalProperties: false,
|
|
296
|
+
properties: {
|
|
297
|
+
en: { type: 'string' },
|
|
298
|
+
cn: { type: 'string' },
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
description: 'Terms to add or update',
|
|
304
|
+
},
|
|
305
|
+
glossaryPath: { type: 'string', description: 'Path of the glossary.json document; defaults to the workspace default glossary. Created with just the new terms when it does not exist yet.' },
|
|
306
|
+
},
|
|
307
|
+
output: {
|
|
308
|
+
schema: {
|
|
309
|
+
type: 'object',
|
|
310
|
+
additionalProperties: false,
|
|
311
|
+
properties: {
|
|
312
|
+
glossary: { type: 'string' },
|
|
313
|
+
added: { type: 'array', items: { type: 'string' } },
|
|
314
|
+
updated: { type: 'array', items: { type: 'string' } },
|
|
315
|
+
totalTerms: { type: 'number' },
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
render: (_args, value) => {
|
|
319
|
+
const result = value;
|
|
320
|
+
const parts = [
|
|
321
|
+
`Glossary updated: ${result.glossary}`,
|
|
322
|
+
`added: ${result.added.length > 0 ? result.added.join(', ') : '(none)'}`,
|
|
323
|
+
`updated: ${result.updated.length > 0 ? result.updated.join(', ') : '(none)'}`,
|
|
324
|
+
`total terms: ${result.totalTerms}`,
|
|
325
|
+
];
|
|
326
|
+
return [{ type: 'text', text: parts.join('\n') }];
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
async execute(args, exec) {
|
|
330
|
+
const path = toWorkspacePath(args.glossaryPath ?? glossaryHome, exec.agent?.session.header.cwd);
|
|
331
|
+
const fs = ctx.fs;
|
|
332
|
+
if (fs === undefined) {
|
|
333
|
+
throw new VeryMathError('verymath_glossary_upsert requires the fs service', 'VERYMATH_IO');
|
|
334
|
+
}
|
|
335
|
+
const target = await fs.resolve(path, { signal: exec.signal });
|
|
336
|
+
// Read the current document if it exists; an absent file starts a new glossary.
|
|
337
|
+
let current;
|
|
338
|
+
try {
|
|
339
|
+
current = await ctx.verymath.loadGlossary(path, exec.signal);
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
if (!(isVeryMathError(error) && error.code === 'VERYMATH_GLOSSARY_UNAVAILABLE'))
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
const requests = (args.terms ?? []).map((request) => {
|
|
346
|
+
const example = request.example;
|
|
347
|
+
const validExample = example !== undefined && typeof example.en === 'string' && typeof example.cn === 'string'
|
|
348
|
+
? { en: example.en, cn: example.cn }
|
|
349
|
+
: undefined;
|
|
350
|
+
return {
|
|
351
|
+
en: request.en ?? '',
|
|
352
|
+
cn: request.cn ?? '',
|
|
353
|
+
...(request.pos !== undefined ? { pos: request.pos } : {}),
|
|
354
|
+
...(request.field !== undefined ? { field: request.field } : {}),
|
|
355
|
+
...(request.source !== undefined ? { source: request.source } : {}),
|
|
356
|
+
...(validExample !== undefined ? { example: validExample } : {}),
|
|
357
|
+
};
|
|
358
|
+
});
|
|
359
|
+
const result = upsertTerms(current?.terms ?? [], requests);
|
|
360
|
+
// Write through ctx.fs so the session sandbox policy governs the write.
|
|
361
|
+
const sandboxPolicy = ctx.get('sandboxPolicy')?.resolve(exec.agent !== undefined ? { session: exec.agent.session } : {});
|
|
362
|
+
await fs.writeText(target, JSON.stringify(result.document, null, 2) + '\n', undefined, exec.signal, sandboxPolicy);
|
|
363
|
+
ctx.verymath.invalidateGlossary(path);
|
|
364
|
+
return {
|
|
365
|
+
glossary: path,
|
|
366
|
+
added: [...result.added],
|
|
367
|
+
updated: [...result.updated],
|
|
368
|
+
totalTerms: result.document.length,
|
|
369
|
+
};
|
|
370
|
+
},
|
|
371
|
+
}));
|
|
372
|
+
ctx.tools.register(defineTool({
|
|
373
|
+
name: 'verymath_python_check',
|
|
374
|
+
description: 'Check whether a Python 3 interpreter is available for the VeryMath math-glossary scripts, and report which executable would be used. Call this before verymath_glossary_build and surface a concrete installation hint when it reports unavailable.',
|
|
375
|
+
parameters: {},
|
|
376
|
+
output: {
|
|
377
|
+
schema: {
|
|
378
|
+
type: 'object',
|
|
379
|
+
additionalProperties: false,
|
|
380
|
+
properties: {
|
|
381
|
+
available: { type: 'boolean' },
|
|
382
|
+
executable: { type: 'string' },
|
|
383
|
+
hint: { type: 'string' },
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
render: renderPythonCheck,
|
|
387
|
+
},
|
|
388
|
+
async execute() {
|
|
389
|
+
const python = await ctx.verymath.resolvePython();
|
|
390
|
+
if (python === undefined) {
|
|
391
|
+
return {
|
|
392
|
+
available: false,
|
|
393
|
+
hint: 'Install Python 3 (macOS: xcode-select --install or brew install python3; Windows: python.org installer) and set verymath.pythonExecutable or DSH_OFFICE_PYTHON if it is not on PATH.',
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
return { available: true, executable: python };
|
|
397
|
+
},
|
|
398
|
+
}));
|
|
399
|
+
ctx.tools.register(defineTool({
|
|
400
|
+
name: 'verymath_osqp_check',
|
|
401
|
+
description: 'Probe the Python environment for the OSQP solver stack (osqp with major-version gate >=1,<2, numpy, scipy) without installing anything. Returns per-package availability plus an install hint. Call before verymath_osqp_solve.',
|
|
402
|
+
parameters: {},
|
|
403
|
+
output: {
|
|
404
|
+
schema: {
|
|
405
|
+
type: 'object',
|
|
406
|
+
additionalProperties: false,
|
|
407
|
+
properties: {
|
|
408
|
+
ready: { type: 'boolean' },
|
|
409
|
+
packages: {
|
|
410
|
+
type: 'array',
|
|
411
|
+
items: {
|
|
412
|
+
type: 'object',
|
|
413
|
+
additionalProperties: false,
|
|
414
|
+
properties: {
|
|
415
|
+
name: { type: 'string' },
|
|
416
|
+
available: { type: 'boolean' },
|
|
417
|
+
version: { type: 'string' },
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
hint: { type: 'string' },
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
render: (_args, value) => {
|
|
425
|
+
const result = value;
|
|
426
|
+
const lines = result.packages.map(pkg => `- ${pkg.name}: ${pkg.available ? `ok${pkg.version !== undefined ? ` ${pkg.version}` : ''}` : 'missing'}`);
|
|
427
|
+
const status = result.ready ? 'OSQP stack ready' : 'OSQP stack NOT ready';
|
|
428
|
+
return [{ type: 'text', text: [status, ...lines, result.hint !== undefined ? `hint: ${result.hint}` : ''].filter(Boolean).join('\n') }];
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
async execute() {
|
|
432
|
+
const python = await ctx.verymath.resolvePython();
|
|
433
|
+
if (python === undefined) {
|
|
434
|
+
return {
|
|
435
|
+
ready: false,
|
|
436
|
+
packages: [{ name: 'python3', available: false }],
|
|
437
|
+
hint: 'Install Python 3 first (see verymath_python_check), then: python3 -m pip install \'osqp>=1,<2\'',
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
const scriptPath = bundledSkillsDir() + '/osqp-solver/scripts/check_osqp_environment.py';
|
|
441
|
+
const result = await ctx.verymath.runPythonCommand(python, [scriptPath, '--json'], 30_000);
|
|
442
|
+
let parsed;
|
|
443
|
+
try {
|
|
444
|
+
parsed = JSON.parse(result.stdout);
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
throw new VeryMathError(`osqp check produced invalid JSON: ${error instanceof Error ? error.message : String(error)}`, 'VERYMATH_IO', { cause: error });
|
|
448
|
+
}
|
|
449
|
+
// Upstream shape: packages is an OBJECT keyed by package name.
|
|
450
|
+
const packagesRaw = parsed.packages ?? {};
|
|
451
|
+
return {
|
|
452
|
+
ready: parsed.ready === true,
|
|
453
|
+
packages: Object.entries(packagesRaw).map(([name, info]) => {
|
|
454
|
+
const detail = (typeof info === 'object' && info !== null ? info : {});
|
|
455
|
+
return {
|
|
456
|
+
name,
|
|
457
|
+
available: detail.available === true,
|
|
458
|
+
...(detail.version !== undefined ? { version: detail.version } : {}),
|
|
459
|
+
};
|
|
460
|
+
}),
|
|
461
|
+
...(parsed.install_hint !== undefined || parsed.hint !== undefined
|
|
462
|
+
? { hint: parsed.install_hint ?? parsed.hint }
|
|
463
|
+
: {}),
|
|
464
|
+
};
|
|
465
|
+
},
|
|
466
|
+
}));
|
|
467
|
+
ctx.tools.register(defineTool({
|
|
468
|
+
name: 'verymath_osqp_solve',
|
|
469
|
+
description: 'Solve a continuous convex quadratic program (minimize 0.5 x^T P x + q^T x subject to l <= Ax <= u, P symmetric PSD) from a JSON problem document with independent verification: 11-state solver-status gates, primal/dual residual and KKT complementarity checks, and infeasibility certificates. Input JSON: {P, q, A, l, u} dense arrays (optionally {name, settings}). Deterministic; atomic output; exit-code contract (0 solved/verified, 2 input error, 3 numerical rejection). Requires the OSQP Python stack (verymath_osqp_check first). Not for mixed-integer, nonconvex, or general conic problems.',
|
|
470
|
+
parameters: {
|
|
471
|
+
problem: { type: 'string', required: true, description: 'Workspace path of the JSON QP problem document' },
|
|
472
|
+
output: { type: 'string', description: 'Workspace path for the result JSON (atomic write); omit to return the result inline' },
|
|
473
|
+
},
|
|
474
|
+
output: {
|
|
475
|
+
schema: {
|
|
476
|
+
type: 'object',
|
|
477
|
+
additionalProperties: false,
|
|
478
|
+
properties: {
|
|
479
|
+
status: { type: 'string' },
|
|
480
|
+
solution: {
|
|
481
|
+
type: 'object',
|
|
482
|
+
additionalProperties: false,
|
|
483
|
+
properties: {
|
|
484
|
+
x: { type: 'array', items: { type: 'number' } },
|
|
485
|
+
y: { type: 'array', items: { type: 'number' } },
|
|
486
|
+
},
|
|
487
|
+
},
|
|
488
|
+
verification: { type: 'json' },
|
|
489
|
+
problem: { type: 'json' },
|
|
490
|
+
outputPath: { type: 'string' },
|
|
491
|
+
raw: { type: 'json' },
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
render: (_args, value) => {
|
|
495
|
+
const result = value;
|
|
496
|
+
const accepted = result.raw?.status?.accepted;
|
|
497
|
+
const parts = [
|
|
498
|
+
`status: ${result.status} (accepted: ${String(accepted ?? false)})`,
|
|
499
|
+
];
|
|
500
|
+
if (result.solution?.x !== undefined)
|
|
501
|
+
parts.push(`x = [${result.solution.x.map(entry => entry.toFixed(4)).join(', ')}]`);
|
|
502
|
+
if (result.verification?.kind !== undefined)
|
|
503
|
+
parts.push(`verification: ${result.verification.kind}`);
|
|
504
|
+
if (result.outputPath !== undefined)
|
|
505
|
+
parts.push(`result written to: ${result.outputPath}`);
|
|
506
|
+
return [{ type: 'text', text: parts.join('\n') }];
|
|
507
|
+
},
|
|
508
|
+
},
|
|
509
|
+
async execute(args, exec) {
|
|
510
|
+
const python = await ctx.verymath.resolvePython();
|
|
511
|
+
if (python === undefined) {
|
|
512
|
+
throw new VeryMathError('verymath_osqp_solve requires Python 3; run verymath_python_check for install hints', 'VERYMATH_PYTHON_UNAVAILABLE');
|
|
513
|
+
}
|
|
514
|
+
const fs = ctx.fs;
|
|
515
|
+
if (fs === undefined) {
|
|
516
|
+
throw new VeryMathError('verymath_osqp_solve requires the fs service', 'VERYMATH_IO');
|
|
517
|
+
}
|
|
518
|
+
const problemPath = toWorkspacePath(args.problem, exec.agent?.session.header.cwd);
|
|
519
|
+
const scriptPath = bundledSkillsDir() + '/osqp-solver/scripts/solve_qp.py';
|
|
520
|
+
const scriptArgs = [scriptPath, fs.processPath(await fs.resolve(problemPath, { signal: exec.signal }))];
|
|
521
|
+
let outputPath;
|
|
522
|
+
if (args.output !== undefined) {
|
|
523
|
+
outputPath = toWorkspacePath(args.output, exec.agent?.session.header.cwd);
|
|
524
|
+
scriptArgs.push('--output', fs.processPath(await fs.resolve(outputPath, { signal: exec.signal })));
|
|
525
|
+
}
|
|
526
|
+
const result = await ctx.verymath.runPythonCommand(python, scriptArgs, 120_000);
|
|
527
|
+
let parsed;
|
|
528
|
+
try {
|
|
529
|
+
parsed = JSON.parse(result.stdout);
|
|
530
|
+
}
|
|
531
|
+
catch (error) {
|
|
532
|
+
throw new VeryMathError(`osqp solve produced invalid JSON (exit ${result.exitCode}): ${(result.stderr || result.stdout).slice(0, 500)}`, 'VERYMATH_IO', { cause: error });
|
|
533
|
+
}
|
|
534
|
+
const status = parsed.status;
|
|
535
|
+
const solution = parsed.solution;
|
|
536
|
+
// JSON-normalize passthrough blocks so they satisfy the JsonValue output contract.
|
|
537
|
+
const normalize = (value) => JSON.parse(JSON.stringify(value ?? null));
|
|
538
|
+
return {
|
|
539
|
+
status: status?.classification ?? status?.reported ?? 'unknown',
|
|
540
|
+
...(solution !== undefined && solution.x !== undefined
|
|
541
|
+
? { solution: { x: solution.x, ...(solution.y !== undefined ? { y: solution.y } : {}) } }
|
|
542
|
+
: {}),
|
|
543
|
+
...(parsed.verification !== undefined ? { verification: normalize(parsed.verification) } : {}),
|
|
544
|
+
...(parsed.problem !== undefined ? { problem: normalize(parsed.problem) } : {}),
|
|
545
|
+
...(outputPath !== undefined ? { outputPath } : {}),
|
|
546
|
+
raw: normalize(parsed),
|
|
547
|
+
};
|
|
548
|
+
},
|
|
549
|
+
}));
|
|
550
|
+
ctx.tools.register(defineTool({
|
|
551
|
+
name: 'verymath_lean_detect_sorry',
|
|
552
|
+
description: 'Scan a Lean 4 file for forbidden proof placeholders: sorry, admit, and axiom declarations (plain-text analysis; does NOT require a Lean toolchain). Part of the lean-formalization safety contract: final patches must not hide unfinished proofs. Use on any .lean file before accepting a proof patch, and on delivery candidates.',
|
|
553
|
+
parameters: {
|
|
554
|
+
path: { type: 'string', required: true, description: 'Workspace path of the .lean file' },
|
|
555
|
+
},
|
|
556
|
+
output: {
|
|
557
|
+
schema: {
|
|
558
|
+
type: 'object',
|
|
559
|
+
additionalProperties: false,
|
|
560
|
+
properties: {
|
|
561
|
+
path: { type: 'string' },
|
|
562
|
+
clean: { type: 'boolean' },
|
|
563
|
+
findings: {
|
|
564
|
+
type: 'array',
|
|
565
|
+
items: {
|
|
566
|
+
type: 'object',
|
|
567
|
+
additionalProperties: false,
|
|
568
|
+
properties: {
|
|
569
|
+
kind: { type: 'string' },
|
|
570
|
+
line: { type: 'number' },
|
|
571
|
+
text: { type: 'string' },
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
render: (_args, value) => {
|
|
578
|
+
const result = value;
|
|
579
|
+
if (result.clean) {
|
|
580
|
+
return [{ type: 'text', text: `${result.path}: clean (no sorry/admit/axiom found).` }];
|
|
581
|
+
}
|
|
582
|
+
const lines = result.findings.slice(0, 20).map(finding => `- line ${finding.line} [${finding.kind}]: ${(finding.text ?? '').slice(0, 80)}`);
|
|
583
|
+
const note = result.findings.length > 20 ? `\n(showing 20 of ${result.findings.length})` : '';
|
|
584
|
+
return [{ type: 'text', text: `${result.path}: ${result.findings.length} forbidden token(s)\n${lines.join('\n')}${note}` }];
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
async execute(args, exec) {
|
|
588
|
+
const python = await ctx.verymath.resolvePython();
|
|
589
|
+
if (python === undefined) {
|
|
590
|
+
throw new VeryMathError('verymath_lean_detect_sorry requires Python 3; run verymath_python_check for install hints', 'VERYMATH_PYTHON_UNAVAILABLE');
|
|
591
|
+
}
|
|
592
|
+
const fs = ctx.fs;
|
|
593
|
+
if (fs === undefined) {
|
|
594
|
+
throw new VeryMathError('verymath_lean_detect_sorry requires the fs service', 'VERYMATH_IO');
|
|
595
|
+
}
|
|
596
|
+
const absolute = toWorkspacePath(args.path, exec.agent?.session.header.cwd);
|
|
597
|
+
const scriptPath = bundledSkillsDir() + '/lean-formalization/lean-runtime/scripts/ai4m_lean.py';
|
|
598
|
+
const result = await ctx.verymath.runPythonCommand(python, [scriptPath, 'detect-sorry', '--file', fs.processPath(await fs.resolve(absolute, { signal: exec.signal }))], 30_000);
|
|
599
|
+
let parsed;
|
|
600
|
+
try {
|
|
601
|
+
parsed = JSON.parse(result.stdout);
|
|
602
|
+
}
|
|
603
|
+
catch (error) {
|
|
604
|
+
throw new VeryMathError(`lean detect-sorry produced invalid JSON: ${error instanceof Error ? error.message : String(error)}`, 'VERYMATH_IO', { cause: error });
|
|
605
|
+
}
|
|
606
|
+
const findings = parsed.findings ?? [];
|
|
607
|
+
return {
|
|
608
|
+
path: args.path,
|
|
609
|
+
clean: findings.length === 0,
|
|
610
|
+
findings: findings.slice(0, 100).map(finding => ({
|
|
611
|
+
kind: finding.kind ?? 'unknown',
|
|
612
|
+
line: finding.line ?? 0,
|
|
613
|
+
...(finding.text !== undefined ? { text: String(finding.text).slice(0, 200) } : {}),
|
|
614
|
+
})),
|
|
615
|
+
};
|
|
616
|
+
},
|
|
617
|
+
}));
|
|
618
|
+
ctx.tools.register(defineTool({
|
|
619
|
+
name: 'verymath_sage_check',
|
|
620
|
+
description: 'Check SageMath availability (the sage binary / SAGE_PYTHON / conda env chain) AND the local SageMath documentation corpus (default ~/.verymath/sagemath-corpus, set VERYMATH_SAGE_CORPUS to override). SageMath itself must be installed by the user (conda-forge sage=10.9 recommended; Windows requires WSL). The corpus is SageMath official documentation (CC BY-SA 3.0), distributed separately from this MIT package.',
|
|
621
|
+
parameters: {},
|
|
622
|
+
output: {
|
|
623
|
+
schema: {
|
|
624
|
+
type: 'object',
|
|
625
|
+
additionalProperties: false,
|
|
626
|
+
properties: {
|
|
627
|
+
corpusAvailable: { type: 'boolean' },
|
|
628
|
+
corpusRoot: { type: 'string' },
|
|
629
|
+
manifestRows: { type: 'number' },
|
|
630
|
+
sageBinaryAvailable: { type: 'boolean' },
|
|
631
|
+
hint: { type: 'string' },
|
|
632
|
+
},
|
|
633
|
+
},
|
|
634
|
+
render: (_args, value) => {
|
|
635
|
+
const result = value;
|
|
636
|
+
const parts = [
|
|
637
|
+
`corpus: ${result.corpusAvailable ? `available at ${result.corpusRoot} (${result.manifestRows} docs)` : 'MISSING'}`,
|
|
638
|
+
`sage binary: ${result.sageBinaryAvailable ? 'found' : 'not found'}`,
|
|
639
|
+
];
|
|
640
|
+
if (result.hint !== undefined)
|
|
641
|
+
parts.push(result.hint);
|
|
642
|
+
return [{ type: 'text', text: parts.join('\n') }];
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
async execute() {
|
|
646
|
+
const sageBinaryAvailable = await new Promise((resolvePromise) => {
|
|
647
|
+
const probe = spawn('sage', ['--version'], { stdio: 'ignore' });
|
|
648
|
+
probe.on('error', () => resolvePromise(false));
|
|
649
|
+
probe.on('close', (code) => resolvePromise(code === 0));
|
|
650
|
+
setTimeout(() => { probe.kill(); resolvePromise(false); }, 5_000);
|
|
651
|
+
});
|
|
652
|
+
try {
|
|
653
|
+
const root = await resolveCorpusRoot();
|
|
654
|
+
const rows = await loadManifest(root);
|
|
655
|
+
const hints = [];
|
|
656
|
+
if (!sageBinaryAvailable) {
|
|
657
|
+
hints.push('SageMath not found: install via miniforge (conda-forge sage=10.9; macOS arm64/x86_64 supported; Windows requires WSL). Search still works without the binary; only execution needs it.');
|
|
658
|
+
}
|
|
659
|
+
return {
|
|
660
|
+
corpusAvailable: true,
|
|
661
|
+
corpusRoot: root,
|
|
662
|
+
manifestRows: rows.length,
|
|
663
|
+
sageBinaryAvailable,
|
|
664
|
+
...(hints.length > 0 ? { hint: hints.join(' ') } : {}),
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
catch (error) {
|
|
668
|
+
if (isVeryMathError(error) && error.code === 'VERYMATH_SAGE_CORPUS_MISSING') {
|
|
669
|
+
return {
|
|
670
|
+
corpusAvailable: false,
|
|
671
|
+
sageBinaryAvailable,
|
|
672
|
+
hint: error.message,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
throw error;
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
}));
|
|
679
|
+
ctx.tools.register(defineTool({
|
|
680
|
+
name: 'verymath_sage_search',
|
|
681
|
+
description: 'Search the local SageMath documentation corpus (3196 docs) for constructors, methods, and examples before writing Sage code — the anti-hallucination lookup step of the sagemath-skill contract: never invent SageMath APIs from memory. Returns ranked documents with titles, symbol/method lists, and excerpts. Requires the docs corpus (verymath_sage_check); execution of Sage code requires the sage binary.',
|
|
682
|
+
parameters: {
|
|
683
|
+
query: { type: 'string', required: true, description: 'English query, e.g. "elliptic curve rank" or "solve ODE numerically"' },
|
|
684
|
+
limit: { type: 'number', description: 'Max documents (default 8)' },
|
|
685
|
+
show: { type: 'string', description: 'Document path (from a previous result) to read a wider window from instead of searching' },
|
|
686
|
+
},
|
|
687
|
+
output: {
|
|
688
|
+
schema: {
|
|
689
|
+
type: 'object',
|
|
690
|
+
additionalProperties: false,
|
|
691
|
+
properties: {
|
|
692
|
+
query: { type: 'string' },
|
|
693
|
+
results: {
|
|
694
|
+
type: 'array',
|
|
695
|
+
items: {
|
|
696
|
+
type: 'object',
|
|
697
|
+
additionalProperties: false,
|
|
698
|
+
properties: {
|
|
699
|
+
title: { type: 'string' },
|
|
700
|
+
domain: { type: 'string' },
|
|
701
|
+
path: { type: 'string' },
|
|
702
|
+
score: { type: 'number' },
|
|
703
|
+
symbols: { type: 'array', items: { type: 'string' } },
|
|
704
|
+
methods: { type: 'array', items: { type: 'string' } },
|
|
705
|
+
excerpt: { type: 'string' },
|
|
706
|
+
},
|
|
707
|
+
},
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
},
|
|
711
|
+
render: (_args, value) => {
|
|
712
|
+
const result = value;
|
|
713
|
+
if (result.results.length === 0) {
|
|
714
|
+
return [{ type: 'text', text: `No corpus documents matched ${JSON.stringify(result.query)}. Try different terms.` }];
|
|
715
|
+
}
|
|
716
|
+
const parts = result.results.map((item, index) => {
|
|
717
|
+
const head = `${index + 1}. [${item.score}] ${item.title} (${item.domain})`;
|
|
718
|
+
const body = item.excerpt !== undefined && item.excerpt.length > 0 ? `\n${item.excerpt}` : '';
|
|
719
|
+
return head + body;
|
|
720
|
+
});
|
|
721
|
+
return [{ type: 'text', text: parts.join('\n\n') }];
|
|
722
|
+
},
|
|
723
|
+
},
|
|
724
|
+
async execute(args) {
|
|
725
|
+
try {
|
|
726
|
+
const root = await resolveCorpusRoot();
|
|
727
|
+
const rows = await loadManifest(root);
|
|
728
|
+
if (args.show !== undefined) {
|
|
729
|
+
const target = rows.find(row => row.path === args.show || row.api_path === args.show);
|
|
730
|
+
if (target === undefined) {
|
|
731
|
+
throw new VeryMathError(`document not in manifest: ${args.show}`, 'VERYMATH_SAGE_CORPUS_MISSING');
|
|
732
|
+
}
|
|
733
|
+
const terms = termsFor(args.query);
|
|
734
|
+
const lines = await readCorpusDocWindow(root, target, 1).catch(() => []);
|
|
735
|
+
const excerpt = findExcerpt(lines, args.query, terms);
|
|
736
|
+
return {
|
|
737
|
+
query: args.query,
|
|
738
|
+
results: [{
|
|
739
|
+
title: target.title,
|
|
740
|
+
...(target.domain !== undefined ? { domain: target.domain } : {}),
|
|
741
|
+
...(target.path !== undefined ? { path: target.path } : {}),
|
|
742
|
+
score: 0,
|
|
743
|
+
excerpt,
|
|
744
|
+
}],
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
const hits = await searchSageRefs(rows, args.query, { limit: args.limit ?? 8, withExcerpts: 3, corpusRoot: root });
|
|
748
|
+
return {
|
|
749
|
+
query: args.query,
|
|
750
|
+
results: hits.map(hit => ({
|
|
751
|
+
title: hit.row.title,
|
|
752
|
+
...(hit.row.domain !== undefined ? { domain: hit.row.domain } : {}),
|
|
753
|
+
...(hit.row.path !== undefined ? { path: hit.row.path } : {}),
|
|
754
|
+
score: hit.score,
|
|
755
|
+
excerpt: hit.excerpt,
|
|
756
|
+
})),
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
catch (error) {
|
|
760
|
+
if (isVeryMathError(error) && error.code === 'VERYMATH_SAGE_CORPUS_MISSING') {
|
|
761
|
+
throw new VeryMathError(error.message, 'VERYMATH_SAGE_CORPUS_MISSING');
|
|
762
|
+
}
|
|
763
|
+
throw toToolError(error);
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
}));
|
|
767
|
+
ctx.tools.register(defineTool({
|
|
768
|
+
name: 'verymath_research_gate',
|
|
769
|
+
description: 'Check a Co-Mathematician-style research workspace against its three completion gates: goal_approval (project/GOALS.yaml has an approved goal), workstream_completion (each workstream has report.md with Provenance/Uncertainty/Failed Explorations sections, an independent reviewer approval, and no unresolved blocking reviews), and final_render (which workstreams are render-ready). Pure file-contract validation; use before declaring a research workstream complete or assembling a final report.',
|
|
770
|
+
parameters: {
|
|
771
|
+
workspace: { type: 'string', required: true, description: 'Workspace root containing project/ and workstreams/' },
|
|
772
|
+
gate: { type: 'string', required: true, description: 'One of goal_approval, workstream_completion, final_render' },
|
|
773
|
+
goalId: { type: 'string', description: 'Specific goal id for goal_approval' },
|
|
774
|
+
workstreamId: { type: 'string', description: 'Specific workstream directory name for workstream_completion' },
|
|
775
|
+
},
|
|
776
|
+
output: {
|
|
777
|
+
schema: {
|
|
778
|
+
type: 'object',
|
|
779
|
+
additionalProperties: false,
|
|
780
|
+
properties: {
|
|
781
|
+
gate: { type: 'string' },
|
|
782
|
+
passed: { type: 'boolean' },
|
|
783
|
+
issues: { type: 'array', items: { type: 'string' } },
|
|
784
|
+
details: { type: 'json' },
|
|
785
|
+
},
|
|
786
|
+
},
|
|
787
|
+
render: (_args, value) => {
|
|
788
|
+
const result = value;
|
|
789
|
+
const head = `${result.gate}: ${result.passed ? 'PASSED' : 'NOT PASSED'}`;
|
|
790
|
+
if (result.issues.length === 0)
|
|
791
|
+
return [{ type: 'text', text: head }];
|
|
792
|
+
return [{ type: 'text', text: [head, ...result.issues.map(issue => `- ${issue}`)].join('\n') }];
|
|
793
|
+
},
|
|
794
|
+
},
|
|
795
|
+
async execute(args) {
|
|
796
|
+
const result = await checkGate(args.workspace, args.gate, {
|
|
797
|
+
...(args.goalId !== undefined ? { goalId: args.goalId } : {}),
|
|
798
|
+
...(args.workstreamId !== undefined ? { workstreamId: args.workstreamId } : {}),
|
|
799
|
+
});
|
|
800
|
+
return {
|
|
801
|
+
gate: result.gate,
|
|
802
|
+
passed: result.passed,
|
|
803
|
+
issues: [...result.issues],
|
|
804
|
+
details: structuredClone(result.details),
|
|
805
|
+
};
|
|
806
|
+
},
|
|
807
|
+
}));
|
|
808
|
+
ctx.tools.register(defineTool({
|
|
809
|
+
name: 'verymath_ocr_scan',
|
|
810
|
+
description: 'Scan an OCR-produced Markdown file (formula-dense academic scans) for likely recognition errors: glued operators, misread fonts (\\mathbb{D} etc.), epsilon-as-index digits, odd dollar counts, unclosed LaTeX environments, environment mismatches. Deterministic candidate locator only — the after-ocr skill\'s line-by-line review discipline is still required after scanning; never treat findings as confirmed errors. Requires Python 3 (check verymath_python_check first).',
|
|
811
|
+
parameters: {
|
|
812
|
+
path: { type: 'string', required: true, description: 'Workspace path of the OCR Markdown file' },
|
|
813
|
+
startLine: { type: 'number', description: 'First line to scan (1-based, inclusive)' },
|
|
814
|
+
endLine: { type: 'number', description: 'Last line to scan (inclusive)' },
|
|
815
|
+
},
|
|
816
|
+
output: {
|
|
817
|
+
schema: {
|
|
818
|
+
type: 'object',
|
|
819
|
+
additionalProperties: false,
|
|
820
|
+
properties: {
|
|
821
|
+
path: { type: 'string' },
|
|
822
|
+
totalLines: { type: 'number' },
|
|
823
|
+
findingCount: { type: 'number' },
|
|
824
|
+
findings: {
|
|
825
|
+
type: 'array',
|
|
826
|
+
items: {
|
|
827
|
+
type: 'object',
|
|
828
|
+
additionalProperties: false,
|
|
829
|
+
properties: {
|
|
830
|
+
rule: { type: 'string' },
|
|
831
|
+
line: { type: 'number' },
|
|
832
|
+
snippet: { type: 'string' },
|
|
833
|
+
},
|
|
834
|
+
},
|
|
835
|
+
},
|
|
836
|
+
},
|
|
837
|
+
},
|
|
838
|
+
render: (_args, value) => {
|
|
839
|
+
const result = value;
|
|
840
|
+
if (result.findingCount === 0) {
|
|
841
|
+
return [{ type: 'text', text: `No OCR error candidates found in ${result.path}. Proceed with the after-ocr line-by-line review protocol.` }];
|
|
842
|
+
}
|
|
843
|
+
const lines = result.findings.slice(0, 30).map(finding => `- line ${finding.line}: ${finding.rule}`);
|
|
844
|
+
const note = result.findingCount > 30 ? `\n(showing 30 of ${result.findingCount})` : '';
|
|
845
|
+
return [{ type: 'text', text: `${result.findingCount} candidate finding(s) in ${result.path}:\n${lines.join('\n')}${note}\nThese are candidates only — verify each against the source per the after-ocr protocol.` }];
|
|
846
|
+
},
|
|
847
|
+
},
|
|
848
|
+
async execute(args, exec) {
|
|
849
|
+
const python = await ctx.verymath.resolvePython();
|
|
850
|
+
if (python === undefined) {
|
|
851
|
+
throw new VeryMathError('verymath_ocr_scan requires Python 3; run verymath_python_check for install hints', 'VERYMATH_PYTHON_UNAVAILABLE');
|
|
852
|
+
}
|
|
853
|
+
const fs = ctx.fs;
|
|
854
|
+
if (fs === undefined) {
|
|
855
|
+
throw new VeryMathError('verymath_ocr_scan requires the fs service', 'VERYMATH_IO');
|
|
856
|
+
}
|
|
857
|
+
const absolute = toWorkspacePath(args.path, exec.agent?.session.header.cwd);
|
|
858
|
+
// The scan script reads host paths: map the workspace path into the fs execution world.
|
|
859
|
+
const processPath = fs.processPath(await fs.resolve(absolute, { signal: exec.signal }));
|
|
860
|
+
const scriptPath = bundledSkillsDir() + '/after-ocr/scripts/scan_markdown_ocr.py';
|
|
861
|
+
const scriptArgs = [scriptPath, '--format', 'json', processPath];
|
|
862
|
+
if (args.startLine !== undefined)
|
|
863
|
+
scriptArgs.push('--start-line', String(args.startLine));
|
|
864
|
+
if (args.endLine !== undefined)
|
|
865
|
+
scriptArgs.push('--end-line', String(args.endLine));
|
|
866
|
+
const result = await ctx.verymath.runPythonCommand(python, scriptArgs, 60_000);
|
|
867
|
+
if (result.exitCode !== 0) {
|
|
868
|
+
throw new VeryMathError(`ocr scan exited ${result.exitCode}: ${(result.stderr || result.stdout).slice(0, 1000)}`, 'VERYMATH_IO');
|
|
869
|
+
}
|
|
870
|
+
let parsed;
|
|
871
|
+
try {
|
|
872
|
+
parsed = JSON.parse(result.stdout);
|
|
873
|
+
}
|
|
874
|
+
catch (error) {
|
|
875
|
+
throw new VeryMathError(`ocr scan produced invalid JSON: ${error instanceof Error ? error.message : String(error)}`, 'VERYMATH_IO', { cause: error });
|
|
876
|
+
}
|
|
877
|
+
const findings = parsed.findings ?? [];
|
|
878
|
+
return {
|
|
879
|
+
path: args.path,
|
|
880
|
+
totalLines: parsed.summary?.total_lines ?? parsed.total_lines ?? 0,
|
|
881
|
+
findingCount: findings.length,
|
|
882
|
+
findings: findings.slice(0, 200).map(finding => ({
|
|
883
|
+
rule: finding.rule ?? 'unknown',
|
|
884
|
+
line: finding.line ?? 0,
|
|
885
|
+
...(finding.snippet !== undefined ? { snippet: String(finding.snippet).slice(0, 120) } : {}),
|
|
886
|
+
})),
|
|
887
|
+
};
|
|
888
|
+
},
|
|
889
|
+
}));
|
|
890
|
+
ctx.tools.register(defineTool({
|
|
891
|
+
name: 'verymath_paper_validate',
|
|
892
|
+
description: 'Validate a VeryMath math-paper-reading structure artifact (<slug>_structure.json): checks required metadata, recognized entity kinds, and dependency-label integrity, then returns a compact digest (entity counts, dependency roots, self-containment). Use after generating or editing a structure artifact.',
|
|
893
|
+
parameters: {
|
|
894
|
+
path: { type: 'string', required: true, description: 'Workspace path of the <slug>_structure.json artifact' },
|
|
895
|
+
},
|
|
896
|
+
output: {
|
|
897
|
+
schema: {
|
|
898
|
+
type: 'object',
|
|
899
|
+
additionalProperties: false,
|
|
900
|
+
properties: {
|
|
901
|
+
title: { type: 'string' },
|
|
902
|
+
entityCount: { type: 'number' },
|
|
903
|
+
mainTheoremCount: { type: 'number' },
|
|
904
|
+
sectionCount: { type: 'number' },
|
|
905
|
+
roots: { type: 'array', items: { type: 'string' } },
|
|
906
|
+
completeness: { type: 'string' },
|
|
907
|
+
valid: { type: 'boolean' },
|
|
908
|
+
},
|
|
909
|
+
},
|
|
910
|
+
render: renderPaperValidate,
|
|
911
|
+
},
|
|
912
|
+
async execute(args, exec) {
|
|
913
|
+
const absolute = toWorkspacePath(args.path, exec.agent?.session.header.cwd);
|
|
914
|
+
const fs = ctx.fs;
|
|
915
|
+
if (fs === undefined) {
|
|
916
|
+
throw new VeryMathError('verymath_paper_validate requires the fs service', 'VERYMATH_IO');
|
|
917
|
+
}
|
|
918
|
+
const raw = await fs.readText(await fs.resolve(absolute, { signal: exec.signal }), exec.signal);
|
|
919
|
+
let parsed;
|
|
920
|
+
try {
|
|
921
|
+
parsed = JSON.parse(raw);
|
|
922
|
+
}
|
|
923
|
+
catch (error) {
|
|
924
|
+
throw new VeryMathError(`paper structure at ${absolute} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`, 'VERYMATH_PAPER_INVALID', { cause: error });
|
|
925
|
+
}
|
|
926
|
+
const structure = parsePaperStructure(parsed);
|
|
927
|
+
const { roots } = dependencySummary(structure);
|
|
928
|
+
return {
|
|
929
|
+
title: structure.paper.title,
|
|
930
|
+
entityCount: structure.entities?.length ?? 0,
|
|
931
|
+
mainTheoremCount: structure.main_theorems?.length ?? 0,
|
|
932
|
+
sectionCount: structure.section_map?.length ?? 0,
|
|
933
|
+
roots: [...roots],
|
|
934
|
+
completeness: structure.completeness_check?.self_containment ?? 'unknown',
|
|
935
|
+
valid: true,
|
|
936
|
+
};
|
|
937
|
+
},
|
|
938
|
+
}));
|
|
939
|
+
ctx.tools.register(defineTool({
|
|
940
|
+
name: 'verymath_paper_graph',
|
|
941
|
+
description: 'Compute the dependency graph of a VeryMath math-paper-reading structure artifact (<slug>_structure.json): merges batch files, rebuilds the cited_by reverse index, computes shared_node hubs, in/out degrees, cycle detection, and both reading directions (top-down from main theorems, bottom-up reproduction order). Optionally renders a Mermaid dependency graph (auto-degrades above 40 entities to THEOREM-rooted depth-2 subgraphs unless forceFull). Use this instead of manually counting dependencies — the upstream skill forbids model-side graph traversal.',
|
|
942
|
+
parameters: {
|
|
943
|
+
path: { type: 'string', required: true, description: 'Workspace path of the <slug>_structure.json artifact' },
|
|
944
|
+
extraPaths: { type: 'array', items: { type: 'string' }, description: 'Additional batch artifact paths to merge (upstream batch merging protocol)' },
|
|
945
|
+
render: { type: 'boolean', description: 'Also render the Mermaid graph (default true)' },
|
|
946
|
+
forceFull: { type: 'boolean', description: 'Force full Mermaid rendering even above the 40-entity degradation threshold' },
|
|
947
|
+
focus: { type: 'string', description: 'Return the transitive dependency closure and layered reading path for this entity label (e.g. a main theorem), instead of (or in addition to) the full-graph digest' },
|
|
948
|
+
},
|
|
949
|
+
output: {
|
|
950
|
+
schema: {
|
|
951
|
+
type: 'object',
|
|
952
|
+
additionalProperties: false,
|
|
953
|
+
properties: {
|
|
954
|
+
title: { type: 'string' },
|
|
955
|
+
totalEntities: { type: 'number' },
|
|
956
|
+
mergedDuplicates: { type: 'number' },
|
|
957
|
+
danglingEdges: { type: 'array', items: { type: 'string' } },
|
|
958
|
+
sharedNodes: { type: 'array', items: { type: 'string' } },
|
|
959
|
+
cycles: { type: 'array', items: { type: 'array', items: { type: 'string' } } },
|
|
960
|
+
isolated: { type: 'array', items: { type: 'string' } },
|
|
961
|
+
longestChain: { type: 'array', items: { type: 'string' } },
|
|
962
|
+
topDownOrder: { type: 'array', items: { type: 'string' } },
|
|
963
|
+
bottomUpLevels: { type: 'array', items: { type: 'array', items: { type: 'string' } } },
|
|
964
|
+
degradedRendering: { type: 'boolean' },
|
|
965
|
+
metrics: {
|
|
966
|
+
type: 'array',
|
|
967
|
+
items: {
|
|
968
|
+
type: 'object',
|
|
969
|
+
additionalProperties: false,
|
|
970
|
+
properties: {
|
|
971
|
+
label: { type: 'string' },
|
|
972
|
+
type: { type: 'string' },
|
|
973
|
+
inDegree: { type: 'number' },
|
|
974
|
+
outDegree: { type: 'number' },
|
|
975
|
+
sharedNode: { type: 'boolean' },
|
|
976
|
+
supportsMainTheorem: { type: 'boolean' },
|
|
977
|
+
},
|
|
978
|
+
},
|
|
979
|
+
},
|
|
980
|
+
mermaid: { type: 'string' },
|
|
981
|
+
focus: {
|
|
982
|
+
type: 'object',
|
|
983
|
+
additionalProperties: false,
|
|
984
|
+
properties: {
|
|
985
|
+
label: { type: 'string' },
|
|
986
|
+
found: { type: 'boolean' },
|
|
987
|
+
closure: { type: 'array', items: { type: 'string' } },
|
|
988
|
+
levels: { type: 'array', items: { type: 'array', items: { type: 'string' } } },
|
|
989
|
+
},
|
|
990
|
+
},
|
|
991
|
+
},
|
|
992
|
+
},
|
|
993
|
+
render: (_args, value) => {
|
|
994
|
+
const result = value;
|
|
995
|
+
const parts = [
|
|
996
|
+
`Dependency graph for ${result.title}: ${result.totalEntities} entities`,
|
|
997
|
+
`shared nodes (hubs): ${result.sharedNodes.length > 0 ? result.sharedNodes.join(', ') : '(none)'}`,
|
|
998
|
+
`cycles: ${result.cycles.length > 0 ? result.cycles.map(cycle => cycle.join(' -> ')).join('; ') : 'none'}`,
|
|
999
|
+
`isolated: ${result.isolated.length > 0 ? result.isolated.join(', ') : '(none)'}`,
|
|
1000
|
+
`longest chain: ${result.longestChain.join(' -> ')}`,
|
|
1001
|
+
`bottom-up levels: ${result.bottomUpLevels.map(level => `[${level.join(', ')}]`).join(' -> ')}`,
|
|
1002
|
+
];
|
|
1003
|
+
if (result.degradedRendering)
|
|
1004
|
+
parts.push('(rendering degraded: >40 entities)');
|
|
1005
|
+
const top = result.metrics
|
|
1006
|
+
.filter(metric => metric.sharedNode || metric.inDegree >= 3)
|
|
1007
|
+
.map(metric => `- ${metric.label} (${metric.type}): in ${metric.inDegree}, out ${metric.outDegree}${metric.sharedNode ? ' [shared]' : ''}`);
|
|
1008
|
+
if (top.length > 0)
|
|
1009
|
+
parts.push('key nodes:', ...top);
|
|
1010
|
+
if (result.focus !== undefined) {
|
|
1011
|
+
if (!result.focus.found) {
|
|
1012
|
+
parts.push(`focus ${JSON.stringify(result.focus.label)}: not found in the merged entities (found=false)`);
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
parts.push(`focus ${JSON.stringify(result.focus.label)}: found=true; reading path (bottom-up layers):`, ...result.focus.levels.map((level, index) => ` L${index}: ${level.join(', ')}`));
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (result.mermaid !== undefined)
|
|
1019
|
+
parts.push('', '```mermaid', result.mermaid, '```');
|
|
1020
|
+
return [{ type: 'text', text: parts.join('\n') }];
|
|
1021
|
+
},
|
|
1022
|
+
},
|
|
1023
|
+
async execute(args, exec) {
|
|
1024
|
+
const paths = [args.path, ...(args.extraPaths ?? [])];
|
|
1025
|
+
const fs = ctx.fs;
|
|
1026
|
+
if (fs === undefined) {
|
|
1027
|
+
throw new VeryMathError('verymath_paper_graph requires the fs service', 'VERYMATH_IO');
|
|
1028
|
+
}
|
|
1029
|
+
const cwd = exec.agent?.session.header.cwd;
|
|
1030
|
+
const documents = await Promise.all(paths.map(async (path) => {
|
|
1031
|
+
const absolute = toWorkspacePath(path, cwd);
|
|
1032
|
+
const raw = await fs.readText(await fs.resolve(absolute, { signal: exec.signal }), exec.signal);
|
|
1033
|
+
let parsed;
|
|
1034
|
+
try {
|
|
1035
|
+
parsed = JSON.parse(raw);
|
|
1036
|
+
}
|
|
1037
|
+
catch (error) {
|
|
1038
|
+
throw new VeryMathError(`paper structure at ${absolute} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`, 'VERYMATH_PAPER_INVALID', { cause: error });
|
|
1039
|
+
}
|
|
1040
|
+
return parsePaperStructure(parsed);
|
|
1041
|
+
}));
|
|
1042
|
+
const primary = documents[0];
|
|
1043
|
+
if (primary === undefined) {
|
|
1044
|
+
throw new VeryMathError('no structure artifact paths resolved', 'VERYMATH_PAPER_INVALID');
|
|
1045
|
+
}
|
|
1046
|
+
const input = documents.length === 1 ? primary : documents;
|
|
1047
|
+
const { report } = mergeBatches(input);
|
|
1048
|
+
const topology = computeTopology(input);
|
|
1049
|
+
const focus = args.focus !== undefined ? focusClosure(input, args.focus) : undefined;
|
|
1050
|
+
const shouldRender = args.render ?? true;
|
|
1051
|
+
const mermaid = shouldRender && primary !== undefined
|
|
1052
|
+
? renderMermaid(primary, { ...(args.forceFull === true ? { forceFull: true } : {}) })
|
|
1053
|
+
: undefined;
|
|
1054
|
+
return {
|
|
1055
|
+
title: primary.paper.title,
|
|
1056
|
+
totalEntities: report.totalEntities,
|
|
1057
|
+
mergedDuplicates: report.mergedDuplicates,
|
|
1058
|
+
danglingEdges: [...report.danglingEdges],
|
|
1059
|
+
sharedNodes: [...report.sharedNodes],
|
|
1060
|
+
cycles: topology.cycles.map(cycle => [...cycle.labels]),
|
|
1061
|
+
isolated: [...topology.isolated],
|
|
1062
|
+
longestChain: [...topology.longestChain],
|
|
1063
|
+
topDownOrder: [...topology.topDownOrder],
|
|
1064
|
+
bottomUpLevels: topology.bottomUpLevels.map(level => [...level]),
|
|
1065
|
+
degradedRendering: topology.degradedRendering,
|
|
1066
|
+
metrics: topology.metrics.map(metric => ({
|
|
1067
|
+
label: metric.label,
|
|
1068
|
+
type: metric.type,
|
|
1069
|
+
inDegree: metric.inDegree,
|
|
1070
|
+
outDegree: metric.outDegree,
|
|
1071
|
+
sharedNode: metric.sharedNode,
|
|
1072
|
+
supportsMainTheorem: metric.supportsMainTheorem,
|
|
1073
|
+
})),
|
|
1074
|
+
...(mermaid !== undefined ? { mermaid } : {}),
|
|
1075
|
+
...(focus !== undefined
|
|
1076
|
+
? {
|
|
1077
|
+
focus: {
|
|
1078
|
+
label: focus.label,
|
|
1079
|
+
found: focus.found,
|
|
1080
|
+
closure: [...focus.closure],
|
|
1081
|
+
levels: focus.levels.map(level => [...level]),
|
|
1082
|
+
},
|
|
1083
|
+
}
|
|
1084
|
+
: {}),
|
|
1085
|
+
};
|
|
1086
|
+
},
|
|
1087
|
+
}));
|
|
1088
|
+
ctx.tools.register(defineTool({
|
|
1089
|
+
name: 'verymath_beamer_check',
|
|
1090
|
+
description: 'Probe the local TeX toolchain for the math-beamer skill before compiling Beamer decks: latexmk (build entry), xelatex (required by Chinese/bilingual ctex templates), and the forest/ctex LaTeX packages located via kpsewhich. Nothing is installed or compiled. Call before the first math-beamer compile; when it reports unavailable, deliver .tex sources only and surface the install hint instead of compiling.',
|
|
1091
|
+
parameters: {},
|
|
1092
|
+
output: {
|
|
1093
|
+
schema: {
|
|
1094
|
+
type: 'object',
|
|
1095
|
+
additionalProperties: false,
|
|
1096
|
+
properties: {
|
|
1097
|
+
available: { type: 'boolean' },
|
|
1098
|
+
tools: {
|
|
1099
|
+
type: 'array',
|
|
1100
|
+
items: {
|
|
1101
|
+
type: 'object',
|
|
1102
|
+
additionalProperties: false,
|
|
1103
|
+
properties: {
|
|
1104
|
+
name: { type: 'string' },
|
|
1105
|
+
kind: { type: 'string' },
|
|
1106
|
+
available: { type: 'boolean' },
|
|
1107
|
+
executable: { type: 'string' },
|
|
1108
|
+
},
|
|
1109
|
+
},
|
|
1110
|
+
},
|
|
1111
|
+
hint: { type: 'string' },
|
|
1112
|
+
},
|
|
1113
|
+
},
|
|
1114
|
+
render: (_args, value) => {
|
|
1115
|
+
const result = value;
|
|
1116
|
+
const lines = result.tools.map((tool) => `- ${tool.name} (${tool.kind}): ${tool.available ? `ok${tool.executable !== undefined ? ` ${tool.executable}` : ''}` : 'missing'}`);
|
|
1117
|
+
const status = result.available ? 'TeX toolchain ready for math-beamer' : 'TeX toolchain NOT ready for math-beamer';
|
|
1118
|
+
return [{ type: 'text', text: [status, ...lines, result.hint !== undefined ? `hint: ${result.hint}` : ''].filter(Boolean).join('\n') }];
|
|
1119
|
+
},
|
|
1120
|
+
},
|
|
1121
|
+
async execute() {
|
|
1122
|
+
const tools = await probeBeamerToolchain();
|
|
1123
|
+
const latexmk = tools.find(tool => tool.name === 'latexmk');
|
|
1124
|
+
const xelatex = tools.find(tool => tool.name === 'xelatex');
|
|
1125
|
+
const forest = tools.find(tool => tool.name === 'forest');
|
|
1126
|
+
const available = latexmk?.available === true && xelatex?.available === true && forest?.available === true;
|
|
1127
|
+
const missingBinaries = tools.filter(tool => !tool.available && tool.kind === 'binary');
|
|
1128
|
+
const missingStyles = tools.filter(tool => !tool.available && tool.kind === 'style');
|
|
1129
|
+
let hint;
|
|
1130
|
+
if (!available) {
|
|
1131
|
+
const parts = [];
|
|
1132
|
+
if (missingBinaries.length > 0) {
|
|
1133
|
+
parts.push(`Install a TeX distribution (macOS: MacTeX or brew install --cask mactex-no-gui; Linux: texlive-full) to provide ${missingBinaries.map(tool => tool.name).join(', ')}.`);
|
|
1134
|
+
}
|
|
1135
|
+
if (missingStyles.length > 0) {
|
|
1136
|
+
parts.push(`LaTeX package(s) ${missingStyles.map(tool => tool.name).join(', ')} not found via kpsewhich (tlmgr install ${missingStyles.map(tool => tool.name).join(' ')}).`);
|
|
1137
|
+
}
|
|
1138
|
+
parts.push('Until then, deliver .tex sources only — do not compile, per the math-beamer gate discipline.');
|
|
1139
|
+
hint = parts.join(' ');
|
|
1140
|
+
}
|
|
1141
|
+
return { available, tools, ...(hint !== undefined ? { hint } : {}) };
|
|
1142
|
+
},
|
|
1143
|
+
}));
|
|
1144
|
+
};
|