Skip to main content

lean_ctx/tools/registered/
ctx_read.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::{Arc, Mutex};
3
4use rmcp::ErrorData;
5use rmcp::model::{ContentBlock, Tool};
6use serde_json::{Map, Value, json};
7
8use crate::server::tool_trait::{
9    McpTool, ToolContext, ToolOutput, get_bool, get_f64, get_int, get_str, get_str_array,
10    require_resolved_path,
11};
12use crate::tool_defs::tool_def;
13
14/// Per-file lock that serializes concurrent reads of the same path.
15///
16/// When multiple subagents read sequentially through a shared set of files,
17/// they tend to hit the same path at the same time. Without per-file locking
18/// they all contend on the global cache write lock while doing redundant I/O.
19/// This lock ensures only one thread reads a given file from disk; the others
20/// wait cheaply on the per-file mutex, then hit the warm cache.
21///
22/// Backed by the shared `core::path_locks` registry so reads and edits of the
23/// same path coordinate through a single mutex (see issue #320).
24fn per_file_lock(path: &str) -> Arc<Mutex<()>> {
25    crate::core::path_locks::per_file_lock(path)
26}
27
28pub struct CtxReadTool;
29
30impl McpTool for CtxReadTool {
31    fn name(&self) -> &'static str {
32        "ctx_read"
33    }
34
35    fn tool_def(&self) -> Tool {
36        tool_def(
37            "ctx_read",
38            "Read source files. mode recommended — choose by intent (see `mode` below); defaults to auto when omitted.\n\
39             To UNDERSTAND code run ctx_compose FIRST; ctx_read after it identified files.\n\
40             anchored → edit by reference via ctx_patch (no exact-recall).",
41            json!({
42                "type": "object",
43                "properties": {
44                    "path": { "type": "string", "description": "Absolute path" },
45                    "paths": { "type": "array", "items": { "type": "string" }, "description": "Batch read" },
46                    "mode": {
47                        "type": "string",
48                        "description": "Recommended (defaults to auto). full=verbatim(edit-ready) anchored=full+N:hh|anchors(edit via ctx_patch) raw=exact-bytes signatures=API map=structure auto=smart diff=git-delta lines:N-M=window (comma multi-selects: lines:5,10-20) reference=quotes task=focus"
49                    },
50                    "raw": { "type": "boolean", "description": "Verbatim (= mode=raw + fresh)" },
51                    "start_line": { "type": "integer", "description": "1-based" },
52                    "offset": { "type": "integer", "description": "start_line alias" },
53                    "limit": { "type": "integer", "description": "Max lines" },
54                    "fresh": { "type": "boolean", "description": "Bypass cache" },
55                    "aggressiveness": { "type": "number", "description": "0.0–1.0 density (entropy/task)" },
56                    "protect": { "type": "array", "items": { "type": "string" }, "description": "Symbols kept verbatim" }
57                },
58                "required": []
59            }),
60        )
61    }
62
63    fn handle(
64        &self,
65        args: &Map<String, Value>,
66        ctx: &ToolContext,
67    ) -> Result<ToolOutput, ErrorData> {
68        // #509: ctx_read absorbs multi-file batch reads (supersedes ctx_multi_read).
69        // A non-empty `paths` array routes to the one shared batch implementation.
70        if args
71            .get("paths")
72            .and_then(|v| v.as_array())
73            .is_some_and(|a| !a.is_empty())
74        {
75            return super::ctx_multi_read::batch_read(args, ctx);
76        }
77
78        let path = if let Some(repo) = get_str(args, "repo") {
79            let root = crate::core::multi_repo::resolve_repo_root(&repo).ok_or_else(|| {
80                let known = crate::core::multi_repo::known_aliases().join(", ");
81                let known = if known.is_empty() {
82                    "none registered — use ctx_multi_repo add_root".to_string()
83                } else {
84                    known
85                };
86                ErrorData::invalid_params(
87                    format!("unknown repo alias: {repo} (known: {known})"),
88                    None,
89                )
90            })?;
91            let rel = get_str(args, "path").unwrap_or_else(|| ".".to_string());
92            crate::core::path_resolve::resolve_tool_path(Some(&root), None, &rel)
93                .map_err(|e| ErrorData::invalid_params(e, None))?
94        } else {
95            require_resolved_path(ctx, args, "path")?
96        };
97
98        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
99            self.handle_inner(args, ctx, &path)
100        })) {
101            Ok(result) => result,
102            Err(_) => Err(ErrorData::internal_error(
103                format!(
104                    "ctx_read panicked while processing '{path}'. This is a bug — please report it."
105                ),
106                None,
107            )),
108        }
109    }
110}
111
112impl CtxReadTool {
113    #[allow(clippy::unused_self)]
114    fn handle_inner(
115        &self,
116        args: &Map<String, Value>,
117        ctx: &ToolContext,
118        path: &str,
119    ) -> Result<ToolOutput, ErrorData> {
120        let session_lock = ctx
121            .session
122            .as_ref()
123            .ok_or_else(|| ErrorData::internal_error("session not available", None))?;
124        let cache_lock = ctx
125            .cache
126            .as_ref()
127            .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
128
129        let current_task = {
130            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(8);
131            let mut attempt = 0u32;
132            loop {
133                // #1018: use try_read_owned instead of Handle::block_on to avoid
134                // the async-runtime-saturation anti-pattern on Windows.
135                if let Ok(guard) = session_lock.clone().try_read_owned() {
136                    break guard.task.as_ref().map(|t| t.description.clone());
137                }
138                attempt += 1;
139                if std::time::Instant::now() >= deadline {
140                    tracing::warn!(
141                        "session read-lock timeout after {attempt} attempts in ctx_read for {path}"
142                    );
143                    break None;
144                }
145                std::thread::sleep(std::time::Duration::from_millis(25));
146            }
147        };
148        let task_ref = current_task.as_deref();
149        // #513: `raw=true` is the intuitive "give me the exact bytes" escape an
150        // agent reaches for. Alias it to mode="raw" (verbatim, unframed) and
151        // force a fresh disk read below so a re-read never collapses to an
152        // `[unchanged]`/auto-delta stub. An explicit raw flag wins over `mode`.
153        let arg_raw = get_bool(args, "raw").unwrap_or(false);
154        let explicit_mode_arg = resolve_raw_alias(arg_raw, get_str(args, "mode"));
155        let explicit_mode = explicit_mode_arg.is_some();
156        // #1209: a malformed line-range payload (e.g. `lines:44:48` with a colon
157        // instead of a dash) must fail loudly, not silently return an empty
158        // window on small files or bounce to a full-file dump on large ones.
159        // `lines:` and `anchored:` share `parse_line_range`, so validating both
160        // line-range modes at the boundary catches the typo for either. Other
161        // modes are left untouched: bare `density:` is a valid fallback and
162        // unknown keywords keep the historical warn-and-fall-back behaviour.
163        if let Some(ref requested) = explicit_mode_arg
164            && (requested.starts_with("lines:") || requested.starts_with("anchored:"))
165            && let Err(e) = requested.parse::<crate::tools::ctx_read::ReadMode>()
166        {
167            return Err(ErrorData::invalid_params(e.user_message(), None));
168        }
169        let configured_mode = (!explicit_mode)
170            .then(crate::core::auto_mode_resolver::configured_default_mode)
171            .flatten();
172        let learned_mode = if !explicit_mode && configured_mode.is_none() {
173            if let Ok(cache) = cache_lock.try_read() {
174                Some(crate::tools::ctx_smart_read::select_mode_with_task(
175                    &cache, path, task_ref,
176                ))
177            } else {
178                tracing::debug!(
179                    "cache lock contested during auto-mode selection for {path}; \
180                     falling back to full"
181                );
182                None
183            }
184        } else {
185            None
186        };
187        let mut mode = crate::core::auto_mode_resolver::resolve_mode_precedence(
188            explicit_mode_arg,
189            configured_mode,
190            learned_mode,
191            "full",
192        );
193        let mut fresh = get_bool(args, "fresh").unwrap_or(false);
194        // #513: a raw/verbatim request always reads from disk — the whole point
195        // is exact current bytes, never a cached stub or delta.
196        if arg_raw {
197            fresh = true;
198        }
199        let cache_policy = crate::server::compaction_sync::effective_cache_policy();
200        if cache_policy == "off" {
201            fresh = true;
202        }
203        let aggressiveness =
204            crate::core::aggressiveness::effective(get_f64(args, "aggressiveness"));
205        let protect = get_str_array(args, "protect").unwrap_or_default();
206        // One-knob UX: when the caller sets aggressiveness without pinning a mode,
207        // route through the proven density path at the mapped target. An explicit
208        // mode (incl. entropy/task) instead has the knob tune it via ReadTuning.
209        if !explicit_mode && let Some(a) = aggressiveness {
210            // SSOT mode construction via the typed `ReadMode` (#528): the typed
211            // `Density` Display emits the same `density:0.NN` the pipeline parses.
212            mode = crate::tools::ctx_read::ReadMode::Density(
213                crate::core::aggressiveness::AggressivenessProfile::from_level(a).density_target,
214            )
215            .to_string();
216        }
217        // `start_line` (and its `offset`/`limit` aliases) can pin a line window.
218        // The resolution lives in `apply_line_window`/`resolve_line_window` so
219        // the runtime path and the unit tests share one implementation and can
220        // never drift (GitHub #432 aliases, #259 explicit-mode, #253 line-1).
221        apply_line_window(
222            &mut mode,
223            &mut fresh,
224            explicit_mode,
225            get_int(args, "start_line"),
226            get_int(args, "offset"),
227            get_int(args, "limit"),
228        );
229
230        let pressure_action = ctx.pressure_snapshot.as_ref().map(|p| &p.recommendation);
231        let resolved_agent_id = ctx.agent_id.as_ref().and_then(|a| match a.try_read() {
232            Ok(guard) => guard.clone(),
233            Err(_) => None,
234        });
235        let gate_result = crate::server::context_gate::pre_dispatch_read_for_agent(
236            path,
237            &mode,
238            task_ref,
239            Some(&ctx.project_root),
240            pressure_action,
241            resolved_agent_id.as_deref(),
242        );
243        if gate_result.budget_blocked {
244            let msg = gate_result
245                .budget_warning
246                .unwrap_or_else(|| "Agent token budget exceeded".to_string());
247            return Err(ErrorData::invalid_params(msg, None));
248        }
249        let budget_warning = gate_result.budget_warning.clone();
250        // #513: an explicit raw/verbatim request is never silently downgraded by
251        // the budget gate — the caller asked for exact bytes.
252        let mut mode_override_note: Option<String> = None;
253        if mode != "raw"
254            && let Some(overridden) = gate_result.overridden_mode
255        {
256            if explicit_mode {
257                let reason = gate_result.reason.unwrap_or("context-gate");
258                mode_override_note = Some(format!(
259                    "[mode overridden: {mode} -> {overridden}, reason={reason}]"
260                ));
261            }
262            mode = overridden;
263        }
264
265        let (instruction_mode, instruction_mode_note) = resolve_instruction_file_mode(path, &mode);
266        let (mut mode, degrade_warning) =
267            if instruction_mode_note.is_some() || instruction_mode != mode {
268                (instruction_mode, None)
269            } else if mode == "raw" || mode.starts_with("anchored") || mode.starts_with("lines:") {
270                // #513: raw bypasses context-pressure degradation (which would
271                // otherwise downgrade to signatures under Block), exactly like
272                // instruction files — explicit lossless modes mean lossless.
273                (mode, None)
274            } else {
275                auto_degrade_read_mode(&mode)
276            };
277
278        // Delta-aware explicit re-reads (opt-in: config `delta_explicit`, env
279        // LCTX_DELTA_EXPLICIT). Re-requesting full/lines:N-M content for a file
280        // this session already read re-emits content the model already holds;
281        // when the file changed on disk, a diff carries the same information in
282        // a fraction of the tokens, and an unchanged lines: request of a
283        // fully-delivered file collapses to the full-mode stub. The decision is
284        // a pure function of (cache, path, mode) — see
285        // `ctx_read::resolve_explicit_delta_mode`. First reads are unaffected;
286        // fresh=true always bypasses. Runs BEFORE the lines:→fresh guard below
287        // so a changed-file lines: re-read can still be diverted to a diff.
288        let mut delta_explicit_note: Option<String> = None;
289        if !fresh
290            && explicit_mode
291            && (mode == "full" || mode == "full-compact" || mode.starts_with("lines:"))
292            && crate::core::config::Config::load().delta_explicit_effective()
293            && let Ok(cache) = cache_lock.try_read()
294        {
295            let decision = crate::tools::ctx_read::resolve_explicit_delta_mode(
296                &cache,
297                path,
298                &mode,
299                explicit_mode,
300                fresh,
301                true,
302            );
303            mode = decision.mode;
304            delta_explicit_note = decision.note;
305        }
306
307        if mode.starts_with("lines:") {
308            fresh = true;
309        }
310
311        if crate::core::binary_detect::is_llm_viewable_image(path) {
312            return read_image_file(path);
313        }
314        if crate::core::binary_detect::is_binary_file(path) {
315            let msg = crate::core::binary_detect::binary_file_message(path);
316            return Err(ErrorData::invalid_params(msg, None));
317        }
318        {
319            let cap = crate::core::limits::max_read_bytes() as u64;
320            if let Ok(meta) = std::fs::metadata(path)
321                && meta.len() > cap
322            {
323                let msg = format!(
324                    "File too large ({} bytes, limit {} bytes via LCTX_MAX_READ_BYTES). \
325                     Use mode=\"lines:1-100\" or start_line+limit for partial reads, \
326                     mode=\"anchored\" with start_line+limit for edit-ready windows, \
327                     or increase the limit.",
328                    meta.len(),
329                    cap
330                );
331                return Err(ErrorData::invalid_params(msg, None));
332            }
333        }
334
335        // Compaction-aware: if host compacted since last check, reset delivery flags
336        // so post-compaction reads deliver full content instead of stubs.
337        if !fresh
338            && let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir()
339            && let Ok(mut cache) = cache_lock.try_write()
340        {
341            crate::server::compaction_sync::sync_if_compacted(&mut cache, &data_dir);
342        }
343
344        // Fast path: if both per-file lock and cache write-lock are immediately
345        // available, execute inline without spawning a thread. This avoids thread +
346        // channel overhead for the ~90% of calls that are cache hits.
347        let read_timeout = std::time::Duration::from_secs(30);
348        let cancelled = Arc::new(AtomicBool::new(false));
349        // Hash once for cross-agent delivery (avoids re-reading on record).
350        let delivery_metadata = crate::core::config::Config::load()
351            .ocla
352            .delivery_enabled()
353            .then(|| crate::tools::ctx_read::file_blake3_prefix(path))
354            .flatten();
355        let (output, resolved_mode, original, is_cache_hit, file_ref, cache_stats) = {
356            let crp_mode = ctx.crp_mode;
357            let fast_result = 'fast: {
358                let file_lock = per_file_lock(path);
359                let Some(_file_guard) = file_lock.try_lock().ok() else {
360                    break 'fast None;
361                };
362
363                // Phase 1 (shared lock): the dominant case is re-reading an
364                // unchanged file. Serve the `[unchanged]` stub under a *read* lock
365                // so parallel reads of distinct files run concurrently instead of
366                // serializing on the global write lock. `auto` is included because
367                // a warm `auto` re-read of a fully-delivered file resolves to a
368                // full cache-hit; `try_stub_hit_readonly` self-guards (returns None
369                // unless full content was delivered and the file is unchanged), so a
370                // first or compressed-only `auto` read still falls through to
371                // Phase 2. When aggressiveness is set `mode` was already rewritten
372                // to `density:` upstream, so it never reaches this `auto` branch.
373                if !fresh
374                    && (mode == "full" || mode == "full-compact" || mode == "auto")
375                    && let Ok(cache) = cache_lock.try_read()
376                    && let Some(read_output) =
377                        crate::tools::ctx_read::try_stub_hit_readonly(&cache, path)
378                {
379                    let hit = read_output.is_cache_hit;
380                    let content = read_output.content;
381                    let rmode = read_output.resolved_mode;
382                    let orig = cache.get(path).map_or(0, |e| e.original_tokens);
383                    let fref = cache.file_ref_map().get(path).cloned();
384                    let stats = cache.get_stats();
385                    let stats_snapshot = (stats.total_reads(), stats.cache_hits());
386                    break 'fast Some((content, rmode, orig, hit, fref, stats_snapshot));
387                }
388
389                // Misses use the slow path's double-check sequence: it does a
390                // second cache check, computes without the global lock, then
391                // briefly takes a write lock to store the result.
392                None
393            };
394
395            if let Some(result) = fast_result {
396                result
397            } else {
398                let cache_lock = cache_lock.clone();
399                let mode = mode.clone();
400                let task_owned = current_task.clone();
401                let protect_owned = protect.clone();
402                let path_owned = path.to_string();
403                let cancel_flag = cancelled.clone();
404                let (tx, rx) = std::sync::mpsc::sync_channel(1);
405                std::thread::spawn(move || {
406                    let file_lock = per_file_lock(&path_owned);
407
408                    let _file_guard = {
409                        let deadline =
410                            std::time::Instant::now() + std::time::Duration::from_secs(25);
411                        loop {
412                            if cancel_flag.load(Ordering::Relaxed) {
413                                return;
414                            }
415                            if let Ok(guard) = file_lock.try_lock() {
416                                break guard;
417                            }
418                            if std::time::Instant::now() >= deadline {
419                                tracing::error!(
420                                    "ctx_read: per-file lock timeout after 25s for {path_owned}"
421                                );
422                                let _ = tx.send((
423                                    format!("per-file lock contention for {path_owned} — retry in a moment"),
424                                    "error".to_string(), 0, false, None, (0, 0),
425                                ));
426                                return;
427                            }
428                            std::thread::sleep(std::time::Duration::from_millis(50));
429                        }
430                    };
431
432                    if cancel_flag.load(Ordering::Relaxed) {
433                        return;
434                    }
435
436                    // ── Two-Phase Read (#1098) ──────────────────────────
437                    //
438                    // Phase 1 (read lock): try the [unchanged] stub — this is the
439                    // ~70% case (repeated reads of unchanged files). Previously
440                    // missing in the slow path, forcing every slow-path call into
441                    // the expensive write-lock branch.
442                    if !fresh
443                        && (mode == "full" || mode == "full-compact" || mode == "auto")
444                        && let Ok(cache) = cache_lock.try_read()
445                        && let Some(read_output) =
446                            crate::tools::ctx_read::try_stub_hit_readonly(&cache, &path_owned)
447                    {
448                        let content = read_output.content;
449                        let rmode = read_output.resolved_mode;
450                        let orig = cache.get(&path_owned).map_or(0, |e| e.original_tokens);
451                        let hit = true;
452                        let fref = cache.file_ref_map().get(path_owned.as_str()).cloned();
453                        let stats = cache.get_stats();
454                        let stats_snapshot = (stats.total_reads(), stats.cache_hits());
455                        let _ = tx.send((content, rmode, orig, hit, fref, stats_snapshot));
456                        return;
457                    }
458
459                    // The session-local stub is checked first so the current
460                    // agent's own delivery always wins. On a miss, a verified
461                    // cross-agent delivery can avoid the disk read below.
462                    if !crate::tools::ctx_read::effective_fresh_for_delivery(fresh)
463                        && let Some((hash, mtime)) = delivery_metadata
464                        && let Some(read_output) = crate::tools::ctx_read::try_cross_agent_stub(
465                            &path_owned,
466                            &mode,
467                            hash,
468                            mtime,
469                        )
470                    {
471                        let _ = tx.send((
472                            read_output.content,
473                            read_output.resolved_mode,
474                            0,
475                            read_output.is_cache_hit,
476                            None,
477                            (0, 0),
478                        ));
479                        return;
480                    }
481
482                    // Phase 2a: disk I/O under per-file lock but WITHOUT cache lock.
483                    let preread = crate::tools::ctx_read::read_file_lossy(&path_owned).ok();
484
485                    if cancel_flag.load(Ordering::Relaxed) {
486                        return;
487                    }
488
489                    // ── Phase 2b: Three-sub-phase read (#807) ──────────
490                    //
491                    // Previously held the global cache write-lock for the
492                    // entire computation (tree-sitter, entropy compression).
493                    // For large files this caused 30+ second lock holds and
494                    // cascading timeouts for all concurrent tool calls.
495                    //
496                    // New: prepare (brief lock) → compute (no lock) → store (brief lock).
497
498                    let task_ref = task_owned.as_deref();
499                    let tuning =
500                        crate::tools::ctx_read::ReadTuning::resolve(aggressiveness, &protect_owned);
501
502                    // Helper: acquire write lock with deadline.
503                    macro_rules! acquire_write {
504                        ($deadline_secs:expr, $label:expr) => {{
505                            let deadline = std::time::Instant::now()
506                                + std::time::Duration::from_secs($deadline_secs);
507                            loop {
508                                if cancel_flag.load(Ordering::Relaxed) {
509                                    return;
510                                }
511                                if let Ok(guard) = cache_lock.try_write() {
512                                    break guard;
513                                }
514                                if std::time::Instant::now() >= deadline {
515                                    tracing::error!(
516                                        "ctx_read: cache write-lock timeout ({}) for {path_owned}",
517                                        $label,
518                                    );
519                                    let _ = tx.send((
520                                        format!(
521                                            "cache lock contention for {path_owned} — retry in a moment"
522                                        ),
523                                        "error".into(),
524                                        0,
525                                        false,
526                                        None,
527                                        (0, 0),
528                                    ));
529                                    return;
530                                }
531                                std::thread::sleep(std::time::Duration::from_millis(50));
532                            }
533                        }};
534                    }
535
536                    // 2b-i: Brief write lock — prepare cache state, resolve
537                    // mode, check for hits. Sub-millisecond: HashMap lookups,
538                    // staleness checks, raw-content storage for new files.
539                    #[allow(clippy::large_enum_variant)]
540                    enum PrepareOutcome {
541                        Hit(String, String, usize, bool, Option<String>, (u64, u64)),
542                        Compute {
543                            file_ref: String,
544                            resolved_mode: String,
545                            content: String,
546                            original_tokens: usize,
547                        },
548                    }
549
550                    let outcome = {
551                        let mut cache = acquire_write!(10, "prepare 10s");
552
553                        if crate::core::plugins::PluginManager::has_listener("pre_read") {
554                            crate::core::plugins::PluginManager::fire_hook_background(
555                                crate::core::plugins::executor::HookPoint::PreRead {
556                                    path: path_owned.clone(),
557                                },
558                            );
559                        }
560                        if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
561                            bt.next_seq();
562                        }
563
564                        let file_ref = cache.get_file_ref(&path_owned);
565
566                        let effective_fresh = fresh
567                            || crate::tools::ctx_read::force_fresh_env()
568                            || (crate::tools::ctx_read::is_subagent_context()
569                                && !crate::core::conversation::scope_enabled());
570
571                        let mode_eff = if mode != "raw"
572                            && !mode.starts_with("lines:")
573                            && crate::core::config::Config::load()
574                                .proxy
575                                .is_path_compress_protected(&path_owned)
576                        {
577                            "full".to_string()
578                        } else {
579                            mode.clone()
580                        };
581
582                        if effective_fresh {
583                            cache.invalidate(&path_owned);
584                        }
585
586                        if !effective_fresh {
587                            let stale = cache.get(&path_owned).is_some_and(|e| {
588                                crate::core::cache::is_cache_entry_stale_verified(
589                                    &path_owned,
590                                    e.stored_mtime,
591                                    &e.hash,
592                                )
593                            });
594                            if stale {
595                                cache.invalidate(&path_owned);
596                            }
597                        }
598
599                        let snap = cache
600                            .get(&path_owned)
601                            .map(|e| (e.original_tokens, e.content()));
602
603                        if let Some((orig_tok, content_opt)) = snap {
604                            let resolved = if mode_eff == "auto" {
605                                tuning.auto_density_mode().unwrap_or_else(|| {
606                                    crate::tools::ctx_read::resolve_auto_mode(
607                                        Some(&cache),
608                                        &path_owned,
609                                        orig_tok,
610                                        None,
611                                        task_ref,
612                                    )
613                                })
614                            } else {
615                                mode_eff
616                            };
617
618                            if (resolved == "full" || resolved == "full-compact")
619                                && let Some(out) = crate::tools::ctx_read::try_stub_hit_readonly(
620                                    &cache,
621                                    &path_owned,
622                                )
623                            {
624                                let orig = cache.get(&path_owned).map_or(0, |e| e.original_tokens);
625                                let fref = cache.file_ref_map().get(path_owned.as_str()).cloned();
626                                let s = cache.get_stats();
627                                PrepareOutcome::Hit(
628                                    out.content,
629                                    out.resolved_mode,
630                                    orig,
631                                    true,
632                                    fref,
633                                    (s.total_reads(), s.cache_hits()),
634                                )
635                            } else if crate::tools::ctx_read::is_cacheable_mode(&resolved) {
636                                let ck = crate::tools::ctx_read::compressed_cache_key(
637                                    &resolved,
638                                    crp_mode,
639                                    task_ref,
640                                    tuning.aggressiveness,
641                                    tuning.protect,
642                                );
643                                if let Some(hit) = cache.get_compressed(&path_owned, &ck).cloned() {
644                                    crate::core::auto_mode_resolver::count_source(
645                                        "compressed_cache_hit",
646                                    );
647                                    let hit = crate::core::redaction::redact_text_if_enabled(&hit);
648                                    let orig =
649                                        cache.get(&path_owned).map_or(0, |e| e.original_tokens);
650                                    let fref =
651                                        cache.file_ref_map().get(path_owned.as_str()).cloned();
652                                    let s = cache.get_stats();
653                                    PrepareOutcome::Hit(
654                                        hit,
655                                        resolved,
656                                        orig,
657                                        true,
658                                        fref,
659                                        (s.total_reads(), s.cache_hits()),
660                                    )
661                                } else {
662                                    let c = content_opt
663                                        .or_else(|| preread.as_deref().map(String::from));
664                                    PrepareOutcome::Compute {
665                                        file_ref,
666                                        resolved_mode: resolved,
667                                        content: c.unwrap_or_default(),
668                                        original_tokens: orig_tok,
669                                    }
670                                }
671                            } else {
672                                let c =
673                                    content_opt.or_else(|| preread.as_deref().map(String::from));
674                                PrepareOutcome::Compute {
675                                    file_ref,
676                                    resolved_mode: resolved,
677                                    content: c.unwrap_or_default(),
678                                    original_tokens: orig_tok,
679                                }
680                            }
681                        } else {
682                            let raw = preread.unwrap_or_else(|| {
683                                crate::tools::ctx_read::read_file_lossy(&path_owned)
684                                    .unwrap_or_default()
685                            });
686                            let sr = cache.store(&path_owned, &raw);
687                            let resolved = if mode_eff == "auto" {
688                                tuning.auto_density_mode().unwrap_or_else(|| {
689                                    crate::tools::ctx_read::resolve_auto_mode(
690                                        None,
691                                        &path_owned,
692                                        sr.original_tokens,
693                                        Some(sr.line_count),
694                                        task_ref,
695                                    )
696                                })
697                            } else {
698                                mode_eff
699                            };
700                            PrepareOutcome::Compute {
701                                file_ref,
702                                resolved_mode: resolved,
703                                content: raw,
704                                original_tokens: sr.original_tokens,
705                            }
706                        }
707                    }; // write lock released
708
709                    if let PrepareOutcome::Hit(c, rm, orig, hit, fref, ss) = outcome {
710                        // Update last_mode for compressed-cache hits so the auto-mode
711                        // resolver can reuse this mode on future re-reads (#E26).
712                        let mut cache = acquire_write!(10, "hit last_mode 10s");
713                        if let Some(entry) = cache.get_mut(&path_owned) {
714                            entry.last_mode.clone_from(&rm);
715                        }
716                        let _ = tx.send((c, rm, orig, hit, fref, ss));
717                        return;
718                    }
719                    let PrepareOutcome::Compute {
720                        file_ref,
721                        resolved_mode,
722                        content: compute_content,
723                        original_tokens,
724                    } = outcome
725                    else {
726                        unreachable!()
727                    };
728
729                    if cancel_flag.load(Ordering::Relaxed) {
730                        return;
731                    }
732
733                    // 2b-ii: Heavy computation WITHOUT cache lock.
734                    // Tree-sitter, entropy compression, mode rendering all
735                    // run under the per-file mutex only (serializes same-file
736                    // reads, but does not block other files or tool calls).
737                    let short = crate::core::protocol::shorten_path(&path_owned);
738                    let ext_s = std::path::Path::new(&*path_owned)
739                        .extension()
740                        .and_then(|e| e.to_str())
741                        .unwrap_or("");
742
743                    let (mut computed, rmode) = if resolved_mode == "full"
744                        || resolved_mode == "full-compact"
745                    {
746                        if resolved_mode == "full-compact" {
747                            let (out, _) = crate::tools::ctx_read::format_full_compact_output(
748                                &compute_content,
749                            );
750                            (out, "full-compact".to_string())
751                        } else {
752                            let lc = compute_content.lines().count();
753                            let (out, _) = crate::tools::ctx_read::format_full_output(
754                                &file_ref,
755                                &short,
756                                ext_s,
757                                &compute_content,
758                                original_tokens,
759                                lc,
760                                task_ref,
761                            );
762                            let ft = crate::core::tokens::count_tokens(&out);
763                            let out = crate::tools::ctx_read::cap_to_raw(
764                                out,
765                                ft,
766                                &compute_content,
767                                original_tokens,
768                            );
769                            (out, "full".to_string())
770                        }
771                    } else {
772                        let (out, _) = crate::tools::ctx_read::process_mode_tuned(
773                            &compute_content,
774                            &resolved_mode,
775                            &file_ref,
776                            &short,
777                            ext_s,
778                            original_tokens,
779                            crp_mode,
780                            &path_owned,
781                            task_ref,
782                            tuning,
783                        );
784                        let out = if crate::tools::ctx_read::mode_allows_raw_cap(&resolved_mode) {
785                            let ft = crate::core::tokens::count_tokens(&out);
786                            crate::tools::ctx_read::cap_to_raw(
787                                out,
788                                ft,
789                                &compute_content,
790                                original_tokens,
791                            )
792                        } else {
793                            out
794                        };
795                        (out, resolved_mode)
796                    };
797
798                    computed = crate::core::redaction::redact_text_if_enabled(&computed);
799
800                    if cancel_flag.load(Ordering::Relaxed) {
801                        return;
802                    }
803
804                    // 2b-iii: Brief write lock — store result + metadata.
805                    // Sub-millisecond: HashMap insert + stats snapshot.
806                    // Graceful degradation: if the lock cannot be acquired
807                    // within 5s, return the result without caching it.
808                    {
809                        let deadline =
810                            std::time::Instant::now() + std::time::Duration::from_secs(5);
811                        let cache_guard = loop {
812                            if cancel_flag.load(Ordering::Relaxed) {
813                                return;
814                            }
815                            if let Ok(g) = cache_lock.try_write() {
816                                break Some(g);
817                            }
818                            if std::time::Instant::now() >= deadline {
819                                tracing::warn!(
820                                    "ctx_read: store-lock timeout (5s) for {path_owned},                                      returning without caching"
821                                );
822                                break None;
823                            }
824                            std::thread::sleep(std::time::Duration::from_millis(50));
825                        };
826
827                        if let Some(mut cache) = cache_guard {
828                            if crate::tools::ctx_read::is_cacheable_mode(&rmode) {
829                                let ck = crate::tools::ctx_read::compressed_cache_key(
830                                    &rmode,
831                                    crp_mode,
832                                    task_ref,
833                                    tuning.aggressiveness,
834                                    tuning.protect,
835                                );
836                                cache.set_compressed(&path_owned, &ck, computed.clone());
837                            }
838                            if rmode == "full" || rmode == "full-compact" {
839                                cache.mark_full_delivered(&path_owned);
840                            }
841                            if let Some(entry) = cache.get_mut(&path_owned) {
842                                entry.last_mode.clone_from(&rmode);
843                            }
844                            if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
845                                bt.record_read(
846                                    &path_owned,
847                                    &rmode,
848                                    crate::core::tokens::count_tokens(&computed),
849                                    original_tokens,
850                                );
851                            }
852                            let orig = cache.get(&path_owned).map_or(0, |e| e.original_tokens);
853                            let fref = cache.file_ref_map().get(path_owned.as_str()).cloned();
854                            let s = cache.get_stats();
855                            let _ = tx.send((
856                                computed,
857                                rmode,
858                                orig,
859                                false,
860                                fref,
861                                (s.total_reads(), s.cache_hits()),
862                            ));
863                        } else {
864                            let _ =
865                                tx.send((computed, rmode, original_tokens, false, None, (0, 0)));
866                        }
867                    }
868                });
869                if let Ok(result) = rx.recv_timeout(read_timeout) {
870                    result
871                } else {
872                    cancelled.store(true, Ordering::Relaxed);
873                    tracing::error!("ctx_read timed out after {read_timeout:?} for {path}");
874                    let msg = format!(
875                        "ERROR: ctx_read timed out after {}s reading {path}. \
876                     The file may be very large or a blocking I/O issue occurred. \
877                     Try mode=\"lines:1-100\" for a partial read.",
878                        read_timeout.as_secs()
879                    );
880                    return Err(ErrorData::internal_error(msg, None));
881                }
882            } // end else (slow path)
883        };
884
885        if resolved_mode == "error" {
886            return Err(ErrorData::invalid_params(output, None));
887        }
888
889        let output_tokens = crate::core::tokens::count_tokens(&output);
890        let saved = original.saturating_sub(output_tokens);
891
892        if !is_cache_hit {
893            if let Some((hash, mtime)) = delivery_metadata {
894                crate::tools::ctx_read::record_cross_agent_delivery(
895                    path,
896                    hash,
897                    mtime,
898                    0,
899                    output_tokens,
900                );
901            }
902        }
903
904        // Session updates (bounded lock — 10s timeout, read already succeeded)
905        let mut ensured_root: Option<String> = None;
906        let mut traversal_working_set: Vec<String> = Vec::new();
907        let project_root_snapshot;
908        {
909            let rt = tokio::runtime::Handle::current();
910            let session_guard = rt.block_on(tokio::time::timeout(
911                std::time::Duration::from_secs(10),
912                session_lock.write(),
913            ));
914            if let Ok(mut session) = session_guard {
915                session.touch_file(path, file_ref.as_deref(), &resolved_mode, original);
916                // Capture the recent working set (under the lock) so the
917                // background thread can record a traversal/co-access edge (#289).
918                traversal_working_set =
919                    crate::core::tool_lifecycle::recent_working_set(&session, path);
920                let file_summary = extract_file_summary(&output, path);
921                if !file_summary.is_empty() {
922                    session.set_file_summary(path, &file_summary);
923                }
924                if is_cache_hit {
925                    session.record_cache_hit();
926                }
927                if session.active_structured_intent.is_none() && session.files_touched.len() >= 2 {
928                    let touched: Vec<String> = session
929                        .files_touched
930                        .iter()
931                        .map(|f| f.path.clone())
932                        .collect();
933                    let inferred =
934                        crate::core::intent_engine::StructuredIntent::from_file_patterns(&touched);
935                    if inferred.confidence >= 0.4 {
936                        session.active_structured_intent = Some(inferred);
937                    }
938                }
939                if session.task.is_none() && session.stats.files_read % 5 == 0 {
940                    session.auto_infer_task();
941                }
942                let root_missing = session
943                    .project_root
944                    .as_deref()
945                    .is_none_or(|r| r.trim().is_empty());
946                if root_missing && let Some(root) = crate::core::protocol::detect_project_root(path)
947                {
948                    session.project_root = Some(root.clone());
949                    ensured_root = Some(root);
950                }
951                project_root_snapshot = session
952                    .project_root
953                    .clone()
954                    .unwrap_or_else(|| ".".to_string());
955            } else {
956                tracing::warn!(
957                    "session write-lock timeout (5s) in ctx_read post-update for {path}"
958                );
959                project_root_snapshot = ctx.project_root.clone();
960            }
961        }
962
963        if let Some(root) = ensured_root.as_deref() {
964            crate::core::index_orchestrator::ensure_all_background(root);
965        }
966
967        // Telemetry + learning are pure side-effects that never influence this
968        // response, yet they did synchronous disk I/O on every read (heatmap
969        // append, ModePredictor load+save, FeedbackStore load). Push them off
970        // the hot path so reads — especially cache-hit stubs — return without
971        // waiting on disk (#149).
972        {
973            let path_bg = path.to_string();
974            let resolved_mode_bg = resolved_mode.clone();
975            let project_root_bg = project_root_snapshot.clone();
976            let (turns, hits) = cache_stats;
977            // #685: model-correct verified-ledger inputs, computed off the hot path.
978            // The default O200kBase model reuses the o200k `original`/`saved` below
979            // (byte-identical, no clone). Only a resolved Claude/Gemini/Llama model
980            // carries the cache handle + output so the bg thread can re-tokenize the
981            // raw source and the sent output in the family the provider actually bills.
982            let ledger_cache = (crate::core::savings_ledger::ledger_family()
983                != crate::core::tokens::TokenizerFamily::O200kBase)
984                .then(|| cache_lock.clone());
985            let ledger_output = ledger_cache.as_ref().map(|_| output.clone());
986            std::thread::spawn(move || {
987                // A panic in telemetry must not poison locks or leave a zombie thread;
988                // it never affects the already-returned read response.
989                let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
990                    crate::core::heatmap::record_file_access(&path_bg, original, saved);
991
992                    // #685: verified savings ledger, decoupled from the heatmap so it
993                    // can denominate in the active model's tokenizer family. O200kBase
994                    // reuses the o200k counts; other families re-tokenize raw (cache)
995                    // + output. A cache miss falls back to o200k (conservative).
996                    {
997                        use crate::core::savings_ledger as ledger;
998                        let (lbase, lsaved) = match (&ledger_cache, &ledger_output) {
999                            (Some(cl), Some(out)) => match cl.try_read().ok().and_then(|c| {
1000                                c.get(&path_bg)
1001                                    .and_then(crate::core::cache::CacheEntry::content)
1002                            }) {
1003                                Some(raw) => {
1004                                    let lo = ledger::count_for_ledger(&raw);
1005                                    (lo, lo.saturating_sub(ledger::count_for_ledger(out)))
1006                                }
1007                                None => (original, saved),
1008                            },
1009                            _ => (original, saved),
1010                        };
1011                        ledger::record_read_event(lbase, lsaved, None, None);
1012                    }
1013
1014                    // Traversal/co-access edge: this read fired together with the
1015                    // recent working set captured under the session lock (#289).
1016                    if let Some(root) =
1017                        crate::core::tool_lifecycle::usable_root(Some(project_root_bg.as_str()))
1018                    {
1019                        crate::core::cooccurrence::record_focus_access(
1020                            root,
1021                            &path_bg,
1022                            &traversal_working_set,
1023                        );
1024                    }
1025
1026                    let sig =
1027                        crate::core::mode_predictor::FileSignature::from_path(&path_bg, original);
1028                    let density = if output_tokens > 0 {
1029                        original as f64 / output_tokens as f64
1030                    } else {
1031                        1.0
1032                    };
1033                    let outcome = crate::core::mode_predictor::ModeOutcome {
1034                        mode: resolved_mode_bg,
1035                        tokens_in: original,
1036                        tokens_out: output_tokens,
1037                        density: density.min(1.0),
1038                    };
1039                    let mut predictor = crate::core::mode_predictor::ModePredictor::new();
1040                    predictor.set_project_root(&project_root_bg);
1041                    predictor.record(sig, outcome);
1042                    predictor.save();
1043
1044                    let ext = std::path::Path::new(&path_bg)
1045                        .extension()
1046                        .and_then(|e| e.to_str())
1047                        .unwrap_or("")
1048                        .to_string();
1049                    let thresholds =
1050                        crate::core::adaptive_thresholds::thresholds_for_path(&path_bg);
1051                    let feedback_outcome = crate::core::feedback::CompressionOutcome {
1052                        session_id: format!("{}", std::process::id()),
1053                        language: ext,
1054                        entropy_threshold: thresholds.bpe_entropy,
1055                        jaccard_threshold: thresholds.jaccard,
1056                        total_turns: turns as u32,
1057                        tokens_saved: saved as u64,
1058                        tokens_original: original as u64,
1059                        cache_hits: hits as u32,
1060                        total_reads: turns as u32,
1061                        // Real behavioral signal instead of a hardcoded success
1062                        // (#593): a compressed read only counts as task-completing
1063                        // when this extension is not in a high-bounce state —
1064                        // compression that keeps forcing full re-reads is not
1065                        // "completing" anything. Unknown (too few reads) stays
1066                        // optimistic so the cold start is unchanged. 0.30 mirrors
1067                        // bounce_tracker::BOUNCE_RATE_THRESHOLD.
1068                        task_completed: crate::core::bounce_tracker::global()
1069                            .lock()
1070                            .ok()
1071                            .and_then(|bt| bt.bounce_rate_for_extension(&path_bg))
1072                            .is_none_or(|rate| rate < 0.30),
1073                        timestamp: chrono::Local::now().to_rfc3339(),
1074                    };
1075                    let mut store = crate::core::feedback::FeedbackStore::load();
1076                    store.project_root = Some(project_root_bg);
1077                    store.record_outcome(feedback_outcome);
1078                }));
1079            });
1080        }
1081
1082        if let Some(aid) = resolved_agent_id.as_deref() {
1083            crate::core::agent_budget::record_consumption(aid, output_tokens);
1084        }
1085
1086        // #1098: graph-related hints (callers/callees) are now computed AFTER the
1087        // cache lock is released. They involve SQLite queries (~50-200ms) that
1088        // previously blocked all parallel reads while holding the write lock.
1089        let graph_hint = if !is_cache_hit
1090            && !resolved_mode.starts_with("lines:")
1091            && crate::core::profiles::active_profile()
1092                .output_hints
1093                .related_hint()
1094        {
1095            crate::tools::ctx_read::graph_related_hint(path)
1096        } else {
1097            None
1098        };
1099
1100        // Cross-source hints: gated by profile `cross_source_hint` (default off).
1101        // When enabled, appends issue/PR/schema references from the property
1102        // graph. Skipped when graph.db doesn't exist (#682).
1103        let hints_suffix = if crate::core::profiles::active_profile()
1104            .output_hints
1105            .cross_source_hint()
1106        {
1107            let graph_db =
1108                crate::core::property_graph::graph_dir(&ctx.project_root).join("graph.db");
1109            let graph = graph_db
1110                .exists()
1111                .then(|| crate::core::property_graph::CodeGraph::open(&ctx.project_root))
1112                .transpose()
1113                .ok()
1114                .flatten();
1115            graph.map_or_else(String::new, |graph| {
1116                let edges = graph.all_cross_source_edges();
1117                if edges.is_empty() {
1118                    String::new()
1119                } else {
1120                    let ranges = scoped_read_ranges(&resolved_mode);
1121                    let relative_path =
1122                        crate::core::graph_index::graph_relative_key(path, &ctx.project_root);
1123                    let hints = crate::core::cross_source_hints::hints_for_file_matching(
1124                        path,
1125                        &edges,
1126                        &ctx.project_root,
1127                        |hint| {
1128                            ranges.as_ref().is_none_or(|ranges| {
1129                                hint_intersects_ranges(hint, ranges, &graph, &relative_path)
1130                            })
1131                        },
1132                    );
1133                    crate::core::cross_source_hints::format_hints(&hints)
1134                }
1135            })
1136        } else {
1137            String::new()
1138        };
1139
1140        // Rule injection (#1325): discover and append rules scoped to this file
1141        // path (CLAUDE.md, AGENTS.md, .cursor/rules, .claude/rules) so the agent
1142        // receives the same context it would get from the native Read tool.
1143        let rules_suffix = {
1144            let client_id = ctx
1145                .client_name
1146                .as_ref()
1147                .map(|c| c.blocking_read().clone())
1148                .unwrap_or_default();
1149            crate::core::rule_discovery::rules_suffix_for_read(path, &ctx.project_root, &client_id)
1150        };
1151
1152        let mut warnings = Vec::new();
1153        if let Some(ref w) = budget_warning {
1154            warnings.push(w.as_str());
1155        }
1156        if let Some(ref w) = degrade_warning {
1157            warnings.push(w.as_str());
1158        }
1159        if let Some(ref w) = delta_explicit_note {
1160            warnings.push(w.as_str());
1161        }
1162        if let Some(ref w) = mode_override_note {
1163            warnings.push(w.as_str());
1164        }
1165        if let Some(ref w) = instruction_mode_note {
1166            warnings.push(w.as_str());
1167        }
1168        let graph_suffix = graph_hint.map(|h| format!("\n{h}")).unwrap_or_default();
1169        // #977: notices (mode override, budget, degradation, delta) go BEFORE the
1170        // payload so client-side truncation of large outputs cannot hide them.
1171        let final_output = if !warnings.is_empty() {
1172            format!(
1173                "{}\n\n{output}{hints_suffix}{graph_suffix}{rules_suffix}",
1174                warnings.join("\n")
1175            )
1176        } else if hints_suffix.is_empty() && graph_suffix.is_empty() && rules_suffix.is_empty() {
1177            output
1178        } else {
1179            format!("{output}{hints_suffix}{graph_suffix}{rules_suffix}")
1180        };
1181        // Proactive context: gated by profile `proactive_context` (default off).
1182        // When enabled, auto-expands previously compressed content that is
1183        // keyword-relevant to the current read (up to 2000 tokens).
1184        let final_output = if crate::core::profiles::active_profile()
1185            .output_hints
1186            .proactive_context()
1187        {
1188            let proactive_query = format!(
1189                "ctx_read path={path} mode={resolved_mode} task={}",
1190                task_ref.unwrap_or_default()
1191            );
1192            if let Some(block) =
1193                crate::core::relevance_tracker::proactive_context_for_path(&proactive_query, path)
1194            {
1195                format!("{final_output}{block}")
1196            } else {
1197                final_output
1198            }
1199        } else {
1200            final_output
1201        };
1202
1203        // Monotonic guard (#1326): re-count tokens on the fully assembled output
1204        // (including hints, warnings, proactive context) and verify the compressed
1205        // result is actually smaller than the original. If annotations inflated the
1206        // output beyond the raw baseline, report zero savings so the ledger stays
1207        // honest. We keep the compressed form (it may still be more useful than raw)
1208        // but correct the accounting.
1209        let final_tokens = crate::core::tokens::count_tokens(&final_output);
1210        let verified_saved = original.saturating_sub(final_tokens);
1211
1212        Ok(ToolOutput {
1213            text: final_output,
1214            original_tokens: original,
1215            saved_tokens: verified_saved,
1216            mode: Some(resolved_mode),
1217            path: Some(path.to_string()),
1218            changed: false,
1219            shell_outcome: None,
1220            content_blocks: None,
1221        })
1222    }
1223}
1224
1225/// Resolve the `start_line`/`offset`/`limit` arguments into `(start, limit)`.
1226///
1227/// `offset` is an alias for `start_line` (1-based first line); `start_line`
1228/// wins if a caller passes both. `limit` (when > 0) bounds the number of lines;
1229/// a bare `limit` reads from line 1. Returns `None` when no windowing argument
1230/// is present, so the caller leaves the mode untouched (GitHub #432).
1231fn resolve_line_window(
1232    start_line: Option<i64>,
1233    offset: Option<i64>,
1234    limit: Option<i64>,
1235) -> Option<(i64, Option<i64>)> {
1236    let start = start_line.or(offset).map(|v| v.max(1));
1237    let limit = limit.filter(|&l| l > 0);
1238    match (start, limit) {
1239        (Some(s), l) => Some((s, l)),
1240        (None, Some(_)) => Some((1, limit)),
1241        (None, None) => None,
1242    }
1243}
1244
1245/// Build the `lines:N-M` mode string for a resolved window. An unbounded window
1246/// (no `limit`) reads to EOF via the historical `999999` sentinel.
1247fn lines_mode(start: i64, limit: Option<i64>) -> String {
1248    match limit {
1249        Some(l) => format!("lines:{start}-{}", start + l - 1),
1250        None => format!("lines:{start}-999999"),
1251    }
1252}
1253
1254/// Build the `anchored:N-M` mode string for a resolved window (#811) — mirrors
1255/// `lines_mode`, keeping the `anchored:` prefix so the render path re-attaches
1256/// hash anchors to the window instead of falling back to plain numbered lines.
1257fn anchored_lines_mode(start: i64, limit: Option<i64>) -> String {
1258    match limit {
1259        Some(l) => format!("anchored:{start}-{}", start + l - 1),
1260        None => format!("anchored:{start}-999999"),
1261    }
1262}
1263
1264fn resolve_instruction_file_mode(path: &str, mode: &str) -> (String, Option<String>) {
1265    if !crate::tools::ctx_read::is_instruction_file(path)
1266        || matches!(mode, "full" | "raw" | "anchored")
1267        || mode.starts_with("anchored:")
1268        || mode.starts_with("lines:")
1269    {
1270        return (mode.to_string(), None);
1271    }
1272
1273    (
1274        "full".to_string(),
1275        Some(format!(
1276            "[mode overridden: {mode} -> full, reason=instruction file requires complete content]"
1277        )),
1278    )
1279}
1280
1281fn scoped_read_ranges(mode: &str) -> Option<Vec<crate::tools::ctx_read::mode::LineRange>> {
1282    use crate::tools::ctx_read::{ReadMode, mode::LineRange};
1283
1284    match mode.parse::<ReadMode>().ok()? {
1285        ReadMode::Lines(range) | ReadMode::Anchored(Some(range)) => Some(vec![range]),
1286        ReadMode::LinesMulti(payload) => Some(
1287            payload
1288                .split(',')
1289                .filter_map(|part| {
1290                    let (start, end) = part.split_once('-').unwrap_or((part, part));
1291                    Some(LineRange::new(start.parse().ok()?, end.parse().ok()?))
1292                })
1293                .collect(),
1294        ),
1295        _ => None,
1296    }
1297}
1298
1299fn hint_intersects_ranges(
1300    hint: &crate::core::cross_source_hints::CrossSourceHint,
1301    ranges: &[crate::tools::ctx_read::mode::LineRange],
1302    graph: &crate::core::property_graph::CodeGraph,
1303    relative_path: &str,
1304) -> bool {
1305    if hint.relation != "health_hotspot" {
1306        return false;
1307    }
1308    let Some((_, symbol)) = hint.source_uri.rsplit_once('#') else {
1309        return false;
1310    };
1311    let Ok(Some(node)) = graph.get_node_by_symbol(symbol, relative_path) else {
1312        return false;
1313    };
1314    let (Some(start), Some(end)) = (node.line_start, node.line_end) else {
1315        return false;
1316    };
1317    ranges
1318        .iter()
1319        .any(|range| start <= range.end as usize && end >= range.start as usize)
1320}
1321
1322/// Apply a resolved line window to `mode`/`fresh`. Explicit `lines:N-M` and
1323/// `anchored:N-M` modes are preserved when `limit` is the only alias (#1254).
1324/// A `start_line` or `offset` still overrides any mode to prevent full-file
1325/// materialization, while `start_line=1` without a limit remains a no-op (#253).
1326fn apply_line_window(
1327    mode: &mut String,
1328    fresh: &mut bool,
1329    explicit_mode: bool,
1330    start_line: Option<i64>,
1331    offset: Option<i64>,
1332    limit: Option<i64>,
1333) {
1334    let preserve_explicit_window = explicit_mode
1335        && start_line.is_none()
1336        && offset.is_none()
1337        && limit.is_some_and(|value| value > 0)
1338        && matches!(
1339            mode.parse::<crate::tools::ctx_read::ReadMode>(),
1340            Ok(crate::tools::ctx_read::ReadMode::Lines(_)
1341                | crate::tools::ctx_read::ReadMode::Anchored(Some(_)))
1342        );
1343    if preserve_explicit_window {
1344        return;
1345    }
1346
1347    let Some((start, limit)) = resolve_line_window(start_line, offset, limit) else {
1348        return;
1349    };
1350    if start <= 1 && limit.is_none() {
1351        return;
1352    }
1353    *fresh = true;
1354    // #811: anchored gets its own windowed variant (preserves hashes for
1355    // ctx_patch); every other mode switches to lines:N-M to prevent
1356    // full-file materialization on large files.
1357    if mode == "anchored" {
1358        *mode = anchored_lines_mode(start, limit);
1359    } else {
1360        *mode = lines_mode(start, limit);
1361    }
1362}
1363
1364/// #513: resolve the `raw=true` convenience flag into the effective explicit
1365/// `mode` argument. Agents reach for `raw:true` to get exact bytes; it aliases
1366/// to `mode="raw"` (verbatim, unframed) and wins over any caller-supplied
1367/// `mode`. When `raw` is unset, the caller's `mode` (if any) passes through
1368/// unchanged. The caller separately forces `fresh=true` for raw so a re-read
1369/// never collapses to an `[unchanged]`/auto-delta stub.
1370fn resolve_raw_alias(arg_raw: bool, mode_arg: Option<String>) -> Option<String> {
1371    if arg_raw {
1372        Some("raw".to_string())
1373    } else {
1374        mode_arg
1375    }
1376}
1377
1378fn apply_verdict(
1379    mode: &str,
1380    verdict: crate::core::degradation_policy::DegradationVerdictV1,
1381) -> (String, bool) {
1382    use crate::core::degradation_policy::DegradationVerdictV1;
1383    match verdict {
1384        DegradationVerdictV1::Ok => (mode.to_string(), false),
1385        DegradationVerdictV1::Warn => match mode {
1386            "full" => ("map".to_string(), true),
1387            other => (other.to_string(), false),
1388        },
1389        DegradationVerdictV1::Throttle => match mode {
1390            "full" | "map" => ("signatures".to_string(), true),
1391            other => (other.to_string(), false),
1392        },
1393        DegradationVerdictV1::Block => {
1394            if mode == "signatures" {
1395                ("signatures".to_string(), false)
1396            } else {
1397                ("signatures".to_string(), true)
1398            }
1399        }
1400    }
1401}
1402
1403fn auto_degrade_read_mode(mode: &str) -> (String, Option<String>) {
1404    if crate::core::config::Config::load().no_degrade_effective() {
1405        return (mode.to_string(), None);
1406    }
1407    let profile = crate::core::profiles::active_profile();
1408    if !profile.degradation.enforce_effective() {
1409        return (mode.to_string(), None);
1410    }
1411    let policy = crate::core::degradation_policy::evaluate_v1_for_tool("ctx_read", None);
1412    let (new_mode, degraded) = apply_verdict(mode, policy.decision.verdict);
1413    let warning = if degraded {
1414        Some(format!(
1415            "⚠ Context pressure: mode={mode} was downgraded to mode={new_mode} \
1416             (verdict: {:?}). Use start_line=1 to bypass, or run ctx_compress to free budget.",
1417            policy.decision.verdict
1418        ))
1419    } else {
1420        None
1421    };
1422    (new_mode, warning)
1423}
1424
1425fn extract_file_summary(output: &str, path: &str) -> String {
1426    let hint = crate::core::auto_findings::extract_content_hint(output);
1427    if !hint.is_empty() {
1428        return hint;
1429    }
1430    let ext = std::path::Path::new(path)
1431        .extension()
1432        .and_then(|e| e.to_str())
1433        .unwrap_or("");
1434    let line_count = output.lines().count();
1435    if line_count > 5 {
1436        format!("{ext} file, {line_count} lines")
1437    } else {
1438        String::new()
1439    }
1440}
1441
1442// #660 LOC gate: inline tests split out to keep this file under the line cap.
1443#[cfg(test)]
1444#[path = "ctx_read_inline_tests.rs"]
1445mod tests;
1446
1447// #660 LOC gate: repo-param tests split out to keep this file under the line
1448// cap — see `ctx_read_repo_param_tests.rs`.
1449
1450/// Read an image file and return it as MCP ContentBlock::Image for visual LLM processing.
1451fn read_image_file(path: &str) -> Result<ToolOutput, ErrorData> {
1452    use crate::core::binary_detect::{IMAGE_MAX_BYTES, image_mime_type};
1453    use base64::Engine;
1454
1455    let metadata = std::fs::metadata(path)
1456        .map_err(|e| ErrorData::invalid_params(format!("Cannot read image: {e}"), None))?;
1457
1458    if metadata.len() > IMAGE_MAX_BYTES {
1459        return Err(ErrorData::invalid_params(
1460            format!(
1461                "Image too large ({:.1} MB, limit {:.0} MB). Resize or use a smaller image.",
1462                metadata.len() as f64 / 1024.0 / 1024.0,
1463                IMAGE_MAX_BYTES as f64 / 1024.0 / 1024.0,
1464            ),
1465            None,
1466        ));
1467    }
1468
1469    let mime_type = image_mime_type(path)
1470        .ok_or_else(|| ErrorData::invalid_params("Unsupported image format".to_string(), None))?;
1471
1472    let bytes = std::fs::read(path)
1473        .map_err(|e| ErrorData::invalid_params(format!("Cannot read image: {e}"), None))?;
1474
1475    let base64_data = base64::prelude::BASE64_STANDARD.encode(&bytes);
1476    let short_name = std::path::Path::new(path)
1477        .file_name()
1478        .and_then(|n| n.to_str())
1479        .unwrap_or(path);
1480
1481    let text_block = ContentBlock::text(format!(
1482        "[Image: {} ({} KB, {})]",
1483        short_name,
1484        bytes.len() / 1024,
1485        mime_type
1486    ));
1487    let image_block = ContentBlock::image(base64_data, mime_type);
1488
1489    Ok(ToolOutput::image(
1490        vec![text_block, image_block],
1491        path.to_string(),
1492    ))
1493}
1494
1495#[cfg(test)]
1496#[path = "ctx_read_repo_param_tests.rs"]
1497mod repo_param_tests;