refactor: extract shared lint record primitives

This commit is contained in:
2026-05-12 21:56:25 +09:00
parent bedaf62cb0
commit cf822dbc5c
33 changed files with 233 additions and 281 deletions
+1 -1
View File
@@ -13,10 +13,10 @@
use std::collections::{BTreeMap, BTreeSet};
use crate::Slug;
use crate::schema::{
DecisionFrontmatter, KnowledgeFrontmatter, RequestFrontmatter, split_frontmatter,
};
use crate::slug::Slug;
use crate::workspace::{RecordKind, WorkspaceLayout};
/// `sources` overflow を flag する閾値。`linter::warnings::SOURCES_OVERFLOW_THRESHOLD`
+3 -8
View File
@@ -2,6 +2,7 @@
use std::path::PathBuf;
use lint_common::RecordLintError;
use thiserror::Error;
/// Top-level error for memory operations that don't fit the lint flow.
@@ -40,14 +41,8 @@ pub enum LintError {
#[error("path is for a different record kind than expected at this location: {}", .0.display())]
WrongRecordKind(PathBuf),
#[error("invalid slug `{0}`: must match ^[a-z0-9](?:[a-z0-9-]{{0,62}}[a-z0-9])?$")]
InvalidSlug(String),
#[error("malformed frontmatter: {0}")]
MalformedFrontmatter(String),
#[error("frontmatter is missing or document is empty")]
MissingFrontmatter,
#[error(transparent)]
Record(#[from] RecordLintError),
#[error("missing required frontmatter field: `{0}`")]
MissingField(&'static str),
+1 -2
View File
@@ -13,17 +13,16 @@ pub mod linter;
pub mod resident;
pub mod schema;
pub mod scope;
pub mod slug;
pub mod tool;
pub mod usage;
pub mod workspace;
pub use error::{LintError, LintWarning, MemoryError};
pub use extract::ExtractPointerPayload;
pub use lint_common::{RecordLintError, Slug, is_valid_slug};
pub use linter::{LintReport, Linter};
pub use resident::{ResidentKnowledgeEntry, collect_resident_knowledge, list_knowledge_slugs};
pub use scope::deny_write_rules;
pub use slug::Slug;
pub use usage::{
UsageEvent, UsageEventKind, UsageRecordSnapshot, UsageReport, UsageReportRecord, UsageSource,
append_resident_exposure_event, append_usage_event, append_use_event, build_usage_report,
+1 -1
View File
@@ -9,10 +9,10 @@ use std::collections::{HashMap, HashSet};
use std::io;
use std::path::Path;
use crate::Slug;
use crate::schema::{
DecisionFrontmatter, KnowledgeFrontmatter, RequestFrontmatter, split_frontmatter,
};
use crate::slug::Slug;
use crate::workspace::{RecordKind, WorkspaceLayout};
/// Snapshot of every record currently on disk under the workspace.
+2 -1
View File
@@ -1,5 +1,6 @@
//! YAML frontmatter parsing helpers shared by every kind.
use lint_common::RecordLintError;
use serde::de::DeserializeOwned;
use crate::error::LintError;
@@ -26,7 +27,7 @@ fn map_serde_error(err: serde_yaml::Error) -> LintError {
}
return LintError::InvalidField { field, message };
}
LintError::MalformedFrontmatter(msg)
LintError::Record(RecordLintError::MalformedFrontmatter(msg))
}
fn parse_missing_field(msg: &str) -> Option<&'static str> {
+5 -3
View File
@@ -18,6 +18,7 @@ mod warnings;
use std::path::Path;
use lint_common::RecordLintError;
use serde::de::DeserializeOwned;
use crate::error::{LintError, LintWarning};
@@ -104,8 +105,8 @@ impl Linter {
let existing = match existing::scan_existing(&self.layout) {
Ok(e) => e,
Err(e) => {
report.push_error(LintError::MalformedFrontmatter(format!(
"failed to scan existing records: {e}"
report.push_error(LintError::Record(RecordLintError::MalformedFrontmatter(
format!("failed to scan existing records: {e}"),
)));
return report;
}
@@ -354,7 +355,8 @@ mod tests {
let report = linter.lint(&path, &content, WriteMode::Create);
assert!(report.errors.iter().any(|e| matches!(
e,
LintError::MissingField(_) | LintError::MalformedFrontmatter(_)
LintError::MissingField(_)
| LintError::Record(RecordLintError::MalformedFrontmatter(_))
)));
}
+1 -1
View File
@@ -2,10 +2,10 @@
use std::collections::HashSet;
use crate::Slug;
use crate::error::LintError;
use crate::linter::ExistingRecords;
use crate::linter::LintReport;
use crate::slug::Slug;
use crate::workspace::RecordKind;
/// Validate a Decision's `replaced_by` against the existing record set.
+1 -1
View File
@@ -4,10 +4,10 @@
//! integrated into the main linter pass when implemented; this file
//! covers per-write checks that only need the proposed content.
use crate::Slug;
use crate::error::LintWarning;
use crate::linter::LintReport;
use crate::linter::existing::ExistingRecords;
use crate::slug::Slug;
use crate::workspace::{ClassifiedPath, RecordKind};
const LARGE_BODY_THRESHOLD: usize = 1500;
+12 -43
View File
@@ -1,10 +1,11 @@
//! Common frontmatter helpers and shared types.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::error::LintError;
pub use lint_common::Frontmatter;
/// Reference to a session-store entry range. Stored in `sources` /
/// `last_sources` arrays for traceability back to raw session logs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -14,53 +15,15 @@ pub struct SourceRef {
pub range: [u64; 2],
}
/// Trait every kind-specific frontmatter implements so the linter can
/// drive them uniformly.
pub trait Frontmatter: Sized {
/// Hard upper bound on body chars (excluding the frontmatter block).
const BODY_LIMIT: usize;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
}
const FRONTMATTER_DELIM: &str = "---";
/// Split a markdown document into `(yaml_frontmatter, body)`.
///
/// Expects the document to start with `---\n` and have a closing
/// `---\n` (or `---` at EOF) somewhere downstream. Trailing newline
/// after the closing delimiter is consumed.
pub fn split_frontmatter(content: &str) -> Result<(&str, &str), LintError> {
// The opening delimiter must be the very first line.
let after_open = content
.strip_prefix(FRONTMATTER_DELIM)
.and_then(|s| s.strip_prefix('\n').or(Some(s)))
.ok_or(LintError::MissingFrontmatter)?;
// Look for the closing `---` on its own line.
let mut yaml_end = None;
let mut byte_offset = 0usize;
for line in after_open.split_inclusive('\n') {
let trimmed = line.trim_end_matches('\n').trim_end_matches('\r');
if trimmed == FRONTMATTER_DELIM {
yaml_end = Some((byte_offset, byte_offset + line.len()));
break;
}
byte_offset += line.len();
}
let (yaml_end_excl, body_start) = yaml_end
.ok_or_else(|| LintError::MalformedFrontmatter("missing closing `---` line".to_string()))?;
let yaml = &after_open[..yaml_end_excl];
let body = &after_open[body_start..];
Ok((yaml, body))
lint_common::split_frontmatter(content).map_err(Into::into)
}
#[cfg(test)]
mod tests {
use super::*;
use lint_common::RecordLintError;
#[test]
fn splits_simple() {
@@ -73,13 +36,19 @@ mod tests {
#[test]
fn no_leading_delim_errors() {
let err = split_frontmatter("hello").unwrap_err();
assert!(matches!(err, LintError::MissingFrontmatter));
assert!(matches!(
err,
LintError::Record(RecordLintError::MissingFrontmatter)
));
}
#[test]
fn no_closing_delim_errors() {
let err = split_frontmatter("---\nfoo: 1\nno close\n").unwrap_err();
assert!(matches!(err, LintError::MalformedFrontmatter(_)));
assert!(matches!(
err,
LintError::Record(RecordLintError::MalformedFrontmatter(_))
));
}
#[test]
+5 -5
View File
@@ -3,8 +3,8 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::Slug;
use crate::schema::common::{Frontmatter, SourceRef};
use crate::slug::Slug;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
@@ -27,10 +27,10 @@ pub struct DecisionFrontmatter {
impl Frontmatter for DecisionFrontmatter {
const BODY_LIMIT: usize = 8000;
fn created_at(&self) -> DateTime<Utc> {
self.created_at
fn created_at(&self) -> Option<DateTime<Utc>> {
Some(self.created_at)
}
fn updated_at(&self) -> DateTime<Utc> {
self.updated_at
fn updated_at(&self) -> Option<DateTime<Utc>> {
Some(self.updated_at)
}
}
+4 -4
View File
@@ -24,10 +24,10 @@ pub struct KnowledgeFrontmatter {
impl Frontmatter for KnowledgeFrontmatter {
const BODY_LIMIT: usize = 8000;
fn created_at(&self) -> DateTime<Utc> {
self.created_at
fn created_at(&self) -> Option<DateTime<Utc>> {
Some(self.created_at)
}
fn updated_at(&self) -> DateTime<Utc> {
self.updated_at
fn updated_at(&self) -> Option<DateTime<Utc>> {
Some(self.updated_at)
}
}
+4 -4
View File
@@ -15,10 +15,10 @@ pub struct RequestFrontmatter {
impl Frontmatter for RequestFrontmatter {
const BODY_LIMIT: usize = 8000;
fn created_at(&self) -> DateTime<Utc> {
self.created_at
fn created_at(&self) -> Option<DateTime<Utc>> {
Some(self.created_at)
}
fn updated_at(&self) -> DateTime<Utc> {
self.updated_at
fn updated_at(&self) -> Option<DateTime<Utc>> {
Some(self.updated_at)
}
}
+4 -4
View File
@@ -23,10 +23,10 @@ impl Frontmatter for SummaryFrontmatter {
/// than per-record kinds (~5k tokens at the upper end).
const BODY_LIMIT: usize = 20000;
fn created_at(&self) -> DateTime<Utc> {
self.created_at.unwrap_or(self.updated_at)
fn created_at(&self) -> Option<DateTime<Utc>> {
Some(self.created_at.unwrap_or(self.updated_at))
}
fn updated_at(&self) -> DateTime<Utc> {
self.updated_at
fn updated_at(&self) -> Option<DateTime<Utc>> {
Some(self.updated_at)
}
}
-146
View File
@@ -1,146 +0,0 @@
//! Slug type and validation.
//!
//! Syntax (agent-skills compatible):
//! ^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$
//! - 164 chars
//! - lowercase ASCII alphanumerics and `-`
//! - cannot start or end with `-`
//! - no consecutive `--`
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize};
use crate::error::LintError;
const MIN_LEN: usize = 1;
const MAX_LEN: usize = 64;
/// Validated slug. Constructible only via [`Slug::parse`].
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
pub struct Slug(String);
impl Slug {
/// Parse and validate. Returns [`LintError::InvalidSlug`] on rejection.
pub fn parse(s: impl Into<String>) -> Result<Self, LintError> {
let s = s.into();
if is_valid_slug(&s) {
Ok(Self(s))
} else {
Err(LintError::InvalidSlug(s))
}
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for Slug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for Slug {
fn as_ref(&self) -> &str {
&self.0
}
}
impl FromStr for Slug {
type Err = LintError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl<'de> Deserialize<'de> for Slug {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let raw = String::deserialize(deserializer)?;
Self::parse(raw).map_err(serde::de::Error::custom)
}
}
/// Pure-fn predicate matching the agent-skills slug regex without
/// pulling in the `regex` crate.
pub fn is_valid_slug(s: &str) -> bool {
let bytes = s.as_bytes();
let len = bytes.len();
if len < MIN_LEN || len > MAX_LEN {
return false;
}
if !is_alnum_lower(bytes[0]) || !is_alnum_lower(bytes[len - 1]) {
return false;
}
let mut prev_dash = false;
for &b in bytes {
if b == b'-' {
if prev_dash {
return false;
}
prev_dash = true;
} else if is_alnum_lower(b) {
prev_dash = false;
} else {
return false;
}
}
true
}
fn is_alnum_lower(b: u8) -> bool {
b.is_ascii_digit() || b.is_ascii_lowercase()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_basic_slugs() {
for s in ["a", "ab", "abc-def", "x9", "a-b-c", "123", "a-1"] {
assert!(is_valid_slug(s), "expected `{s}` valid");
assert!(Slug::parse(s).is_ok());
}
}
#[test]
fn rejects_bad_slugs() {
for s in [
"", "-", "-foo", "foo-", "Foo", "foo_bar", "foo bar", "foo--bar", "foo.bar", "ä",
] {
assert!(!is_valid_slug(s), "expected `{s}` invalid");
assert!(Slug::parse(s).is_err());
}
}
#[test]
fn enforces_length_bounds() {
let too_long = "a".repeat(MAX_LEN + 1);
assert!(!is_valid_slug(&too_long));
let max = "a".repeat(MAX_LEN);
assert!(is_valid_slug(&max));
}
#[test]
fn deserializes_via_serde() {
let json = "\"valid-slug\"";
let slug: Slug = serde_json::from_str(json).unwrap();
assert_eq!(slug.as_str(), "valid-slug");
let bad = "\"BAD\"";
let err: Result<Slug, _> = serde_json::from_str(bad);
assert!(err.is_err());
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ use std::path::PathBuf;
use llm_worker::tool::ToolError;
use serde::Deserialize;
use crate::slug::Slug;
use crate::Slug;
use crate::workspace::{RecordKind, WorkspaceLayout};
pub use edit::edit_tool;
+1 -1
View File
@@ -227,7 +227,7 @@ fn record_path(
}
}
fn invalid_slug_error(err: crate::LintError) -> io::Error {
fn invalid_slug_error(err: lint_common::RecordLintError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, err)
}
+7 -3
View File
@@ -22,8 +22,9 @@
use std::path::{Path, PathBuf};
use crate::Slug;
use crate::error::LintError;
use crate::slug::Slug;
use lint_common::RecordLintError;
const INSOMNIA_DIR: &str = ".insomnia";
const MEMORY_DIR: &str = "memory";
@@ -159,7 +160,7 @@ impl WorkspaceLayout {
///
/// On a conventional path that's *almost* a record but malformed
/// (e.g. `.insomnia/memory/decisions/Foo.md` with an invalid slug),
/// returns `Err(LintError::InvalidSlug | InvalidPath)` so the caller
/// returns `Err(LintError::Record(InvalidSlug) | InvalidPath)` so the caller
/// can surface it as a write violation.
pub fn classify(&self, path: &Path) -> Result<Option<ClassifiedPath>, LintError> {
let memory = self.memory_dir();
@@ -320,7 +321,10 @@ mod tests {
let err = layout()
.classify(&PathBuf::from("/ws/.insomnia/memory/decisions/Foo.md"))
.unwrap_err();
assert!(matches!(err, LintError::InvalidSlug(_)));
assert!(matches!(
err,
LintError::Record(RecordLintError::InvalidSlug(_))
));
}
#[test]