doCompact orchestrates a session compaction. It is intentionally thin: the heavy lifting (extracting the conversation, running the LLM, computing the kept-tail boundary) lives in [pkg/runtime/compactor]; this function owns only what's runtime-private: hook dispatch, session mutation, event emission,
(ctx context.Context, sess *session.Session, a *agent.Agent, additionalPrompt, reason string, events EventSink)
| 55 | // env / cwd / OS info is automatically present after a compaction without |
| 56 | // any extra dispatch. |
| 57 | func (r *LocalRuntime) doCompact(ctx context.Context, sess *session.Session, a *agent.Agent, additionalPrompt, reason string, events EventSink) { |
| 58 | contextLimit := r.compactionContextLimit(ctx, a) |
| 59 | |
| 60 | // before_compaction: hooks can veto or supply a custom summary. |
| 61 | pre := r.executeBeforeCompactionHooks(ctx, sess, a, reason, contextLimit, events) |
| 62 | if pre != nil && !pre.Allowed { |
| 63 | slog.InfoContext(ctx, "Session compaction skipped by before_compaction hook", |
| 64 | "session_id", sess.ID, "agent", a.Name(), "reason", reason, |
| 65 | "hook_message", pre.Message, |
| 66 | ) |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | slog.DebugContext(ctx, "Generating summary for session", "session_id", sess.ID, "reason", reason) |
| 71 | events.Emit(SessionCompaction(sess.ID, "started", a.Name())) |
| 72 | defer func() { |
| 73 | events.Emit(SessionCompaction(sess.ID, "completed", a.Name())) |
| 74 | }() |
| 75 | |
| 76 | // Choose the strategy: a hook-supplied summary if before_compaction |
| 77 | // returned one, otherwise the default LLM strategy. |
| 78 | result := summaryFromHook(sess, a, pre, contextLimit) |
| 79 | if result == nil { |
| 80 | if contextLimit <= 0 { |
| 81 | slog.ErrorContext(ctx, "Failed to generate session summary", |
| 82 | "error", "model definition unavailable") |
| 83 | events.Emit(ErrorForSession(sess.ID, "Failed to get model definition")) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | var err error |
| 88 | result, err = compactor.RunLLM(ctx, compactor.LLMArgs{ |
| 89 | Session: sess, |
| 90 | Agent: a, |
| 91 | AdditionalPrompt: additionalPrompt, |
| 92 | ContextLimit: contextLimit, |
| 93 | RunAgent: r.runCompactionAgent, |
| 94 | }) |
| 95 | if err != nil { |
| 96 | slog.ErrorContext(ctx, "Failed to generate session summary", "error", err) |
| 97 | events.Emit(ErrorForSession(sess.ID, err.Error())) |
| 98 | return |
| 99 | } |
| 100 | if result == nil { |
| 101 | // Empty summary — bail without applying anything. |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Capture the pre-compaction token counts so the after_compaction |
| 107 | // hook can observe what was summarized ("compacted from X to Y"). |
| 108 | // We snapshot before applying the result because the apply step |
| 109 | // resets sess.OutputTokens to 0 and replaces sess.InputTokens with |
| 110 | // the new summary's estimated size. |
| 111 | preInputTokens, preOutputTokens := sess.Usage() |
| 112 | |
| 113 | // Apply the summary to the session. This is intrinsically |
| 114 | // runtime-private: it mutates session-internal state and persists |
no test coverage detected