diff --git a/README.md b/README.md index 6b59c7a..f2ad001 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,9 @@ spans: Unicode, and invisible or bidirectional controls. - `john-slop-fmt` previews, checks, or explicitly applies configured ASCII replacements, including the default printable-ASCII UTS #39 profile. -- `john-slop-lsp` publishes the detector's heuristic diagnostics over stdio, - offers quick fixes for safe replacements, and provides formatter-equivalent - document edits without writing editor-owned files. +- `john-slop-lsp` publishes Unicode diagnostics over stdio by default, offers + explicit weighted or all-diagnostic opt-ins, provides quick fixes for safe + replacements, and formats without writing editor-owned files. The detector accepts UTF-8 files, directories, ZIP archives, and common ZIP-based office documents. Each finding includes its source span, stable rule diff --git a/crates/john-slop-lsp/src/lib.rs b/crates/john-slop-lsp/src/lib.rs index 8b6a5a8..ef9f305 100644 --- a/crates/john-slop-lsp/src/lib.rs +++ b/crates/john-slop-lsp/src/lib.rs @@ -19,9 +19,34 @@ use tower_lsp_server::{ }; use john_slop_core::{ - AnalysisContext, Config, ConfigLoader, Detector, FindingSeverity, plan_replacements, + AnalysisContext, Config, ConfigLoader, Detector, Finding, FindingSeverity, plan_replacements, }; +/// Select which shared-detector findings are published to an editor. +#[derive(Debug, Clone, Copy, Default, PartialEq)] +pub enum DiagnosticFilter { + /// Publish only exact Unicode replacement and control findings. + #[default] + UnicodeOnly, + /// Publish Unicode findings plus non-Unicode findings at or above this weight. + MinimumWeight(f64), + /// Publish every finding produced by the resolved detector configuration. + All, +} + +impl DiagnosticFilter { + fn includes(self, finding: &Finding) -> bool { + let unicode = finding.rule_id.as_str().starts_with("unicode-"); + match self { + Self::UnicodeOnly => unicode, + Self::MinimumWeight(minimum) => { + unicode || (minimum.is_finite() && minimum >= 0.0 && finding.weight >= minimum) + } + Self::All => true, + } + } +} + /// Failure to convert a canonical UTF-8 byte offset into an LSP position. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum PositionError { @@ -148,12 +173,14 @@ fn finding_diagnostics( source: &str, context: &AnalysisContext, version: i32, + filter: DiagnosticFilter, ) -> Result, PositionError> { let index = LineIndex::new(source); detector .analyze_with(source, context) .findings .into_iter() + .filter(|finding| filter.includes(finding)) .map(|finding| { let message = match (&finding.fix, &finding.help) { (Some(edit), Some(help)) => format!( @@ -179,6 +206,7 @@ fn finding_diagnostics( diagnostic.data = Some(serde_json::json!({ "category": finding.category.as_str(), "fixable": finding.fix.is_some(), + "weight": finding.weight, "version": version, })); Ok(diagnostic) @@ -392,6 +420,7 @@ pub struct LspBackend { config_loader: ConfigLoader, runtime: RwLock, documents: RwLock, + diagnostic_filter: DiagnosticFilter, log_json: bool, } @@ -408,10 +437,18 @@ impl LspBackend { config_loader, runtime: RwLock::new(runtime), documents: RwLock::new(DocumentStore::default()), + diagnostic_filter: DiagnosticFilter::default(), log_json: false, }) } + /// Set the editor-facing diagnostic policy without changing detector scoring. + #[must_use] + pub const fn with_diagnostic_filter(mut self, filter: DiagnosticFilter) -> Self { + self.diagnostic_filter = filter; + self + } + /// Enable newline-delimited JSON lifecycle logging on standard error. #[must_use] pub const fn with_json_logging(mut self, enabled: bool) -> Self { @@ -449,6 +486,7 @@ impl LspBackend { &snapshot.text, &context, snapshot.version, + self.diagnostic_filter, ) .unwrap_or_default() }; @@ -612,7 +650,7 @@ mod tests { }; use super::{ - DocumentStore, LineIndex, OpenDocument, PositionError, analysis_context, + DiagnosticFilter, DocumentStore, LineIndex, OpenDocument, PositionError, analysis_context, finding_diagnostics, ranges_overlap, replacement_code_actions, replacement_diagnostics, requested_version_is_current, }; @@ -770,6 +808,7 @@ mod tests { source, &crate::AnalysisContext::for_path("example.txt"), 12, + DiagnosticFilter::All, ) .unwrap(); @@ -797,6 +836,49 @@ mod tests { assert_eq!(diagnostics[2].data.as_ref().unwrap()["fixable"], false); assert_eq!(diagnostics[2].data.as_ref().unwrap()["version"], 12); assert_eq!(diagnostics[2].data.as_ref().unwrap()["category"], "style"); + assert_eq!(diagnostics[2].data.as_ref().unwrap()["weight"], 4.0); + } + + #[test] + fn diagnostic_filters_keep_unicode_and_gate_non_unicode_by_weight() { + let detector = crate::Detector::new(&crate::Config::embedded()).unwrap(); + let source = "As an AI language model— I hope this helps."; + let context = crate::AnalysisContext::for_path("example.txt"); + + let unicode = finding_diagnostics( + &detector, + source, + &context, + 1, + DiagnosticFilter::UnicodeOnly, + ) + .unwrap(); + assert_eq!(unicode.len(), 1); + assert_eq!( + unicode[0].code, + Some(NumberOrString::String( + "unicode-ascii-replacement".to_owned() + )) + ); + + let weighted = finding_diagnostics( + &detector, + source, + &context, + 1, + DiagnosticFilter::MinimumWeight(10.0), + ) + .unwrap(); + assert_eq!(weighted.len(), 2); + assert!(weighted.iter().any(|diagnostic| { + diagnostic.code + == Some(NumberOrString::String( + "assistant-self-identification".to_owned(), + )) + })); + assert!(!weighted.iter().any(|diagnostic| { + diagnostic.code == Some(NumberOrString::String("stock-helpful-closing".to_owned())) + })); } #[test] diff --git a/docs/architecture.md b/docs/architecture.md index 727ac3d..3a6b952 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -97,8 +97,9 @@ Configuration reload swaps only a fully validated model and republishes open documents in deterministic URI order. The detector is compiled once per resolved configuration rather than once per edit. File URI paths drive normal scope applicability; language IDs supply source/prose context for untitled or -extensionless buffers. All findings become diagnostics, while only findings -with core `TextEdit`s become code actions. Document formatting calls the same +extensionless buffers. The default publication filter retains only Unicode +findings; explicit all-diagnostic and minimum-weight modes expose broader +findings, while only findings with core `TextEdit`s become code actions. Document formatting calls the same replacement planner directly and never derives mutations from diagnostics. See [ADR 0003](decisions/0003-lsp-framework-and-protocol.md) for framework and protocol choices. diff --git a/docs/decisions/0003-lsp-framework-and-protocol.md b/docs/decisions/0003-lsp-framework-and-protocol.md index 42d5324..77d32e5 100644 --- a/docs/decisions/0003-lsp-framework-and-protocol.md +++ b/docs/decisions/0003-lsp-framework-and-protocol.md @@ -77,6 +77,17 @@ This amendment does not broaden mutation behavior: The original full-snapshot synchronization, UTF-16 conversion, version checks, stdio purity, no-write boundary, and stale-action guarantees remain unchanged. +## Amendment: quiet diagnostic publication + +On 2026-09-30, Milestone 26 changed the presentation default without changing +the shared detector. The LSP now publishes only Unicode findings unless it is +started with `--all-diagnostics` or `--minimum-diagnostic-weight WEIGHT`. +Minimum-weight mode always retains Unicode findings and filters only the other +categories. This makes exact, fixable problems the quiet editor default while +keeping the full catalog easy to exercise in tests. Filtering occurs after +analysis, so CLI results, evidence scores, formatting, configuration reload, +and the core finding contract remain unchanged. + ## Amendment: document formatting parity Milestone 21 adds the standard `textDocument/formatting` capability. The server diff --git a/docs/editors.md b/docs/editors.md index f378176..f7766fa 100644 --- a/docs/editors.md +++ b/docs/editors.md @@ -1,11 +1,11 @@ # Editor setup -Both `john-slop-fmt` and `john-slop-lsp` are operational. The LSP publishes all -applicable configured heuristic findings, including prompt leakage, repeated -transitions, source placeholders, tutorial narration, and typography. Safe -ASCII-replacement findings additionally provide versioned quick fixes, and the -server implements whole-document formatting with the exact same replacement -planner as `john-slop-fmt`. +Both `john-slop-fmt` and `john-slop-lsp` are operational. The LSP publishes only +configured Unicode replacement and control findings by default, keeping noisy +or error-prone heuristics out of the normal editor loop. Safe ASCII-replacement +findings additionally provide versioned quick fixes, and the server implements +whole-document formatting with the exact same replacement planner as +`john-slop-fmt`. Install all three binaries somewhere on `PATH`, then confirm the editor-facing help is available: @@ -111,6 +111,10 @@ The server uses the same configuration layers as the other binaries. Add append those arguments to the Vim `cmd` list, when an editor-specific named configuration is wanted. Use `args = ["--diagnostics", "diagnostics.toml"]` (and the equivalent Vim command argument) for a project runtime diagnostic catalog. +Add `--all-diagnostics` to exercise every configured rule during testing, or +`--minimum-diagnostic-weight 10` to retain Unicode findings and opt into only +non-Unicode findings with weight 10 or greater. This visibility filter does not +change detector scoring or formatting. `--log-json` is available for troubleshooting and writes lifecycle metadata to stderr without document contents. @@ -128,7 +132,7 @@ cargo run --bin john-slop-fmt -- \ examples/fixtures/typography/example.txt ``` -For non-fixable heuristic diagnostics, open +After starting the server with `--all-diagnostics`, open `examples/fixtures/style/example.txt` to see `repeated-transition` and other style findings, or `examples/fixtures/source/example.rs` to see source-only placeholder and narration findings alongside typography. Advice maps to LSP diff --git a/docs/usage.md b/docs/usage.md index 6e62217..b54cbfd 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -68,6 +68,12 @@ Use `john-slop config show` to print the resolved TOML and `john-slop config paths` to inspect source precedence. See [Configuration](configuration.md) for the schema and merge rules. +The LSP publishes Unicode findings by default. Start `john-slop-lsp` with +`--all-diagnostics` to publish the complete configured detector catalog, or use +`--minimum-diagnostic-weight WEIGHT` to retain Unicode findings and add only +non-Unicode findings at or above that weight. This editor-only filter does not +change CLI analysis, evidence scores, replacement planning, or formatting. + ## Rust examples The [`examples` directory](../examples/README.md) contains runnable Cargo @@ -141,15 +147,19 @@ john-slop-lsp john-slop-lsp --config-name editorial john-slop-lsp --config ./john-slop.toml john-slop-lsp --diagnostics ./diagnostics.toml +john-slop-lsp --all-diagnostics +john-slop-lsp --minimum-diagnostic-weight 10 ``` The server advertises full-document synchronization and quick-fix code actions. It stores only currently open UTF-8 snapshots, their URI, language ID, and monotonically increasing version. On open or an accepted newer full-document change it runs the same configured detector as the CLI and publishes every -applicable heuristic finding. File URI extensions select prose/source and path -scopes; editor language IDs provide the prose/source fallback for untitled or -extensionless buffers. +applicable Unicode finding. `--all-diagnostics` publishes the complete result; +`--minimum-diagnostic-weight WEIGHT` adds only non-Unicode findings meeting the +threshold. File URI extensions select prose/source and path scopes; editor +language IDs provide the prose/source fallback for untitled or extensionless +buffers. Each diagnostic has source `john-slop`, the stable heuristic rule ID as its code, the configured severity and remediation text, document-version metadata, @@ -165,7 +175,7 @@ source-review findings are diagnostic-only. A request carrying stale John Slop diagnostic metadata is rejected. Closing a document removes its snapshot and publishes an empty diagnostic set. `workspace/didChangeConfiguration` reloads the normal embedded, user, named, and explicit layers, recompiles the detector, -and republishes every open snapshot; a failed reload keeps the last valid +and republishes every open snapshot under the unchanged visibility filter; a failed reload keeps the last valid configuration and detector. Stdout is reserved for JSON-RPC framing. The server is quiet by default; pass diff --git a/src/bin/john-slop-lsp.rs b/src/bin/john-slop-lsp.rs index e36ec3c..46bca81 100644 --- a/src/bin/john-slop-lsp.rs +++ b/src/bin/john-slop-lsp.rs @@ -6,7 +6,10 @@ mod editor_help; use std::path::PathBuf; use clap::Parser; -use john_slop::{ConfigLoader, ConfigName, lsp::LspBackend}; +use john_slop::{ + ConfigLoader, ConfigName, + lsp::{DiagnosticFilter, LspBackend}, +}; use miette::Result; use tower_lsp_server::{LspService, Server}; @@ -31,6 +34,19 @@ struct Cli { #[arg(long, value_name = "FILE", action = clap::ArgAction::Append)] diagnostics: Vec, + /// Publish all configured diagnostics instead of the Unicode-only default. + #[arg(long, conflicts_with = "minimum_diagnostic_weight")] + all_diagnostics: bool, + + /// Also publish non-Unicode diagnostics at or above this configured weight. + #[arg( + long, + value_name = "WEIGHT", + value_parser = parse_diagnostic_weight, + conflicts_with = "all_diagnostics" + )] + minimum_diagnostic_weight: Option, + /// Emit newline-delimited JSON lifecycle logs to standard error. #[arg(long)] log_json: bool, @@ -39,6 +55,13 @@ struct Cli { #[tokio::main] async fn main() -> Result<()> { let cli = Cli::parse(); + let diagnostic_filter = if cli.all_diagnostics { + DiagnosticFilter::All + } else if let Some(minimum) = cli.minimum_diagnostic_weight { + DiagnosticFilter::MinimumWeight(minimum) + } else { + DiagnosticFilter::UnicodeOnly + }; let loader = ConfigLoader::discover(cli.config_name.as_ref(), cli.config)? .with_diagnostic_files(cli.diagnostics); let config = loader.load()?; @@ -46,6 +69,7 @@ async fn main() -> Result<()> { // `ConfigLoader::load` validated this exact snapshot immediately above. LspBackend::new(client, loader, config) .expect("validated configuration must build the LSP detector") + .with_diagnostic_filter(diagnostic_filter) .with_json_logging(cli.log_json) }); Server::new(tokio::io::stdin(), tokio::io::stdout(), socket) @@ -53,3 +77,13 @@ async fn main() -> Result<()> { .await; Ok(()) } + +fn parse_diagnostic_weight(value: &str) -> Result { + let weight = value + .parse::() + .map_err(|_| "weight must be a finite non-negative number".to_owned())?; + if !weight.is_finite() || weight < 0.0 { + return Err("weight must be a finite non-negative number".to_owned()); + } + Ok(weight) +} diff --git a/tasks.md b/tasks.md index 1a34602..5402017 100644 --- a/tasks.md +++ b/tasks.md @@ -672,7 +672,7 @@ and all release metadata and binary versions agree on `0.1.25`. low-confidence population-level signals, and unreliable authorship inference. Add conservative data-driven rules with positive and near-miss evaluation fixtures, while explicitly excluding fluency and perplexity proxies. -- [ ] Make LSP diagnostics Unicode-only by default. Add an +- [x] Make LSP diagnostics Unicode-only by default. Add an `--all-diagnostics` testing switch and a minimum-weight opt-in that always retains Unicode findings while suppressing lighter non-Unicode signals. Cover the policy through unit, protocol, and CLI-help tests. diff --git a/tests/adapter_help.rs b/tests/adapter_help.rs index 5eb4b2e..6d9e162 100644 --- a/tests/adapter_help.rs +++ b/tests/adapter_help.rs @@ -34,4 +34,26 @@ fn lsp_help_contains_helix_and_vim_setup() { assert!(stdout.contains("lsp#register_server")); assert!(stdout.contains("['john-slop-lsp']")); assert!(stdout.contains("--diagnostics ")); + assert!(stdout.contains("--all-diagnostics")); + assert!(stdout.contains("--minimum-diagnostic-weight ")); +} + +#[test] +fn lsp_rejects_invalid_or_conflicting_diagnostic_filters() { + let invalid = Command::new(env!("CARGO_BIN_EXE_john-slop-lsp")) + .args(["--minimum-diagnostic-weight", "NaN"]) + .output() + .expect("run LSP with invalid weight"); + assert!(!invalid.status.success()); + assert!( + String::from_utf8(invalid.stderr) + .unwrap() + .contains("finite non-negative") + ); + + let conflicting = Command::new(env!("CARGO_BIN_EXE_john-slop-lsp")) + .args(["--all-diagnostics", "--minimum-diagnostic-weight", "10"]) + .output() + .expect("run LSP with conflicting filters"); + assert!(!conflicting.status.success()); } diff --git a/tests/lsp.rs b/tests/lsp.rs index b764f96..bd789c0 100644 --- a/tests/lsp.rs +++ b/tests/lsp.rs @@ -4,7 +4,9 @@ use std::{fs, ops::Range as ByteRange, path::PathBuf}; use futures::{FutureExt, StreamExt}; use john_slop::{ - Config, ConfigLoader, ConfigPaths, apply_replacements, lsp::LspBackend, plan_replacements, + Config, ConfigLoader, ConfigPaths, apply_replacements, + lsp::{DiagnosticFilter, LspBackend}, + plan_replacements, }; use serde_json::{Value, json}; use tempfile::TempDir; @@ -24,12 +26,24 @@ struct Harness { impl Harness { async fn new(explicit_config: Option<&str>) -> Self { - Self::new_with_diagnostics(explicit_config, None).await + Self::new_with_filter(explicit_config, None, DiagnosticFilter::UnicodeOnly).await + } + + async fn new_all(explicit_config: Option<&str>) -> Self { + Self::new_with_filter(explicit_config, None, DiagnosticFilter::All).await } async fn new_with_diagnostics( explicit_config: Option<&str>, diagnostic_catalog: Option<&str>, + ) -> Self { + Self::new_with_filter(explicit_config, diagnostic_catalog, DiagnosticFilter::All).await + } + + async fn new_with_filter( + explicit_config: Option<&str>, + diagnostic_catalog: Option<&str>, + diagnostic_filter: DiagnosticFilter, ) -> Self { let directory = tempfile::tempdir().unwrap(); let explicit = explicit_config.map(|contents| { @@ -52,7 +66,9 @@ impl Harness { ConfigLoader::from_paths(paths).with_diagnostic_files(diagnostics.iter().cloned()); let config = loader.load().unwrap(); let (service, socket) = LspService::new(|client| { - LspBackend::new(client, loader, config).expect("build test LSP backend") + LspBackend::new(client, loader, config) + .expect("build test LSP backend") + .with_diagnostic_filter(diagnostic_filter) }); let mut harness = Self { service, @@ -163,7 +179,7 @@ fn open_params_with_language(uri: &str, language_id: &str, version: i32, text: & #[tokio::test] async fn publishes_style_and_source_findings_without_non_fixable_actions() { - let mut harness = Harness::new(None).await; + let mut harness = Harness::new_all(None).await; let prose_uri = "file:///tmp/article.txt"; let prose = "Moreover, the first claim.\nAdditionally, the second claim."; harness @@ -256,6 +272,57 @@ async fn publishes_style_and_source_findings_without_non_fixable_actions() { ); } +#[tokio::test] +async fn default_lsp_publishes_unicode_but_suppresses_other_findings() { + let mut harness = Harness::new(None).await; + let uri = "file:///tmp/quiet-default.txt"; + harness + .notify( + "textDocument/didOpen", + open_params(uri, 1, "As an AI language model—I hope this helps."), + ) + .await; + + let published = harness + .notification("textDocument/publishDiagnostics") + .await; + let diagnostics = published["diagnostics"].as_array().unwrap(); + assert_eq!(diagnostics.len(), 1); + assert_eq!(diagnostics[0]["code"], "unicode-ascii-replacement"); +} + +#[tokio::test] +async fn minimum_weight_keeps_unicode_and_only_heavier_non_unicode_findings() { + let mut harness = + Harness::new_with_filter(None, None, DiagnosticFilter::MinimumWeight(10.0)).await; + harness + .notify( + "textDocument/didOpen", + open_params( + "file:///tmp/weighted.txt", + 1, + "As an AI language model—I hope this helps.", + ), + ) + .await; + + let published = harness + .notification("textDocument/publishDiagnostics") + .await; + let codes = published["diagnostics"] + .as_array() + .unwrap() + .iter() + .map(|diagnostic| diagnostic["code"].as_str().unwrap()) + .collect::>(); + assert_eq!( + codes, + ["assistant-self-identification", "unicode-ascii-replacement"] + .into_iter() + .collect() + ); +} + #[tokio::test] async fn unicode_review_findings_separate_safe_fixes_from_controls() { let mut harness = Harness::new(None).await; @@ -630,7 +697,7 @@ enabled = true #[tokio::test] async fn disabled_category_suppresses_lsp_diagnostics() { let mut harness = - Harness::new(Some("[diagnostics.categories]\nprompt-leakage = false\n")).await; + Harness::new_all(Some("[diagnostics.categories]\nprompt-leakage = false\n")).await; harness .notify( "textDocument/didOpen",