diff --git a/context.lua b/context.lua index 97d50a8..4f2d50c 100644 --- a/context.lua +++ b/context.lua @@ -56,6 +56,17 @@ function M.new(opts) summarize_fn = opts.summarize_fn, summary = nil, -- rolling summary string max_summary_chars = opts.max_summary_chars or 2000, + -- #101: proactive periodic summarization (cadence-triggered, + -- in addition to Phase 5's eviction-triggered path). When + -- summarize_every_n_turns is set AND summarize_fn is wired, + -- enforce_cadence() folds turns older than the last + -- summarize_keep_recent into ctx.summary every N appends. + -- Goal: keep the wire prompt tight from the start so small + -- local models aren't fed near-budget context until eviction + -- forces a fold. nil = disabled (existing behavior). + summarize_every_n_turns = opts.summarize_every_n_turns, + summarize_keep_recent = opts.summarize_keep_recent or 4, + _turns_since_summarize = 0, -- Phase 6 (#issue Phase 6 §6): project file-tree block, set by -- repl.lua via :tree meta or the cfg.project.auto_tree startup -- hook. nil = no block injected. Cached scan opts (depth / @@ -117,6 +128,8 @@ function Context:append(turn) assert(turn.content, "context:append requires content for role=" .. turn.role) end self.turns[#self.turns + 1] = stored + -- #101: bump cadence counter so enforce_cadence knows when to fire. + self._turns_since_summarize = (self._turns_since_summarize or 0) + 1 end -- Buffer captured shell-exec output. Per §6 (post user-test fix), exec output @@ -388,6 +401,66 @@ function Context:to_messages(opts) return msgs end +-- #101: proactive periodic summarization. Fires every +-- summarize_every_n_turns appends, folding turns older than the last +-- summarize_keep_recent into ctx.summary via summarize_fn. Returns +-- the number of turns folded (0 if disabled / not yet due / nothing +-- to fold / Norris-mode / callback failed). +-- +-- Norris suppression (Phase 5 R-C4 parity): the planner stays +-- focused on its goal anchor — folding history mid-loop would +-- change its perceived progress. +-- +-- Orphan-tool guard: never fold an assistant-with-tool_calls turn +-- without its matching role=tool turn(s). When the slice would end +-- on such an assistant, peel back until it doesn't (the unfolded +-- tail then becomes part of the live window — temporarily larger +-- than summarize_keep_recent, but chat-template-legal). +function Context:enforce_cadence() + if self.norris_active then return 0 end + if not self.summarize_fn then return 0 end + if not self.summarize_every_n_turns then return 0 end + if (self._turns_since_summarize or 0) < self.summarize_every_n_turns then + return 0 + end + local keep = self.summarize_keep_recent or 4 + local n = #self.turns + if n <= keep then return 0 end + + local fold_count = n - keep + -- Orphan-tool guard: peel back from the right edge of the fold + -- slice while the last folded turn is assistant-with-tool_calls. + while fold_count > 0 do + local last = self.turns[fold_count] + if last and last.role == "assistant" + and last.tool_calls and #last.tool_calls > 0 then + fold_count = fold_count - 1 + else + break + end + end + if fold_count == 0 then return 0 end + + local pair = {} + for i = 1, fold_count do pair[i] = self.turns[i] end + + local ok, new_summary = pcall(self.summarize_fn, self.summary, pair) + if not ok or type(new_summary) ~= "string" or new_summary == "" then + return 0 -- failure: leave turns; eviction will handle them later + end + self.summary = new_summary + if #self.summary > self.max_summary_chars then + local ok2, compressed = pcall(self.summarize_fn, self.summary, nil) + if ok2 and type(compressed) == "string" and compressed ~= "" then + self.summary = compressed + end + end + + for _ = 1, fold_count do table.remove(self.turns, 1) end + self._turns_since_summarize = 0 + return fold_count +end + -- Evict the oldest pair (user + assistant) while we exceed max_turns -- OR token_budget (Phase 8 pillar 5). Returns total turns evicted. -- Caller is responsible for rendering the §8 status line. diff --git a/repl.lua b/repl.lua index 848807d..2436057 100644 --- a/repl.lua +++ b/repl.lua @@ -361,7 +361,11 @@ function M.run(config) if config.context then for k, v in pairs(config.context) do ctx_opts[k] = v end end - if config.context and config.context.summarize_on_evict then + -- #101: summarize_fn is also needed for cadence-triggered + -- summarization (Context:enforce_cadence). Wire it whenever + -- EITHER feature is enabled — same closure, different trigger. + if config.context and (config.context.summarize_on_evict + or config.context.summarize_every_n_turns) then ctx_opts.summarize_fn = make_summarize_fn() end -- Phase 8 (docs/PHASE8.md): when cfg.tokenize.use_endpoint is true, @@ -674,6 +678,13 @@ function M.run(config) end end + -- #101: status line for cadence-triggered fold. + local function status_cadence_fold(n) + if n and n > 0 then + renderer.status(("proactively summarized %d older turns"):format(n)) + end + end + -- ── Phase 5: fallback eligibility per PHASE5.md §5 ────────────────── -- All transport-failure patterns must match against the err string -- as broker.lua emits it (with "transport: " prefix). The matcher @@ -1146,6 +1157,11 @@ function M.run(config) -- loop body re-runs broker.chat_stream with the now-extended ctx end + -- #101: proactively fold older turns into ctx.summary on + -- cadence (when cfg.context.summarize_every_n_turns is set). + -- BEFORE enforce_budget so it sees a tighter token estimate + -- and doesn't evict turns we just summarized. + status_cadence_fold(ctx:enforce_cadence()) status_evictions(ctx:enforce_budget()) -- CMD: extraction on the final pure-text response only.