diff --git a/agents.md b/agents.md index 9078b5aaace7eac7ed34ea2f0d86974e56b0fcb6..8f6f532f25dcb27c7c5c44ad54b771c9c45443b8 100644 --- a/agents.md +++ b/agents.md @@ -38,6 +38,8 @@ Strict **CSP** that blocks `eval` would break the current app. Other projects ma - **`RpcCommand` / `POST /api/v0/rpc`** (`types/src/lib.rs`, `server/src/api/rpc.rs`): **Bearer-authenticated** JSON API for CLI, automation, and programmatic clients. Durable effects go through here (append to event log, then `apply_event`). +- **Check / `check_sorter` diagnostics:** Parse errors include a 1-based **`line N:`** prefix. A dotted tilde token (`~mcdonalds.com`) fails on that line (not a valid tilde item name; `.` is not allowed; charset is `[a-z0-9_-]`), not on a later valid claim. A URL-shaped line without `http(s)://` (`mcdonalds.com { body }`) stays **prose** and adds a warning. `https://… { body }` remains a URL item (not prose). **`CheckOk.warnings`** also notes when a document defined items but includes no votes (ranking preview empty). + - **ChatGPT / Codex MCP (`POST /mcp`)** (`server/src/mcp/`): Streamable-HTTP JSON-RPC for the Plugins Directory. Tools call `dispatch_rpc` (same handlers as `POST /api/v0/rpc`). Public reads are `noauth` (and also accept a bearer). Private-room reads and all writes require `Authorization: Bearer slug_…` (issued as the OAuth access token). `post_sorter` **requires** `delegate` (`uuid:rig:provider/model`); the writer binds that delegate to the linked human and rejects other humans. `create_room` only creates private rooms (`visibility` must be `"private"`) and can grant `members`. Read results for posts expose `actor` and `delegate`. Authenticated private-room reads are first-class MCP tools: `list_rooms`, `read_room`, and `get_feed` use `oauth2` + `slug.read`. `get_feed` accepts the same `delegate` as CLI `feed` (server-side cutoff = that delegate's last ingest). `get_matchup` is CLI `garden matchup`. `health` is `GET /healthz` (liveness). Garden `item_path` / `parent_path` queries accept a bare leaf (`ship-sets` = `~ship-sets`). Private `room_id` accepts `shortid/slug` or the bare 7-char shortid. `identity_start` / `identity_poll` mint a conversation delegate (linked callers get it immediately; unlinked callers get a Google URL). Dual-mode garden/forum reads list `slug.read` before `noauth` so linked clients send the bearer. OAuth 2.1 + PKCE lives at `/oauth/authorize` + `/oauth/token` and reuses Google login. Redirect allowlist is ChatGPT (`chatgpt.com` / `chat.openai.com`), Claude (`claude.ai`, including `https://claude.ai/api/mcp/auth_callback`), and loopback HTTP. Well-known docs are `/.well-known/oauth-protected-resource` and `/.well-known/oauth-authorization-server`. Domain verification token is `GET /.well-known/openai-apps-challenge` from `SLUG_OPENAI_APPS_CHALLENGE`. Do not add a parallel write path. Do not reuse `POST /ui` + `eval` morph as a ChatGPT widget. The website/RPC still allow human posts with no delegate; only the MCP tool requires it. - **Forum thread tags on write:** `validate_thread_tag` (`types/src/paths.rs`) canonicalizes then rejects empty tags and any tag containing `/` (would break `/t/:tag` routing). The new-thread form already constrains the charset client-side (`pattern="[a-z0-9_\\-]{1,64}"`); server write paths (`WriteCmd::Post` / `SystemIngest`, and UI `PostIngest` / `CheckIngest` / `VoteComparePost`) enforce the slash rule. Read/replay still uses `canonicalize_tag` only so historical tags keep resolving. System import tags use `:` instead of `/` (e.g. `import:https:::github.com:org:repo`). diff --git a/cli/src/main.rs b/cli/src/main.rs index aa80ce3586b9f8f5102abfd5930ee39cd6e83bac..8d4f682f8553c04970dedb2a1a55653135006182 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1597,6 +1597,7 @@ async fn run_scoped(base: &str, room: &str, sub: ScopedCmd) -> Result<()> { rankings, threads, next, + warnings, } => { if json { println!( @@ -1606,10 +1607,17 @@ async fn run_scoped(base: &str, room: &str, sub: ScopedCmd) -> Result<()> { "rankings": rankings, "threads": threads, "next": next, + "warnings": warnings, }))? ); } else { println!("✓ check ok (dry-run)"); + if !warnings.is_empty() { + println!("warnings:"); + for w in warnings.iter() { + println!(" {w}"); + } + } if !threads.is_empty() { println!("threads:"); for t in threads { diff --git a/server/src/api/rpc.rs b/server/src/api/rpc.rs index adce34f4f9d7ff1ed3a773fa4cc4a925c1fe95f4..36b423117aa7e5c384c987d6da9f25d8dfa08945 100644 --- a/server/src/api/rpc.rs +++ b/server/src/api/rpc.rs @@ -539,6 +539,7 @@ async fn rpc_check( rankings, threads: vec![format!("#{}", thread_id)], next: check_next, + warnings: v.doc.check_warnings(), }) } diff --git a/server/src/dsl.rs b/server/src/dsl.rs index 4eb8da1f22a18f120eabe86aaf56352adeab5a21..8f6311564f5a061dc77371695a93ed3f51a1c9d3 100644 --- a/server/src/dsl.rs +++ b/server/src/dsl.rs @@ -4,6 +4,8 @@ use std::collections::HashMap; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Document { pub statements: Vec, + /// Non-fatal diagnostics (e.g. URL-shaped lines that were parsed as prose). + pub warnings: Vec, } /// A single statement in the DSL (or prose when using `parse_full`). @@ -43,6 +45,34 @@ pub enum Stmt { }, } +impl Document { + /// Extra dry-run notes: parse warnings plus a hint when items were defined + /// but this document cannot fill the ranking preview (no votes). + pub fn check_warnings(&self) -> Vec { + let mut warnings = self.warnings.clone(); + let item_titles: Vec<&str> = self + .statements + .iter() + .filter_map(|s| match s { + Stmt::Item { title, .. } => Some(title.as_str()), + _ => None, + }) + .collect(); + let has_vote = self + .statements + .iter() + .any(|s| matches!(s, Stmt::Vote { .. })); + if !item_titles.is_empty() && !has_vote { + warnings.push(format!( + "defined {} item(s) ({}); ranking preview is empty until this document includes a vote", + item_titles.len(), + item_titles.join(", ") + )); + } + warnings + } +} + #[derive(Debug, thiserror::Error)] pub enum DslError { #[error("parse error: {0}")] @@ -599,6 +629,37 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { parse_item_name_at_with_mode(s, i, false) } +/// `mcdonalds.com { body }` / `www.foo.com { x }` — looks like a definition but is prose. +fn looks_like_lone_definition(stripped: &str) -> bool { + if stripped.is_empty() + || stripped.starts_with('~') + || stripped.starts_with("http://") + || stripped.starts_with("https://") + || stripped.starts_with("-/") + || stripped.starts_with('#') + || stripped.starts_with(':') + { + return false; + } + let bytes = stripped.as_bytes(); + let mut i = 0usize; + while i < bytes.len() && !is_ws_byte(bytes[i]) && !bytes[i..].starts_with(b"__BLOCK_") { + i += 1; + } + if i == 0 { + return false; + } + let name = &stripped[..i]; + if !name.contains('.') && !name.contains("://") { + return false; + } + i = skip_ws(stripped, i); + let Some((_, end)) = parse_block_token_at(stripped, i) else { + return false; + }; + stripped[end..].trim().is_empty() +} + /// `~mcdonalds.com` lexes as `~mcdonalds` then leftover `.com`. Report the /// dotted spelling instead of a vote-explanation error that reads like a later /// valid claim failed. @@ -904,9 +965,13 @@ fn parse_item_definition_statement( return Ok(stmts); } - Err(DslError::Parse( - "vote explanations must start with a `{ ... }` block before the comparison".to_string(), - )) + if parse_comparison_at(stripped, i).is_some() { + return Err(DslError::Parse( + "vote explanations must start with a `{ ... }` block before the comparison".to_string(), + )); + } + + Err(DslError::Parse("extra tokens after item definition".to_string())) } fn parse_line(masked_line: &str, masker: &BlockMasker) -> Result, DslError> { @@ -975,6 +1040,7 @@ pub fn parse_full_strict(text: &str) -> Result { fn parse_full_with(text: &str, strict: bool) -> Result { let (masker, masked) = mask_all(BlockMasker::new(), text); let mut statements: Vec = Vec::new(); + let mut warnings: Vec = Vec::new(); let mut prose_buffer: Vec<&str> = Vec::new(); let mut pending_block: Option = None; let mut current_aspect: Option = None; @@ -1083,6 +1149,11 @@ fn parse_full_with(text: &str, strict: bool) -> Result { Err(DslError::Parse(msg)) => return Err(at_line(line_no, msg)), } } else { + if looks_like_lone_definition(stripped) { + warnings.push(format!( + "line {line_no} looks like an item definition but does not start with '~'; parsed as prose" + )); + } prose_buffer.push(line); } } @@ -1097,7 +1168,10 @@ fn parse_full_with(text: &str, strict: bool) -> Result { // Final flush flush_prose(&mut prose_buffer, &mut statements, &masker); - Ok(Document { statements }) + Ok(Document { + statements, + warnings, + }) } #[cfg(test)] @@ -2080,6 +2154,105 @@ kfc is a chain assert!(edges.is_empty()); } + #[test] + fn parse_dotted_tilde_token_errors_on_line_1_not_later_claim() { + let input = "\ +~mcdonalds.com { https://mcdonalds.com } +~fast-food-chains { the chains } + +{ if this resolves, the dot silently split the token } +~mcdonalds <: ~fast-food-chains +"; + let err = parse_full(input).unwrap_err().to_string(); + assert!( + err.contains("line 1:"), + "error must name the dotted definition line, got: {err}" + ); + assert!( + err.contains("~mcdonalds.com") + && err.contains("not a valid tilde item name") + && err.contains("`.`"), + "error must name the dotted token, got: {err}" + ); + assert!( + !err.contains("vote explanations must start"), + "must not misreport the later containment claim, got: {err}" + ); + } + + #[test] + fn parse_undotted_repro_still_succeeds() { + let input = "\ +~mcdonalds { https://mcdonalds.com } +~fast-food-chains { the chains } + +{ identical claim, only the dotted line above was removed } +~mcdonalds <: ~fast-food-chains +"; + let doc = parse_full(input).unwrap(); + assert!(doc.warnings.is_empty()); + assert_eq!(items(&doc).len(), 2); + assert_eq!(containments(&doc).len(), 1); + } + + #[test] + fn parse_url_shaped_prose_definition_warns() { + let input = "mcdonalds.com { fast food }\n~fast-food { chains }"; + let doc = parse_full(input).unwrap(); + assert_eq!( + doc.warnings, + vec![ + "line 1 looks like an item definition but does not start with '~'; parsed as prose" + .to_string() + ] + ); + assert!( + items(&doc) + .iter() + .all(|s| matches!(s, Stmt::Item { title, .. } if title == "~fast-food")), + "dotted host line must stay prose, not become an item" + ); + assert!(matches!( + doc.statements.first(), + Some(Stmt::Prose { text }) if text.contains("mcdonalds.com") + )); + } + + #[test] + fn parse_https_item_definition_still_creates_url_item() { + let doc = parse_full("https://mcdonalds.com { fast food }").unwrap(); + assert!( + doc.warnings.is_empty(), + "valid URL items are not prose; got {:?}", + doc.warnings + ); + assert_eq!( + items(&doc), + vec![&Stmt::Item { + title: "https://mcdonalds.com".to_string(), + body: Some("fast food".to_string()), + }] + ); + let notes = doc.check_warnings(); + assert!( + notes.iter().any(|w| w.contains("https://mcdonalds.com") + && w.contains("ranking preview is empty")), + "item-only docs must not look like a silent no-op, got {notes:?}" + ); + } + + #[test] + fn parse_nested_path_with_dot_errors_on_that_line() { + let err = parse_full("~a {a}\n~/food/subway.com { sandwich }\n") + .unwrap_err() + .to_string(); + assert!(err.contains("line 2:"), "got: {err}"); + assert!( + err.contains("subway.com") && err.contains("not a valid tilde item name"), + "got: {err}" + ); + } + #[test] fn nested_path_sugar_is_not_an_explicit_claim() { let doc = diff --git a/server/src/html/editor.rs b/server/src/html/editor.rs index 40bd231bc17fde96129489637f51828b613006eb..cf25ceb26e0a0d75fde4f3a24a5e79622eebfd3f 100644 --- a/server/src/html/editor.rs +++ b/server/src/html/editor.rs @@ -107,6 +107,7 @@ pub async fn editor_check( &v.doc, ); + let check_warnings = v.doc.check_warnings(); let status = html! { span class="editor-ok" { "valid" @@ -115,6 +116,13 @@ pub async fn editor_check( let results = html! { div id="editor-results" { + @if !check_warnings.is_empty() { + ul class="editor-warnings" { + @for w in &check_warnings { + li class="muted" { (w) } + } + } + } @for scope in &rankings { @let heading = match &scope.aspect { Some(slug) => format!("ranking: {} :{}", scope.parent, slug), diff --git a/server/src/mcp/mod.rs b/server/src/mcp/mod.rs index 520f4e759186819ff139e97c57b88cd01e08c151..e0f5d25a58f81aa40869c3384d8116c31b765dd8 100644 --- a/server/src/mcp/mod.rs +++ b/server/src/mcp/mod.rs @@ -467,7 +467,7 @@ fn tools_list() -> Value { tool( "check_sorter", "Dry-run a .sorter document", - "Parse and preview ranking and containment effects of a .sorter document without writing. Items are `~name` (or `~/path` sugar). Claims require a leading { explanation } then `~a <: ~b` / `~a !<: ~b` (explanation after the claim is a parse error, not a silent no-op). `==` is not a statement. Tilde names are [a-z0-9_-] (`~mcdonalds.com` is invalid).", + "Parse and preview ranking and containment effects of a .sorter document without writing. Items are `~name` (or `~/path` sugar). Claims require a leading { explanation } then `~a <: ~b` / `~a !<: ~b` (explanation after the claim is a parse error, not a silent no-op). `==` is not a statement. Tilde names are [a-z0-9_-] (`~mcdonalds.com` is invalid). URL-shaped lines without `https://` stay prose and return a warning. Parse errors include `line N:`.", json!({ "type": "object", "properties": { diff --git a/server/src/offline.rs b/server/src/offline.rs index 420f088c99b04f23ef29b32ec9f24cef4824dcce..7a2c85d9a1ba8f54afc44d00de1be0fcd2840ddd 100644 --- a/server/src/offline.rs +++ b/server/src/offline.rs @@ -28,6 +28,8 @@ pub struct CompileResult { pub threads: Vec, pub rankings: Vec, pub stats: CompileStats, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub warnings: Vec, #[serde(skip_serializing_if = "Option::is_none")] pub ingest_id: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -255,6 +257,7 @@ fn compile_document_inner( threads: threads_in_document(text), rankings: rankings_for_document(&simulated, &scope, room_key, &validated.doc), stats: document_stats(&validated.doc), + warnings: validated.doc.check_warnings(), ingest_id, ingest_line, }) @@ -421,6 +424,42 @@ mod tests { assert!(err.error.contains("undefined")); } + #[test] + fn compile_dotted_tilde_token_is_line_1_parse_error() { + let err = compile_document( + &ReducerState::default(), + "public", + "~mcdonalds.com { https://mcdonalds.com }\n~fast-food-chains { the chains }\n\n{ why }\n~mcdonalds <: ~fast-food-chains\n", + ) + .unwrap_err(); + let detail = err.parse_error.as_deref().unwrap_or(&err.error); + assert!(detail.contains("line 1:"), "got {detail}"); + assert!( + detail.contains("~mcdonalds.com") && detail.contains("not a valid tilde item name"), + "got {detail}" + ); + assert!(!detail.contains("vote explanations must start"), "got {detail}"); + } + + #[test] + fn compile_url_shaped_prose_definition_warns() { + let result = compile_document( + &ReducerState::default(), + "public", + "mcdonalds.com { fast food }\n~chains { the chains }", + ) + .unwrap(); + assert!(result.ok); + assert!( + result.warnings.iter().any(|w| w.contains("line 1") + && w.contains("does not start with '~'") + && w.contains("parsed as prose")), + "got {:?}", + result.warnings + ); + assert_eq!(result.stats.items, 1); + } + #[test] fn compile_canonical_plus_two_aspects_emits_three_ranking_groups() { let text = "\ diff --git a/types/src/lib.rs b/types/src/lib.rs index 282669cac6d979bf68071c5b8a174dcda035cc16..d84a33538dfef2c748716566fd8ac110b5b5abbb 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -546,6 +546,8 @@ pub enum RpcResult { rankings: Vec, threads: Vec, next: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + warnings: Vec, }, GardenRank(RankResponse), GardenItem(ItemResponse),