fix(agent): read step data for summarization check

Amolith created

StopWhen was reading token counts from currentSession, which is fetched
once at Run() start and never updated. OnStepFinish updates a separate
updatedSession variable, leaving currentSession stale.

It now reads directly from the steps parameter that fantasy passes to
StopWhen, which contains fresh usage data from completed steps.

Fixes: charmbracelet/crush#1750
Assisted-by: Claude Opus 4.5 via Crush

Change summary

internal/agent/agent.go | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

Detailed changes

internal/agent/agent.go 🔗

@@ -371,9 +371,14 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*fantasy
 			return a.messages.Update(genCtx, *currentAssistant)
 		},
 		StopWhen: []fantasy.StopCondition{
-			func(_ []fantasy.StepResult) bool {
+			func(steps []fantasy.StepResult) bool {
+				if len(steps) == 0 {
+					return false
+				}
+				lastStep := steps[len(steps)-1]
+				usage := lastStep.Usage
+				tokens := usage.InputTokens + usage.OutputTokens + usage.CacheCreationTokens + usage.CacheReadTokens
 				cw := int64(a.largeModel.CatwalkCfg.ContextWindow)
-				tokens := currentSession.CompletionTokens + currentSession.PromptTokens
 				remaining := cw - tokens
 				var threshold int64
 				if cw > 200_000 {