-
Notifications
You must be signed in to change notification settings - Fork 340
Make /compact non-blocking with spinner feedback #1687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -717,15 +717,25 @@ func (a *App) ShouldExitAfterFirstResponse() bool { | |
| return a.exitAfterFirstResponse | ||
| } | ||
|
|
||
| func (a *App) CompactSession(additionalPrompt string) { | ||
| if a.session != nil { | ||
| func (a *App) CompactSession(ctx context.Context, additionalPrompt string) { | ||
| sess := a.session | ||
| if sess == nil { | ||
| return | ||
| } | ||
|
|
||
| go func() { | ||
| events := make(chan runtime.Event, 100) | ||
| a.runtime.Summarize(context.Background(), a.session, additionalPrompt, events) | ||
| close(events) | ||
| go func() { | ||
| defer close(events) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential race condition: session pointer accessed without synchronization
Although Go's GC keeps the old session alive while referenced, this creates a race where the goroutine operates on a stale session that's no longer considered "current" by the app. Suggested fix: sess := a.session
if sess == nil {
return
}
go func() {
events := make(chan runtime.Event, 100)
go func() {
defer close(events)
a.runtime.Summarize(ctx, sess, additionalPrompt, events)
}()
// ... rest of code
}()Capturing the session pointer in a local variable before the goroutine prevents the race. |
||
| a.runtime.Summarize(ctx, sess, additionalPrompt, events) | ||
| }() | ||
| for event := range events { | ||
| a.events <- event | ||
| if ctx.Err() != nil { | ||
| return | ||
| } | ||
| a.sendEvent(ctx, event) | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (a *App) PlainTextTranscript() string { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1155,11 +1155,18 @@ func (p *chatPage) processMessage(msg msgtypes.SendMsg) tea.Cmd { | |
| // CompactSession generates a summary and compacts the session history | ||
| func (p *chatPage) CompactSession(additionalPrompt string) tea.Cmd { | ||
| // Cancel any active stream without showing cancellation message | ||
| p.cancelStream(false) | ||
| cancelCmd := p.cancelStream(false) | ||
|
|
||
| p.app.CompactSession(additionalPrompt) | ||
| var ctx context.Context | ||
| ctx, p.msgCancel = context.WithCancel(context.Background()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Context lifecycle management issue The context created with
While this mirrors the pattern in Consider:
The immediate risk is moderate since compaction isn't called frequently, but it's worth addressing for proper resource management. |
||
| p.app.CompactSession(ctx, additionalPrompt) | ||
|
|
||
| return p.messages.ScrollToBottom() | ||
| return tea.Batch( | ||
| cancelCmd, | ||
| p.setWorking(true), | ||
| p.setPendingResponse(true), | ||
| p.messages.ScrollToBottom(), | ||
| ) | ||
| } | ||
|
|
||
| func (p *chatPage) Cleanup() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Goroutine leak: outer goroutine doesn't respect context cancellation
The outer goroutine launched at line 725 runs a
for event := range eventsloop without checkingctx.Done(). If the context is cancelled while waiting for events, the goroutine will remain blocked until the inner goroutine closes the channel.This is problematic because:
Summarize()is slow or hangs, the outer goroutine leaksCompactSessioncould accumulate orphaned goroutinesthrottleEventsat line 850) use a properselectstatementSuggested fix:
This pattern ensures the goroutine exits promptly when the context is cancelled.