feat: configure diagnostics by category
Rust CI JohnSlop passed all CI checks.
Gitea/john_slop/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-09-30 00:46:45 +02:00
parent cdc05af416
commit cd15bb8503
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
10 changed files with 290 additions and 7 deletions

View File

@ -117,7 +117,9 @@ Start with high-precision, independently testable signals:
Avoid weak demographic, dialect, spelling, grammar, vocabulary, or
English-fluency proxies. Repetition alone must not trigger a high evidence-band
verdict. Give users a per-rule disable/weight override and path/language scopes.
verdict. Give users category and per-rule enablement controls, per-rule weight
overrides, and path/language scopes. A rule is active only when both its
category gate and individual `enabled` field are active.
Scoring must be documented and deterministic. Use bounded weighted evidence,
apply a per-rule occurrence cap, and damp correlated findings from the same rule

View File

@ -3,6 +3,15 @@
schema_version = 1
replacements = []
# Diagnostic category gates are applied in addition to each rule's `enabled`
# field. Set a category to false to silence that family without copying every
# embedded rule into this file.
[diagnostics.categories]
typography = true
style = true
prompt-leakage = true
source-code = true
[input]
max_document_bytes = 8388608
max_archive_bytes = 67108864

View File

@ -495,6 +495,7 @@ fn apply_cli_overrides(config: &mut Config, overrides: &ConfigOverrides) {
#[serde(default, deny_unknown_fields)]
struct ConfigLayer {
schema_version: Option<u32>,
diagnostics: DiagnosticLayer,
input: InputLayer,
thresholds: ThresholdLayer,
unicode: UnicodeLayer,
@ -502,6 +503,21 @@ struct ConfigLayer {
rules: Vec<RulePatch>,
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct DiagnosticLayer {
categories: DiagnosticCategoryLayer,
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct DiagnosticCategoryLayer {
typography: Option<bool>,
style: Option<bool>,
prompt_leakage: Option<bool>,
source_code: Option<bool>,
}
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct UnicodeLayer {
@ -561,6 +577,7 @@ fn apply_layer(
if let Some(value) = layer.schema_version {
config.schema_version = value;
}
apply_diagnostic_layer(&mut config.diagnostics, layer.diagnostics);
apply_input_layer(&mut config.input, layer.input);
apply_threshold_layer(&mut config.thresholds, layer.thresholds);
if let Some(value) = layer.unicode.confusables {
@ -570,6 +587,22 @@ fn apply_layer(
merge_rules(&mut config.rules, layer.rules, path)
}
fn apply_diagnostic_layer(target: &mut DiagnosticPolicy, layer: DiagnosticLayer) {
let categories = layer.categories;
if let Some(value) = categories.typography {
target.categories.typography = value;
}
if let Some(value) = categories.style {
target.categories.style = value;
}
if let Some(value) = categories.prompt_leakage {
target.categories.prompt_leakage = value;
}
if let Some(value) = categories.source_code {
target.categories.source_code = value;
}
}
fn apply_input_layer(target: &mut InputLimits, layer: InputLayer) {
if let Some(value) = layer.max_document_bytes {
target.max_document_bytes = value;
@ -782,6 +815,8 @@ pub struct ReplacementRule {
pub struct Config {
/// Configuration schema understood by this build.
pub schema_version: u32,
/// Category-level diagnostic switches.
pub diagnostics: DiagnosticPolicy,
/// Enabled replacement rules in deterministic matching order.
pub replacements: Vec<ReplacementRule>,
/// Bulk behavior for generated Unicode mappings.
@ -795,6 +830,51 @@ pub struct Config {
pub input: InputLimits,
}
/// Category-level switches applied in addition to individual rule settings.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DiagnosticPolicy {
/// Enablement of each stable diagnostic category.
pub categories: DiagnosticCategories,
}
impl DiagnosticPolicy {
/// Return whether diagnostics in `category` are enabled.
pub const fn category_enabled(self, category: Category) -> bool {
match category {
Category::Typography => self.categories.typography,
Category::Style => self.categories.style,
Category::PromptLeakage => self.categories.prompt_leakage,
Category::SourceCode => self.categories.source_code,
}
}
}
/// Enablement switches for the four built-in diagnostic categories.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct DiagnosticCategories {
/// Suspicious typography or character usage.
pub typography: bool,
/// Repeated or templated writing style.
pub style: bool,
/// Prompt, response, or assistant scaffolding.
pub prompt_leakage: bool,
/// Low-care source-code patterns.
pub source_code: bool,
}
impl Default for DiagnosticCategories {
fn default() -> Self {
Self {
typography: true,
style: true,
prompt_leakage: true,
source_code: true,
}
}
}
/// Bulk policy for Unicode mappings whose targets come from UTS #39.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
@ -1337,6 +1417,53 @@ mod tests {
assert_eq!(config.thresholds.medium, 50);
}
#[test]
fn diagnostic_categories_follow_layer_precedence() {
let directory = tempfile::tempdir().unwrap();
let name = "strict".parse::<ConfigName>().unwrap();
let explicit = directory.path().join("project.toml");
std::fs::write(
directory.path().join("config.toml"),
"[diagnostics.categories]\nstyle = false\nprompt-leakage = false\n",
)
.unwrap();
std::fs::write(
directory.path().join("strict.toml"),
"[diagnostics.categories]\nstyle = true\n",
)
.unwrap();
std::fs::write(
&explicit,
"[diagnostics.categories]\nprompt-leakage = true\nsource-code = false\n",
)
.unwrap();
let paths =
ConfigPaths::in_directory(directory.path().to_owned(), Some(&name), Some(explicit));
let config = ConfigLoader::from_paths(paths).load().unwrap();
assert!(config.diagnostics.categories.typography);
assert!(config.diagnostics.categories.style);
assert!(config.diagnostics.categories.prompt_leakage);
assert!(!config.diagnostics.categories.source_code);
}
#[test]
fn unknown_diagnostic_category_is_a_layer_error() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("config.toml");
std::fs::write(
&path,
"[diagnostics.categories]\nmade-up-category = false\n",
)
.unwrap();
let paths = ConfigPaths::in_directory(directory.path().to_owned(), None, None);
let error = ConfigLoader::from_paths(paths).load().unwrap_err();
assert!(matches!(error, ConfigError::Layer(layer) if layer.path == path));
}
#[test]
fn absent_optional_files_are_ignored_but_missing_explicit_file_is_fatal() {
let directory = tempfile::tempdir().unwrap();

View File

@ -165,7 +165,11 @@ impl Detector {
let mut findings = self
.rules
.iter()
.filter(|rule| rule.is_enabled() && rule.applies(context))
.filter(|rule| {
rule.is_enabled()
&& self.config.diagnostics.category_enabled(rule.category())
&& rule.applies(context)
})
.flat_map(|rule| rule.findings(&self.config, source))
.collect::<Vec<_>>();
normalize_findings(&mut findings);
@ -555,6 +559,50 @@ mod tests {
assert_eq!(analysis.evidence_band, Some(EvidenceBand::BelowThreshold));
}
#[test]
fn category_and_individual_switches_are_both_required() {
let source = "In conclusion, it is complete.";
let mut config = Config::embedded();
config.diagnostics.categories.style = false;
assert!(
Detector::new(&config)
.unwrap()
.analyze(source)
.findings
.is_empty()
);
config.diagnostics.categories.style = true;
config
.rules
.iter_mut()
.find(|rule| rule.id == "templated-conclusion")
.unwrap()
.enabled = false;
assert!(
Detector::new(&config)
.unwrap()
.analyze(source)
.findings
.is_empty()
);
config
.rules
.iter_mut()
.find(|rule| rule.id == "templated-conclusion")
.unwrap()
.enabled = true;
assert_eq!(
Detector::new(&config)
.unwrap()
.analyze(source)
.findings
.len(),
1
);
}
#[test]
fn unicode_review_findings_have_exact_spans_and_safe_fixes() {
let detector = Detector::new(&Config::embedded()).unwrap();

View File

@ -17,8 +17,8 @@ mod unicode_review;
pub use config::{
Config, ConfigError, ConfigLayerError, ConfigLoader, ConfigName, ConfigNameError,
ConfigOverrides, ConfigPaths, ConfigValidationError, ConfusablePolicy, ContentKind,
EvidenceThresholds, HeuristicRule, InputLimits, InputOverrides, MatcherType, ReplacementRule,
RuleScope, ThresholdOverrides, UnicodePolicy,
DiagnosticCategories, DiagnosticPolicy, EvidenceThresholds, HeuristicRule, InputLimits,
InputOverrides, MatcherType, ReplacementRule, RuleScope, ThresholdOverrides, UnicodePolicy,
};
pub use detector::{AnalysisContext, Detector, DetectorBuildError, Rule};
pub use diagnostic::FindingDiagnostic;

View File

@ -41,15 +41,27 @@ Replacement and heuristic arrays merge by stable `id`. A partial record changes
only the supplied fields:
```toml
[diagnostics.categories]
# Disable every style diagnostic as one family.
style = false
[[replacements]]
id = "em-dash"
enabled = false
[[rules]]
id = "templated-conclusion"
weight = 2.5
# An individual rule switch is independent of the category switch.
enabled = false
```
The category keys are `typography`, `style`, `prompt-leakage`, and
`source-code`. A diagnostic runs only when its category switch and its
individual `[[rules]]` `enabled` field are both `true`. Consequently, enabling
a category does not silently re-enable a rule that another layer disabled
individually; set that rule's `enabled = true` explicitly when that is intended.
Later files override earlier category values using the normal precedence above.
A new ID must provide every field. Scope arrays replace the prior array as a
whole. A rule scope can select `prose` or `source-code`, restrict lowercase
extensions, and use `include`/`exclude` path globs. The `skip_generated`,

View File

@ -2,6 +2,12 @@
# it out makes compatibility expectations explicit.
schema_version = 1
[diagnostics.categories]
# Category gates can silence a complete diagnostic family. All four categories
# (`typography`, `style`, `prompt-leakage`, and `source-code`) default to true.
style = true
prompt-leakage = true
[input]
# Bound each decoded document to 4 MiB for this installation.
max_document_bytes = 4194304
@ -20,7 +26,7 @@ enabled = false
[[rules]]
id = "templated-conclusion"
# Disable a rule that is unsuitable for the user's normal documents.
# Disable one rule even while its `style` category remains enabled.
enabled = false
[[rules]]

View File

@ -581,7 +581,7 @@ is 93.05% (1,487/1,598 lines), and release metadata agrees on `0.1.22`.
## Milestone 23 — Diagnostic policy and local configuration bootstrap
- [ ] Add typed category gates to the existing layered TOML schema while
- [x] Add typed category gates to the existing layered TOML schema while
retaining `[[rules]] enabled = true|false` for individual diagnostics. A rule
runs only when both its category and individual switches are enabled; test
user, named, and explicit-layer precedence plus unknown configuration keys.

View File

@ -124,6 +124,63 @@ fn config_show_emits_resolved_toml_with_explicit_overrides() {
assert_eq!(resolved["replacements"].as_array().unwrap().len(), 1_829);
}
#[test]
fn category_and_individual_configuration_disable_cli_diagnostics() {
let directory = tempdir().expect("temp dir");
let source = directory.path().join("article.md");
fs::write(&source, "As an AI language model, I can help.\n").unwrap();
let category_config = directory.path().join("category.toml");
fs::write(
&category_config,
"[diagnostics.categories]\nprompt-leakage = false\n",
)
.unwrap();
let category = detector()
.env(
"XDG_CONFIG_HOME",
directory.path().join("empty-config-home"),
)
.args(["--output", "json"])
.arg("--config")
.arg(&category_config)
.arg(&source)
.output()
.unwrap();
assert!(category.status.success());
assert!(
json_records(&category)[0]["findings"]
.as_array()
.unwrap()
.is_empty()
);
let rule_config = directory.path().join("rule.toml");
fs::write(
&rule_config,
"[[rules]]\nid = \"assistant-self-identification\"\nenabled = false\n",
)
.unwrap();
let individual = detector()
.env(
"XDG_CONFIG_HOME",
directory.path().join("empty-config-home"),
)
.args(["--output", "json"])
.arg("--config")
.arg(&rule_config)
.arg(&source)
.output()
.unwrap();
assert!(individual.status.success());
assert!(
json_records(&individual)[0]["findings"]
.as_array()
.unwrap()
.is_empty()
);
}
#[test]
fn config_paths_lists_embedded_and_platform_sources() {
let directory = tempdir().expect("temp dir");

View File

@ -542,6 +542,28 @@ 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
.notify(
"textDocument/didOpen",
open_params(
"file:///tmp/category.md",
1,
"As an AI language model, I can help.",
),
)
.await;
let published = harness
.notification("textDocument/publishDiagnostics")
.await;
assert!(published["diagnostics"].as_array().unwrap().is_empty());
}
#[tokio::test]
async fn malformed_messages_and_invalid_uris_are_rejected_without_panics() {
assert!("not JSON".parse::<Request>().is_err());