diff --git a/repl.lua b/repl.lua index 366cf13..a865dcf 100644 --- a/repl.lua +++ b/repl.lua @@ -362,6 +362,18 @@ function M.run(config) if config.context and config.context.summarize_on_evict then ctx_opts.summarize_fn = make_summarize_fn() end + -- Phase 8 (docs/PHASE8.md): when cfg.tokenize.use_endpoint is true, + -- wire a tokenize_fn so Context:estimate_tokens uses real counts + -- from /tokenize (broker.token_count handles per-endpoint + -- capability cache + char/4 fallback). R4: the closure body MUST + -- reference `active_cfg` directly as an upvalue (NOT capture by + -- value) so :model switches naturally re-route to the new model's + -- tokenizer. A5 verified Lua upvalue semantics resolve at call time. + if config.tokenize and config.tokenize.use_endpoint then + ctx_opts.tokenize_fn = function(text) + return broker.token_count(active_cfg, text) + end + end local ctx = Context.new(ctx_opts) -- Phase 2: MCP sessions. Populated from config.mcp.servers at startup @@ -2095,6 +2107,16 @@ function M.run(config) r.model, r.category, r.calls, r.prompt, r.completion, r.cost, r.is_local and " (local)" or "")) end + -- Phase 8 R3: trailing summary line — current ctx snapshot + -- (NOT a comparison against the accumulator sums above; the + -- accumulator carries cumulative across all calls including + -- evicted turns, while estimate_tokens is current-in-memory + -- only). Shows budget utilization at-a-glance. + local est = ctx:estimate_tokens() + local budget = ctx.token_budget or 0 + local pct = (budget > 0) and (est * 100 / budget) or 0 + renderer.status(("estimated session ctx: %d tokens; token_budget=%d (%.1f%% used)"):format( + est, budget, pct)) return end renderer.status("usage: :cost [detail|reset]")