Skip to main content

sqz_engine/
engine.rs

1use std::path::Path;
2use std::sync::{Arc, Mutex};
3
4use crate::ast_parser::AstParser;
5use crate::budget_tracker::{BudgetTracker, UsageReport};
6use crate::cache_manager::CacheManager;
7use crate::confidence_router::ConfidenceRouter;
8use crate::cost_calculator::{CostCalculator, SessionCostSummary};
9use crate::ctx_format::CtxFormat;
10use crate::error::{Result, SqzError};
11use crate::model_router::ModelRouter;
12use crate::pin_manager::PinManager;
13use crate::pipeline::CompressionPipeline;
14use crate::plugin_api::PluginLoader;
15use crate::preset::{Preset, PresetParser};
16use crate::session_store::{SessionStore, SessionSummary};
17use crate::terse_mode::TerseMode;
18use crate::types::{CompressedContent, PinEntry, Provenance, SessionId};
19use crate::verifier::Verifier;
20
21/// Top-level facade that wires all sqz_engine modules together.
22///
23/// # Concurrency design
24///
25/// `SqzEngine` is designed for single-threaded use on the main thread.
26/// The only cross-thread sharing happens during preset hot-reload: the
27/// file-watcher callback runs on a background thread and needs to update
28/// the preset, pipeline, and model router. These three fields are wrapped
29/// in `Arc<Mutex<>>` specifically for that purpose. All other fields are
30/// owned directly — no unnecessary synchronization.
31pub struct SqzEngine {
32    // --- Hot-reloadable state (shared with file-watcher thread) ---
33    preset: Arc<Mutex<Preset>>,
34    pipeline: Arc<Mutex<CompressionPipeline>>,
35    model_router: Arc<Mutex<ModelRouter>>,
36
37    // --- Single-owner state (no cross-thread sharing needed) ---
38    session_store: SessionStore,
39    cache_manager: CacheManager,
40    budget_tracker: BudgetTracker,
41    cost_calculator: CostCalculator,
42    ast_parser: AstParser,
43    terse_mode: TerseMode,
44    pin_manager: PinManager,
45    confidence_router: ConfidenceRouter,
46    _plugin_loader: PluginLoader,
47}
48
49impl SqzEngine {
50    /// Create a new engine with the default preset and a persistent session store.
51    ///
52    /// Sessions are stored in `~/.sqz/sessions.db` for cross-session continuity.
53    /// Falls back to a temp-file store if the home directory is unavailable.
54    pub fn new() -> Result<Self> {
55        let preset = Preset::default();
56        let store_path = Self::default_store_path();
57        Self::with_preset_and_store(preset, &store_path)
58    }
59
60    /// Resolve the default session store path: `~/.sqz/sessions.db`.
61    /// Falls back to a temp-file path if home dir is unavailable.
62    fn default_store_path() -> std::path::PathBuf {
63        if let Some(home) = dirs_next::home_dir() {
64            let sqz_dir = home.join(".sqz");
65            if std::fs::create_dir_all(&sqz_dir).is_ok() {
66                // Harden permissions on Unix: ~/.sqz/ contains session data
67                // and cached content that may include sensitive output.
68                #[cfg(unix)]
69                {
70                    use std::os::unix::fs::PermissionsExt;
71                    let _ = std::fs::set_permissions(
72                        &sqz_dir,
73                        std::fs::Permissions::from_mode(0o700),
74                    );
75                }
76                return sqz_dir.join("sessions.db");
77            }
78        }
79        // Fallback: temp dir with unique name
80        let dir = std::env::temp_dir();
81        dir.join(format!(
82            "sqz_session_{}_{}.db",
83            std::process::id(),
84            std::time::SystemTime::now()
85                .duration_since(std::time::UNIX_EPOCH)
86                .map(|d| d.as_nanos())
87                .unwrap_or(0)
88        ))
89    }
90
91    /// Create with a custom preset and a file-backed session store.
92    ///
93    /// Opens a single SQLite connection for the session store. The cache
94    /// manager and pin manager share the same store via separate connections
95    /// (SQLite WAL mode supports concurrent readers).
96    pub fn with_preset_and_store(preset: Preset, store_path: &Path) -> Result<Self> {
97        let pipeline = CompressionPipeline::new(&preset);
98        let window_size = preset.budget.default_window_size;
99
100        // One connection per consumer. SQLite WAL mode handles concurrency.
101        let session_store = SessionStore::open_or_create(store_path)?;
102        let cache_store = SessionStore::open_or_create(store_path)?;
103        let pin_store = SessionStore::open_or_create(store_path)?;
104
105        Ok(SqzEngine {
106            preset: Arc::new(Mutex::new(preset.clone())),
107            pipeline: Arc::new(Mutex::new(pipeline)),
108            model_router: Arc::new(Mutex::new(ModelRouter::new(&preset))),
109            session_store,
110            cache_manager: CacheManager::new(cache_store, 512 * 1024 * 1024),
111            budget_tracker: BudgetTracker::new(window_size, &preset),
112            cost_calculator: CostCalculator::with_defaults(),
113            ast_parser: AstParser::new(),
114            terse_mode: TerseMode,
115            pin_manager: PinManager::new(pin_store),
116            confidence_router: ConfidenceRouter::new(),
117            _plugin_loader: PluginLoader::new(Path::new("plugins")),
118        })
119    }
120
121    /// Compress input text using the current preset.
122    ///
123    /// Two-pass pipeline:
124    /// 1. Route to compression mode based on content entropy and risk patterns.
125    /// 2. Compress using the pipeline (safe preset for Safe mode, default otherwise).
126    /// 3. Verify invariants (error lines, JSON keys, diff hunks, etc.).
127    /// 4. If verification confidence is low, fall back to safe mode and re-compress.
128    pub fn compress(&self, input: &str) -> Result<CompressedContent> {
129        let preset = self.preset.lock()
130            .map_err(|_| SqzError::Other("preset lock poisoned".into()))?;
131        let pipeline = self.pipeline.lock()
132            .map_err(|_| SqzError::Other("pipeline lock poisoned".into()))?;
133        let ctx = crate::pipeline::SessionContext {
134            session_id: "engine".to_string(),
135        };
136
137        // Step 1: Route — check content risk before compressing
138        let mode = self.confidence_router.route(input);
139
140        // Step 2: If Safe mode, skip aggressive pipeline and go straight to safe compress
141        if mode == crate::confidence_router::CompressionMode::Safe {
142            eprintln!("[sqz] fallback: safe mode — content classified as high-risk (stack trace / migration / secret)");
143            return self.compress_safe(input, &pipeline, &ctx);
144        }
145
146        // Step 3: Compress with the configured pipeline
147        let mut result = pipeline.compress(input, &ctx, &preset)?;
148
149        // Step 4: Verify invariants
150        let verify = Verifier::verify(input, &result.data);
151        let fallback = verify.fallback_triggered;
152        result.verify = Some(verify);
153
154        // Step 5: If verifier signals low confidence, re-compress with safe settings
155        if fallback && result.data != input {
156            eprintln!("[sqz] fallback: verifier confidence {:.2} below threshold — re-compressing in safe mode",
157                result.verify.as_ref().map(|v| v.confidence).unwrap_or(0.0));
158            let safe_result = self.compress_safe(input, &pipeline, &ctx)?;
159            return Ok(safe_result);
160        }
161
162        Ok(result)
163    }
164
165    /// Compress with dedup cache lookup.
166    ///
167    /// Unlike [`compress`], this method consults the persistent cache
168    /// (`~/.sqz/sessions.db`). On a hit with a fresh ref, it returns a
169    /// 13-token `§ref:HASH§` token instead of re-compressing. On a
170    /// near-duplicate, it returns a compact delta. On a cache miss, it
171    /// runs the full pipeline, stores the result, and returns it.
172    ///
173    /// This is the path the MCP `sqz_read_file` / `sqz_grep` /
174    /// `sqz_list_dir` tools use — repeat reads in the same session
175    /// (and across sessions if the DB survives) collapse to 13 tokens.
176    /// Reported on issue #12: the MCP file tools bypassed the cache so
177    /// dedup never fired, and users were getting 30%-range pipeline
178    /// compression instead of the 92% dedup path advertised in the
179    /// README.
180    pub fn compress_with_cache(&self, input: &str) -> Result<crate::cache_manager::CacheResult> {
181        let pipeline = self.pipeline.lock()
182            .map_err(|_| SqzError::Other("pipeline lock poisoned".into()))?;
183        // The `path` argument is informational only — cache keys are
184        // SHA-256 of the content bytes, not the path. Pass an empty
185        // path so we don't partition the cache by accidental file
186        // location differences (e.g. `./src/main.rs` vs
187        // `/abs/path/to/src/main.rs`).
188        self.cache_manager.get_or_compress(
189            std::path::Path::new(""),
190            input.as_bytes(),
191            &pipeline,
192        )
193    }
194
195    /// Defensive compression: any input in, `CompressedContent` out, guaranteed.
196    ///
197    /// Unlike `compress()` which returns `Result`, this method never returns
198    /// an error. On any internal failure it returns the original input
199    /// unchanged with a 1.0 compression ratio. This makes it safe to call
200    /// from contexts where error handling is impractical (e.g. shell hooks,
201    /// browser extension bridges).
202    pub fn compress_or_passthrough(&self, input: &str) -> CompressedContent {
203        match self.compress(input) {
204            Ok(result) => result,
205            Err(_) => {
206                let tokens = (input.len() as u32 + 3) / 4;
207                CompressedContent {
208                    data: input.to_string(),
209                    tokens_compressed: tokens,
210                    tokens_original: tokens,
211                    stages_applied: vec![],
212                    compression_ratio: 1.0,
213                    provenance: crate::types::Provenance::default(),
214                    verify: None,
215                }
216            }
217        }
218    }
219
220    /// Compress with explicit mode override, bypassing the confidence router.
221    ///
222    /// - `CompressionMode::Safe` → safe pipeline only (ANSI strip + condense)
223    /// - `CompressionMode::Default` → standard pipeline
224    /// - `CompressionMode::Aggressive` → standard pipeline (aggressive preset TBD)
225    pub fn compress_with_mode(&self, input: &str, mode: crate::confidence_router::CompressionMode) -> Result<CompressedContent> {
226        let pipeline = self.pipeline.lock()
227            .map_err(|_| SqzError::Other("pipeline lock poisoned".into()))?;
228        let ctx = crate::pipeline::SessionContext {
229            session_id: "engine".to_string(),
230        };
231
232        match mode {
233            crate::confidence_router::CompressionMode::Safe => {
234                self.compress_safe(input, &pipeline, &ctx)
235            }
236            _ => {
237                // Default and Aggressive: run normal pipeline + verify
238                drop(pipeline); // release lock before calling compress()
239                self.compress(input)
240            }
241        }
242    }
243
244    /// Safe-mode compression: minimal transforms only (ANSI strip + condense).
245    fn compress_safe(
246        &self,
247        input: &str,
248        pipeline: &crate::pipeline::CompressionPipeline,
249        ctx: &crate::pipeline::SessionContext,
250    ) -> Result<CompressedContent> {
251        use crate::preset::{
252            CompressionConfig, CondenseConfig, CustomTransformsConfig, BudgetConfig,
253            ModelConfig, PresetMeta, TerseModeConfig, TerseLevel, ToolSelectionConfig,
254        };
255
256        let safe_preset = Preset {
257            preset: PresetMeta {
258                name: "safe".to_string(),
259                version: "1.0".to_string(),
260                description: "Safe fallback — minimal compression".to_string(),
261            },
262            compression: CompressionConfig {
263                stages: vec!["condense".to_string()],
264                keep_fields: None,
265                strip_fields: None,
266                condense: Some(CondenseConfig { enabled: true, max_repeated_lines: 3 }),
267                git_diff_fold: None,
268                strip_nulls: None,
269                flatten: None,
270                truncate_strings: None,
271                collapse_arrays: None,
272                custom_transforms: Some(CustomTransformsConfig { enabled: false }),
273            },
274            tool_selection: ToolSelectionConfig {
275                max_tools: 5,
276                similarity_threshold: 0.7,
277                default_tools: vec![],
278            },
279            budget: BudgetConfig {
280                warning_threshold: 0.70,
281                ceiling_threshold: 0.85,
282                default_window_size: 200_000,
283                agents: Default::default(),
284            },
285            terse_mode: TerseModeConfig { enabled: false, level: TerseLevel::Moderate },
286            model: ModelConfig {
287                family: "anthropic".to_string(),
288                primary: String::new(),
289                local: String::new(),
290                complexity_threshold: 0.4,
291                pricing: None,
292            },
293        };
294
295        let mut result = pipeline.compress(input, ctx, &safe_preset)?;
296        let verify = Verifier::verify(input, &result.data);
297        result.verify = Some(verify);
298        result.provenance = Provenance {
299            label: Some("safe-fallback".to_string()),
300            ..Default::default()
301        };
302        Ok(result)
303    }
304
305    /// Compress with explicit provenance metadata attached to the result.
306    pub fn compress_with_provenance(
307        &self,
308        input: &str,
309        provenance: Provenance,
310    ) -> Result<CompressedContent> {
311        let mut result = self.compress(input)?;
312        result.provenance = provenance;
313        Ok(result)
314    }
315
316    /// Export a session to CTX format.
317    pub fn export_ctx(&self, session_id: &str) -> Result<String> {
318        let session = self.session_store.load_session(session_id.to_string())?;
319        CtxFormat::serialize(&session)
320    }
321
322    /// Import a CTX string and save as a new session.
323    pub fn import_ctx(&self, ctx: &str) -> Result<SessionId> {
324        let session = CtxFormat::deserialize(ctx)?;
325        self.session_store.save_session(&session)
326    }
327
328    /// Pin a conversation turn.
329    pub fn pin(&self, session_id: &str, turn_index: usize, reason: &str, tokens: u32) -> Result<PinEntry> {
330        self.pin_manager.pin(session_id, turn_index, reason, tokens)
331    }
332
333    /// Unpin a conversation turn.
334    pub fn unpin(&self, session_id: &str, turn_index: usize) -> Result<()> {
335        self.pin_manager.unpin(session_id, turn_index)
336    }
337
338    /// Search sessions by keyword.
339    pub fn search_sessions(&self, query: &str) -> Result<Vec<SessionSummary>> {
340        self.session_store.search(query)
341    }
342
343    /// Get usage report for an agent.
344    pub fn usage_report(&self, agent_id: &str) -> UsageReport {
345        self.budget_tracker.usage_report(agent_id.to_string())
346    }
347
348    /// Get cost summary for a session.
349    pub fn cost_summary(&self, session_id: &str) -> Result<SessionCostSummary> {
350        let session = self.session_store.load_session(session_id.to_string())?;
351        Ok(self.cost_calculator.session_summary(&session))
352    }
353
354    /// Reload the preset from a TOML string (hot-reload support).
355    pub fn reload_preset(&mut self, toml: &str) -> Result<()> {
356        let new_preset = PresetParser::parse(toml)?;
357        if let Ok(mut pipeline) = self.pipeline.lock() {
358            pipeline.reload_preset(&new_preset)?;
359        }
360        if let Ok(mut router) = self.model_router.lock() {
361            *router = ModelRouter::new(&new_preset);
362        }
363        if let Ok(mut preset) = self.preset.lock() {
364            *preset = new_preset;
365        }
366        Ok(())
367    }
368
369    /// Spawn a background thread that watches `path` for preset file changes.
370    ///
371    /// Only the preset, pipeline, and model_router are shared with the watcher
372    /// thread (via `Arc<Mutex<>>`). All other engine state stays on the main thread.
373    pub fn watch_preset_file(&self, path: &Path) -> Result<notify::RecommendedWatcher> {
374        use notify::{Event, EventKind, RecursiveMode, Watcher};
375
376        let preset_arc = Arc::clone(&self.preset);
377        let pipeline_arc = Arc::clone(&self.pipeline);
378        let router_arc = Arc::clone(&self.model_router);
379        let watched_path = path.to_owned();
380
381        let mut watcher = notify::recommended_watcher(move |res: notify::Result<Event>| {
382            if let Ok(event) = res {
383                if matches!(event.kind, EventKind::Modify(_) | EventKind::Create(_)) {
384                    match std::fs::read_to_string(&watched_path) {
385                        Ok(toml_str) => match PresetParser::parse(&toml_str) {
386                            Ok(new_preset) => {
387                                if let Ok(mut p) = pipeline_arc.lock() {
388                                    let _ = p.reload_preset(&new_preset);
389                                }
390                                if let Ok(mut r) = router_arc.lock() {
391                                    *r = ModelRouter::new(&new_preset);
392                                }
393                                if let Ok(mut pr) = preset_arc.lock() {
394                                    *pr = new_preset;
395                                }
396                            }
397                            Err(e) => eprintln!("[sqz] invalid preset: {e}"),
398                        },
399                        Err(e) => eprintln!("[sqz] preset read error: {e}"),
400                    }
401                }
402            }
403        })
404        .map_err(|e| SqzError::Other(format!("watcher error: {e}")))?;
405
406        watcher
407            .watch(path, RecursiveMode::NonRecursive)
408            .map_err(|e| SqzError::Other(format!("watch error: {e}")))?;
409
410        Ok(watcher)
411    }
412
413    /// Access the underlying `SessionStore`.
414    pub fn session_store(&self) -> &SessionStore {
415        &self.session_store
416    }
417
418    /// Access the `CacheManager` for persistent dedup.
419    pub fn cache_manager(&self) -> &CacheManager {
420        &self.cache_manager
421    }
422
423    /// Access the `AstParser`.
424    pub fn ast_parser(&self) -> &AstParser {
425        &self.ast_parser
426    }
427
428    /// Access the `TerseMode` helper.
429    pub fn terse_mode(&self) -> &TerseMode {
430        &self.terse_mode
431    }
432
433    /// Reorder context sections using the LITM positioner to mitigate
434    /// the "Lost In The Middle" attention bias in long-context models.
435    ///
436    /// Places highest-priority sections at the beginning and end of the
437    /// context window, lowest-priority in the middle.
438    pub fn reorder_context(
439        &self,
440        sections: &mut Vec<crate::litm_positioner::ContextSection>,
441        strategy: crate::litm_positioner::LitmStrategy,
442    ) {
443        let positioner = crate::litm_positioner::LitmPositioner::new(strategy);
444        positioner.reorder(sections);
445    }
446
447    /// Route content to the appropriate compression mode based on entropy
448    /// and risk pattern analysis.
449    pub fn route_compression_mode(&self, content: &str) -> crate::confidence_router::CompressionMode {
450        self.confidence_router.route(content)
451    }
452}
453
454
455#[cfg(test)]
456mod tests {
457    use super::*;
458    use crate::types::{BudgetState, CorrectionLog, ModelFamily, SessionState};
459    use chrono::Utc;
460    use std::path::PathBuf;
461
462    fn make_session(id: &str) -> SessionState {
463        let now = Utc::now();
464        SessionState {
465            id: id.to_string(),
466            project_dir: PathBuf::from("/tmp/test"),
467            conversation: vec![],
468            corrections: CorrectionLog::default(),
469            pins: vec![],
470            learnings: vec![],
471            compressed_summary: "test session".to_string(),
472            budget: BudgetState {
473                window_size: 200_000,
474                consumed: 0,
475                pinned: 0,
476                model_family: ModelFamily::AnthropicClaude,
477            },
478            tool_usage: vec![],
479            created_at: now,
480            updated_at: now,
481        }
482    }
483
484    #[test]
485    fn test_engine_new() {
486        let engine = SqzEngine::new();
487        assert!(engine.is_ok(), "SqzEngine::new() should succeed");
488    }
489
490    #[test]
491    fn test_compress_or_passthrough_returns_result_on_valid_input() {
492        let engine = SqzEngine::new().unwrap();
493        let result = engine.compress_or_passthrough("hello world");
494        assert_eq!(result.data, "hello world");
495        assert!(result.tokens_original > 0);
496    }
497
498    #[test]
499    fn test_compress_or_passthrough_never_panics_on_empty() {
500        let engine = SqzEngine::new().unwrap();
501        let result = engine.compress_or_passthrough("");
502        assert_eq!(result.data, "");
503        assert_eq!(result.compression_ratio, 1.0);
504    }
505
506    #[test]
507    fn test_compress_or_passthrough_handles_json() {
508        let engine = SqzEngine::new().unwrap();
509        let result = engine.compress_or_passthrough(r#"{"key":"value"}"#);
510        // Should compress successfully — data may be TOON-encoded
511        assert!(!result.data.is_empty());
512    }
513
514    #[test]
515    fn test_compress_or_passthrough_handles_binary_garbage() {
516        let engine = SqzEngine::new().unwrap();
517        // Feed it something weird — should never panic, always return something
518        let garbage = "\x00\x01\x02\x7f invalid control chars \t\n\r";
519        let result = engine.compress_or_passthrough(garbage);
520        assert!(!result.data.is_empty());
521    }
522
523    #[test]
524    fn test_compress_plain_text() {
525        let engine = SqzEngine::new().unwrap();
526        let result = engine.compress("hello world");
527        assert!(result.is_ok());
528        assert_eq!(result.unwrap().data, "hello world");
529    }
530
531    #[test]
532    fn test_compress_json_applies_toon() {
533        let engine = SqzEngine::new().unwrap();
534        let result = engine.compress(r#"{"name":"Alice","age":30}"#).unwrap();
535        assert!(result.data.starts_with("TOON:"), "JSON should be TOON-encoded");
536    }
537
538    #[test]
539    fn test_export_import_ctx_round_trip() {
540        let dir = tempfile::tempdir().unwrap();
541        let store_path = dir.path().join("store.db");
542        let engine = SqzEngine::with_preset_and_store(Preset::default(), &store_path).unwrap();
543
544        let session = make_session("sess-rt");
545        engine.session_store().save_session(&session).unwrap();
546
547        let ctx = engine.export_ctx("sess-rt").unwrap();
548        let imported_id = engine.import_ctx(&ctx).unwrap();
549        assert_eq!(imported_id, "sess-rt");
550    }
551
552    #[test]
553    fn test_search_sessions() {
554        let dir = tempfile::tempdir().unwrap();
555        let store_path = dir.path().join("store.db");
556        let engine = SqzEngine::with_preset_and_store(Preset::default(), &store_path).unwrap();
557
558        let mut session = make_session("sess-search");
559        session.compressed_summary = "authentication refactor".to_string();
560        engine.session_store().save_session(&session).unwrap();
561
562        let results = engine.search_sessions("authentication").unwrap();
563        assert_eq!(results.len(), 1);
564        assert_eq!(results[0].id, "sess-search");
565    }
566
567    #[test]
568    fn test_usage_report_starts_at_zero() {
569        let engine = SqzEngine::new().unwrap();
570        let report = engine.usage_report("default");
571        assert_eq!(report.consumed, 0);
572        assert_eq!(report.available, report.allocated);
573    }
574
575    #[test]
576    fn test_cost_summary() {
577        let dir = tempfile::tempdir().unwrap();
578        let store_path = dir.path().join("store.db");
579        let engine = SqzEngine::with_preset_and_store(Preset::default(), &store_path).unwrap();
580
581        let session = make_session("sess-cost");
582        engine.session_store().save_session(&session).unwrap();
583
584        let summary = engine.cost_summary("sess-cost").unwrap();
585        assert_eq!(summary.total_tokens, 0);
586        assert!((summary.total_usd - 0.0).abs() < f64::EPSILON);
587    }
588
589    #[test]
590    fn test_reload_preset_updates_state() {
591        let mut engine = SqzEngine::new().unwrap();
592        let toml = r#"
593[preset]
594name = "reloaded"
595version = "2.0"
596
597[compression]
598stages = []
599
600[tool_selection]
601max_tools = 5
602similarity_threshold = 0.7
603
604[budget]
605warning_threshold = 0.70
606ceiling_threshold = 0.85
607default_window_size = 200000
608
609[terse_mode]
610enabled = false
611level = "moderate"
612
613[model]
614family = "anthropic"
615primary = "claude-sonnet-4-20250514"
616complexity_threshold = 0.4
617"#;
618        assert!(engine.reload_preset(toml).is_ok());
619        // Verify the preset was actually updated
620        let preset = engine.preset.lock().unwrap();
621        assert_eq!(preset.preset.name, "reloaded");
622    }
623
624    #[test]
625    fn test_reload_invalid_preset_returns_error() {
626        let mut engine = SqzEngine::new().unwrap();
627        let result = engine.reload_preset("not valid toml [[[");
628        assert!(result.is_err(), "invalid TOML should return error");
629    }
630
631    #[test]
632    fn test_export_nonexistent_session_returns_error() {
633        let engine = SqzEngine::new().unwrap();
634        let result = engine.export_ctx("does-not-exist");
635        assert!(result.is_err());
636    }
637
638    #[test]
639    fn test_import_invalid_ctx_returns_error() {
640        let engine = SqzEngine::new().unwrap();
641        let result = engine.import_ctx("not valid json {{{");
642        assert!(result.is_err());
643    }
644}