Skip to content

Manage agent run outside view life cycle - #724

Merged
haiphucnguyen merged 4 commits into
askimo-ai:mainfrom
totodev90:feature/manage-agent-run-outside-view-life-cycle
Sep 19, 2026
Merged

haiphucnguyen merged 4 commits into
askimo-ai:mainfrom
totodev90:feature/manage-agent-run-outside-view-life-cycle

Conversation

@totodev90

Copy link
Copy Markdown
Contributor

Summary

Decouple AgentRunViewModel lifecycle from Compose composables by implementing a global AgentRunManager service. This ensures in-flight agent runs complete and persist to history even when users navigate away from the agents view (e.g., to Settings, Projects, or other views).

Related Issue(s)

No assigned issue — resolved through architectural improvement.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • [] 💥 Breaking change
  • 📝 Documentation
  • ♻️ Refactor (no functional change)
  • 🧪 Tests
  • 🔧 Chore / tooling / CI

What Changed & Why

Problem:
Agent runs were tied to the composable lifecycle via remember { AgentRunViewModel(...) } with a DisposableEffect that called close() on unmount. This caused:

  • In-flight runs to be cancelled when navigating away from the agents view
  • History records not persisting when users switched to other views mid-run
  • No way to track completion notifications after navigation

Solution:
Implemented AgentRunManager — a global singleton service mirroring SessionManager's proven pattern:

  1. Created AgentRunManager.kt — caches AgentRunViewModel instances by workspace ID with a persistent CoroutineScope independent of Compose lifecycle
  2. Updated AgentRunArea.kt — changed from local remember { AgentRunViewModel(...) } to AgentRunManager.getOrCreateAgentRunViewModel()
  3. Updated AgentsViewModel.selectWorkspace() — closes old workspace ViewModel when switching to a new workspace
  4. Updated AgentView.kt — added DisposableEffect to clean up current workspace ViewModel when leaving the agents view

Benefits:

  • ✅ In-flight runs survive navigation away from agents view
  • ✅ History always persists via historyRepo.save() on global scope
  • ✅ Seamless reattachment when returning to agents view
  • ✅ Mirrors proven SessionManager architecture for chat sessions
  • ✅ Graceful cleanup on app shutdown via registered shutdown hook

How to Test

  1. Start an agent run (any agent, any workspace)
  2. While the run is in-flight:
    • Navigate to Settings → verify run continues
    • Navigate to Projects → verify run continues
    • Navigate to Chat → verify run continues
  3. Return to Agents view → verify conversation still visible with completed result
  4. Verify history was persisted correctly in the side panel
./gradlew desktop-shared:compileKotlin -x test
./gradlew test

## Screenshots / Recordings (if applicable)

<!-- For UI changes, add before/after screenshots or a screen recording -->

## Checklist

- [ ] I have read the [CONTRIBUTING](../CONTRIBUTING.md) guide
- [ ] My branch is up to date with `main`
- [ ] I ran `./gradlew spotlessApply` and `./gradlew detekt` locally
- [ ] I ran `./gradlew test` and all tests pass
- [ ] I added/updated tests for my changes (if applicable)
- [ ] I added/updated documentation (if applicable)
- [ ] My commits are signed off (DCO) — `git commit -s`
- [ ] This PR is focused on a single feature/fix

## Additional Notes

<!-- Anything else reviewers should know -->

@CLAassistant

CLAassistant commented Sep 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

One or more issues must be addressed before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

Decouples agent run ViewModels from Compose navigation by introducing centralized caching and lifecycle management.

Changes:

  • Adds global AgentRunManager caching ViewModels by workspace.
  • Routes AgentRunArea through the manager.
  • Adds workspace/view disposal cleanup.
File Description
desktop-shared/​src/​main/​kotlin/​io/​askimo/​ui/​agent/​AgentView.kt Updated as part of this pull request.
desktop-shared/​src/​main/​kotlin/​io/​askimo/​ui/​agent/​AgentsViewModel.kt Updated as part of this pull request.
desktop-shared/​src/​main/​kotlin/​io/​askimo/​ui/​agent/​AgentRunManager.kt Updated as part of this pull request.
desktop-shared/​src/​main/​kotlin/​io/​askimo/​ui/​agent/​AgentRunArea.kt Updated as part of this pull request.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Critical lifecycle issues can leak ViewModels and abort in-flight runs, while completion refreshes can be lost.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 High severity

Open (2)
Resolved since last review (2)

Comment on lines +34 to +38
// Cache of AgentRunViewModel instances by workspace ID
private val agentRunViewModels = mutableMapOf<String, AgentRunViewModel>()

// Shared coroutine scope for all agent runs (not tied to any UI context)
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

One or more issues must be addressed before approval.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (1)

@haiphucnguyen haiphucnguyen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @totodev90 !

@haiphucnguyen
haiphucnguyen merged commit f2aadd7 into askimo-ai:main Sep 19, 2026
10 checks passed
@totodev90
totodev90 deleted the feature/manage-agent-run-outside-view-life-cycle branch October 3, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants