Skip to main content

Module output_echo

Module output_echo 

Source
Expand description

Output-echo detection (#501).

lean-ctx aggressively optimizes the input side, but never looked at the agent’s output — although output tokens cost 4-5x more. The most common waste pattern is the code echo: the agent re-quotes file content that is already in context. The afterAgentResponse hook delivers the reply text; this module measures which share of its code lines were echoed from recently read files (context radar tail) and feeds three consumers:

  1. rolling stats (~/.lean-ctx/output_echo.json) for ctx_metrics, ctx_session output_stats and the dashboard,
  2. an automatic LlmFeedbackEvent so the adaptive mode policy finally receives continuous data instead of voluntary ctx_feedback calls,
  3. a stable, cache-friendly CEP nudge the MCP server appends to a tool result when echo stays high (cooldown-limited).

Structs§

EchoReport
EchoStats

Functions§

analyze
Pure analysis: which share of the response’s code lines appear verbatim in any source document (recently read file contents)?
analyze_and_record
Hook entry point: analyze an agent response against the radar tail, persist rolling stats and emit the automatic feedback event.
current_avg_ratio
Average echo ratio over the rolling window — CEP score input.
load_stats
take_pending_nudge
CEP nudge for the MCP server to append to a tool result. Stable text in 10%-steps (prompt-cache friendly, #498), cooldown-limited. Consuming the nudge advances the cooldown marker.