Unified source-verified contracts, accurate evidence grades and independently replayable Lean certificates; version sync, bilingual notes and guarded full release CI. Preserve native AI extension updates from main.
2431 lines
91 KiB
Rust
Executable File
2431 lines
91 KiB
Rust
Executable File
use std::path::PathBuf;
|
||
use std::process::ExitCode;
|
||
|
||
use anyhow::Result;
|
||
use clap::{Parser, Subcommand, ValueEnum};
|
||
|
||
use marqdo::catalog::{write_catalog, CatalogOptions};
|
||
use marqdo::diagnostics::{Diagnostic, Span};
|
||
use marqdo::ext_cli::{add_ext, list_ext, remove_ext};
|
||
use marqdo::input_feed::load_stdin_file;
|
||
use marqdo::knowledge::{
|
||
compile_policy, conflicts_json, find, find_conflicts, find_duplicates, find_json, find_stale,
|
||
format_conflicts_text, format_duplicates_text, format_find_text, format_impact_text,
|
||
format_preflight_yaml, format_reuse_text, format_stale_text, format_verify_text, graph_for_query,
|
||
impact, impact_json, learn, preflight_from_reuse, preflight_json, record_decision, resolve,
|
||
reuse_json, stale_json, verify, verify_json, write_knowledge, KnowledgeOptions, LearnInput,
|
||
ReuseBudget,
|
||
};
|
||
use marqdo::view::{
|
||
serve, serve_debug, write_capture, write_static, CaptureOptions, DebugOptions, OutputOptions,
|
||
ViewOptions,
|
||
};
|
||
use marqdo::{Backend, RunOptions};
|
||
|
||
#[derive(Parser, Debug)]
|
||
#[command(
|
||
name = "marqdo",
|
||
version,
|
||
about = "Marqdo interpreter — run, view, debug, catalog, knowledge, and ext"
|
||
)]
|
||
struct Cli {
|
||
#[command(subcommand)]
|
||
command: Commands,
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, ValueEnum)]
|
||
enum BackendCli {
|
||
Tree,
|
||
Bytecode,
|
||
}
|
||
|
||
impl From<BackendCli> for Backend {
|
||
fn from(b: BackendCli) -> Self {
|
||
match b {
|
||
BackendCli::Tree => Backend::Tree,
|
||
BackendCli::Bytecode => Backend::Bytecode,
|
||
}
|
||
}
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum MigrateAction {
|
||
/// `[键](集合)` → `access(集合, 键)`;`- [项](集合)` → `- [项](遍历 集合 为 项)`
|
||
Labels {
|
||
#[arg(value_name = "PATH", default_value = ".")]
|
||
paths: Vec<PathBuf>,
|
||
/// Actually rewrite files (default: dry run)
|
||
#[arg(long)]
|
||
write: bool,
|
||
/// Add `markup: occlusion` to every unmarked file, including files
|
||
/// with no legacy index. Use this when migrating a shipped library.
|
||
#[arg(long)]
|
||
mark: bool,
|
||
/// One JSON object on stdout
|
||
#[arg(long)]
|
||
json: bool,
|
||
},
|
||
/// One-liner `import bind:target` → nested YAML `import:\n bind: target`
|
||
Imports {
|
||
#[arg(value_name = "PATH", default_value = ".")]
|
||
paths: Vec<PathBuf>,
|
||
/// Actually rewrite files (default: dry run)
|
||
#[arg(long)]
|
||
write: bool,
|
||
/// One JSON array on stdout
|
||
#[arg(long)]
|
||
json: bool,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum Commands {
|
||
/// Execute a Marqdo source file (defaults to ./index.mq.md)
|
||
Run {
|
||
#[arg(value_name = "FILE")]
|
||
file: Option<PathBuf>,
|
||
|
||
#[arg(long, value_enum, default_value_t = BackendCli::Tree)]
|
||
backend: BackendCli,
|
||
|
||
#[arg(long)]
|
||
dump_lines: bool,
|
||
#[arg(long)]
|
||
dump_tokens: bool,
|
||
#[arg(long)]
|
||
dump_ast: bool,
|
||
#[arg(long)]
|
||
dump_sema: bool,
|
||
#[arg(long)]
|
||
dump_bytecode: bool,
|
||
#[arg(long)]
|
||
trace_eval: bool,
|
||
#[arg(long)]
|
||
dump_all: bool,
|
||
|
||
/// Feed `input` from a text file (one line per call), instead of / after the terminal.
|
||
#[arg(long, value_name = "FILE")]
|
||
stdin_file: Option<PathBuf>,
|
||
|
||
/// Write `# main` return value as JSON (used by file subtasks).
|
||
#[arg(long, value_name = "FILE")]
|
||
emit_result: Option<PathBuf>,
|
||
|
||
/// Artifact Metadata Binding: `KEY=VALUE` (repeatable). Feeds `arg.KEY` and overlays metadata.
|
||
#[arg(long = "bind", value_name = "KEY=VALUE")]
|
||
binds: Vec<String>,
|
||
|
||
/// Filesystem sandbox + process cwd root (default: entry file's directory).
|
||
/// Also read from env `MARQDO_FS_ROOT`. Use repo root when running `cli/…` entries.
|
||
#[arg(long = "fs-root", value_name = "DIR")]
|
||
fs_root: Option<PathBuf>,
|
||
|
||
/// Deny filesystem writes in this run and its file subtasks.
|
||
#[arg(long)]
|
||
deny_fs_write: bool,
|
||
/// Deny process execution.
|
||
#[arg(long)]
|
||
deny_exec: bool,
|
||
/// Deny network access.
|
||
#[arg(long)]
|
||
deny_net: bool,
|
||
/// Deny native plugin loading.
|
||
#[arg(long)]
|
||
deny_plugin: bool,
|
||
|
||
/// Isolated worker: call `lib.member` on the site entry instead of `# main`.
|
||
#[arg(long = "call-lib-path", value_name = "LIB.MEMBER")]
|
||
call_lib_path: Option<String>,
|
||
|
||
/// Named args JSON for `--call-lib-path` (file path, or `-` for stdin).
|
||
#[arg(long = "args-json", value_name = "FILE")]
|
||
args_json: Option<PathBuf>,
|
||
|
||
/// Emit diagnostics as JSON on stderr (machine-readable; AI/MLSP 面).
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Parse unmarked files using the legacy `[key](collection)` grammar.
|
||
/// Explicit per-file `markup:` still wins; this flag is transitional.
|
||
#[arg(long)]
|
||
legacy_markup: bool,
|
||
},
|
||
/// Call one function in a `.mq.md` file: `marqdo call <file> <function> name=value`.
|
||
Call {
|
||
/// Source file.
|
||
file: PathBuf,
|
||
|
||
/// Function display path, for example `计算器.加法` or nested `parent.child`.
|
||
function: String,
|
||
|
||
/// Named arguments `name=value` (integer, decimal, quoted text, True/False/None, JSON, `$name`).
|
||
/// A value may be negative (`a=-2`). Trailing `toget <name>` stores the result in the session.
|
||
/// `--json` and `--backend` remain flags.
|
||
args: Vec<String>,
|
||
|
||
/// Print one JSON object: `{"ok":true,"result":...}` or `{"ok":false,"error":...}`.
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Runtime. Both backends execute the named function.
|
||
#[arg(long, value_enum, default_value_t = BackendCli::Tree)]
|
||
backend: BackendCli,
|
||
|
||
/// Comma-separated effect grants: none, plugin, fs.read, fs.write, output.write, etc.
|
||
#[arg(long, default_value = "none")]
|
||
effects: String,
|
||
|
||
/// Literal named arguments as a JSON object; '-' reads stdin (no shell/argv size limit).
|
||
/// JSON strings never resolve call-session $name or @name references.
|
||
#[arg(long, value_name = "FILE_OR_STDIN")]
|
||
args_json: Option<PathBuf>,
|
||
|
||
/// Return a failing exit status when the function returns Result {ok:false}.
|
||
/// The JSON result is preserved; no effect is retried or rolled back.
|
||
#[arg(long)]
|
||
require_ok: bool,
|
||
|
||
/// Directory available to file tools; defaults to the called file directory.
|
||
#[arg(long)]
|
||
fs_root: Option<PathBuf>,
|
||
|
||
/// Call-session id for `toget` / `$name` (default: env `MARQDO_CALL_SESSION` or `default`).
|
||
#[arg(long)]
|
||
session: Option<String>,
|
||
|
||
/// Store the return value under this name in the call session (also: trailing `toget NAME`).
|
||
#[arg(long)]
|
||
toget: Option<String>,
|
||
},
|
||
/// Static contract cross-check (渐进式契约): drift, unknown types, misplaced tables
|
||
Check {
|
||
/// Program path (.mq.md)
|
||
#[arg(value_name = "FILE")]
|
||
file: PathBuf,
|
||
|
||
/// Emit one JSON diagnostic per line on stdout (machine-readable; AI/MLSP 面)
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Also check prose above `>` calls (implicit occlusion; fuzzy, off by default)
|
||
#[arg(long)]
|
||
implicit_labels: bool,
|
||
|
||
/// Require narration ≥ 50% in each function unit (ext skill contract).
|
||
#[arg(long)]
|
||
narrative_ratio: bool,
|
||
|
||
/// Parse an unmarked file with the legacy grammar for migration review.
|
||
#[arg(long)]
|
||
legacy_markup: bool,
|
||
|
||
/// Validate theorem tables (ADR 0010; 默认关): shape, symbols, shadowing
|
||
#[arg(long)]
|
||
proof: bool,
|
||
},
|
||
/// Occlusion label queries: pending stubs (built-in words) or keyword search
|
||
Labels {
|
||
/// Files or directories (recursively scans `*.mq.md`)
|
||
#[arg(value_name = "PATH", default_value = ".")]
|
||
paths: Vec<PathBuf>,
|
||
|
||
/// Keywords (comma separated). Replaces the built-in pending set.
|
||
#[arg(long, value_name = "WORDS")]
|
||
grep: Option<String>,
|
||
|
||
/// Union with the built-in groups instead of replacing them
|
||
#[arg(long)]
|
||
also_builtin: bool,
|
||
|
||
/// Built-in group names, comma separated (default: pending = 未完成,假数据)
|
||
#[arg(long, value_name = "GROUPS")]
|
||
group: Option<String>,
|
||
|
||
/// One JSON array on stdout
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Print only the hit count
|
||
#[arg(long)]
|
||
count: bool,
|
||
|
||
/// Debt ledger (JSON) for the ratchet: seed it, then only allow decreases
|
||
#[arg(long, value_name = "FILE")]
|
||
baseline: Option<PathBuf>,
|
||
|
||
/// Fail (exit 1) when the hit count exceeds the baseline (`<TAB>` M8)
|
||
#[arg(long)]
|
||
fail_on_increase: bool,
|
||
},
|
||
/// Migrate legacy syntax to the purity forms (`<TAB>` M7)
|
||
Migrate {
|
||
#[command(subcommand)]
|
||
action: MigrateAction,
|
||
},
|
||
/// MLSP for AI: line-delimited JSON on stdio (locate / syntax / validate / repair_targets / schema)
|
||
Mlsp,
|
||
/// Browse `.mq.md` structure + execution (live server or static output)
|
||
View {
|
||
#[command(subcommand)]
|
||
action: Option<ViewAction>,
|
||
|
||
/// Path when running the live server (default: `.`)
|
||
#[arg(value_name = "PATH", global = true)]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(long, default_value = "127.0.0.1", global = true)]
|
||
host: String,
|
||
|
||
#[arg(long, default_value_t = 7429, global = true)]
|
||
port: u16,
|
||
|
||
#[arg(long, global = true)]
|
||
no_open: bool,
|
||
},
|
||
/// Interactive debugger (tree-walk breakpoints; separate UI from `view`)
|
||
Debug {
|
||
/// Path to a `.mq.md` file or directory (default: `.`)
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(long, default_value = "127.0.0.1")]
|
||
host: String,
|
||
|
||
#[arg(long, default_value_t = 7430)]
|
||
port: u16,
|
||
|
||
#[arg(long)]
|
||
no_open: bool,
|
||
},
|
||
/// Generate OKF-compatible catalog YAML + module pages (+ Engineering Knowledge)
|
||
Catalog {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
/// Skip Engineering Knowledge Compiler after catalog
|
||
#[arg(long)]
|
||
no_knowledge: bool,
|
||
},
|
||
/// Alias for `catalog`
|
||
Sync {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
no_knowledge: bool,
|
||
},
|
||
/// Engineering Knowledge Compiler (L0–L4 catalog / graph / projections)
|
||
Knowledge {
|
||
#[command(subcommand)]
|
||
action: Option<KnowledgeAction>,
|
||
|
||
#[arg(value_name = "PATH", global = true)]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo", global = true)]
|
||
out: PathBuf,
|
||
|
||
#[arg(long, global = true)]
|
||
json: bool,
|
||
|
||
/// Force a full knowledge recompile before query actions (preflight, …)
|
||
#[arg(long, global = true)]
|
||
refresh: bool,
|
||
},
|
||
/// Find engineering capabilities / symbols / knowledge
|
||
Find {
|
||
#[arg(value_name = "QUERY")]
|
||
query: String,
|
||
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long, default_value_t = 20)]
|
||
limit: usize,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Engineering First Information: REUSE / ADAPT / CREATE
|
||
Reuse {
|
||
#[arg(value_name = "TASK")]
|
||
task: String,
|
||
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
/// Also write preflight context pack + policy
|
||
#[arg(long)]
|
||
preflight: bool,
|
||
|
||
#[arg(long, default_value_t = 3)]
|
||
min_candidates: usize,
|
||
|
||
#[arg(long, default_value_t = 2)]
|
||
allow_create_after: usize,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Knowledge impact of changed paths
|
||
Impact {
|
||
#[arg(value_name = "PATH")]
|
||
paths: Vec<PathBuf>,
|
||
|
||
#[arg(long, value_name = "ROOT", default_value = ".")]
|
||
root: PathBuf,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Detect knowledge conflicts / possible duplicates edges
|
||
Conflicts {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Exit non-zero when conflicts found (CI gate; default remains report-only)
|
||
#[arg(long)]
|
||
fail: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// List stale / deprecated / superseded knowledge
|
||
Stale {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Exit non-zero when stale items found (CI gate; default remains report-only)
|
||
#[arg(long)]
|
||
fail: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Detect duplicate capabilities via symbol fingerprints
|
||
Duplicate {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long, default_value_t = 0.75)]
|
||
threshold: f64,
|
||
|
||
/// Exit non-zero when duplicates found (CI gate)
|
||
#[arg(long)]
|
||
fail: bool,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Verify code ↔ knowledge consistency (EKC structural; exit 1 only on errors)
|
||
Verify {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", default_value = ".marqdo")]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Also fail on warnings (does not change default success-when-no-errors)
|
||
#[arg(long)]
|
||
fail_on_warning: bool,
|
||
|
||
/// Force a full knowledge recompile before querying
|
||
#[arg(long)]
|
||
refresh: bool,
|
||
},
|
||
/// Official extension installer (`list` / `add` / `remove`)
|
||
Ext {
|
||
#[command(subcommand)]
|
||
action: ExtAction,
|
||
},
|
||
/// Print version; `--check` compares with the latest GitHub release
|
||
Version {
|
||
#[arg(long)]
|
||
check: bool,
|
||
},
|
||
/// Build browser WASM artifact (route C)
|
||
Wasm {
|
||
#[command(subcommand)]
|
||
action: WasmAction,
|
||
},
|
||
/// Compile SKILL orchestration to Lean 4 and (P1+) prove theorems (推荐但不强制)
|
||
Prove {
|
||
/// Action (default: emit + report)
|
||
#[command(subcommand)]
|
||
action: Option<ProveAction>,
|
||
|
||
/// Program path (.mq.md; default ./index.mq.md)
|
||
#[arg(value_name = "FILE")]
|
||
file: Option<PathBuf>,
|
||
|
||
/// One JSON object on stdout (agent-facing stable contract)
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Only lower/emit these functions (display path, repeatable)
|
||
#[arg(long = "only", value_name = "FN")]
|
||
only: Vec<String>,
|
||
|
||
/// Output dir for emit (default .marqdo/prove/<key>/)
|
||
#[arg(long = "emit", short = 'o', value_name = "DIR")]
|
||
emit_dir: Option<PathBuf>,
|
||
|
||
/// Don't write artifacts; report only
|
||
#[arg(long)]
|
||
report_only: bool,
|
||
|
||
/// Downgrade this function wholesale to one opaque op (repeatable)
|
||
#[arg(long = "allow-opaque", value_name = "FN")]
|
||
allow_opaque: Vec<String>,
|
||
|
||
/// Type inference is strict by default; this relaxes to Val (weaker proofs)
|
||
#[arg(long = "no-strict-types")]
|
||
no_strict_types: bool,
|
||
|
||
/// CI strict mode: unjudged statuses exit 2 (P1+)
|
||
#[arg(long)]
|
||
require_lean: bool,
|
||
|
||
/// Explicit lean binary (else MARQDO_PROOF_LEAN, else PATH)
|
||
#[arg(long, value_name = "PATH")]
|
||
lean: Option<PathBuf>,
|
||
|
||
/// Per-lean-call timeout in seconds (default 120)
|
||
#[arg(long, value_name = "SEC", default_value_t = 120)]
|
||
timeout: u64,
|
||
|
||
/// Bypass the certificate cache (also MARQDO_PROOF_NO_CACHE)
|
||
#[arg(long)]
|
||
no_cache: bool,
|
||
|
||
/// maxHeartbeats for lean (default: lean's own)
|
||
#[arg(long, value_name = "N")]
|
||
heartbeats: Option<u64>,
|
||
|
||
/// Directory mode: recursively prove every .mq.md (aggregated, worst status wins)
|
||
#[arg(long)]
|
||
all: bool,
|
||
|
||
/// Cap for --all file collection (default 200)
|
||
#[arg(long = "max-files", value_name = "N", default_value_t = 200)]
|
||
max_files: usize,
|
||
|
||
/// Debug fault injection: `offline-claim` flips one Lean-proved claim to
|
||
/// force proof.divergence (monotonicity tripwire verification)
|
||
#[arg(long = "fault-inject", value_name = "SPEC")]
|
||
fault_inject: Option<String>,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum ProveAction {
|
||
/// Generate deterministic Lean module + plan.json (no judging)
|
||
Emit {
|
||
#[arg(value_name = "FILE")]
|
||
file: Option<PathBuf>,
|
||
#[arg(long = "emit", short = 'o', value_name = "DIR")]
|
||
emit_dir: Option<PathBuf>,
|
||
#[arg(long)]
|
||
json: bool,
|
||
#[arg(long = "only", value_name = "FN")]
|
||
only: Vec<String>,
|
||
#[arg(long = "allow-opaque", value_name = "FN")]
|
||
allow_opaque: Vec<String>,
|
||
#[arg(long = "no-strict-types")]
|
||
no_strict_types: bool,
|
||
},
|
||
/// Report inline/opaque/rejected classification (no judging)
|
||
Audit {
|
||
#[arg(value_name = "FILE")]
|
||
file: Option<PathBuf>,
|
||
#[arg(long)]
|
||
json: bool,
|
||
#[arg(long = "only", value_name = "FN")]
|
||
only: Vec<String>,
|
||
#[arg(long = "allow-opaque", value_name = "FN")]
|
||
allow_opaque: Vec<String>,
|
||
#[arg(long = "no-strict-types")]
|
||
no_strict_types: bool,
|
||
},
|
||
/// Delete .marqdo/prove caches and certificates
|
||
Clean {
|
||
#[arg(long)]
|
||
json: bool,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum KnowledgeAction {
|
||
/// Compile L0–L4 projections (default)
|
||
Compile,
|
||
/// Inspect verified theorem receipts and inter-module proof obligations.
|
||
ProofDeps {
|
||
/// Fail if the receipt/contract graph contains unresolved obligations.
|
||
#[arg(long)]
|
||
strict: bool,
|
||
/// Actually discharge closed callsite preconditions in Lean; without
|
||
/// this switch the command only emits pending proof obligations.
|
||
#[arg(long)]
|
||
verify: bool,
|
||
},
|
||
/// Emit independent Lean callee+caller proofs for restricted pure forwarders.
|
||
/// Unsupported constructs are rejected and never receive certificates.
|
||
Compose {
|
||
/// Exit nonzero if any call is unsupported or Lean rejects it.
|
||
#[arg(long)]
|
||
strict: bool,
|
||
},
|
||
/// Independently replay a previously issued composition certificate
|
||
/// against current source, Lean modules and toolchain.
|
||
Replay {
|
||
#[arg(value_name = "CERTIFICATE")]
|
||
certificate: PathBuf,
|
||
},
|
||
/// Inspect stable project symbols by exact URI or display name
|
||
Inspect {
|
||
/// Symbol URI (`mq://…`) or declaration name
|
||
#[arg(value_name = "SYMBOL")]
|
||
symbol: String,
|
||
},
|
||
/// Resolve a new-style knowledge path in the import scope of a source file
|
||
Resolve {
|
||
/// `mq://…` URI or a value-path such as `facts.Person.birth_year`
|
||
#[arg(value_name = "REFERENCE")]
|
||
reference: String,
|
||
|
||
/// Source file whose frontmatter imports supply the path's first segment
|
||
#[arg(long, value_name = "FILE")]
|
||
from: Option<PathBuf>,
|
||
},
|
||
/// Check or update the reviewed semantic target lock (`links.lock.json`)
|
||
Lock {
|
||
/// Replace the lock with the current deterministic semantic graph
|
||
#[arg(long)]
|
||
update: bool,
|
||
/// Return exit code 3 when the lock is missing or differs
|
||
#[arg(long)]
|
||
strict: bool,
|
||
},
|
||
/// Show transitive reverse dependencies of a symbol, artifact, or source path
|
||
Impact {
|
||
#[arg(value_name = "SYMBOL_OR_PATH")]
|
||
query: String,
|
||
},
|
||
/// Machine adjudication of semantic artifacts (M03): may_bind / may_publish
|
||
Adjudicate {
|
||
/// Artifact id, symbol id, or display name (omit to report all functions)
|
||
#[arg(value_name = "ARTIFACT")]
|
||
artifact: Option<String>,
|
||
/// Policy: pure | business | static
|
||
#[arg(long, default_value = "business")]
|
||
policy: String,
|
||
/// Exit 1 when any reported artifact has may_bind=false
|
||
#[arg(long)]
|
||
fail: bool,
|
||
},
|
||
/// Run engineering preflight for a task
|
||
Preflight {
|
||
#[arg(value_name = "TASK")]
|
||
task: String,
|
||
|
||
#[arg(long, default_value_t = 3)]
|
||
min_candidates: usize,
|
||
|
||
#[arg(long, default_value_t = 2)]
|
||
allow_create_after: usize,
|
||
},
|
||
/// Record a reuse decision into metrics
|
||
Record {
|
||
#[arg(value_name = "DECISION")]
|
||
decision: String,
|
||
|
||
#[arg(long)]
|
||
duplicated: bool,
|
||
},
|
||
/// Learn failure/decision/constraint into knowledge/
|
||
Learn {
|
||
#[arg(long)]
|
||
task: String,
|
||
#[arg(long)]
|
||
failed_approach: Option<String>,
|
||
#[arg(long)]
|
||
failure: Option<String>,
|
||
#[arg(long)]
|
||
correct_approach: Option<String>,
|
||
#[arg(long)]
|
||
decision_title: Option<String>,
|
||
#[arg(long)]
|
||
decision_body: Option<String>,
|
||
#[arg(long)]
|
||
constraint_title: Option<String>,
|
||
#[arg(long)]
|
||
constraint_body: Option<String>,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum ExtAction {
|
||
/// List official extensions and install status
|
||
List,
|
||
/// Install an official extension into the local ext root
|
||
Add {
|
||
#[arg(value_name = "NAME")]
|
||
name: String,
|
||
},
|
||
/// Remove an installed official extension
|
||
Remove {
|
||
#[arg(value_name = "NAME")]
|
||
name: String,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum WasmAction {
|
||
/// `cargo build -p marqdo-wasm --target wasm32-unknown-unknown --release` and copy `.wasm`
|
||
Build {
|
||
/// Destination directory (default: examples/browser-hello)
|
||
#[arg(short = 'o', long = "out", default_value = "examples/browser-hello")]
|
||
out: PathBuf,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
enum ViewAction {
|
||
/// Write static HTML documentation site
|
||
Output {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", required = true)]
|
||
out: PathBuf,
|
||
|
||
#[arg(long)]
|
||
no_exec: bool,
|
||
|
||
#[arg(long, value_name = "FILE")]
|
||
snapshot: Option<PathBuf>,
|
||
},
|
||
/// Capture live cell results for `view output --snapshot`
|
||
Capture {
|
||
#[arg(value_name = "PATH")]
|
||
path: Option<PathBuf>,
|
||
|
||
#[arg(short = 'o', long = "out", required = true)]
|
||
out: PathBuf,
|
||
},
|
||
/// Read-only DocumentViewV1 (never executes). JSON to stdout or `--out`.
|
||
Document {
|
||
/// Project root (defaults to cwd / CLI path)
|
||
#[arg(long)]
|
||
project: Option<PathBuf>,
|
||
|
||
/// Document path relative to project
|
||
#[arg(long)]
|
||
document: String,
|
||
|
||
#[arg(long)]
|
||
json: bool,
|
||
|
||
/// Optional HTML wrapper for browser inspection (embed fragment only)
|
||
#[arg(long, value_name = "FILE")]
|
||
out: Option<PathBuf>,
|
||
},
|
||
}
|
||
|
||
fn main() -> ExitCode {
|
||
let cli = Cli::parse();
|
||
let json_errors = matches!(
|
||
&cli.command,
|
||
Commands::Run { json: true, .. }
|
||
| Commands::Check { json: true, .. }
|
||
| Commands::Prove { json: true, .. }
|
||
);
|
||
match try_main(cli) {
|
||
Ok(code) => ExitCode::from(code as u8),
|
||
Err(err) => {
|
||
if json_errors {
|
||
let diag = match err.downcast::<Diagnostic>() {
|
||
Ok(d) => d,
|
||
Err(e) => Diagnostic::find(&e).cloned().unwrap_or_else(|| {
|
||
Diagnostic::new(None, Span::new(0, 0), format!("{e:#}"))
|
||
}),
|
||
};
|
||
eprintln!("{}", diag.to_json());
|
||
} else {
|
||
eprintln!("error: {err:#}");
|
||
}
|
||
ExitCode::from(1)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// `prove --all`:逐文件出报告,整体取最差状态(差者胜,§3.2)。
|
||
/// JSON:`{"schema":1,"kind":"prove-all","status":…,"files":[…],"duration_ms":…}`。
|
||
fn run_prove_all(
|
||
dir: &std::path::Path,
|
||
max_files: usize,
|
||
opts: &marqdo::prove::ProveOptions,
|
||
json: bool,
|
||
require_lean: bool,
|
||
) -> Result<i32> {
|
||
let mut files = collect_mq_files(dir)?;
|
||
anyhow::ensure!(
|
||
!files.is_empty(),
|
||
"目录中没有 .mq.md 文件:{}",
|
||
dir.display()
|
||
);
|
||
anyhow::ensure!(
|
||
files.len() <= max_files,
|
||
"--max-files {max_files} 已达上限(实际 {} 个文件;请调大或缩小范围)",
|
||
files.len()
|
||
);
|
||
files.sort();
|
||
let started = std::time::Instant::now();
|
||
let mut worst = marqdo::prove::Verdict::Proved;
|
||
let mut file_items: Vec<serde_json::Value> = Vec::new();
|
||
let mut text_rows: Vec<(String, marqdo::prove::Verdict, usize)> = Vec::new();
|
||
for f in &files {
|
||
let report = marqdo::prove::prove_path(f, opts)?;
|
||
for d in &report.diagnostics {
|
||
eprintln!("{}", d.format_message());
|
||
}
|
||
let claims: usize = report
|
||
.functions
|
||
.iter()
|
||
.map(|fun| fun.claims.len())
|
||
.sum();
|
||
worst = marqdo::prove::Verdict::worst(worst, report.status);
|
||
text_rows.push((
|
||
f.strip_prefix(dir).unwrap_or(f).display().to_string(),
|
||
report.status,
|
||
claims,
|
||
));
|
||
let mut item = report.to_json();
|
||
if let Some(obj) = item.as_object_mut() {
|
||
obj.insert(
|
||
"file".into(),
|
||
serde_json::Value::String(
|
||
f.strip_prefix(dir).unwrap_or(f).display().to_string(),
|
||
),
|
||
);
|
||
}
|
||
file_items.push(item);
|
||
}
|
||
let duration_ms = started.elapsed().as_millis() as u64;
|
||
if json {
|
||
let agg = serde_json::json!({
|
||
"schema": 1,
|
||
"kind": "prove-all",
|
||
"root": dir.display().to_string(),
|
||
"status": worst.as_str(),
|
||
"files": file_items,
|
||
"file_count": files.len(),
|
||
"duration_ms": duration_ms,
|
||
});
|
||
println!("{agg}");
|
||
} else {
|
||
println!(
|
||
"prove --all {}:{} 个文件,{} ms,整体 {}",
|
||
dir.display(),
|
||
files.len(),
|
||
duration_ms,
|
||
worst.as_str()
|
||
);
|
||
for (rel, status, claims) in &text_rows {
|
||
println!(" {rel}: {}(定理 {claims})", status.as_str());
|
||
}
|
||
}
|
||
Ok(worst.exit_code(require_lean))
|
||
}
|
||
|
||
/// Resolve the value-path spelling introduced for executable knowledge without
|
||
/// evaluating it. The import bind comes from the caller's source file, so
|
||
/// `math.pi` never degrades into a project-wide name guess.
|
||
fn resolve_knowledge_reference<'a>(
|
||
index: &'a marqdo::sema::ProjectIndex,
|
||
reference: &str,
|
||
from: Option<&std::path::Path>,
|
||
) -> Result<&'a marqdo::sema::SymbolDecl> {
|
||
if reference.starts_with("mq://") {
|
||
return index
|
||
.symbols()
|
||
.iter()
|
||
.find(|symbol| symbol.id.as_str() == reference)
|
||
.ok_or_else(|| anyhow::anyhow!("unknown symbol URI `{reference}`"));
|
||
}
|
||
|
||
let expression = marqdo::parse::expr::parse_expr_prefer_var(reference)
|
||
.map_err(|error| anyhow::anyhow!("invalid knowledge reference `{reference}`: {error}"))?;
|
||
let marqdo::ast::Expr::Call(call) = expression else {
|
||
anyhow::bail!("knowledge reference `{reference}` must be a dotted value path or mq:// URI");
|
||
};
|
||
if !call.args.is_empty() {
|
||
anyhow::bail!(
|
||
"knowledge resolve only binds a zero-argument path; `{reference}` has arguments"
|
||
);
|
||
}
|
||
let path = call.path.ok_or_else(|| {
|
||
anyhow::anyhow!("knowledge reference `{reference}` must start with an imported module bind")
|
||
})?;
|
||
if path.len() < 2 {
|
||
anyhow::bail!("knowledge reference `{reference}` needs at least `module.symbol`");
|
||
}
|
||
let from = from.ok_or_else(|| {
|
||
anyhow::anyhow!("`--from FILE` is required to resolve imported path `{reference}`")
|
||
})?;
|
||
let module = marqdo::load::load_module(from)?;
|
||
let imported = module
|
||
.imports
|
||
.iter()
|
||
.find(|item| item.bind == path[0])
|
||
.ok_or_else(|| {
|
||
anyhow::anyhow!("unknown import bind `{}` in {}", path[0], from.display())
|
||
})?;
|
||
let base = from.parent().unwrap_or_else(|| std::path::Path::new("."));
|
||
let imported_path = marqdo::load::resolve_import(base, &imported.path)?;
|
||
let imported_path = imported_path.canonicalize().unwrap_or(imported_path);
|
||
let module_id = index
|
||
.modules()
|
||
.iter()
|
||
.find_map(|(module_id, source)| {
|
||
(source.canonicalize().unwrap_or_else(|_| source.clone()) == imported_path)
|
||
.then_some(module_id)
|
||
})
|
||
.ok_or_else(|| {
|
||
anyhow::anyhow!(
|
||
"import `{}` resolves to {}, which is outside the indexed project",
|
||
path[0],
|
||
imported_path.display()
|
||
)
|
||
})?;
|
||
let declaration_path = &path[1..];
|
||
index
|
||
.symbols()
|
||
.iter()
|
||
.find(|symbol| symbol.module == *module_id && symbol.declaration_path == declaration_path)
|
||
.ok_or_else(|| anyhow::anyhow!("unknown knowledge path `{reference}`"))
|
||
}
|
||
|
||
/// Recursively collect `*.mq.md` under `path` (file → itself).
|
||
fn collect_mq_files(path: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
|
||
fn walk(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
|
||
let Ok(rd) = std::fs::read_dir(dir) else {
|
||
return;
|
||
};
|
||
for e in rd.flatten() {
|
||
let p = e.path();
|
||
let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||
if p.is_dir() {
|
||
if matches!(
|
||
name,
|
||
"target" | "dist" | "node_modules" | ".git" | ".marqdo"
|
||
) {
|
||
continue;
|
||
}
|
||
walk(&p, out);
|
||
} else if name.ends_with(".mq.md") {
|
||
out.push(p);
|
||
}
|
||
}
|
||
}
|
||
if path.is_file() {
|
||
return Ok(vec![path.to_path_buf()]);
|
||
}
|
||
let mut out = Vec::new();
|
||
walk(path, &mut out);
|
||
out.sort();
|
||
Ok(out)
|
||
}
|
||
|
||
fn try_main(cli: Cli) -> Result<i32> {
|
||
match cli.command {
|
||
Commands::Run {
|
||
file,
|
||
backend,
|
||
dump_lines,
|
||
dump_tokens,
|
||
dump_ast,
|
||
dump_sema,
|
||
dump_bytecode,
|
||
trace_eval,
|
||
dump_all,
|
||
stdin_file,
|
||
emit_result,
|
||
binds,
|
||
fs_root,
|
||
call_lib_path,
|
||
args_json,
|
||
json: _,
|
||
legacy_markup,
|
||
deny_fs_write,
|
||
deny_exec,
|
||
deny_net,
|
||
deny_plugin,
|
||
} => {
|
||
let path = file.unwrap_or_else(|| PathBuf::from("index.mq.md"));
|
||
let stdin_lines = match stdin_file {
|
||
Some(p) => load_stdin_file(&p)?,
|
||
None => Vec::new(),
|
||
};
|
||
let mut bind_pairs = Vec::new();
|
||
for b in &binds {
|
||
bind_pairs.push(marqdo::binding::parse_bind_kv(b)?);
|
||
}
|
||
let env_fs_root = std::env::var_os("MARQDO_FS_ROOT").map(PathBuf::from);
|
||
let fs_root = fs_root.or(env_fs_root);
|
||
let mut opts = if dump_all {
|
||
RunOptions {
|
||
stdin_lines: stdin_lines.clone(),
|
||
emit_result: emit_result.clone(),
|
||
binds: bind_pairs.clone(),
|
||
fs_root: fs_root.clone(),
|
||
call_lib_path: call_lib_path.clone(),
|
||
args_json: args_json.clone(),
|
||
..RunOptions::dump_all()
|
||
}
|
||
} else {
|
||
RunOptions {
|
||
dump_lines,
|
||
dump_tokens,
|
||
dump_ast,
|
||
dump_sema,
|
||
dump_bytecode,
|
||
trace_eval,
|
||
backend: backend.into(),
|
||
stdin_lines,
|
||
emit_result,
|
||
binds: bind_pairs,
|
||
fs_root,
|
||
call_lib_path,
|
||
args_json,
|
||
..RunOptions::default()
|
||
}
|
||
};
|
||
opts.allow_fs_write = !deny_fs_write;
|
||
opts.allow_exec = !deny_exec;
|
||
opts.allow_net = !deny_net;
|
||
opts.allow_plugin = !deny_plugin;
|
||
if dump_all {
|
||
opts.backend = backend.into();
|
||
}
|
||
if legacy_markup {
|
||
marqdo::parse::expr::with_markup(marqdo::parse::expr::Markup::Classic, || {
|
||
marqdo::run_file(&path, &opts)
|
||
})?;
|
||
} else {
|
||
marqdo::run_file(&path, &opts)?;
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Call {
|
||
file,
|
||
function,
|
||
args,
|
||
args_json,
|
||
require_ok,
|
||
json,
|
||
backend,
|
||
effects,
|
||
fs_root,
|
||
session,
|
||
toget,
|
||
} => {
|
||
let outcome = (|| -> Result<marqdo::value::Value> {
|
||
let effects = marqdo::call::CallEffects::parse(&effects)?;
|
||
let (arg_tokens, trailing_toget) = marqdo::call::split_toget(&args)?;
|
||
let toget = toget.or(trailing_toget);
|
||
let literal_args = args_json.is_some();
|
||
let parsed = if let Some(input) = args_json {
|
||
use std::io::Read;
|
||
if !arg_tokens.is_empty() {
|
||
anyhow::bail!("--args-json cannot be combined with name=value arguments");
|
||
}
|
||
let mut source = String::new();
|
||
if input.as_os_str() == "-" {
|
||
std::io::stdin().take(64 * 1024 * 1024 + 1).read_to_string(&mut source)?;
|
||
} else {
|
||
std::fs::File::open(&input)?.take(64 * 1024 * 1024 + 1).read_to_string(&mut source)?;
|
||
}
|
||
if source.len() > 64 * 1024 * 1024 { anyhow::bail!("--args-json exceeds 64 MiB"); }
|
||
marqdo::call::parse_call_args_json(&source)?
|
||
} else {
|
||
marqdo::call::parse_call_args(&arg_tokens)?
|
||
};
|
||
let session = session.or_else(|| {
|
||
if toget.is_some()
|
||
|| (!literal_args && parsed.iter().any(|(_, v)| {
|
||
matches!(
|
||
v,
|
||
marqdo::value::Value::Text(t)
|
||
if t.starts_with('$') || t.starts_with('@')
|
||
)
|
||
}))
|
||
{
|
||
Some(marqdo::call_session::default_session_id())
|
||
} else {
|
||
None
|
||
}
|
||
});
|
||
marqdo::call::call_file_with(
|
||
&file,
|
||
&function,
|
||
&parsed,
|
||
backend.into(),
|
||
marqdo::call::CallOptions {
|
||
effects,
|
||
fs_root,
|
||
session,
|
||
toget,
|
||
literal_args,
|
||
},
|
||
)
|
||
})();
|
||
match outcome {
|
||
Ok(value) => {
|
||
if json {
|
||
let result = marqdo::value_as_json(&value)
|
||
.map_err(|e| anyhow::anyhow!(e))?;
|
||
println!(
|
||
"{}",
|
||
serde_json::json!({"ok": true, "result": result})
|
||
);
|
||
} else {
|
||
println!("{}", value.as_display());
|
||
}
|
||
let failed = require_ok && matches!(&value,
|
||
marqdo::value::Value::Map(fields) if fields.iter().any(|(k,v)|
|
||
k == "ok" && matches!(v, marqdo::value::Value::Bool(false))));
|
||
Ok(if failed { 1 } else { 0 })
|
||
}
|
||
Err(err) => {
|
||
let diag = Diagnostic::find(&err).cloned().unwrap_or_else(|| {
|
||
Diagnostic::new(None, Span::new(0, 0), format!("{err:#}")).with_code("call.failed")
|
||
});
|
||
if json {
|
||
println!(
|
||
"{}",
|
||
serde_json::json!({"ok": false, "error": diag.to_json()})
|
||
);
|
||
} else {
|
||
eprintln!("error: {diag}");
|
||
}
|
||
Ok(1)
|
||
}
|
||
}
|
||
}
|
||
Commands::Check {
|
||
file,
|
||
json,
|
||
implicit_labels,
|
||
narrative_ratio,
|
||
legacy_markup,
|
||
proof,
|
||
} => {
|
||
let mut diags = if legacy_markup {
|
||
marqdo::parse::expr::with_markup(marqdo::parse::expr::Markup::Classic, || {
|
||
marqdo::check::check_path_opts(&file, implicit_labels, proof)
|
||
})?
|
||
} else {
|
||
marqdo::check::check_path_opts(&file, implicit_labels, proof)?
|
||
};
|
||
if narrative_ratio {
|
||
diags.extend(marqdo::narrative_ratio::check_file(&file)?);
|
||
}
|
||
let mut errors = 0i32;
|
||
for d in &diags {
|
||
if matches!(d.severity, marqdo::diagnostics::Severity::Error) {
|
||
errors += 1;
|
||
}
|
||
if json {
|
||
println!("{}", d.to_json());
|
||
} else {
|
||
println!("{}", d.format_message());
|
||
}
|
||
}
|
||
if !json {
|
||
if diags.is_empty() {
|
||
println!("contract check OK: 无契约问题");
|
||
} else {
|
||
println!("contract check: {} 条诊断({} error)", diags.len(), errors);
|
||
}
|
||
}
|
||
Ok(errors.min(1))
|
||
}
|
||
Commands::Prove {
|
||
action,
|
||
file,
|
||
json,
|
||
only,
|
||
emit_dir,
|
||
report_only,
|
||
allow_opaque,
|
||
no_strict_types,
|
||
require_lean,
|
||
lean,
|
||
timeout,
|
||
no_cache,
|
||
heartbeats,
|
||
all,
|
||
max_files,
|
||
fault_inject,
|
||
} => {
|
||
// 调试注入(§6 A3):仅面向测试的分歧触发器。
|
||
if fault_inject.as_deref() == Some("offline-claim") {
|
||
std::env::set_var("MARQDO_PROOF_INJECT_DIVERGENCE", "1");
|
||
}
|
||
// 子命令参数覆盖顶层默认;默认动作 = emit + 报告。
|
||
let (file, action_json, action_emit, action_only, action_opaque, action_relax) =
|
||
match &action {
|
||
Some(ProveAction::Emit {
|
||
file,
|
||
emit_dir,
|
||
json,
|
||
only,
|
||
allow_opaque,
|
||
no_strict_types,
|
||
}) => (
|
||
file.clone(),
|
||
*json,
|
||
Some(emit_dir.clone()),
|
||
only.clone(),
|
||
allow_opaque.clone(),
|
||
*no_strict_types,
|
||
),
|
||
Some(ProveAction::Audit {
|
||
file,
|
||
json,
|
||
only,
|
||
allow_opaque,
|
||
no_strict_types,
|
||
}) => (
|
||
file.clone(),
|
||
*json,
|
||
None,
|
||
only.clone(),
|
||
allow_opaque.clone(),
|
||
*no_strict_types,
|
||
),
|
||
Some(ProveAction::Clean { json }) => {
|
||
let removed = marqdo::prove::clean()?;
|
||
if *json {
|
||
println!("{{\"ok\":true,\"removed\":{removed}}}");
|
||
} else {
|
||
println!("prove clean: 已删除 {removed} 项(.marqdo/prove)");
|
||
}
|
||
return Ok(0);
|
||
}
|
||
None => (file.clone(), false, Some(emit_dir.clone()), only.clone(), allow_opaque.clone(), no_strict_types),
|
||
};
|
||
let file = file.unwrap_or_else(|| PathBuf::from("index.mq.md"));
|
||
// emit 目标:显式 --emit 优先;None → mod.rs 用默认 `.marqdo/prove/<key8>`。
|
||
// audit 永不写盘;默认动作受 --report-only 控制;emit 子命令总是写盘。
|
||
let emit_target = action_emit.flatten();
|
||
let report_only = matches!(action, Some(ProveAction::Audit { .. }))
|
||
|| (action.is_none() && report_only);
|
||
// emit 子命令只生成不判定(§8.2)。
|
||
let judge = !matches!(action, Some(ProveAction::Emit { .. }));
|
||
let no_cache = no_cache || std::env::var_os("MARQDO_PROOF_NO_CACHE").is_some();
|
||
// 目录必须显式 --all(§3.2 proof.need_all)。
|
||
if file.is_dir() {
|
||
if !all {
|
||
eprintln!(
|
||
"error: {} 是目录:逐文件证明请加 --all(--max-files 默认 200)",
|
||
file.display()
|
||
);
|
||
return Ok(1);
|
||
}
|
||
let out_json = json || action_json;
|
||
let code = run_prove_all(
|
||
&file,
|
||
max_files,
|
||
&marqdo::prove::ProveOptions {
|
||
entry: None,
|
||
only: action_only,
|
||
allow_opaque: action_opaque,
|
||
emit_dir: emit_target,
|
||
strict_types: !action_relax,
|
||
require_lean,
|
||
report_only,
|
||
judge,
|
||
no_cache,
|
||
lean,
|
||
timeout: std::time::Duration::from_secs(timeout),
|
||
max_heartbeats: heartbeats,
|
||
},
|
||
out_json,
|
||
require_lean,
|
||
)?;
|
||
return Ok(code);
|
||
}
|
||
let opts = marqdo::prove::ProveOptions {
|
||
entry: None,
|
||
only: action_only,
|
||
allow_opaque: action_opaque,
|
||
emit_dir: emit_target,
|
||
strict_types: !action_relax,
|
||
require_lean,
|
||
report_only,
|
||
judge,
|
||
no_cache,
|
||
lean,
|
||
timeout: std::time::Duration::from_secs(timeout),
|
||
max_heartbeats: heartbeats,
|
||
};
|
||
let report = marqdo::prove::prove_path(&file, &opts)?;
|
||
for d in &report.diagnostics {
|
||
// 诊断走 stderr(stdout 只放结果/报告)。
|
||
eprintln!("{}", d.format_message());
|
||
}
|
||
if json || action_json {
|
||
println!("{}", report.to_json());
|
||
} else {
|
||
print!("{}", report.to_text());
|
||
}
|
||
Ok(report.status.exit_code(require_lean))
|
||
}
|
||
Commands::Labels {
|
||
paths,
|
||
grep,
|
||
also_builtin,
|
||
group,
|
||
json,
|
||
count,
|
||
baseline,
|
||
fail_on_increase,
|
||
} => {
|
||
let mut words: Vec<String> = match &grep {
|
||
Some(spec) => marqdo::label::parse_words(spec),
|
||
None => {
|
||
let groups: Vec<&str> = match &group {
|
||
Some(g) => g
|
||
.split(',')
|
||
.map(|s| s.trim())
|
||
.filter(|s| !s.is_empty())
|
||
.collect(),
|
||
None => marqdo::label::PENDING_GROUPS.to_vec(),
|
||
};
|
||
marqdo::label::group_words(&groups)
|
||
}
|
||
};
|
||
if also_builtin {
|
||
let groups: Vec<&str> = match &group {
|
||
Some(g) => g
|
||
.split(',')
|
||
.map(|s| s.trim())
|
||
.filter(|s| !s.is_empty())
|
||
.collect(),
|
||
None => marqdo::label::PENDING_GROUPS.to_vec(),
|
||
};
|
||
for w in marqdo::label::group_words(&groups) {
|
||
if !words.contains(&w) {
|
||
words.push(w);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Accept the documented shapes `labels pending` / `labels search`
|
||
// as no-op words (the flags carry the real intent).
|
||
let paths: Vec<&std::path::PathBuf> = paths
|
||
.iter()
|
||
.filter(|p| {
|
||
let s = p.to_string_lossy();
|
||
!(s == "pending" || s == "search")
|
||
})
|
||
.collect();
|
||
let mut files: Vec<std::path::PathBuf> = Vec::new();
|
||
for p in &paths {
|
||
files.extend(collect_mq_files(p)?);
|
||
}
|
||
files.sort();
|
||
files.dedup();
|
||
let mut rows: Vec<(String, u32, String, String, Vec<String>, bool)> = Vec::new();
|
||
for f in &files {
|
||
let module = match marqdo::load::load_module(f) {
|
||
Ok(m) => m,
|
||
Err(_) => continue, // unparsable files are `check`'s problem, not ours
|
||
};
|
||
for hit in marqdo::label::search(&module, &words) {
|
||
rows.push((
|
||
f.display().to_string(),
|
||
hit.site.line,
|
||
hit.site.unit.clone(),
|
||
hit.site.label.clone(),
|
||
hit.words.clone(),
|
||
hit.site.has_payload,
|
||
));
|
||
}
|
||
}
|
||
|
||
if json {
|
||
let arr: Vec<serde_json::Value> = rows
|
||
.iter()
|
||
.map(|(file, line, unit, label, words, has_payload)| {
|
||
serde_json::json!({
|
||
"file": file, "line": line, "unit": unit,
|
||
"label": label, "words": words, "has_payload": has_payload,
|
||
})
|
||
})
|
||
.collect();
|
||
println!("{}", serde_json::Value::Array(arr));
|
||
} else if count {
|
||
println!("{}", rows.len());
|
||
} else {
|
||
for (file, line, unit, label, words, _) in &rows {
|
||
println!("{file}:{line} [{unit}] {label} ← {}", words.join(","));
|
||
}
|
||
println!(
|
||
"label search: {} 命中({} 文件,词={:?})",
|
||
rows.len(),
|
||
files.len(),
|
||
words
|
||
);
|
||
}
|
||
|
||
// `<TAB>` M8: ratchet — debt may shrink, never grow.
|
||
let current = rows.len();
|
||
let mut violation = false;
|
||
if let Some(ledger) = &baseline {
|
||
let prior = std::fs::read_to_string(ledger)
|
||
.ok()
|
||
.and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
|
||
.and_then(|v| v.get("count").and_then(|c| c.as_u64()))
|
||
.map(|n| n as usize);
|
||
match prior {
|
||
None => {
|
||
let body = serde_json::json!({"count": current, "words": words});
|
||
if let Some(dir) = ledger.parent() {
|
||
if !dir.as_os_str().is_empty() {
|
||
let _ = std::fs::create_dir_all(dir);
|
||
}
|
||
}
|
||
std::fs::write(ledger, format!("{body}\n"))?;
|
||
if !json && !count {
|
||
println!("baseline: 已建立 {current}({})", ledger.display());
|
||
}
|
||
}
|
||
Some(prior) if current > prior => {
|
||
violation = true;
|
||
if !count {
|
||
println!(
|
||
"baseline: {current} > {prior} —— 未完成项增加了 {}({})",
|
||
current - prior,
|
||
ledger.display()
|
||
);
|
||
}
|
||
}
|
||
Some(prior) => {
|
||
if current < prior || std::fs::read_to_string(ledger).is_err() {
|
||
let body = serde_json::json!({"count": current, "words": words});
|
||
std::fs::write(ledger, format!("{body}\n"))?;
|
||
if !json && !count {
|
||
println!(
|
||
"baseline: {prior} → {current}(棘轮收紧,{})",
|
||
ledger.display()
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if violation && fail_on_increase {
|
||
return Ok(1);
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Migrate { action } => match action {
|
||
MigrateAction::Labels {
|
||
paths,
|
||
write,
|
||
mark,
|
||
json,
|
||
} => {
|
||
let mut reports = Vec::new();
|
||
for p in &paths {
|
||
reports.extend(marqdo::migrate::migrate_path_with_options(p, write, mark)?);
|
||
}
|
||
let (mut idx, mut loops, mut marked, mut applied) =
|
||
(0usize, 0usize, 0usize, 0usize);
|
||
for r in &reports {
|
||
idx += r.indexes;
|
||
loops += r.loops;
|
||
marked += usize::from(r.mode_marked);
|
||
applied += usize::from(r.applied);
|
||
}
|
||
if json {
|
||
let arr: Vec<serde_json::Value> = reports
|
||
.iter()
|
||
.filter(|r| {
|
||
r.indexes > 0 || r.loops > 0 || r.mode_marked || r.note.is_some()
|
||
})
|
||
.map(|r| {
|
||
serde_json::json!({
|
||
"file": r.file.display().to_string(),
|
||
"indexes": r.indexes,
|
||
"loops": r.loops,
|
||
"mode_marked": r.mode_marked,
|
||
"applied": r.applied,
|
||
"note": r.note,
|
||
})
|
||
})
|
||
.collect();
|
||
println!("{}", serde_json::Value::Array(arr));
|
||
} else {
|
||
for r in &reports {
|
||
if r.indexes == 0 && r.loops == 0 && !r.mode_marked {
|
||
continue;
|
||
}
|
||
let state = if r.applied {
|
||
"written"
|
||
} else if r.note.is_some() {
|
||
"skipped"
|
||
} else {
|
||
"would change"
|
||
};
|
||
println!(
|
||
"{} indexes={} loops={} mode={} {}{}",
|
||
r.file.display(),
|
||
r.indexes,
|
||
r.loops,
|
||
if r.mode_marked {
|
||
"occlusion"
|
||
} else {
|
||
"unchanged"
|
||
},
|
||
state,
|
||
r.note
|
||
.as_ref()
|
||
.map(|n| format!(" ({n})"))
|
||
.unwrap_or_default()
|
||
);
|
||
}
|
||
println!(
|
||
"migrate labels: {idx} index, {loops} loop, {marked} explicit mode, {applied} file written{}",
|
||
if write {
|
||
""
|
||
} else {
|
||
" (dry run; pass --write)"
|
||
}
|
||
);
|
||
}
|
||
Ok(0)
|
||
}
|
||
MigrateAction::Imports { paths, write, json } => {
|
||
let mut reports = Vec::new();
|
||
for p in &paths {
|
||
reports.extend(marqdo::migrate::migrate_imports_path(p, write)?);
|
||
}
|
||
let (mut bindings, mut applied, mut problems) = (0usize, 0usize, 0usize);
|
||
for r in &reports {
|
||
bindings += r.bindings;
|
||
applied += usize::from(r.applied);
|
||
if r.note
|
||
.as_ref()
|
||
.is_some_and(|n| n != "no legacy import one-liners")
|
||
&& r.bindings == 0
|
||
{
|
||
problems += 1;
|
||
}
|
||
}
|
||
if json {
|
||
let arr: Vec<serde_json::Value> = reports
|
||
.iter()
|
||
.filter(|r| {
|
||
r.bindings > 0
|
||
|| r.note
|
||
.as_ref()
|
||
.is_some_and(|n| n != "no legacy import one-liners")
|
||
})
|
||
.map(|r| {
|
||
serde_json::json!({
|
||
"file": r.file.display().to_string(),
|
||
"bindings": r.bindings,
|
||
"applied": r.applied,
|
||
"note": r.note,
|
||
})
|
||
})
|
||
.collect();
|
||
println!("{}", serde_json::Value::Array(arr));
|
||
} else {
|
||
for r in &reports {
|
||
if r.bindings == 0
|
||
&& r.note
|
||
.as_ref()
|
||
.is_some_and(|n| n == "no legacy import one-liners")
|
||
{
|
||
continue;
|
||
}
|
||
if r.bindings == 0 {
|
||
println!(
|
||
"{} skipped ({})",
|
||
r.file.display(),
|
||
r.note.as_deref().unwrap_or("?")
|
||
);
|
||
continue;
|
||
}
|
||
let state = if r.applied {
|
||
"written"
|
||
} else if r.note.is_some() {
|
||
"skipped"
|
||
} else {
|
||
"would change"
|
||
};
|
||
println!(
|
||
"{} bindings={} {}{}",
|
||
r.file.display(),
|
||
r.bindings,
|
||
state,
|
||
r.note
|
||
.as_ref()
|
||
.map(|n| format!(" ({n})"))
|
||
.unwrap_or_default()
|
||
);
|
||
}
|
||
println!(
|
||
"migrate imports: {bindings} binding(s), {applied} file written, {problems} problem(s){}",
|
||
if write { "" } else { " (dry run; pass --write)" }
|
||
);
|
||
}
|
||
Ok(0)
|
||
}
|
||
},
|
||
Commands::Mlsp => {
|
||
use std::io::{BufRead, Write};
|
||
let stdin = std::io::stdin();
|
||
let stdout = std::io::stdout();
|
||
let mut out = stdout.lock();
|
||
for line in stdin.lock().lines() {
|
||
let line = line?;
|
||
if line.trim().is_empty() {
|
||
continue;
|
||
}
|
||
writeln!(out, "{}", marqdo::mlsp::handle_line(&line))?;
|
||
out.flush()?;
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::View {
|
||
action,
|
||
path,
|
||
host,
|
||
port,
|
||
no_open,
|
||
} => match action {
|
||
Some(ViewAction::Output {
|
||
path: out_path,
|
||
out,
|
||
no_exec,
|
||
snapshot,
|
||
}) => {
|
||
write_static(OutputOptions {
|
||
path: out_path.or(path).unwrap_or_else(|| PathBuf::from(".")),
|
||
out_dir: out,
|
||
no_exec,
|
||
snapshot,
|
||
})?;
|
||
Ok(0)
|
||
}
|
||
Some(ViewAction::Capture {
|
||
path: out_path,
|
||
out,
|
||
}) => {
|
||
write_capture(CaptureOptions {
|
||
path: out_path.or(path).unwrap_or_else(|| PathBuf::from(".")),
|
||
out,
|
||
})?;
|
||
Ok(0)
|
||
}
|
||
Some(ViewAction::Document {
|
||
project,
|
||
document,
|
||
json,
|
||
out,
|
||
}) => {
|
||
let project = project
|
||
.or(path)
|
||
.unwrap_or_else(|| PathBuf::from("."));
|
||
let req = marqdo::workbench::DocumentViewRequest {
|
||
project,
|
||
document,
|
||
revision: None,
|
||
generation: None,
|
||
node_id: None,
|
||
include: vec![
|
||
"fragment".into(),
|
||
"outline".into(),
|
||
"nodes".into(),
|
||
"refs".into(),
|
||
"diagnostics".into(),
|
||
],
|
||
};
|
||
let view = marqdo::workbench::build_document_view(&req).map_err(|e| {
|
||
anyhow::anyhow!("{}", e)
|
||
})?;
|
||
if let Some(out_path) = out {
|
||
let fragment = view
|
||
.pointer("/render/fragment")
|
||
.and_then(|v| v.as_str())
|
||
.unwrap_or("");
|
||
let title = view
|
||
.pointer("/document/title")
|
||
.and_then(|v| v.as_str())
|
||
.unwrap_or("DocumentView");
|
||
let html = format!(
|
||
"<!doctype html><html><head><meta charset=\"utf-8\"/><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"/><title>{title}</title><style>:root{{--mq-bg:#f7f5f0;--mq-text:#1a1a1a;--mq-muted:#5c5c5c;--mq-border:#d0cbc2;--mq-accent:#0b5fff;--mq-font-body:\"Iowan Old Style\",\"Palatino Linotype\",Palatino,serif;--mq-font-code:ui-monospace,Consolas,monospace}}body{{margin:0;background:var(--mq-bg);color:var(--mq-text);font:16px/1.75 var(--mq-font-body)}}.wrap{{max-width:46rem;margin:0 auto;padding:1.5rem}} .mq-document a{{color:var(--mq-accent)}}</style></head><body><div class=\"wrap\">{fragment}</div></body></html>"
|
||
);
|
||
std::fs::write(&out_path, html)?;
|
||
if json {
|
||
println!("{}", serde_json::to_string_pretty(&view)?);
|
||
} else {
|
||
println!("wrote {}", out_path.display());
|
||
}
|
||
} else {
|
||
println!("{}", serde_json::to_string_pretty(&view)?);
|
||
}
|
||
let _ = json;
|
||
Ok(0)
|
||
}
|
||
None => {
|
||
serve(ViewOptions {
|
||
path: path.unwrap_or_else(|| PathBuf::from(".")),
|
||
host,
|
||
port,
|
||
open_browser: !no_open,
|
||
})?;
|
||
Ok(0)
|
||
}
|
||
},
|
||
Commands::Debug {
|
||
path,
|
||
host,
|
||
port,
|
||
no_open,
|
||
} => {
|
||
serve_debug(DebugOptions {
|
||
path: path.unwrap_or_else(|| PathBuf::from(".")),
|
||
host,
|
||
port,
|
||
open_browser: !no_open,
|
||
})?;
|
||
Ok(0)
|
||
}
|
||
Commands::Catalog {
|
||
path,
|
||
out,
|
||
no_knowledge,
|
||
}
|
||
| Commands::Sync {
|
||
path,
|
||
out,
|
||
no_knowledge,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
write_catalog(CatalogOptions {
|
||
path: path.clone(),
|
||
out_dir: out.clone(),
|
||
})?;
|
||
if !no_knowledge {
|
||
write_knowledge(KnowledgeOptions { path, out_dir: out })?;
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Knowledge {
|
||
action,
|
||
path,
|
||
out,
|
||
json,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
match action.unwrap_or(KnowledgeAction::Compile) {
|
||
KnowledgeAction::Compile => {
|
||
write_knowledge(KnowledgeOptions { path, out_dir: out })?;
|
||
Ok(0)
|
||
}
|
||
KnowledgeAction::Replay { certificate } => {
|
||
let root=if path.is_file(){
|
||
path.parent().filter(|p|!p.as_os_str().is_empty())
|
||
.unwrap_or_else(||std::path::Path::new(".")).to_path_buf()
|
||
}else{path.clone()};
|
||
let compiled=marqdo::sema::ir::compile_project(
|
||
&marqdo::sema::ir::CompileOptions{
|
||
project_root:root,
|
||
entry:path.is_file().then_some(path),
|
||
lock_mode:marqdo::sema::ir::LockMode::Off
|
||
})?;
|
||
let lean=marqdo::prove::driver::Toolchain::discover(None)?
|
||
.ok_or_else(||anyhow::anyhow!("proof.compose_lean_missing"))?;
|
||
let proof=marqdo::knowledge::composition::replay(
|
||
&compiled,&certificate,&lean)?;
|
||
if json{println!("{proof}");}else{
|
||
println!("{}",serde_json::to_string_pretty(&proof)?);
|
||
}
|
||
Ok(0)
|
||
}
|
||
KnowledgeAction::Compose { strict } => {
|
||
let root=if path.is_file(){
|
||
path.parent().filter(|p|!p.as_os_str().is_empty())
|
||
.unwrap_or_else(||std::path::Path::new(".")).to_path_buf()
|
||
}else{path.clone()};
|
||
let compiled=marqdo::sema::ir::compile_project(
|
||
&marqdo::sema::ir::CompileOptions{
|
||
project_root:root,
|
||
entry:path.is_file().then_some(path),
|
||
lock_mode:marqdo::sema::ir::LockMode::Off
|
||
})?;
|
||
let lean=marqdo::prove::driver::Toolchain::discover(None)?
|
||
.ok_or_else(||anyhow::anyhow!(
|
||
"proof.compose_lean_missing: Lean required"))?;
|
||
let result=marqdo::knowledge::composition::prove(
|
||
&compiled,&out.join("modular"),&lean)?;
|
||
let failures=result["results"].as_array().map_or(true,|entries|
|
||
entries.is_empty()||entries.iter().any(|e|
|
||
e["status"]!="lean_kernel_proved_model"));
|
||
if json {println!("{result}");} else {
|
||
println!("{}",serde_json::to_string_pretty(&result)?);
|
||
}
|
||
Ok(if strict && failures {2} else {0})
|
||
}
|
||
KnowledgeAction::ProofDeps { strict,verify } => {
|
||
let semantic_root=if path.is_file() {
|
||
path.parent().filter(|p|!p.as_os_str().is_empty())
|
||
.unwrap_or_else(||std::path::Path::new(".")).to_path_buf()
|
||
} else {path.clone()};
|
||
let compiled=marqdo::sema::ir::compile_project(
|
||
&marqdo::sema::ir::CompileOptions {
|
||
project_root:semantic_root,
|
||
entry:path.is_file().then_some(path),
|
||
lock_mode:marqdo::sema::ir::LockMode::Off,
|
||
})?;
|
||
let mut receipts=marqdo::knowledge::proof::index_links(
|
||
&out.join("prove"),&compiled.semantics);
|
||
let toolchain=if verify {
|
||
Some(marqdo::prove::driver::Toolchain::discover(None)?
|
||
.ok_or_else(||anyhow::anyhow!(
|
||
"proof.modular_lean_missing: Lean is required for --verify"))?)
|
||
} else {None};
|
||
if let Some(lean)=toolchain.as_ref(){
|
||
marqdo::knowledge::proof::replay_index(
|
||
&out.join("prove"),&mut receipts,lean,
|
||
std::time::Duration::from_secs(120))?;
|
||
}
|
||
let replayed=receipts["claims"].as_array().map_or(0,|records|
|
||
records.iter().filter(|c|c["kernel_replayed"]==true).count());
|
||
let draft=marqdo::knowledge::modular::analyze(
|
||
&compiled.semantics,&receipts);
|
||
let mut accepted=std::collections::BTreeSet::new();
|
||
let verified_toolchain=toolchain.as_ref().map(|t|t.version.clone());
|
||
if verify {
|
||
let goals:Vec<(String,marqdo::knowledge::vc::Goal)>=draft["obligations"]
|
||
.as_array().into_iter().flat_map(|rows|rows.iter())
|
||
.flat_map(|row|row["contracts"].as_array().into_iter()
|
||
.flat_map(|contracts|contracts.iter()))
|
||
.filter_map(|c|{
|
||
if c["callee_certificate_valid"]!=true
|
||
|| c["closed_host_check"]!=true {return None}
|
||
Some((
|
||
c["precondition_goal_key"].as_str()?.to_string(),
|
||
marqdo::knowledge::vc::Goal{
|
||
holds:true,
|
||
proposition:c["lean_proposition"].as_str()?.to_string()
|
||
}
|
||
))
|
||
}).collect();
|
||
if !goals.is_empty(){
|
||
let toolchain=toolchain.as_ref().expect("verify implies Lean");
|
||
let target=out.join("modular");
|
||
std::fs::create_dir_all(&target)?;
|
||
let lean_file=target.join("closed-goals.lean");
|
||
let content=marqdo::knowledge::vc::emit_lean(&goals);
|
||
std::fs::write(&lean_file,content)?;
|
||
marqdo::prove::driver::check_closed_goals(
|
||
&toolchain,&lean_file,std::time::Duration::from_secs(120))?;
|
||
accepted.extend(goals.into_iter().map(|(id,_)|id));
|
||
}
|
||
}
|
||
let mut symbolic_checked=0usize;
|
||
if let Some(lean)=toolchain.as_ref(){
|
||
let symbolic:Vec<(String,marqdo::knowledge::symbolic::SymbolicGoal)>=
|
||
compiled.semantics.bound_calls.iter().flat_map(|call|{
|
||
compiled.semantics.claims.iter()
|
||
.filter(|c|c.owner==call.target)
|
||
.filter(|c|receipts["claims"].as_array().is_some_and(|rows|
|
||
rows.iter().any(|r|
|
||
r["claim_anchor_id"]==c.anchor_id
|
||
&& r["status"]=="proved"
|
||
&& r["usable"]==true)))
|
||
.filter_map(|c|Some((
|
||
c.anchor_id.clone(),
|
||
marqdo::knowledge::symbolic::plan(
|
||
&compiled.semantics,call,c.premise.as_deref()?)?
|
||
)))
|
||
.filter(|(_,g)|g.abstract_integer_model_supported)
|
||
.collect::<Vec<_>>()
|
||
}).collect();
|
||
if !symbolic.is_empty(){
|
||
let target=out.join("modular");
|
||
std::fs::create_dir_all(&target)?;
|
||
let path=target.join("symbolic-path-goals.lean");
|
||
std::fs::write(&path,
|
||
marqdo::knowledge::symbolic::emit_lean(&symbolic))?;
|
||
marqdo::prove::driver::check_closed_goals(lean,&path,
|
||
std::time::Duration::from_secs(120))?;
|
||
symbolic_checked=symbolic.len();
|
||
}
|
||
}
|
||
let mut report=marqdo::knowledge::modular::analyze_with_verified(
|
||
&compiled.semantics,&receipts,&accepted);
|
||
report["symbolic_path_lean_model_proved"]=
|
||
serde_json::json!(symbolic_checked);
|
||
report["symbolic_proof_scope"]=
|
||
serde_json::json!("integer_path_model_not_runtime_equivalence");
|
||
report["lean_preconditions_verified"]=serde_json::json!(accepted.len());
|
||
report["lean_toolchain"]=serde_json::json!(verified_toolchain);
|
||
report["lean_receipts_replayed"]=serde_json::json!(replayed);
|
||
if json {
|
||
println!("{report}");
|
||
} else {
|
||
println!("{}",serde_json::to_string_pretty(&report)?);
|
||
}
|
||
Ok(if strict && report["audit_passes"]!=true {2} else {0})
|
||
}
|
||
KnowledgeAction::Inspect { symbol } => {
|
||
// A supplied directory is an explicit compilation scope;
|
||
// a supplied file inherits the nearest project boundary.
|
||
let project = if path.is_dir() {
|
||
marqdo::project::ProjectRoot::new(&path)?
|
||
} else {
|
||
marqdo::project::ProjectRoot::discover(&path)?
|
||
};
|
||
let index = marqdo::sema::ProjectIndex::build(project)?;
|
||
let candidates: Vec<_> = if symbol.starts_with("mq://") {
|
||
index
|
||
.symbols()
|
||
.iter()
|
||
.filter(|item| item.id.as_str() == symbol)
|
||
.collect()
|
||
} else {
|
||
index.symbols_named(&symbol)
|
||
};
|
||
if candidates.is_empty() {
|
||
anyhow::bail!(
|
||
"no semantic symbol matching `{symbol}` in project {}",
|
||
index.root().path().display()
|
||
);
|
||
}
|
||
if json {
|
||
println!(
|
||
"{}",
|
||
serde_json::json!({
|
||
"project": index.project_id(),
|
||
"query": symbol,
|
||
"ambiguous": candidates.len() > 1,
|
||
"symbols": candidates.iter().map(|item| serde_json::json!({
|
||
"id": item.id.as_str(),
|
||
"module": item.module.as_str(),
|
||
"kind": item.kind.as_str(),
|
||
"callable": item.callable.map(|kind| kind.as_str()),
|
||
"name": item.name,
|
||
"declaration_path": item.declaration_path,
|
||
"source": {
|
||
"file": item.source,
|
||
"line": item.line,
|
||
},
|
||
})).collect::<Vec<_>>(),
|
||
})
|
||
);
|
||
} else {
|
||
for item in &candidates {
|
||
println!(
|
||
"{}\n {}{} {} · module {}\n {}:{}",
|
||
item.id,
|
||
item.kind.as_str(),
|
||
item.callable
|
||
.map(|kind| format!(" ({})", kind.as_str()))
|
||
.unwrap_or_default(),
|
||
item.declaration_path.join("."),
|
||
item.module,
|
||
item.source.display(),
|
||
item.line,
|
||
);
|
||
}
|
||
if candidates.len() > 1 {
|
||
println!(
|
||
"ambiguous: {} symbols named `{symbol}`; use one full mq:// URI",
|
||
candidates.len()
|
||
);
|
||
}
|
||
}
|
||
Ok(0)
|
||
}
|
||
KnowledgeAction::Resolve { reference, from } => {
|
||
let from = from
|
||
.as_deref()
|
||
.or_else(|| path.is_file().then_some(path.as_path()));
|
||
let index = marqdo::sema::ProjectIndex::build(if path.is_dir() {
|
||
marqdo::project::ProjectRoot::new(&path)?
|
||
} else {
|
||
marqdo::project::ProjectRoot::discover(&path)?
|
||
})?;
|
||
let symbol = resolve_knowledge_reference(&index, &reference, from)?;
|
||
let instance_only =
|
||
symbol.callable == Some(marqdo::sema::CallableKind::InstanceMethod);
|
||
if json {
|
||
println!(
|
||
"{}",
|
||
serde_json::json!({
|
||
"reference": reference,
|
||
"resolution": if instance_only { "instance_only" } else { "symbol" },
|
||
"symbol": {
|
||
"id": symbol.id.as_str(),
|
||
"module": symbol.module.as_str(),
|
||
"kind": symbol.kind.as_str(),
|
||
"callable": symbol.callable.map(|kind| kind.as_str()),
|
||
"declaration_path": symbol.declaration_path,
|
||
"source": { "file": symbol.source, "line": symbol.line },
|
||
},
|
||
})
|
||
);
|
||
} else {
|
||
println!("{}", symbol.id);
|
||
println!(" {}", symbol.declaration_path.join("."));
|
||
println!(" {}:{}", symbol.source.display(), symbol.line);
|
||
if instance_only {
|
||
println!(" instance_only: call through an instance receiver");
|
||
}
|
||
}
|
||
Ok(if instance_only { 1 } else { 0 })
|
||
}
|
||
KnowledgeAction::Lock { update, strict } => {
|
||
let semantic_root = if path.is_file() {
|
||
path.parent()
|
||
.unwrap_or_else(|| std::path::Path::new("."))
|
||
.to_path_buf()
|
||
} else {
|
||
path.clone()
|
||
};
|
||
let compilation =
|
||
marqdo::sema::compile_project(&marqdo::sema::CompileOptions {
|
||
project_root: semantic_root,
|
||
entry: path.is_file().then(|| path.clone()),
|
||
lock_mode: if strict {
|
||
marqdo::sema::LockMode::Strict
|
||
} else {
|
||
marqdo::sema::LockMode::Check
|
||
},
|
||
})?;
|
||
if update {
|
||
marqdo::sema::update_links_lock(&out, &compilation.semantics)?;
|
||
let body = serde_json::json!({"schema_version": 1, "status": "updated", "path": out.join("links.lock.json")});
|
||
if json {
|
||
println!("{body}");
|
||
} else {
|
||
println!(
|
||
"knowledge lock updated: {}",
|
||
out.join("links.lock.json").display()
|
||
);
|
||
}
|
||
Ok(0)
|
||
} else {
|
||
let report = marqdo::sema::check_links_lock(&out, &compilation.semantics)?;
|
||
if json {
|
||
println!("{}", report.to_json());
|
||
} else {
|
||
println!("knowledge lock: {}", report.status);
|
||
for id in report
|
||
.missing
|
||
.iter()
|
||
.chain(report.changed.iter())
|
||
.chain(report.removed.iter())
|
||
{
|
||
println!(" {id}");
|
||
}
|
||
}
|
||
Ok(if strict && !report.is_clean() { 3 } else { 0 })
|
||
}
|
||
}
|
||
KnowledgeAction::Impact { query } => {
|
||
let semantic_root = if path.is_file() {
|
||
path.parent()
|
||
.unwrap_or_else(|| std::path::Path::new("."))
|
||
.to_path_buf()
|
||
} else {
|
||
path.clone()
|
||
};
|
||
let compilation =
|
||
marqdo::sema::compile_project(&marqdo::sema::CompileOptions {
|
||
project_root: semantic_root,
|
||
entry: path.is_file().then(|| path.clone()),
|
||
lock_mode: marqdo::sema::LockMode::Off,
|
||
})?;
|
||
let report = marqdo::sema::semantic_impact(&compilation.semantics, &query);
|
||
if json {
|
||
println!("{report}");
|
||
} else {
|
||
for item in report["affected"].as_array().into_iter().flatten() {
|
||
println!(
|
||
"{} {}",
|
||
item["artifact_id"].as_str().unwrap_or(""),
|
||
item["display_name"].as_str().unwrap_or("")
|
||
);
|
||
}
|
||
}
|
||
Ok(if report["resolved"].as_bool() == Some(true) {
|
||
0
|
||
} else {
|
||
1
|
||
})
|
||
}
|
||
KnowledgeAction::Adjudicate {
|
||
artifact,
|
||
policy,
|
||
fail,
|
||
} => {
|
||
let semantic_root = if path.is_file() {
|
||
path.parent()
|
||
.unwrap_or_else(|| std::path::Path::new("."))
|
||
.to_path_buf()
|
||
} else {
|
||
path.clone()
|
||
};
|
||
let report = marqdo::mlsp::adjudicate_project(
|
||
&semantic_root,
|
||
artifact.as_deref(),
|
||
&policy,
|
||
)
|
||
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||
if json {
|
||
println!("{report}");
|
||
} else {
|
||
println!(
|
||
"knowledge adjudicate policy={} may_bind_all={} count={}",
|
||
policy,
|
||
report["may_bind_all"],
|
||
report["count"]
|
||
);
|
||
for item in report["reports"].as_array().into_iter().flatten() {
|
||
println!(
|
||
" {} evidence={} may_bind={} may_publish={}",
|
||
item["artifact_id"].as_str().unwrap_or(""),
|
||
item["verification"]["evidence"].as_str().unwrap_or(""),
|
||
item["may_bind"],
|
||
item["may_publish"]
|
||
);
|
||
}
|
||
}
|
||
Ok(
|
||
if fail && report["may_bind_all"].as_bool() != Some(true) {
|
||
1
|
||
} else {
|
||
0
|
||
},
|
||
)
|
||
}
|
||
KnowledgeAction::Preflight {
|
||
task,
|
||
min_candidates,
|
||
allow_create_after,
|
||
} => {
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let budget = ReuseBudget {
|
||
min_candidates,
|
||
allow_create_after,
|
||
..ReuseBudget::default()
|
||
};
|
||
let reuse = resolve(&graph, &task, &budget);
|
||
let pf = preflight_from_reuse(&graph, &task, &reuse, Some(&out))?;
|
||
let _ = compile_policy(&out, &task, &reuse);
|
||
if json {
|
||
println!("{}", preflight_json(&pf));
|
||
} else {
|
||
print!("{}", format_preflight_yaml(&pf));
|
||
}
|
||
Ok(
|
||
if pf.create_allowed || pf.decision == "REUSE" || pf.decision == "ADAPT" {
|
||
0
|
||
} else {
|
||
2
|
||
},
|
||
)
|
||
}
|
||
KnowledgeAction::Record {
|
||
decision,
|
||
duplicated,
|
||
} => {
|
||
let m = record_decision(&out, &decision, duplicated)?;
|
||
if json {
|
||
println!("{}", m.to_json());
|
||
} else {
|
||
println!(
|
||
"reuse_ratio={:.2} adapt_ratio={:.2} novel_ratio={:.2} duplication_rate={:.2}",
|
||
m.reuse_ratio(),
|
||
m.adapt_ratio(),
|
||
m.novel_ratio(),
|
||
m.duplication_rate()
|
||
);
|
||
}
|
||
Ok(0)
|
||
}
|
||
KnowledgeAction::Learn {
|
||
task,
|
||
failed_approach,
|
||
failure,
|
||
correct_approach,
|
||
decision_title,
|
||
decision_body,
|
||
constraint_title,
|
||
constraint_body,
|
||
} => {
|
||
let written = learn(
|
||
&out,
|
||
&LearnInput {
|
||
task,
|
||
failed_approach,
|
||
failure,
|
||
correct_approach,
|
||
decision_title,
|
||
decision_body,
|
||
constraint_title,
|
||
constraint_body,
|
||
},
|
||
)?;
|
||
for p in written {
|
||
println!("{}", p.display());
|
||
}
|
||
Ok(0)
|
||
}
|
||
}
|
||
}
|
||
Commands::Find {
|
||
query,
|
||
path,
|
||
out,
|
||
limit,
|
||
json,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let hits = find(&graph, &query, limit);
|
||
if json {
|
||
println!("{}", find_json(&query, &hits));
|
||
} else {
|
||
print!("{}", format_find_text(&query, &hits, &graph));
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Reuse {
|
||
task,
|
||
path,
|
||
out,
|
||
preflight: do_preflight,
|
||
min_candidates,
|
||
allow_create_after,
|
||
json,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let budget = ReuseBudget {
|
||
min_candidates,
|
||
allow_create_after,
|
||
..ReuseBudget::default()
|
||
};
|
||
let result = resolve(&graph, &task, &budget);
|
||
let _ = compile_policy(&out, &task, &result);
|
||
if do_preflight {
|
||
let _ = preflight_from_reuse(&graph, &task, &result, Some(&out))?;
|
||
}
|
||
if json {
|
||
println!("{}", reuse_json(&task, &result));
|
||
} else {
|
||
print!("{}", format_reuse_text(&task, &result));
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Impact {
|
||
paths,
|
||
root,
|
||
out,
|
||
json,
|
||
refresh,
|
||
} => {
|
||
let graph = graph_for_query(&root, &out, refresh)?;
|
||
let path_strs: Vec<String> = paths
|
||
.iter()
|
||
.map(|p| p.to_string_lossy().replace('\\', "/"))
|
||
.collect();
|
||
let report = impact(&graph, &path_strs);
|
||
if json {
|
||
println!("{}", impact_json(&report));
|
||
} else {
|
||
print!("{}", format_impact_text(&report));
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Conflicts {
|
||
path,
|
||
out,
|
||
json,
|
||
fail,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let items = find_conflicts(&graph);
|
||
if json {
|
||
println!("{}", conflicts_json(&items));
|
||
} else {
|
||
print!("{}", format_conflicts_text(&items));
|
||
}
|
||
Ok(if fail && !items.is_empty() { 1 } else { 0 })
|
||
}
|
||
Commands::Stale {
|
||
path,
|
||
out,
|
||
json,
|
||
fail,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let items = find_stale(&graph, &path);
|
||
if json {
|
||
println!("{}", stale_json(&items));
|
||
} else {
|
||
print!("{}", format_stale_text(&items));
|
||
}
|
||
Ok(if fail && !items.is_empty() { 1 } else { 0 })
|
||
}
|
||
Commands::Duplicate {
|
||
path,
|
||
out,
|
||
threshold,
|
||
fail,
|
||
json,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let pairs = find_duplicates(&graph.symbols, threshold);
|
||
if json {
|
||
println!(
|
||
"{}",
|
||
serde_json::json!({
|
||
"threshold": threshold,
|
||
"duplicates": pairs.iter().map(|p| serde_json::json!({
|
||
"left": p.left,
|
||
"right": p.right,
|
||
"similarity": p.similarity,
|
||
"left_resource": p.left_resource,
|
||
"right_resource": p.right_resource,
|
||
})).collect::<Vec<_>>(),
|
||
})
|
||
);
|
||
} else {
|
||
print!("{}", format_duplicates_text(&pairs));
|
||
}
|
||
if fail && !pairs.is_empty() {
|
||
Ok(1)
|
||
} else {
|
||
Ok(0)
|
||
}
|
||
}
|
||
Commands::Verify {
|
||
path,
|
||
out,
|
||
json,
|
||
fail_on_warning,
|
||
refresh,
|
||
} => {
|
||
let path = path.unwrap_or_else(|| PathBuf::from("."));
|
||
let graph = graph_for_query(&path, &out, refresh)?;
|
||
let issues = verify(&graph);
|
||
if json {
|
||
println!("{}", verify_json(&issues));
|
||
} else {
|
||
print!("{}", format_verify_text(&issues));
|
||
}
|
||
let errors = issues.iter().filter(|i| i.severity == "error").count();
|
||
let warnings = issues.iter().filter(|i| i.severity == "warning").count();
|
||
Ok(if errors > 0 || (fail_on_warning && warnings > 0) {
|
||
1
|
||
} else {
|
||
0
|
||
})
|
||
}
|
||
Commands::Ext { action } => {
|
||
match action {
|
||
ExtAction::List => list_ext()?,
|
||
ExtAction::Add { name } => add_ext(&name)?,
|
||
ExtAction::Remove { name } => remove_ext(&name)?,
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Version { check } => {
|
||
if check {
|
||
marqdo::version_check::check_latest().map_err(|e| anyhow::anyhow!(e))?;
|
||
} else {
|
||
marqdo::version_check::print_version();
|
||
}
|
||
Ok(0)
|
||
}
|
||
Commands::Wasm { action } => match action {
|
||
WasmAction::Build { out } => {
|
||
marqdo::wasm_cli::build_wasm(&out)?;
|
||
Ok(0)
|
||
}
|
||
},
|
||
}
|
||
}
|