Skip to content

refactor: refactor session handling and i18n defaults - #670

Merged
haiphucnguyen merged 7 commits into
mainfrom
feature/minor-improvements
Aug 23, 2026
Merged

haiphucnguyen merged 7 commits into
mainfrom
feature/minor-improvements

Conversation

@haiphucnguyen

Copy link
Copy Markdown
Collaborator
  • Update ChatViewModel to set session title immediately using raw message text.
  • Adjust SessionManager to perform web search setting on a background thread.
  • Add default directive labels and placeholders to all i18n resource files.
  • Clean up unused imports and minor test helper adjustments.
  • Update Main.kt to pass project context to UI content.

- Update ChatViewModel to set session title immediately using raw message text.
- Adjust SessionManager to perform web search setting on a background thread.
- Add default directive labels and placeholders to all i18n resource files.
- Clean up unused imports and minor test helper adjustments.
- Update Main.kt to pass project context to UI content.
Copilot AI lite review requested due to automatic review settings August 23, 2026 20:32

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.

Pull request overview

This PR refactors “start chat from project” session initialization to reduce race conditions between DB reloads and in-memory message updates, and it rounds out i18n resources with new/default directive-related strings.

Changes:

  • Update session creation flow to (a) perform DB writes on Dispatchers.IO and (b) bind a freshly-created ChatViewModel to a new session without an async DB reload.
  • Set the session title immediately from the first raw user message text while waiting for the AI-generated summary title.
  • Add/move directive default labels and add “set/unset default directive” strings across i18n bundles; minor cleanup in a CLI integration test helper.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
shared/src/main/kotlin/io/askimo/core/exception/UserException.kt Removes trailing whitespace at EOF.
desktop/src/main/kotlin/io/askimo/desktop/Main.kt Passes the loaded project into session creation when starting a chat from project detail.
desktop-shared/src/main/resources/i18n/messages.properties Reorders project default-directive strings; adds base “set/unset default directive” strings.
desktop-shared/src/main/resources/i18n/messages_zh_TW.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_zh_CN.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_vi_VN.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_pt_BR.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_ko_KR.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_ja_JP.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_fr.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_es.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/resources/i18n/messages_de.properties Adds project default-directive strings and directive default set/unset strings.
desktop-shared/src/main/kotlin/io/askimo/ui/session/SessionManager.kt Refactors project session creation: IO dispatcher for DB work; uses Snapshot.withMutableSnapshot; binds new session before sending first message; sets web search flag off-thread.
desktop-shared/src/main/kotlin/io/askimo/ui/chat/ChatViewModel.kt Sets session title immediately on first message; adds bindNewSession to avoid async DB reload races for brand-new sessions.
cli/src/test/kotlin/io/askimo/core/chat/service/ChatDirectiveServiceIT.kt Minor formatting cleanup of a helper method.

💡 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.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

desktop-shared/src/main/kotlin/io/askimo/ui/session/SessionManager.kt:699

  • Wrapping viewModel.sendMessage(...) in Snapshot.withMutableSnapshot means the snapshot block includes side effects (EventBus.post, starting timers, launching coroutines, etc.). If snapshot apply fails, those side effects may already have happened even though the state mutations were rejected. Prefer keeping snapshot blocks side-effect free by moving snapshot management into ChatViewModel.sendMessage (wrap only its state writes) or by splitting sendMessage into a pure state-mutation part + a side-effect part.
                Snapshot.withMutableSnapshot {
                    viewModel.sendMessage(projectId, mode, message, attachments, enabledServerIds)
                }

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.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

Suppressed comments (1)

desktop-shared/src/main/kotlin/io/askimo/ui/chat/ChatViewModel.kt:876

  • bindNewSession() performs a synchronous DB existence check via chatDirectiveService.resolveDefaultDirectiveId(project), which calls ChatDirectiveRepository.exists() (Exposed transaction). This is invoked from SessionManager.createProjectSessionAndSendMessage() inside Snapshot.withMutableSnapshot, so it can block the snapshot/state mutation section and run on a non-IO dispatcher. Consider resolving the default directive ID on Dispatchers.IO before the snapshot/state writes and passing the resolved ID into bindNewSession (or setting it after binding) so bindNewSession becomes pure state initialization.
    fun bindNewSession(sessionId: String, title: String, project: Project?) {
        currentSessionId.value = sessionId
        sessionTitle = title
        this.project = project
        messages = emptyList()
        currentCursor = null
        hasMoreMessages = false
        bookmarkedMessageIds = emptySet()
        selectedDirective = chatDirectiveService.resolveDefaultDirectiveId(project)
        errorMessage = null

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.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

Suppressed comments (3)

Previously missed (3) — in code that hasn't changed since the last review.

desktop-shared/src/main/kotlin/io/askimo/ui/session/SessionManager.kt:704

  • getOrCreateChatViewModel() is executed inside Snapshot.withMutableSnapshot. Creating a ChatViewModel currently runs chatDirectiveService.resolveDefaultDirectiveId(...) during property initialization (see ChatViewModel.kt:110), which performs blocking DB access. That can stall the snapshot apply and defeats the goal of keeping the snapshot block to plain, fast state writes.

Create the ViewModel outside the snapshot and only do the Compose state updates + bindNewSession inside the snapshot.

                lateinit var viewModel: ChatViewModel
                Snapshot.withMutableSnapshot {
                    // bindNewSession (not switchToSession/resumeSession) avoids an async DB
                    // reload racing sendMessage()'s in-memory mutation below.
                    activeSessionId = newSession.id
                    createdSessions.add(newSession.id)
                    viewModel = getOrCreateChatViewModel(newSession.id)
                    viewModel.bindNewSession(sessionId = newSession.id, title = message, project = project, defaultDirectiveId = resolvedDirectiveId)

desktop-shared/src/main/kotlin/io/askimo/ui/session/SessionManager.kt:694

  • resolvedDirectiveId is applied only to the in-memory ViewModel (bindNewSession), but the newly-created session in the DB still has directiveId = null when the caller didn’t explicitly pass one. Since resumeSession() restores selectedDirective from the persisted session, the default directive selection will be lost after restart / reload.

Persist the resolved default to the session (or set it on createSession) when directiveId is null and a default was resolved.

                val resolvedDirectiveId = directiveId ?: withContext(Dispatchers.IO) {
                    chatDirectiveService.resolveDefaultDirectiveId(project)
                }

cli/src/test/kotlin/io/askimo/core/chat/service/ChatDirectiveServiceIT.kt:77

  • The single-line createDirective helper is very long and hurts readability (and may violate any line-length formatting rules). Keep it formatted like the surrounding helpers for maintainability.
    private fun createDirective(name: String): ChatDirective = directiveRepository.save(ChatDirective(name = name, content = "Some instructions"))

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.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

desktop-shared/src/main/kotlin/io/askimo/ui/session/SessionManager.kt:689

  • Snapshot.withMutableSnapshot is meant to batch Compose snapshot state writes; mixing in non-snapshot side effects (like mutating createdSessions, which is a plain MutableSet) inside the block can undermine the goal of using a snapshot here and makes the block harder to reason about. Move createdSessions.add(...) outside the snapshot so the snapshot only contains Compose-observed state writes.
                Snapshot.withMutableSnapshot {
                    // bindNewSession (not switchToSession/resumeSession) avoids an async DB
                    // reload racing sendMessage()'s in-memory mutation below.
                    activeSessionId = newSession.id
                    createdSessions.add(newSession.id)

@haiphucnguyen
haiphucnguyen merged commit d4a8f2e into main Aug 23, 2026
10 checks passed
@haiphucnguyen
haiphucnguyen deleted the feature/minor-improvements branch August 23, 2026 22:20
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.

2 participants