Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(agent): a chat run keeps going when Gotcha is swiped out of Recents (#111)

Durganshu6d8c92eff884
diff --git a/app/src/main/java/com/gotcha/GotchaApp.kt b/app/src/main/java/com/gotcha/GotchaApp.kt
index 1c03105..ce4118d 100644
--- a/app/src/main/java/com/gotcha/GotchaApp.kt
+++ b/app/src/main/java/com/gotcha/GotchaApp.kt
@@ -2,6 +2,7 @@ package com.gotcha
import android.app.Application
import android.util.Log
+import com.gotcha.agent.ChatRunner
import java.io.File
import java.io.PrintWriter
import java.io.StringWriter
@@ -21,6 +22,13 @@ import java.util.Locale
*/
class GotchaApp : Application() {
+ /**
+ * The chat agent and the run in progress (issue #111). Held here rather
+ * than by the chat screen's ViewModel, so a run goes on after MainActivity
+ * finishes; created by the first chat screen.
+ */
+ val chatRunner: ChatRunner by lazy { ChatRunner(this) }
+
override fun onCreate() {
super.onCreate()
// pdfbox ships its glyph tables as Android assets; the resource loader
diff --git a/app/src/main/java/com/gotcha/agent/ChatRunner.kt b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
new file mode 100644
index 0000000..38de0df
--- /dev/null
+++ b/app/src/main/java/com/gotcha/agent/ChatRunner.kt
@@ -0,0 +1,1136 @@
+package com.gotcha.agent
+
+import android.app.Application
+import com.gotcha.GotchaApp
+import com.gotcha.audio.AudioModel
+import com.gotcha.audio.AudioProvider
+import com.gotcha.audio.CompletionFeedback
+import com.gotcha.audio.TtsEngine
+import com.gotcha.data.ChatHistoryRepository
+import com.gotcha.data.ChatSession
+import com.gotcha.data.LlmProvider
+import com.gotcha.data.Settings
+import com.gotcha.data.SettingsRepository
+import com.gotcha.llm.ChatMessage
+import com.gotcha.llm.DocumentPart
+import com.gotcha.llm.LLMClient
+import com.gotcha.llm.attachmentsUserMessage
+import com.gotcha.notifications.AttentionKind
+import com.gotcha.notifications.AttentionNotifier
+import com.gotcha.notifications.ChatCompletionNotifier
+import com.gotcha.notifications.LocalNotificationStore
+import com.gotcha.notifications.NotificationCategory
+import com.gotcha.notifications.NotificationTarget
+import com.gotcha.notifications.RunOutcome
+import com.gotcha.service.ChatRunService
+import com.gotcha.service.RunningChat
+import com.gotcha.tools.AgentMode
+import com.gotcha.tools.GotchaSettingsUpdate
+import com.gotcha.tools.ScreenPerception
+import com.gotcha.tools.ToolResult
+import com.gotcha.tools.mergeProfileUpdate
+import com.gotcha.ui.ConfirmationOverlay
+import com.gotcha.ui.ForegroundControlIndicator
+import kotlinx.coroutines.CancellationException
+import kotlinx.coroutines.CompletableDeferred
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.SharedFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asSharedFlow
+import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.update
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import kotlinx.coroutines.withTimeoutOrNull
+import kotlinx.serialization.json.JsonPrimitive
+import java.util.concurrent.CopyOnWriteArrayList
+
+/**
+ * What the chat screen shows of the session bound to [ChatRunner]'s engine and
+ * of the run in progress, if any. [transcript], [activity], the sub-agent
+ * fields and [tokenCount] are about [boundSessionId]; the gates and the run
+ * fields are shown whichever chat is being viewed.
+ */
+data class RunState(
+ val boundSessionId: String? = null,
+ val transcript: List<UiMessage> = emptyList(),
+ val isBusy: Boolean = false,
+ val runningSessionId: String? = null,
+ val runningSessionTitle: String? = null,
+ val backgroundHint: String? = null,
+ val activity: String? = null,
+ val subAgentRunning: String? = null,
+ val subAgentCurrentAction: String? = null,
+ val pendingConfirmation: PendingConfirmation? = null,
+ val pendingForegroundControl: ForegroundControlRequest? = null,
+ val foregroundControlStatus: String? = null,
+ val pendingQuestion: PendingQuestion? = null,
+ val pendingPermission: String? = null,
+ val tokenCount: Int = 0,
+ val isConfigured: Boolean = false,
+ val isSpeaking: Boolean = false
+)
+
+/** A chat screen following [ChatRunner]. Called on the thread that changed the state. */
+interface ChatRunObserver {
+ fun onRunStateChanged(old: RunState, new: RunState)
+
+ /** [ChatRunner.settings] was reloaded, by the screen or by the agent itself. */
+ fun onSettingsChanged() {}
+}
+
+/** The chat the user is looking at when they send: what a run binds the engine to. */
+data class ViewedChat(
+ val sessionId: String?,
+ val messages: List<UiMessage>,
+ val agent: AgentMode,
+ val personaId: String?
+)
+
+/**
+ * Owns the chat agent and the run in progress, for as long as the process lives
+ * (issue #111). A run used to live in `ChatViewModel.viewModelScope`, so it ended
+ * whenever `MainActivity` finished: swiped out of Recents, or Back on the home
+ * screen. The runner is held by [GotchaApp]; `ChatViewModel` binds it to the chat
+ * being viewed, starts runs on it and follows its [state], and a ViewModel
+ * created later picks up the run where it is.
+ *
+ * Everything that must keep working with no screen lives here: the engine and
+ * its transcript, the question/confirmation/permission/foreground-control gates,
+ * the keep-alive service, the task-finished and needs-input notifications, and
+ * reading a reply aloud.
+ */
+@Suppress("TooManyFunctions", "LargeClass")
+class ChatRunner(private val app: Application) : AgentEvents {
+
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
+
+ val settingsRepository = SettingsRepository(app)
+ val historyRepository = ChatHistoryRepository(app)
+ private val confirmationOverlay = ConfirmationOverlay(app)
+ private val foregroundControlIndicator = ForegroundControlIndicator(app)
+ private val completionNotifier = ChatCompletionNotifier(app)
+ private val attentionNotifier = AttentionNotifier(app)
+ private val localNotificationStore = LocalNotificationStore(app)
+ private val runMarker = RunInProgressMarker(settingsRepository.prefs)
+
+ var settings: Settings = Settings()
+ private set
+ var client: LLMClient? = null
+ private set
+
+ val ttsEngine: TtsEngine = TtsEngine(
+ app,
+ settings.effectiveTtsBaseUrl,
+ settings.effectiveTtsApiKey,
+ onUnauthorized = { scope.launch { onSamosaUnauthorized() } }
+ )
+
+ /** The TTS models last fetched by the screen; they name the default voice. */
+ @Volatile
+ var ttsModels: List<AudioModel> = emptyList()
+
+ /** True when the active LLM run was initiated by voice dictation. */
+ @Volatile
+ private var currentRunIsVoice = false
+
+ /**
+ * Set by the Activity in onStart/onStop; drives whether gates notify and use
+ * the overlay. Once the Activity is gone its last onStop left this false.
+ */
+ @Volatile
+ var appInForeground = true
+ private set
+
+ internal val engine = AgentEngine(
+ appContext = app,
+ events = this,
+ historyRepository = historyRepository,
+ settingsProvider = { settings },
+ clientProvider = { client },
+ onUpdateUserProfile = { update ->
+ // Reload so a manual Personal Info edit isn't clobbered by a stale snapshot,
+ // then persist and refresh the cached settings the engine reads next turn.
+ val merged = mergeProfileUpdate(settingsRepository.load(), update)
+ ?: return@AgentEngine ToolResult.ok("No material change — profile left as is.")
+ settingsRepository.save(merged.updated)
+ settings = merged.updated
+ // Surface the change to the user: the profile is silently re-injected into every
+ // future prompt, so a prompt-injected update_user_profile call must not go
+ // unnoticed. A TOOL bubble in the transcript gives the user a chance to catch
+ // and revert a poisoned update. Dispatched to Main because this handler runs
+ // inside the tool executor's IO context while appendTranscript touches UI state.
+ withContext(Dispatchers.Main) {
+ appendTranscript(
+ MessageKind.TOOL,
+ "Assistant updated your profile: " +
+ merged.changedFields.joinToString(", ") + "."
+ )
+ }
+ ToolResult.ok(
+ "Updated " + merged.changedFields.joinToString(", ") + ". " +
+ "The new value will be used from the next message."
+ )
+ },
+ onUpdateGotchaSettings = { plan ->
+ // Only reached after the user approved this exact change. Apply it onto a
+ // fresh load so a concurrent write elsewhere (the Settings screen, the
+ // assistive ball) is not clobbered by the snapshot the prompt was built from.
+ settingsRepository.save(plan.applyTo(settingsRepository.load()))
+ withContext(Dispatchers.Main) {
+ // Rebuilds the cached settings the engine reads next round, and the
+ // speech engines; the skin and services follow settingsChangeNotifier.
+ refreshSettings()
+ appendTranscript(
+ MessageKind.TOOL,
+ "Assistant changed settings: " + plan.lines().joinToString("; ") + "."
+ )
+ }
+ ToolResult.ok(GotchaSettingsUpdate.appliedMessage(plan))
+ },
+ // Persist the ENGINE session's own data, never the viewed session's —
+ // the user may be browsing another chat while this run continues.
+ displayMessagesProvider = { _state.value.transcript },
+ agentModeProvider = { engineAgent }
+ )
+
+ private var confirmationGate: CompletableDeferred<Boolean>? = null
+ private var questionGate: CompletableDeferred<String>? = null
+ private var permissionGate: CompletableDeferred<Boolean>? = null
+ private var foregroundControlGate: CompletableDeferred<Boolean>? = null
+ private var agentJob: Job? = null
+
+ /** Next UiMessage id in [RunState.transcript]. */
+ private var nextTranscriptId = 0L
+
+ /** Agent mode of the session currently bound to [engine]. */
+ internal var engineAgent: AgentMode = AgentMode.MONITOR
+
+ /**
+ * True when the run in flight has surfaced an error bubble (LLM failure,
+ * user interruption, …). Decides whether an arriving reply gets the normal
+ * alert or the error buzz, since the engine reports both outcomes through
+ * the same `onAssistantReply` path.
+ */
+ @Volatile
+ private var runHadError = false
+
+ private val _state = MutableStateFlow(RunState())
+ val state: StateFlow<RunState> = _state.asStateFlow()
+
+ private val observers = CopyOnWriteArrayList<ChatRunObserver>()
+
+ /**
+ * Live per-session token counts. Updated on every [onTokenCount] so the
+ * drawer's per-row readout doesn't lag one round behind the running
+ * session. The persisted [ChatSession.tokenCount] on disk catches up
+ * through [AgentEngine.saveCurrentSession], so this overlay is read-first,
+ * disk-second.
+ */
+ private val _liveTokenBySession = MutableStateFlow<Map<String, Int>>(emptyMap())
+ val liveTokenBySession: StateFlow<Map<String, Int>> = _liveTokenBySession.asStateFlow()
+
+ /**
+ * Special-access markers ("special:*") the Activity should deep-link to.
+ * Runtime permissions travel as [RunState.pendingPermission] instead —
+ * they need an answer, and this is a fire-and-forget signal, dropped when
+ * no Activity is there to take it.
+ */
+ private val _permissionRequests = MutableSharedFlow<String>(extraBufferCapacity = 4)
+ val permissionRequests: SharedFlow<String> = _permissionRequests.asSharedFlow()
+
+ /**
+ * Bumped when the runner adds an inbox entry of its own (an interrupted
+ * run, issue #105), so the bell re-reads its unread count: the Activity's
+ * read in onResume can come before the entry exists.
+ */
+ private val _inboxChanges = MutableStateFlow(0)
+ val inboxChanges: StateFlow<Int> = _inboxChanges.asStateFlow()
+
+ /**
+ * Once-per-process startup: the chat-directory migration, the sample
+ * seeding and the report of a run that died with the last process. Anything
+ * that loads a saved chat before the UI is up (a notification tap) waits for
+ * it, or the migration could move the chat out from under the load.
+ */
+ val startup: Job
+
+ init {
+ ScreenPerception.appContext = app
+ com.gotcha.agent.skills.SkillRegistry.init(app)
+ refreshSettings()
+ bindFresh(java.util.UUID.randomUUID().toString(), AgentMode.MONITOR)
+ startup = scope.launch {
+ migrateChatDirsIfNeeded()
+ com.gotcha.data.SampleChatSeeder.seedIfNeeded(historyRepository, settingsRepository.prefs)
+ reportInterruptedRun()
+ }
+ // Stop on the ongoing "Gotcha is working…" notification (issue #105),
+ // which works with no Activity at all.
+ scope.launch { ChatRunService.stopRequests.collect { stop() } }
+ }
+
+ fun addObserver(observer: ChatRunObserver) {
+ observers += observer
+ }
+
+ fun removeObserver(observer: ChatRunObserver) {
+ observers -= observer
+ }
+
+ private fun setState(transform: (RunState) -> RunState) {
+ val old: RunState
+ val new: RunState
+ synchronized(_state) {
+ old = _state.value
+ new = transform(old)
+ _state.value = new
+ }
+ if (old != new) observers.forEach { it.onRunStateChanged(old, new) }
+ }
+
+ /** True while a run is in flight, or about to be. */
+ val isRunning: Boolean get() = _state.value.let { it.isBusy || it.runningSessionId != null }
+
+ // ---- Binding the engine to a chat (only while idle) ----
+
+ /** Points the engine at a new, empty chat [sessionId]. */
+ internal fun bindFresh(sessionId: String, agent: AgentMode) {
+ engine.history.clear()
+ engine.sessionId = sessionId
+ engine.tokenCount = 0
+ engine.restoreTitle(null)
+ // Without this the previous chat's run summaries survive into the
+ // new session: the share card would promote the old conversation
+ // and saveCurrentSession() would persist the stale list into the
+ // new chat file.
+ engine.restoreRunSummaries(emptyList())
+ engine.sessionIsSample = false
+ // A new chat never inherits the previous one's persona: it is picked
+ // per chat, on the home screen this call is about to show.
+ engine.sessionPersonaId = null
+ engine.setupWorkingDir(create = false)
+ engineAgent = agent
+ bound(sessionId, emptyList(), 0)
+ }
+
+ /** Points the engine at the saved chat [session], shown as [transcript]. */
+ internal fun bindSaved(session: ChatSession, agent: AgentMode, transcript: List<UiMessage>) {
+ engine.history.clear()
+ engine.history.addAll(session.messages)
+ engine.sessionId = session.id
+ engine.tokenCount = session.tokenCount
+ engine.restoreTitle(if (session.isFallbackTitle()) null else session.title)
+ engine.restoreRunSummaries(session.runSummaries)
+ engine.sessionIsSample = session.isSample
+ engine.sessionPersonaId = session.personaId
+ engine.setupWorkingDir()
+ engineAgent = agent
+ bound(session.id, transcript, session.tokenCount)
+ }
+
+ private fun bound(sessionId: String, transcript: List<UiMessage>, tokens: Int) {
+ nextTranscriptId = nextIdAfter(transcript)
+ setState {
+ it.copy(
+ boundSessionId = sessionId,
+ transcript = transcript,
+ tokenCount = tokens,
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
+ )
+ }
+ }
+
+ /**
+ * Point [engine] at [view] (the session being viewed) so a run operates on
+ * it. Only called while nothing else runs, so re-pointing the engine here
+ * is safe. Reloads the session's LLM history from disk when the engine had
+ * drifted to another session.
+ */
+ internal suspend fun bindForSend(view: ViewedChat) {
+ val viewedId = view.sessionId
+ if (viewedId != null && engine.sessionId != viewedId) {
+ val saved = historyRepository.loadSession(viewedId)
+ engine.history.clear()
+ engine.sessionId = viewedId
+ if (saved != null) {
+ engine.history.addAll(saved.messages)
+ engine.tokenCount = saved.tokenCount
+ engine.restoreTitle(if (saved.isFallbackTitle()) null else saved.title)
+ engine.restoreRunSummaries(saved.runSummaries)
+ engine.sessionIsSample = saved.isSample
+ } else {
+ engine.tokenCount = 0
+ engine.restoreTitle(null)
+ engine.restoreRunSummaries(emptyList())
+ engine.sessionIsSample = false
+ }
+ engine.setupWorkingDir()
+ }
+ engineAgent = view.agent
+ engine.sessionPersonaId = view.personaId
+ bound(engine.sessionId ?: return, view.messages, engine.tokenCount)
+ }
+
+ /**
+ * Adds a bubble to the bound chat's transcript: the agent's output, or a
+ * notice from the screen while the bound chat is the one being viewed.
+ */
+ internal fun appendTranscript(
+ kind: MessageKind,
+ text: String,
+ imageBase64: String? = null,
+ attachments: List<ComposerAttachment> = emptyList(),
+ subAgentSteps: List<String> = emptyList(),
+ reasoningContent: String? = null
+ ) {
+ if (kind == MessageKind.ERROR) runHadError = true
+ setState {
+ val message = UiMessage(
+ id = nextTranscriptId++,
+ kind = kind,
+ text = text,
+ imageBase64 = imageBase64,
+ subAgentSteps = subAgentSteps,
+ subAgentCollapsed = true,
+ reasoningContent = reasoningContent,
+ attachments = attachments
+ )
+ it.copy(transcript = it.transcript + message)
+ }
+ }
+
+ /** Drops [sessionId]'s live token count, e.g. once the chat is deleted. */
+ internal fun forgetLiveTokens(sessionId: String) {
+ _liveTokenBySession.update { it - sessionId }
+ }
+
+ // ---- Runs ----
+
+ /** Sends [text] with [attachments] in [view] and runs the agent on it. */
+ internal fun send(view: ViewedChat, text: String, attachments: List<ComposerAttachment>, isVoice: Boolean) {
+ currentRunIsVoice = isVoice
+ agentJob = scope.launch {
+ // Ensure the engine is bound to the session being viewed. After a
+ // previous run finished while the user browsed elsewhere, the engine
+ // may still point at that older session — reload the viewed one.
+ bindForSend(view)
+ val msg = buildUserMessage(text, attachments)
+ engine.history += msg
+ appendTranscript(MessageKind.USER, userDisplayText(text, msg, attachments), attachments = attachments)
+ val runningId = engine.sessionId ?: return@launch
+ executeRun(engineAgent, runningId)
+ }
+ }
+
+ /**
+ * Replaces the user message [targetId] in [view] with [text] and
+ * [attachments] (null keeps the original ones), drops everything after it
+ * and runs the agent again.
+ */
+ internal fun edit(view: ViewedChat, targetId: Long, text: String, attachments: List<ComposerAttachment>?) {
+ currentRunIsVoice = false
+ // Cancel any in-flight edit/run before overwriting the reference, so a
+ // rapid second tap can't leave two coroutines truncating the same history.
+ agentJob?.cancel()
+ agentJob = scope.launch {
+ // Re-check the busy guard inside the coroutine: a second invocation can
+ // slip past the caller's synchronous check (isBusy only becomes true once
+ // executeRun runs), so refuse rather than interleave two truncations.
+ if (isRunning) return@launch
+ bindForSend(view)
+ val transcript = _state.value.transcript
+ val target = transcript.firstOrNull { it.id == targetId && it.kind == MessageKind.USER }
+ ?: return@launch
+ val k = transcript.takeWhile { it.id != targetId }.count { it.kind == MessageKind.USER }
+ // History/transcript desync guard (e.g. after compaction the transcript
+ // is reset), so a stale target id bails without partial truncation.
+ if (k >= userTurnStarts(engine.history).size) return@launch
+ val kept = truncateHistoryAtTurn(engine.history, k, dropTurn = true)
+ engine.history.clear()
+ engine.history.addAll(kept)
+ setState {
+ it.copy(
+ transcript = transcript.take(transcript.indexOf(target)),
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
+ )
+ }
+ // Never promote undone work on the share card.
+ engine.restoreRunSummaries(emptyList())
+ // A previously-sent document keeps its extracted text (the file grant is
+ // long gone); a newly-picked one carries its own.
+ val editAttachments = attachments ?: target.attachments
+ val msg = buildUserMessage(text, editAttachments)
+ engine.history += msg
+ appendTranscript(
+ MessageKind.USER,
+ userDisplayText(text, msg, editAttachments),
+ attachments = editAttachments
+ )
+ executeRun(engineAgent, engine.sessionId ?: return@launch)
+ }
+ }
+
+ /**
+ * Truncates [view] so the user message [targetId] becomes the last message.
+ * Returns false when there was nothing to revert.
+ */
+ internal suspend fun revertTo(view: ViewedChat, targetId: Long): Boolean {
+ // Cancel any in-flight edit first so a revert can't interleave with a
+ // coroutine that is mid-truncation on the same engine history.
+ agentJob?.cancel()
+ if (isRunning) return false
+ bindForSend(view)
+ val transcript = _state.value.transcript
+ val target = transcript.firstOrNull { it.id == targetId && it.kind == MessageKind.USER }
+ ?: return false
+ val k = transcript.takeWhile { it.id != targetId }.count { it.kind == MessageKind.USER }
+ // History/transcript desync guard (e.g. after compaction the transcript
+ // is reset), so a stale target id bails without partial truncation.
+ if (k >= userTurnStarts(engine.history).size) return false
+ val kept = truncateHistoryAtTurn(engine.history, k, dropTurn = false)
+ engine.history.clear()
+ engine.history.addAll(kept)
+ // Re-derive the context readout from the truncated history, matching
+ // the engine's own estimate units so the meter doesn't lie after revert.
+ val tokens = kept.sumOf { it.textContent.length / 4 } + AgentEngine.PROMPT_OVERHEAD_TOKENS
+ engine.tokenCount = tokens
+ // Keep the drawer's live overlay in sync so the reverted session's
+ // row doesn't keep showing the pre-truncation count.
+ engine.sessionId?.let { sid -> _liveTokenBySession.update { it + (sid to tokens) } }
+ setState {
+ it.copy(
+ transcript = transcript.take(transcript.indexOf(target) + 1),
+ tokenCount = tokens,
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
+ )
+ }
+ // Never promote undone work on the share card.
+ engine.restoreRunSummaries(emptyList())
+ engine.saveCurrentSession()
+ return true
+ }
+
+ fun stop() {
+ agentJob?.cancel()
+ }
+
+ /** Busy-marking + agent run + NonCancellable cleanup. */
+ private suspend fun executeRun(agent: AgentMode, runningId: String) {
+ val runningTitle = _state.value.transcript.firstOrNull { it.kind == MessageKind.USER }
+ ?.text?.take(30) ?: "New Chat"
+ setState {
+ it.copy(
+ isBusy = true,
+ runningSessionId = runningId,
+ runningSessionTitle = runningTitle,
+ backgroundHint = currentBackgroundHint(),
+ foregroundControlStatus = null
+ )
+ }
+ // Issue #105: hold a foreground slot for the run, so Android keeps the
+ // process alive if the user leaves mid-task. Started here, while the user
+ // who just sent the message is still in Gotcha.
+ ChatRunService.start(app, runningChat(runningId))
+ // Survives the process: if Android kills it mid-run, the next start says so.
+ withContext(Dispatchers.IO) { runMarker.mark(runningId) }
+ runHadError = false
+ var stopped = false
+ try {
+ engine.run(agent)
+ } catch (_: CancellationException) {
+ stopped = true
+ appendTranscript(MessageKind.ERROR, "Agent was interrupted by the user.")
+ // The interrupt may have orphaned an assistant with tool_calls but no
+ // matching tool results. Repair it in NonCancellable before the next
+ // turn is built — otherwise the provider 400s every later request.
+ withContext(NonCancellable) {
+ engine.sanitizeLastOrphanedAssistant()
+ }
+ } finally {
+ withContext(NonCancellable) {
+ currentRunIsVoice = false
+ // Belt-and-suspenders: an interrupt that slipped past the engine's
+ // own sanitize still gets repaired here before persisting.
+ engine.sanitizeLastOrphanedAssistant()
+ engine.saveCurrentSession()
+ notifyRunFinished(
+ sessionId = runningId,
+ outcome = when {
+ stopped -> RunOutcome.STOPPED
+ runHadError -> RunOutcome.FAILED
+ else -> RunOutcome.DONE
+ }
+ )
+ // After the finished notification is up, so in the background the
+ // ongoing one is replaced by it rather than leaving a gap.
+ ChatRunService.stop()
+ withContext(Dispatchers.IO) { runMarker.clear() }
+ setState {
+ it.copy(
+ isBusy = false,
+ runningSessionId = null,
+ runningSessionTitle = null,
+ backgroundHint = null,
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
+ )
+ }
+ agentJob = null
+ }
+ }
+ }
+
+ // ---- Screen → runner ----
+
+ /**
+ * From the Activity's onStart/onStop, through the ViewModel. [recreating] is
+ * an onStop for a rotation or other configuration change, which is back
+ * within a moment and should not raise a notification.
+ */
+ fun setForeground(foreground: Boolean, recreating: Boolean = false) {
+ appInForeground = foreground
+ // A question or confirmation left on screen is out of sight now; back
+ // in the app, its dialog is showing again.
+ when {
+ foreground -> attentionNotifier.cancel()
+ !recreating -> notifyNeedsInput()
+ }
+ }
+
+ /**
+ * The chat screen is gone for good (its ViewModel was cleared) while the run
+ * may go on. A runtime-permission dialog needs that screen, so an ask still
+ * waiting is answered "not granted" now rather than at its timeout. A
+ * question or confirmation keeps waiting: its notification reopens Gotcha.
+ */
+ fun onUiDetached() {
+ setState { it.copy(pendingPermission = null) }
+ permissionGate?.complete(false)
+ permissionGate = null
+ }
+
+ fun confirmPendingActions(approved: Boolean) {
+ setState { it.copy(pendingConfirmation = null) }
+ confirmationGate?.complete(approved)
+ confirmationGate = null
+ }
+
+ fun submitAnswer(answer: String?) {
+ setState { it.copy(pendingQuestion = null) }
+ questionGate?.complete(answer ?: "")
+ questionGate = null
+ }
+
+ /** The in-app dialog's answer to [awaitForegroundControl]. */
+ fun answerForegroundControl(allowed: Boolean) {
+ setState { it.copy(pendingForegroundControl = null) }
+ foregroundControlGate?.complete(allowed)
+ foregroundControlGate = null
+ }
+
+ /** The Activity's answer to [awaitPermissionGrant]: the system dialog's outcome. */
+ fun onPermissionResult(granted: Boolean) {
+ setState { it.copy(pendingPermission = null) }
+ permissionGate?.complete(granted)
+ permissionGate = null
+ }
+
+ /** A notification-permission grant mid-run: the hint on screen can now promise one. */
+ fun refreshBackgroundHint() {
+ setState { if (it.backgroundHint != null) it.copy(backgroundHint = currentBackgroundHint()) else it }
+ }
+
+ /** The screen switched chats: a reply to a voice-started run is no longer read aloud. */
+ fun clearVoiceFlag() {
+ currentRunIsVoice = false
+ }
+
+ // ---- Settings / speech ----
+
+ /** Re-reads settings; call after the settings screen saves. */
+ fun refreshSettings() {
+ settings = settingsRepository.load()
+ client = if (settings.isConfigured) {
+ LLMClient(
+ apiKey = settings.effectiveApiKey,
+ baseUrl = settings.effectiveBaseUrl,
+ model = settings.model,
+ context = app,
+ apiTimeoutSeconds = settings.apiTimeoutSeconds,
+ onUnauthorized = { onSamosaUnauthorized() }
+ )
+ } else {
+ null
+ }
+ ttsEngine.configureApi(settings.effectiveTtsBaseUrl, settings.effectiveTtsApiKey)
+ setState { it.copy(isConfigured = settings.isConfigured) }
+ observers.forEach { it.onSettingsChanged() }
+ }
+
+ /**
+ * On a 401 while using Samosa AI, the JWT is expired/blacklisted: drop it so
+ * the app returns to the unauthenticated state and prompts sign-in again.
+ */
+ fun onSamosaUnauthorized() {
+ val usingSamosa = settings.provider == LlmProvider.SAMOSA_AI
+ if (!usingSamosa) return
+ settingsRepository.clearSamosaSession()
+ settings = settingsRepository.load()
+ client = null
+ setState { it.copy(isConfigured = false) }
+ }
+
+ /** Speak the given text aloud using the configured TTS provider. */
+ fun speak(text: String) {
+ if (settings.ttsProvider == AudioProvider.NONE) return
+ scope.launch {
+ ttsEngine.stop()
+ setState { it.copy(isSpeaking = true) }
+ try {
+ val language = settings.effectiveVoiceLanguage
+ val defaultVoice = ttsModels
+ .firstOrNull { it.id == settings.ttsApiModel }
+ ?.defaultVoiceFor(language) ?: "af_heart"
+ val voice = settings.ttsVoice.ifBlank { defaultVoice }
+ ttsEngine.speak(
+ text = text,
+ provider = settings.ttsProvider,
+ apiModel = settings.ttsApiModel,
+ voice = voice,
+ language = language
+ )
+ } finally {
+ setState { it.copy(isSpeaking = false) }
+ }
+ }
+ }
+
+ /** Stop any ongoing TTS speech output. */
+ fun stopSpeaking() {
+ ttsEngine.stop()
+ setState { it.copy(isSpeaking = false) }
+ }
+
+ // ---- AgentEvents (engine → screen) ----
+
+ override fun onUi(
+ kind: MessageKind,
+ text: String,
+ imageBase64: String?,
+ subAgentSteps: List<String>,
+ reasoningContent: String?
+ ) {
+ appendTranscript(
+ kind = kind,
+ text = text,
+ imageBase64 = imageBase64,
+ subAgentSteps = subAgentSteps,
+ reasoningContent = reasoningContent
+ )
+ }
+
+ override fun onActivity(activity: String?) {
+ setState { it.copy(activity = activity) }
+ // Once per round: picks up the chat's title when the first save generates it.
+ _state.value.runningSessionId?.let { ChatRunService.update(runningChat(it)) }
+ }
+
+ override fun onTokenCount(totalTokens: Int) {
+ val engineId = engine.sessionId ?: return
+ // Publish live to the drawer so the running session's row updates in
+ // the same frame, without waiting for the disk save at end-of-round.
+ _liveTokenBySession.update { it + (engineId to totalTokens) }
+ setState { it.copy(tokenCount = totalTokens) }
+ // Best-effort disk write so a crash mid-run doesn't lose the count.
+ scope.launch { engine.saveCurrentSession() }
+ }
+
+ override fun onAssistantReply(text: String) {
+ signalReplyArrived()
+ val shouldRead = currentRunIsVoice ||
+ (settings.autoReadReplies && settings.ttsProvider != AudioProvider.NONE)
+ if (shouldRead && settings.ttsProvider != AudioProvider.NONE) {
+ speak(text)
+ }
+ currentRunIsVoice = false
+ }
+
+ override fun onSubAgentUpdate(running: String?, currentAction: String?) {
+ setState { it.copy(subAgentRunning = running, subAgentCurrentAction = currentAction) }
+ }
+
+ override fun onPermissionRequest(marker: String) {
+ _permissionRequests.tryEmit(marker)
+ }
+
+ /**
+ * A tool needs a runtime permission right now. The Activity collecting
+ * [RunState.pendingPermission] explains why and raises the system dialog,
+ * then answers through [onPermissionResult].
+ *
+ * A runtime dialog can only be raised by a foreground Activity, so a
+ * backgrounded run says "not granted" immediately rather than stalling the
+ * agent behind a prompt nobody can see — the tool's own error message
+ * already tells the model (and, on screen, the user) what is missing.
+ */
+ override suspend fun awaitPermissionGrant(permission: String): Boolean {
+ if (!appInForeground) return false
+ val gate = CompletableDeferred<Boolean>()
+ permissionGate = gate
+ setState { it.copy(activity = null, pendingPermission = permission) }
+
+ val granted = withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
+
+ setState { it.copy(pendingPermission = null) }
+ permissionGate = null
+ return granted
+ }
+
+ /** Compaction dropped the LLM history; clear the transcript to match. */
+ override fun onHistoryReset() {
+ nextTranscriptId = 0
+ setState { it.copy(transcript = emptyList()) }
+ }
+
+ /**
+ * Pauses the run on the agent's question. Answering often means leaving
+ * Gotcha first (to run a command in Termux, say), so the question waits
+ * [QUESTION_TIMEOUT_MS] rather than the other gates' two minutes, and a
+ * notification says so whenever Gotcha is out of sight (issue #108).
+ */
+ override suspend fun awaitQuestionAnswer(question: PendingQuestion): String {
+ val gate = CompletableDeferred<String>()
+ questionGate = gate
+ setState { it.copy(activity = null, pendingQuestion = question) }
+ if (!appInForeground) notifyNeedsInput()
+ return try {
+ withTimeoutOrNull(QUESTION_TIMEOUT_MS) { gate.await() } ?: ""
+ } finally {
+ attentionNotifier.cancel()
+ setState { it.copy(pendingQuestion = null) }
+ questionGate = null
+ }
+ }
+
+ override suspend fun awaitConfirmation(toolNames: List<String>, description: String): Boolean {
+ val gate = CompletableDeferred<Boolean>()
+ confirmationGate = gate
+ setState {
+ it.copy(activity = null, pendingConfirmation = PendingConfirmation(toolNames, description))
+ }
+ if (!appInForeground) notifyNeedsInput()
+ return try {
+ withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
+ } finally {
+ attentionNotifier.cancel()
+ confirmationOverlay.dismiss()
+ setState { it.copy(pendingConfirmation = null) }
+ confirmationGate = null
+ }
+ }
+
+ /**
+ * The once-per-request ask before Gotcha controls another app (issue #98).
+ * Out of the app the in-app dialog can't be seen, so the same question is
+ * also drawn over whatever is on screen; either answer counts.
+ */
+ override suspend fun awaitForegroundControl(request: ForegroundControlRequest): Boolean {
+ val gate = CompletableDeferred<Boolean>()
+ foregroundControlGate = gate
+ setState { it.copy(activity = null, pendingForegroundControl = request) }
+ if (!appInForeground && confirmationOverlay.canShow()) {
+ confirmationOverlay.show(
+ summary = request.promptText(),
+ onAllow = { gate.complete(true) },
+ onDeny = { gate.complete(false) },
+ title = request.title,
+ allowLabel = ForegroundControlRequest.ALLOW_LABEL,
+ denyLabel = ForegroundControlRequest.DENY_LABEL
+ )
+ }
+ return try {
+ withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
+ } finally {
+ confirmationOverlay.dismiss()
+ setState { it.copy(pendingForegroundControl = null) }
+ foregroundControlGate = null
+ }
+ }
+
+ override fun onForegroundControlChanged(active: Boolean, appLabel: String?) {
+ if (active) {
+ val text = ForegroundControlRequest.controllingMessage(appLabel)
+ setState { it.copy(foregroundControlStatus = text) }
+ foregroundControlIndicator.showControlling(text)
+ } else {
+ setState { it.copy(foregroundControlStatus = ForegroundControlRequest.DONE_MESSAGE) }
+ // In the app the status line says it; outside, the card over their app does.
+ if (appInForeground) {
+ foregroundControlIndicator.dismiss()
+ } else {
+ foregroundControlIndicator.showDone(ForegroundControlRequest.DONE_MESSAGE)
+ }
+ }
+ }
+
+ override fun onScreenCaptureChrome(hide: Boolean) {
+ foregroundControlIndicator.setCaptureHidden(hide)
+ }
+
+ // ---- Notifications ----
+
+ /**
+ * Tells the user, out of the app, that the run is paused on a question or
+ * confirmation they can't see (issue #108). The foreground-control ask
+ * needs none: it is drawn over whatever app is in front.
+ */
+ private fun notifyNeedsInput() {
+ val state = _state.value
+ val kind = when {
+ state.pendingQuestion != null -> AttentionKind.QUESTION
+ state.pendingConfirmation != null -> AttentionKind.CONFIRMATION
+ else -> return
+ }
+ val sessionId = state.runningSessionId ?: engine.sessionId ?: return
+ // Issue #100: a chat kept out of notifications is not named, nor is its question.
+ val named = mayNameChat(sessionId)
+ attentionNotifier.notify(
+ sessionId = sessionId,
+ kind = kind,
+ chatTitle = engine.currentTitle(),
+ question = state.pendingQuestion?.question,
+ preview = settings.chatCompletionPreview,
+ named = named
+ )
+ }
+
+ /** Issue #100: whether a notification about the engine's chat [sessionId] may name it. */
+ private fun mayNameChat(sessionId: String): Boolean =
+ settings.notificationsMentionChats &&
+ !localNotificationStore.isChatSensitive(sessionId, engine.sessionPersonaId)
+
+ /**
+ * The run in [sessionId] as its ongoing notification shows it: the chat's
+ * title once one is generated, until then the start of the first message,
+ * cut at a word.
+ */
+ private fun runningChat(sessionId: String): RunningChat {
+ val title = engine.generatedTitle
+ ?: engine.history.firstOrNull { it.role == "user" }?.textContent?.let(::shortTitle)
+ return RunningChat(sessionId, title.takeIf { mayNameChat(sessionId) })
+ }
+
+ private fun currentBackgroundHint(): String = backgroundHintText(
+ vibrate = settings.notifyVibrationEnabled,
+ chime = settings.notifyChimeEnabled,
+ notify = settings.chatCompletionNotificationsEnabled && completionNotifier.canPost()
+ )
+
+ /**
+ * Buzz/chime as a reply lands. The chat screen has no spoken "I'm done"
+ * unless auto-read is on, so without this a reply that arrives while the
+ * user is elsewhere goes unnoticed.
+ */
+ private fun signalReplyArrived() {
+ if (runHadError) {
+ CompletionFeedback.error(app)
+ } else {
+ CompletionFeedback.replyArrived(
+ context = app,
+ vibrate = settings.notifyVibrationEnabled,
+ chime = settings.notifyChimeEnabled
+ )
+ }
+ }
+
+ /**
+ * The run in [sessionId] just ended. Posts the task-finished notification
+ * when the user is away from Gotcha; in the foreground the reply buzz is
+ * enough. Called once per run, from [executeRun]'s cleanup, so a run never
+ * produces two — the engine reports text mid-run too, which is why this is
+ * not driven by [onAssistantReply].
+ */
+ private fun notifyRunFinished(sessionId: String, outcome: RunOutcome) {
+ if (appInForeground || !settings.chatCompletionNotificationsEnabled) return
+ if (!completionNotifier.canPost()) return
+ val reply = _state.value.transcript.lastOrNull {
+ it.kind == MessageKind.ASSISTANT || it.kind == MessageKind.ERROR
+ }?.text
+ // Issue #100: a chat kept out of notifications, or chats not to be named
+ // at all, get a notification that says only that a task finished.
+ val named = mayNameChat(sessionId)
+ val title = if (named) {
+ ChatCompletionNotifier.notificationTitle(engine.currentTitle(), outcome)
+ } else {
+ ChatCompletionNotifier.anonymousTitle(outcome)
+ }
+ val entryId = localNotificationStore.addEntry(
+ category = NotificationCategory.TASK_FINISHED,
+ title = title,
+ body = ChatCompletionNotifier.defaultBody(outcome),
+ target = NotificationTarget.Chat(sessionId)
+ )
+ completionNotifier.notify(
+ sessionId = sessionId,
+ chatTitle = engine.currentTitle(),
+ outcome = outcome,
+ reply = reply,
+ preview = settings.chatCompletionPreview,
+ named = named,
+ inboxEntryId = entryId
+ )
+ }
+
+ // ---- Startup ----
+
+ /**
+ * Issue #105: the last run died with the process, most likely killed by
+ * Android while Gotcha was in the background. Its chat gets a notice at the
+ * end, its history is repaired so the chat can go on, and the inbox gets an
+ * entry that opens it. Gotcha still starts on a fresh chat, as always.
+ */
+ private suspend fun reportInterruptedRun() {
+ val sessionId = runMarker.interruptedSession() ?: return
+ historyRepository.loadSession(sessionId)?.let { session ->
+ // A legacy chat without a saved transcript is rebuilt from its history on
+ // open; a lone notice would replace all of it.
+ val shown = if (session.displayMessages.isEmpty()) {
+ session.displayMessages
+ } else {
+ val nextId = session.displayMessages.maxOf { it.id } + 1
+ session.displayMessages + UiMessage(nextId, MessageKind.ERROR, INTERRUPTED_NOTICE)
+ }
+ historyRepository.saveSession(
+ session.copy(messages = closeOrphanedToolCalls(session.messages), displayMessages = shown),
+ touch = false
+ )
+ val named = settings.notificationsMentionChats &&
+ !localNotificationStore.isChatSensitive(sessionId, session.personaId)
+ localNotificationStore.addEntry(
+ category = NotificationCategory.TASK_FINISHED,
+ title = if (named) "Interrupted: ${session.title}" else "Task interrupted",
+ body = INTERRUPTED_INBOX_BODY,
+ target = NotificationTarget.Chat(sessionId)
+ )
+ _inboxChanges.update { it + 1 }
+ }
+ withContext(Dispatchers.IO) { runMarker.clear() }
+ }
+
+ /**
+ * One-time migration: rename any pre-existing UUID-named chat working dirs
+ * to the readable "Slug_shortId" scheme. The "done" flag is only persisted
+ * once every session was handled, so a partial migration (a rename blocked by
+ * a missing storage grant, say) is retried on the next launch instead of
+ * leaving UUID-named dirs stranded forever.
+ */
+ private suspend fun migrateChatDirsIfNeeded() {
+ val prefs = settingsRepository.prefs
+ if (prefs.getBoolean(MIGRATED_CHAT_DIRS_KEY, false)) return
+ val sessions = historyRepository.listSessions()
+ val chatsRoot = com.gotcha.data.GotchaStorage.chatsRoot()
+ var allMigrated = true
+ for (session in sessions) {
+ try {
+ val rawDir = java.io.File(chatsRoot, session.id)
+ if (!rawDir.exists() || !rawDir.isDirectory) continue
+ val target = java.io.File(
+ chatsRoot,
+ com.gotcha.data.GotchaStorage.chatDirName(session.title, session.id)
+ )
+ // An already-migrated target is not a failure: keep the raw dir
+ // out of the way rather than clobbering the renamed one.
+ if (!rawDir.renameTo(target) && !target.isDirectory) {
+ allMigrated = false
+ android.util.Log.w("Gotcha", "migrateChatDirsIfNeeded: rename failed for ${session.id}")
+ }
+ } catch (e: Exception) {
+ allMigrated = false
+ android.util.Log.w("Gotcha", "migrateChatDirsIfNeeded: failed for ${session.id}", e)
+ }
+ }
+ if (allMigrated) prefs.edit().putBoolean(MIGRATED_CHAT_DIRS_KEY, true).apply()
+ }
+
+ companion object {
+ const val GATE_TIMEOUT_MS = 120_000L
+
+ /** How long a question waits; see [awaitQuestionAnswer]. */
+ const val QUESTION_TIMEOUT_MS = 600_000L
+
+ private const val MIGRATED_CHAT_DIRS_KEY = "migrated_chat_dirs_v1"
+
+ /** Ends the chat of a run that died with the process; see [reportInterruptedRun]. */
+ private const val INTERRUPTED_NOTICE = "This task was interrupted: Android closed Gotcha while it was " +
+ "working, usually to free memory. Send a message to pick up where it left off."
+ private const val INTERRUPTED_INBOX_BODY = "Android closed Gotcha while it was working on this task. " +
+ "Tap to open the chat and pick up where it left off."
+
+ @Volatile
+ private var fallback: ChatRunner? = null
+
+ /** The process's runner: [GotchaApp]'s, or one of its own under another Application. */
+ fun of(app: Application): ChatRunner =
+ (app as? GotchaApp)?.chatRunner ?: fallback ?: synchronized(this) {
+ fallback ?: ChatRunner(app).also { fallback = it }
+ }
+ }
+}
+
+/** The next free id after [messages]. */
+private fun nextIdAfter(messages: List<UiMessage>): Long = messages.maxOfOrNull { it.id }?.plus(1) ?: 0L
+
+/**
+ * True when [ChatSession.title] is still the legacy truncated-first-message
+ * fallback rather than an LLM-generated title, so it's eligible to be
+ * (re)generated next time the session is saved.
+ */
+internal fun ChatSession.isFallbackTitle(): Boolean {
+ val fallback = messages.firstOrNull { it.role == "user" }?.textContent?.take(30)
+ return title.isBlank() || title == fallback
+}
+
+/**
+ * Builds the LLM user message for the given prompt and attachments: every
+ * document's text joins the prompt in the first text part, and every image
+ * follows as its own image part.
+ */
+internal fun buildUserMessage(text: String, attachments: List<ComposerAttachment>): ChatMessage {
+ if (attachments.isEmpty()) return ChatMessage(role = "user", content = JsonPrimitive(text))
+ val documents = attachments.filterIsInstance<ComposerAttachment.Document>().map {
+ DocumentPart(it.attachment.name, it.attachment.mimeType, it.attachment.text, it.attachment.pageCount)
+ }
+ val images = attachments.filterIsInstance<ComposerAttachment.Image>().map { it.base64 }
+ return attachmentsUserMessage(text, documents, images, imageFormat = "jpeg")
+}
+
+/**
+ * The transcript label for a sent message: the prompt text, or a placeholder
+ * when the message is attachment-only. Messages with documents show the
+ * prompt rather than the extracted bodies; image-only messages show a
+ * placeholder, never the whitespace text part sent to the model.
+ */
+internal fun userDisplayText(userText: String, msg: ChatMessage, attachments: List<ComposerAttachment>): String =
+ when {
+ attachments.isEmpty() -> msg.textContent
+ attachments.none { it is ComposerAttachment.Document } -> userText.ifEmpty {
+ if (attachments.size == 1) "(image attached)" else "(files attached)"
+ }
+ else -> userText.ifEmpty {
+ if (attachments.size == 1) "(document attached)" else "(files attached)"
+ }
+ }
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 281255e..6f7126e 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -11,55 +11,33 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.gotcha.audio.AudioModel
import com.gotcha.audio.AudioProvider
-import com.gotcha.audio.CompletionFeedback
import com.gotcha.audio.SttEngine
-import com.gotcha.audio.TtsEngine
import com.gotcha.data.ChatArchive
-import com.gotcha.data.ChatHistoryRepository
import com.gotcha.data.ChatImporter
import com.gotcha.data.ChatMarkdown
import com.gotcha.data.ChatSession
import com.gotcha.data.DuplicateStrategy
import com.gotcha.data.ImportParseResult
-import com.gotcha.data.LlmProvider
import com.gotcha.data.RunSummary
import com.gotcha.data.Settings
-import com.gotcha.data.SettingsRepository
import com.gotcha.data.documentPromptText
import com.gotcha.i18n.Language
import com.gotcha.i18n.SpokenPhrases
import com.gotcha.llm.ChatMessage
-import com.gotcha.llm.DocumentPart
import com.gotcha.llm.LLMClient
-import com.gotcha.llm.attachmentsUserMessage
import com.gotcha.marketing.PosterRenderer
import com.gotcha.marketing.PosterStatsBuilder
import com.gotcha.marketing.ShareCardClient
-import com.gotcha.notifications.AttentionKind
-import com.gotcha.notifications.AttentionNotifier
import com.gotcha.notifications.ChatCompletionNotifier
import com.gotcha.notifications.LocalNotificationStore
-import com.gotcha.notifications.NotificationCategory
-import com.gotcha.notifications.NotificationTarget
-import com.gotcha.notifications.RunOutcome
-import com.gotcha.service.ChatRunService
-import com.gotcha.service.RunningChat
import com.gotcha.tools.AgentMode
import com.gotcha.tools.DocumentError
import com.gotcha.tools.DocumentParser
-import com.gotcha.tools.GotchaSettingsUpdate
-import com.gotcha.tools.ScreenPerception
-import com.gotcha.tools.ToolResult
-import com.gotcha.tools.mergeProfileUpdate
-import com.gotcha.ui.ConfirmationOverlay
-import com.gotcha.ui.ForegroundControlIndicator
import com.gotcha.ui.Persona
import com.gotcha.util.HumanReadableError
import kotlinx.coroutines.CancellationException
-import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
-import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
@@ -69,7 +47,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
-import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.json.JsonPrimitive
/**
@@ -264,147 +241,49 @@ data class ChatUiState(
// In-app chat host: session/UI state, dialogs, and TTS/STT wiring. The agent
// loop itself lives in AgentEngine and reports back through AgentEvents.
+/**
+ * The chat screen's state. The agent and the run in progress belong to the
+ * app-scoped [ChatRunner] (issue #111), so a run outlives this ViewModel when
+ * the Activity finishes; this binds the runner to the chat being viewed, starts
+ * runs on it and mirrors its [RunState] into [uiState].
+ */
@Suppress("TooManyFunctions", "LargeClass")
-class ChatViewModel(application: Application) : AndroidViewModel(application), AgentEvents {
+class ChatViewModel(application: Application) : AndroidViewModel(application), ChatRunObserver {
+
+ /** `internal` so tests can drive the engine's side of a run directly. */
+ internal val runner = ChatRunner.of(application)
- private val settingsRepository = SettingsRepository(application)
- private val historyRepository = ChatHistoryRepository(application)
- private val confirmationOverlay = ConfirmationOverlay(application)
- private val foregroundControlIndicator = ForegroundControlIndicator(application)
+ private val settingsRepository = runner.settingsRepository
+ private val historyRepository = runner.historyRepository
private val completionNotifier = ChatCompletionNotifier(application)
- private val attentionNotifier = AttentionNotifier(application)
private val localNotificationStore = LocalNotificationStore(application)
- private val runMarker = RunInProgressMarker(settingsRepository.prefs)
- private var settings: Settings = Settings()
- private var client: LLMClient? = null
+ private val settings: Settings get() = runner.settings
+ private val client: LLMClient? get() = runner.client
/** True when the most recent user message was sent via voice (STT). */
@Volatile
private var lastInputWasVoice = false
- /** True when the active LLM run was initiated by voice dictation. */
- @Volatile
- private var currentRunIsVoice = false
-
- private val ttsEngine: TtsEngine = TtsEngine(
- getApplication(),
- settings.effectiveTtsBaseUrl,
- settings.effectiveTtsApiKey,
- onUnauthorized = { viewModelScope.launch { onSamosaUnauthorized() } }
- )
private val sttEngine: SttEngine = SttEngine(
getApplication(),
settings.effectiveSttBaseUrl,
settings.effectiveSttApiKey,
- onUnauthorized = { viewModelScope.launch { onSamosaUnauthorized() } }
- )
-
- /** Set by the Activity in onStart/onStop; drives whether confirmations use the overlay. */
- @Volatile
- private var appInForeground = true
-
- private val agentEngine = AgentEngine(
- appContext = application,
- events = this,
- historyRepository = historyRepository,
- settingsProvider = { settings },
- clientProvider = { client },
- onUpdateUserProfile = { update ->
- // Reload so a manual Personal Info edit isn't clobbered by a stale snapshot,
- // then persist and refresh the cached settings the engine reads next turn.
- val merged = mergeProfileUpdate(settingsRepository.load(), update)
- ?: return@AgentEngine ToolResult.ok("No material change — profile left as is.")
- settingsRepository.save(merged.updated)
- settings = merged.updated
- // Surface the change to the user: the profile is silently re-injected into every
- // future prompt, so a prompt-injected update_user_profile call must not go
- // unnoticed. A TOOL bubble in the transcript gives the user a chance to catch
- // and revert a poisoned update. Dispatched to Main because this handler runs
- // inside the tool executor's IO context while appendEngineUi touches UI state.
- withContext(Dispatchers.Main) {
- appendEngineUi(
- MessageKind.TOOL,
- "Assistant updated your profile: " +
- merged.changedFields.joinToString(", ") + "."
- )
- }
- ToolResult.ok(
- "Updated " + merged.changedFields.joinToString(", ") + ". " +
- "The new value will be used from the next message."
- )
- },
- onUpdateGotchaSettings = { plan ->
- // Only reached after the user approved this exact change. Apply it onto a
- // fresh load so a concurrent write elsewhere (the Settings screen, the
- // assistive ball) is not clobbered by the snapshot the prompt was built from.
- settingsRepository.save(plan.applyTo(settingsRepository.load()))
- withContext(Dispatchers.Main) {
- // Rebuilds the cached settings the engine reads next round, and the
- // speech engines; the skin and services follow settingsChangeNotifier.
- refreshSettings()
- appendEngineUi(
- MessageKind.TOOL,
- "Assistant changed settings: " + plan.lines().joinToString("; ") + "."
- )
- }
- ToolResult.ok(GotchaSettingsUpdate.appliedMessage(plan))
- },
- // Persist the ENGINE session's own data, never the viewed session's —
- // the user may be browsing another chat while this run continues.
- displayMessagesProvider = { engineTranscript },
- agentModeProvider = { engineAgent }
+ onUnauthorized = { viewModelScope.launch { runner.onSamosaUnauthorized() } }
)
+ /** Next UiMessage id for a chat shown while the engine is bound to another one. */
private var nextId = 0L
- private var confirmationGate: CompletableDeferred<Boolean>? = null
- private var questionGate: CompletableDeferred<String>? = null
- private var permissionGate: CompletableDeferred<Boolean>? = null
- private var foregroundControlGate: CompletableDeferred<Boolean>? = null
- private var agentJob: Job? = null
-
- /**
- * Live on-screen transcript of the session currently bound to [agentEngine]
- * (the one that runs). Kept separate from [_uiState].messages so the user
- * can browse to another chat while a run continues in the background without
- * the engine's output overwriting — or being overwritten by — the viewed chat.
- */
- private var engineTranscript: List<UiMessage> = emptyList()
-
- /** Monotonic UiMessage id for engine bubbles while browsing a different chat. */
- private var engineNextId: Long = 1_000_000_000L
-
- /** Agent mode of the session currently bound to [agentEngine]. */
- private var engineAgent: AgentMode = AgentMode.MONITOR
-
- /**
- * True when the run in flight has surfaced an error bubble (LLM failure,
- * user interruption, …). Decides whether an arriving reply gets the normal
- * alert or the error buzz, since the engine reports both outcomes through
- * the same `onAssistantReply` path.
- */
- @Volatile
- private var runHadError = false
/** True when the session the user is viewing is the one bound to the engine. */
private fun viewingEngineSession(): Boolean =
- _uiState.value.activeSessionId == agentEngine.sessionId
+ _uiState.value.activeSessionId == runner.state.value.boundSessionId
/**
- * True when [ChatSession.title] is still the legacy truncated-first-message
- * fallback rather than an LLM-generated title, so it's eligible to be
- * (re)generated next time the session is saved.
- */
- private fun ChatSession.isFallbackTitle(): Boolean {
- val fallback = messages.firstOrNull { it.role == "user" }?.textContent?.take(30)
- return title.isBlank() || title == fallback
- }
-
- /**
- * Startup: the fresh session, the chat-directory migration and the sample
- * seeding. Anything that loads a saved chat on the user's behalf before the
- * UI is up (a notification tap) waits for it, or the migration could move
- * the chat out from under the load.
+ * Startup: waits for the runner's once-per-process startup (the migration,
+ * the sample seeding) before the drawer lists chats. Anything that loads a
+ * saved chat on the user's behalf before the UI is up (a notification tap)
+ * waits for it too.
*/
private val initJob: Job
@@ -414,271 +293,109 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
private val _sessions = MutableStateFlow<List<ChatSession>>(emptyList())
val sessions: StateFlow<List<ChatSession>> = _sessions.asStateFlow()
- /**
- * Live per-session token counts. Updated on every [onTokenCount] so the
- * drawer's per-row readout doesn't lag one round behind the running
- * session. The persisted [ChatSession.tokenCount] on disk catches up
- * through [saveCurrentSession], so this overlay is read-first, disk-second.
- */
- private val _liveTokenBySession = MutableStateFlow<Map<String, Int>>(emptyMap())
- val liveTokenBySession: StateFlow<Map<String, Int>> = _liveTokenBySession.asStateFlow()
+ /** See [ChatRunner.liveTokenBySession]. */
+ val liveTokenBySession: StateFlow<Map<String, Int>> = runner.liveTokenBySession
- /**
- * Special-access markers ("special:*") the Activity should deep-link to.
- * Runtime permissions travel as [ChatUiState.pendingPermission] instead —
- * they need an answer, and this is a fire-and-forget signal.
- */
- private val _permissionRequests = MutableSharedFlow<String>(extraBufferCapacity = 4)
- val permissionRequests: SharedFlow<String> = _permissionRequests.asSharedFlow()
+ /** See [ChatRunner.permissionRequests]. */
+ val permissionRequests: SharedFlow<String> = runner.permissionRequests
- /** Exported chat markdown content the Activity should share. */
- /**
- * Bumped when the view model adds an inbox entry of its own (an interrupted
- * run, issue #105), so the bell re-reads its unread count: the Activity's
- * read in onResume can come before the entry exists.
- */
- private val _inboxChanges = MutableStateFlow(0)
- val inboxChanges: StateFlow<Int> = _inboxChanges.asStateFlow()
+ /** See [ChatRunner.inboxChanges]. */
+ val inboxChanges: StateFlow<Int> = runner.inboxChanges
+ /** Exported chat markdown content the Activity should share. */
private val _exportContent = MutableSharedFlow<String>(extraBufferCapacity = 2)
val exportContent: SharedFlow<String> = _exportContent.asSharedFlow()
init {
- ScreenPerception.appContext = application
- com.gotcha.agent.skills.SkillRegistry.init(application)
- refreshSettings()
- initJob = viewModelScope.launch {
+ runner.addObserver(this)
+ onSettingsChanged()
+ val run = runner.state.value
+ if (runner.isRunning && run.boundSessionId != null) {
+ // Issue #111: the run outlived the screen that started it; show it.
+ showBoundSession(run.boundSessionId)
+ } else {
// Always start on a fresh session so the home screen greets with an
// empty chat; past sessions remain one tap away in the drawer.
- agentEngine.sessionId = java.util.UUID.randomUUID().toString()
- agentEngine.tokenCount = 0
- agentEngine.restoreTitle(null)
- agentEngine.setupWorkingDir(create = false)
- _uiState.update { it.copy(activeSessionId = agentEngine.sessionId) }
- updateContextUsage()
- migrateChatDirsIfNeeded()
- com.gotcha.data.SampleChatSeeder.seedIfNeeded(historyRepository, settingsRepository.prefs)
- reportInterruptedRun()
- refreshSessions()
+ val newId = java.util.UUID.randomUUID().toString()
+ runner.bindFresh(newId, AgentMode.MONITOR)
+ _uiState.update { it.copy(activeSessionId = newId) }
}
- // Stop on the ongoing "Gotcha is working…" notification (issue #105).
- viewModelScope.launch { ChatRunService.stopRequests.collect { stopAgent() } }
- }
-
- // ---- AgentEvents (engine → UI) ----
-
- override fun onUi(
- kind: MessageKind,
- text: String,
- imageBase64: String?,
- subAgentSteps: List<String>,
- reasoningContent: String?
- ) {
- appendEngineUi(
- kind = kind,
- text = text,
- imageBase64 = imageBase64,
- subAgentSteps = subAgentSteps,
- reasoningContent = reasoningContent
- )
- }
-
- override fun onActivity(activity: String?) {
- if (viewingEngineSession()) {
- _uiState.update { it.copy(activity = activity) }
- }
- // Once per round: picks up the chat's title when the first save generates it.
- _uiState.value.runningSessionId?.let { ChatRunService.update(runningChat(it)) }
- }
-
- override fun onTokenCount(totalTokens: Int) {
- val engineId = agentEngine.sessionId ?: return
- // Publish live to the drawer so the running session's row updates in
- // the same frame, without waiting for the disk save at end-of-round.
- _liveTokenBySession.update { it + (engineId to totalTokens) }
- // Pass the new count explicitly: updateContextUsage() re-applies the
- // count already on screen, so the meter would never move mid-run (#71).
- if (viewingEngineSession()) applyContextUsage(totalTokens)
- // Best-effort disk write so a crash mid-run doesn't lose the count.
- viewModelScope.launch { agentEngine.saveCurrentSession() }
- }
-
- override fun onAssistantReply(text: String) {
- signalReplyArrived()
- val shouldRead = lastInputWasVoice || currentRunIsVoice ||
- (settings.autoReadReplies && settings.ttsProvider != AudioProvider.NONE)
- if (shouldRead && settings.ttsProvider != AudioProvider.NONE) {
- speak(text)
- }
- lastInputWasVoice = false
- currentRunIsVoice = false
- }
-
- override fun onSubAgentUpdate(running: String?, currentAction: String?) {
- if (viewingEngineSession()) {
- _uiState.update { it.copy(subAgentRunning = running, subAgentCurrentAction = currentAction) }
+ applyRunState(runner.state.value, initial = true)
+ updateContextUsage()
+ initJob = viewModelScope.launch {
+ runner.startup.join()
+ refreshSessions()
}
}
- override fun onPermissionRequest(marker: String) {
- _permissionRequests.tryEmit(marker)
- }
-
- /**
- * A tool needs a runtime permission right now. The Activity collecting
- * [permissionRequests] explains why and raises the system dialog, then
- * answers through [onPermissionResult].
- *
- * A runtime dialog can only be raised by a foreground Activity, so a
- * backgrounded run says "not granted" immediately rather than stalling the
- * agent behind a prompt nobody can see — the tool's own error message
- * already tells the model (and, on screen, the user) what is missing.
- */
- override suspend fun awaitPermissionGrant(permission: String): Boolean {
- if (!appInForeground) return false
- val gate = CompletableDeferred<Boolean>()
- permissionGate = gate
- _uiState.update { it.copy(activity = null, pendingPermission = permission) }
-
- val granted = withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
-
- _uiState.update { it.copy(pendingPermission = null) }
- permissionGate = null
- return granted
- }
-
- /** The Activity's answer to [awaitPermissionGrant]: the system dialog's outcome. */
- fun onPermissionResult(granted: Boolean) {
- _uiState.update { it.copy(pendingPermission = null) }
- permissionGate?.complete(granted)
- permissionGate = null
- }
+ // ---- ChatRunObserver (runner → screen) ----
- /** Compaction dropped the LLM history; clear the engine transcript to match. */
- override fun onHistoryReset() {
- engineTranscript = emptyList()
- if (viewingEngineSession()) {
- nextId = 0
- _uiState.update { it.copy(messages = emptyList()) }
- }
+ override fun onRunStateChanged(old: RunState, new: RunState) {
+ applyRunState(new, old)
}
/**
- * Pauses the run on the agent's question. Answering often means leaving
- * Gotcha first (to run a command in Termux, say), so the question waits
- * [QUESTION_TIMEOUT_MS] rather than the other gates' two minutes, and a
- * notification says so whenever Gotcha is out of sight (issue #108).
+ * Mirrors [run] into [uiState]. The run and its gates show whichever chat
+ * is viewed; the bound chat's transcript, activity and token count only
+ * while it is the one on screen.
*/
- override suspend fun awaitQuestionAnswer(question: PendingQuestion): String {
- val gate = CompletableDeferred<String>()
- questionGate = gate
- _uiState.update { it.copy(activity = null, pendingQuestion = question) }
- if (!appInForeground) notifyNeedsInput()
- return try {
- withTimeoutOrNull(QUESTION_TIMEOUT_MS) { gate.await() } ?: ""
- } finally {
- attentionNotifier.cancel()
- _uiState.update { it.copy(pendingQuestion = null) }
- questionGate = null
- }
- }
-
- override suspend fun awaitConfirmation(toolNames: List<String>, description: String): Boolean {
- val gate = CompletableDeferred<Boolean>()
- confirmationGate = gate
+ private fun applyRunState(run: RunState, old: RunState? = null, initial: Boolean = false) {
+ val viewing = _uiState.value.activeSessionId == run.boundSessionId
_uiState.update {
- it.copy(activity = null, pendingConfirmation = PendingConfirmation(toolNames, description))
- }
- if (!appInForeground) notifyNeedsInput()
- return try {
- withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
- } finally {
- attentionNotifier.cancel()
- confirmationOverlay.dismiss()
- _uiState.update { it.copy(pendingConfirmation = null) }
- confirmationGate = null
+ it.copy(
+ isBusy = run.isBusy,
+ runningSessionId = run.runningSessionId,
+ runningSessionTitle = run.runningSessionTitle,
+ backgroundHint = run.backgroundHint,
+ pendingConfirmation = run.pendingConfirmation,
+ pendingForegroundControl = run.pendingForegroundControl,
+ foregroundControlStatus = run.foregroundControlStatus,
+ pendingQuestion = run.pendingQuestion,
+ pendingPermission = run.pendingPermission,
+ isConfigured = run.isConfigured,
+ isSpeaking = run.isSpeaking,
+ messages = if (viewing && (initial || old?.transcript !== run.transcript)) {
+ run.transcript
+ } else {
+ it.messages
+ },
+ activity = if (viewing) run.activity else it.activity,
+ subAgentRunning = if (viewing) run.subAgentRunning else it.subAgentRunning,
+ subAgentCurrentAction = if (viewing) run.subAgentCurrentAction else it.subAgentCurrentAction
+ )
}
+ // Pass the new count explicitly: updateContextUsage() re-applies the
+ // count already on screen, so the meter would never move mid-run (#71).
+ if (viewing && old != null && old.tokenCount != run.tokenCount) applyContextUsage(run.tokenCount)
}
- /**
- * Tells the user, out of the app, that the run is paused on a question or
- * confirmation they can't see (issue #108). The foreground-control ask
- * needs none: it is drawn over whatever app is in front.
- */
- private fun notifyNeedsInput() {
- val state = _uiState.value
- val kind = when {
- state.pendingQuestion != null -> AttentionKind.QUESTION
- state.pendingConfirmation != null -> AttentionKind.CONFIRMATION
- else -> return
- }
- val sessionId = state.runningSessionId ?: agentEngine.sessionId ?: return
- // Issue #100: a chat kept out of notifications is not named, nor is its question.
- val named = mayNameChat(sessionId)
- attentionNotifier.notify(
- sessionId = sessionId,
- kind = kind,
- chatTitle = agentEngine.currentTitle(),
- question = state.pendingQuestion?.question,
- preview = settings.chatCompletionPreview,
- named = named
- )
+ override fun onSettingsChanged() {
+ sttEngine.configureApi(settings.effectiveSttBaseUrl, settings.effectiveSttApiKey)
+ updateContextUsage()
}
- /**
- * The once-per-request ask before Gotcha controls another app (issue #98).
- * Out of the app the in-app dialog can't be seen, so the same question is
- * also drawn over whatever is on screen; either answer counts.
- */
- override suspend fun awaitForegroundControl(request: ForegroundControlRequest): Boolean {
- val gate = CompletableDeferred<Boolean>()
- foregroundControlGate = gate
- _uiState.update { it.copy(activity = null, pendingForegroundControl = request) }
- if (!appInForeground && confirmationOverlay.canShow()) {
- confirmationOverlay.show(
- summary = request.promptText(),
- onAllow = { gate.complete(true) },
- onDeny = { gate.complete(false) },
- title = request.title,
- allowLabel = ForegroundControlRequest.ALLOW_LABEL,
- denyLabel = ForegroundControlRequest.DENY_LABEL
+ /** Shows the chat bound to the engine, live: the running one, typically. */
+ private fun showBoundSession(id: String) {
+ val run = runner.state.value
+ _uiState.update {
+ it.copy(
+ activeSessionId = id,
+ activeAgent = runner.engineAgent,
+ activePersonaId = runner.engine.sessionPersonaId,
+ messages = run.transcript,
+ viewingSample = _sessions.value.firstOrNull { s -> s.id == id }?.isSample
+ ?: runner.engine.sessionIsSample,
+ activity = run.activity,
+ subAgentRunning = run.subAgentRunning,
+ subAgentCurrentAction = run.subAgentCurrentAction
)
}
- return try {
- withTimeoutOrNull(GATE_TIMEOUT_MS) { gate.await() } ?: false
- } finally {
- confirmationOverlay.dismiss()
- _uiState.update { it.copy(pendingForegroundControl = null) }
- foregroundControlGate = null
- }
+ applyContextUsage(runner.engine.tokenCount)
}
- /** The in-app dialog's answer to [awaitForegroundControl]. */
- fun answerForegroundControl(allowed: Boolean) {
- _uiState.update { it.copy(pendingForegroundControl = null) }
- foregroundControlGate?.complete(allowed)
- foregroundControlGate = null
- }
+ fun onPermissionResult(granted: Boolean) = runner.onPermissionResult(granted)
- override fun onForegroundControlChanged(active: Boolean, appLabel: String?) {
- if (active) {
- val text = ForegroundControlRequest.controllingMessage(appLabel)
- _uiState.update { it.copy(foregroundControlStatus = text) }
- foregroundControlIndicator.showControlling(text)
- } else {
- _uiState.update { it.copy(foregroundControlStatus = ForegroundControlRequest.DONE_MESSAGE) }
- // In the app the status line says it; outside, the card over their app does.
- if (appInForeground) {
- foregroundControlIndicator.dismiss()
- } else {
- foregroundControlIndicator.showDone(ForegroundControlRequest.DONE_MESSAGE)
- }
- }
- }
-
- override fun onScreenCaptureChrome(hide: Boolean) {
- foregroundControlIndicator.setCaptureHidden(hide)
- }
+ fun answerForegroundControl(allowed: Boolean) = runner.answerForegroundControl(allowed)
// ---- Settings / models ----
@@ -702,38 +419,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}
/** Re-reads settings; call after the settings screen saves. */
- fun refreshSettings() {
- settings = settingsRepository.load()
- client = if (settings.isConfigured) {
- LLMClient(
- apiKey = settings.effectiveApiKey,
- baseUrl = settings.effectiveBaseUrl,
- model = settings.model,
- context = getApplication(),
- apiTimeoutSeconds = settings.apiTimeoutSeconds,
- onUnauthorized = { onSamosaUnauthorized() }
- )
- } else {
- null
- }
- ttsEngine.configureApi(settings.effectiveTtsBaseUrl, settings.effectiveTtsApiKey)
- sttEngine.configureApi(settings.effectiveSttBaseUrl, settings.effectiveSttApiKey)
- _uiState.update { it.copy(isConfigured = settings.isConfigured) }
- updateContextUsage()
- }
-
- /**
- * On a 401 while using Samosa AI, the JWT is expired/blacklisted: drop it so
- * the app returns to the unauthenticated state and prompts sign-in again.
- */
- private fun onSamosaUnauthorized() {
- val usingSamosa = settings.provider == LlmProvider.SAMOSA_AI
- if (!usingSamosa) return
- settingsRepository.clearSamosaSession()
- settings = settingsRepository.load()
- client = null
- _uiState.update { it.copy(isConfigured = false) }
- }
+ fun refreshSettings() = runner.refreshSettings()
suspend fun refreshChatModels(): Result<List<String>> {
val cfg = settings
@@ -762,234 +448,22 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
appendUi(MessageKind.ERROR, "No API key configured. Open settings to add one.")
return
}
- val msg = buildUserMessage(trimmed, attachments)
- launchUserRun(msg, attachments, trimmed, isVoiceInput)
- }
-
- /**
- * Builds the LLM user message for the given prompt and attachments: every
- * document's text joins the prompt in the first text part, and every image
- * follows as its own image part.
- */
- private fun buildUserMessage(text: String, attachments: List<ComposerAttachment>): ChatMessage {
- if (attachments.isEmpty()) return ChatMessage(role = "user", content = JsonPrimitive(text))
- val documents = attachments.filterIsInstance<ComposerAttachment.Document>().map {
- DocumentPart(it.attachment.name, it.attachment.mimeType, it.attachment.text, it.attachment.pageCount)
- }
- val images = attachments.filterIsInstance<ComposerAttachment.Image>().map { it.base64 }
- return attachmentsUserMessage(text, documents, images, imageFormat = "jpeg")
- }
-
- /**
- * Appends [msg] to the viewed session's LLM history, shows the USER bubble,
- * and starts an agent run. Shared by [sendMessage] and [editMessage], so both
- * paths go through the same busy-marking, run, and NonCancellable cleanup.
- */
- private fun launchUserRun(
- msg: ChatMessage,
- attachments: List<ComposerAttachment>,
- userText: String,
- isVoiceInput: Boolean
- ) {
- currentRunIsVoice = isVoiceInput || lastInputWasVoice
+ val isVoice = isVoiceInput || lastInputWasVoice
lastInputWasVoice = false
- val viewedId = _uiState.value.activeSessionId
- agentJob = viewModelScope.launch {
- // Ensure the engine is bound to the session being viewed. After a
- // previous run finished while the user browsed elsewhere, the engine
- // may still point at that older session — reload the viewed one.
- bindEngineToViewedSession(viewedId)
-
- agentEngine.history += msg
- appendEngineUi(
- MessageKind.USER,
- userDisplayText(userText, msg, attachments),
- attachments = attachments
- )
-
- val runningId = agentEngine.sessionId ?: return@launch
- executeRun(engineAgent, runningId)
- }
- }
-
- /**
- * The transcript label for a sent message: the prompt text, or a placeholder
- * when the message is attachment-only. Messages with documents show the
- * prompt rather than the extracted bodies; image-only messages show a
- * placeholder, never the whitespace text part sent to the model.
- */
- private fun userDisplayText(userText: String, msg: ChatMessage, attachments: List<ComposerAttachment>): String =
- when {
- attachments.isEmpty() -> msg.textContent
- attachments.none { it is ComposerAttachment.Document } -> userText.ifEmpty {
- if (attachments.size == 1) "(image attached)" else "(files attached)"
- }
- else -> userText.ifEmpty {
- if (attachments.size == 1) "(document attached)" else "(files attached)"
- }
- }
-
- /** Busy-marking + agent run + NonCancellable cleanup, from the old sendMessage body. */
- private suspend fun executeRun(agent: AgentMode, runningId: String) {
- val runningTitle = engineTranscript.firstOrNull { it.kind == MessageKind.USER }
- ?.text?.take(30) ?: "New Chat"
- _uiState.update {
- it.copy(
- isBusy = true,
- runningSessionId = runningId,
- runningSessionTitle = runningTitle,
- backgroundHint = currentBackgroundHint(),
- foregroundControlStatus = null,
- askNotificationPermission = it.askNotificationPermission || claimNotificationPermissionAsk()
- )
- }
- // Issue #105: hold a foreground slot for the run, so Android keeps the
- // process alive if the user leaves mid-task. Started here, while the user
- // who just sent the message is still in Gotcha.
- ChatRunService.start(getApplication(), runningChat(runningId))
- // Survives the process: if Android kills it mid-run, the next start says so.
- withContext(Dispatchers.IO) { runMarker.mark(runningId) }
- runHadError = false
- var stopped = false
- try {
- agentEngine.run(agent)
- } catch (_: CancellationException) {
- stopped = true
- appendEngineUi(MessageKind.ERROR, "Agent was interrupted by the user.")
- // The interrupt may have orphaned an assistant with tool_calls but no
- // matching tool results. Repair it in NonCancellable before the next
- // turn is built — otherwise the provider 400s every later request.
- withContext(NonCancellable) {
- agentEngine.sanitizeLastOrphanedAssistant()
- }
- } finally {
- withContext(NonCancellable) {
- currentRunIsVoice = false
- lastInputWasVoice = false
- // Belt-and-suspenders: an interrupt that slipped past the engine's
- // own sanitize still gets repaired here before persisting.
- agentEngine.sanitizeLastOrphanedAssistant()
- agentEngine.saveCurrentSession()
- notifyRunFinished(
- sessionId = runningId,
- outcome = when {
- stopped -> RunOutcome.STOPPED
- runHadError -> RunOutcome.FAILED
- else -> RunOutcome.DONE
- }
- )
- // After the finished notification is up, so in the background the
- // ongoing one is replaced by it rather than leaving a gap.
- ChatRunService.stop()
- withContext(Dispatchers.IO) { runMarker.clear() }
- _uiState.update {
- it.copy(
- isBusy = false,
- runningSessionId = null,
- runningSessionTitle = null,
- backgroundHint = null,
- activity = if (viewingEngineSession()) null else it.activity,
- subAgentRunning = if (viewingEngineSession()) null else it.subAgentRunning,
- subAgentCurrentAction = if (viewingEngineSession()) null else it.subAgentCurrentAction
- )
- }
- agentJob = null
- }
- }
+ claimNotificationPermissionAskForRun()
+ runner.send(viewedChat(), trimmed, attachments, isVoice)
}
- /** Issue #100: whether a notification about the engine's chat [sessionId] may name it. */
- private fun mayNameChat(sessionId: String): Boolean =
- settings.notificationsMentionChats &&
- !localNotificationStore.isChatSensitive(sessionId, agentEngine.sessionPersonaId)
-
- /**
- * Issue #105: the last run died with the process, most likely killed by
- * Android while Gotcha was in the background. Its chat gets a notice at the
- * end, its history is repaired so the chat can go on, and the inbox gets an
- * entry that opens it. Gotcha still starts on a fresh chat, as always.
- */
- private suspend fun reportInterruptedRun() {
- val sessionId = runMarker.interruptedSession() ?: return
- historyRepository.loadSession(sessionId)?.let { session ->
- // A legacy chat without a saved transcript is rebuilt from its history on
- // open; a lone notice would replace all of it.
- val shown = if (session.displayMessages.isEmpty()) {
- session.displayMessages
- } else {
- val nextId = session.displayMessages.maxOf { it.id } + 1
- session.displayMessages + UiMessage(nextId, MessageKind.ERROR, INTERRUPTED_NOTICE)
- }
- historyRepository.saveSession(
- session.copy(messages = closeOrphanedToolCalls(session.messages), displayMessages = shown),
- touch = false
- )
- val named = settings.notificationsMentionChats &&
- !localNotificationStore.isChatSensitive(sessionId, session.personaId)
- localNotificationStore.addEntry(
- category = NotificationCategory.TASK_FINISHED,
- title = if (named) "Interrupted: ${session.title}" else "Task interrupted",
- body = INTERRUPTED_INBOX_BODY,
- target = NotificationTarget.Chat(sessionId)
- )
- _inboxChanges.update { it + 1 }
- }
- withContext(Dispatchers.IO) { runMarker.clear() }
+ /** What a run started now binds the engine to. */
+ private fun viewedChat(): ViewedChat = _uiState.value.let {
+ ViewedChat(it.activeSessionId, it.messages, it.activeAgent, it.activePersonaId)
}
- /**
- * The run in [sessionId] as its ongoing notification shows it: the chat's
- * title once one is generated, until then the start of the first message,
- * cut at a word.
- */
- private fun runningChat(sessionId: String): RunningChat {
- val title = agentEngine.generatedTitle
- ?: agentEngine.history.firstOrNull { it.role == "user" }?.textContent?.let(::shortTitle)
- return RunningChat(sessionId, title.takeIf { mayNameChat(sessionId) })
- }
-
- private fun currentBackgroundHint(): String = backgroundHintText(
- vibrate = settings.notifyVibrationEnabled,
- chime = settings.notifyChimeEnabled,
- notify = settings.chatCompletionNotificationsEnabled && completionNotifier.canPost()
- )
-
- /**
- * The run in [sessionId] just ended. Posts the task-finished notification
- * when the user is away from Gotcha; in the foreground the reply buzz is
- * enough. Called once per run, from [executeRun]'s cleanup, so a run never
- * produces two — the engine reports text mid-run too, which is why this is
- * not driven by [onAssistantReply].
- */
- private fun notifyRunFinished(sessionId: String, outcome: RunOutcome) {
- if (appInForeground || !settings.chatCompletionNotificationsEnabled) return
- if (!completionNotifier.canPost()) return
- val reply = engineTranscript.lastOrNull {
- it.kind == MessageKind.ASSISTANT || it.kind == MessageKind.ERROR
- }?.text
- // Issue #100: a chat kept out of notifications, or chats not to be named
- // at all, get a notification that says only that a task finished.
- val named = mayNameChat(sessionId)
- val title = if (named) {
- ChatCompletionNotifier.notificationTitle(agentEngine.currentTitle(), outcome)
- } else {
- ChatCompletionNotifier.anonymousTitle(outcome)
+ /** Raises the one-time notification-permission ask as a run starts, if it is due. */
+ private fun claimNotificationPermissionAskForRun() {
+ _uiState.update {
+ it.copy(askNotificationPermission = it.askNotificationPermission || claimNotificationPermissionAsk())
}
- val entryId = localNotificationStore.addEntry(
- category = NotificationCategory.TASK_FINISHED,
- title = title,
- body = ChatCompletionNotifier.defaultBody(outcome),
- target = NotificationTarget.Chat(sessionId)
- )
- completionNotifier.notify(
- sessionId = sessionId,
- chatTitle = agentEngine.currentTitle(),
- outcome = outcome,
- reply = reply,
- preview = settings.chatCompletionPreview,
- named = named,
- inboxEntryId = entryId
- )
}
/**
@@ -999,7 +473,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
* "Not now" is final — the Notifications settings page can still ask.
*/
private fun claimNotificationPermissionAsk(): Boolean {
- if (!appInForeground || !settings.chatCompletionNotificationsEnabled) return false
+ if (!runner.appInForeground || !settings.chatCompletionNotificationsEnabled) return false
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return false
if (completionNotifier.canPost()) return false
val prefs = settingsRepository.prefs
@@ -1013,12 +487,8 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
* upgrades the hint on screen to the notification promise it can now keep.
*/
fun onNotificationPermissionResult(granted: Boolean) {
- _uiState.update {
- it.copy(
- askNotificationPermission = false,
- backgroundHint = if (granted && it.backgroundHint != null) currentBackgroundHint() else it.backgroundHint
- )
- }
+ _uiState.update { it.copy(askNotificationPermission = false) }
+ if (granted) runner.refreshBackgroundHint()
}
/**
@@ -1082,51 +552,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
appendUi(MessageKind.ERROR, "No API key configured. Open settings to add one.")
return
}
- currentRunIsVoice = false
lastInputWasVoice = false
- val viewedId = _uiState.value.activeSessionId
- // Cancel any in-flight edit/run before overwriting the reference, so a
- // rapid second tap can't leave two coroutines truncating the same history.
- agentJob?.cancel()
- agentJob = viewModelScope.launch {
- // Re-check the busy guard inside the coroutine: a second invocation can
- // slip past the synchronous check above (isBusy only becomes true once
- // executeRun runs), so refuse rather than interleave two truncations.
- if (_uiState.value.isBusy || _uiState.value.runningSessionId != null) return@launch
- bindEngineToViewedSession(viewedId)
- val transcript = engineTranscript
- val target = transcript.firstOrNull { it.id == targetId && it.kind == MessageKind.USER }
- ?: return@launch
- val k = transcript.takeWhile { it.id != targetId }.count { it.kind == MessageKind.USER }
- // History/transcript desync guard (e.g. after compaction the transcript
- // is reset), so a stale target id bails without partial truncation.
- if (k >= userTurnStarts(agentEngine.history).size) return@launch
- val kept = truncateHistoryAtTurn(agentEngine.history, k, dropTurn = true)
- agentEngine.history.clear()
- agentEngine.history.addAll(kept)
- engineTranscript = transcript.take(transcript.indexOf(target))
- _uiState.update {
- it.copy(
- messages = engineTranscript,
- activity = null,
- subAgentRunning = null,
- subAgentCurrentAction = null
- )
- }
- // Never promote undone work on the share card.
- agentEngine.restoreRunSummaries(emptyList())
- // A previously-sent document keeps its extracted text (the file grant is
- // long gone); a newly-picked one carries its own.
- val editAttachments = attachments ?: target.attachments
- val msg = buildUserMessage(trimmed, editAttachments)
- agentEngine.history += msg
- appendEngineUi(
- MessageKind.USER,
- userDisplayText(trimmed, msg, editAttachments),
- attachments = editAttachments
- )
- executeRun(engineAgent, agentEngine.sessionId ?: return@launch)
- }
+ claimNotificationPermissionAskForRun()
+ runner.edit(viewedChat(), targetId, trimmed, attachments)
}
/**
@@ -1137,101 +565,22 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
*/
fun revertTo(targetId: Long) {
if (_uiState.value.isBusy || _uiState.value.runningSessionId != null) return
- val viewedId = _uiState.value.activeSessionId
- // Cancel any in-flight edit/run first so a revert can't interleave with a
- // coroutine that is mid-truncation on the same engine history.
- agentJob?.cancel()
+ val view = viewedChat()
viewModelScope.launch {
- // Re-check the busy guard inside the coroutine (mirrors editMessage):
- // the synchronous check above can be raced by a second dispatch before
- // isBusy is set, so refuse rather than truncate concurrently.
- if (_uiState.value.isBusy || _uiState.value.runningSessionId != null) return@launch
- bindEngineToViewedSession(viewedId)
- val transcript = engineTranscript
- val target = transcript.firstOrNull { it.id == targetId && it.kind == MessageKind.USER }
- ?: return@launch
- val k = transcript.takeWhile { it.id != targetId }.count { it.kind == MessageKind.USER }
- // History/transcript desync guard (e.g. after compaction the transcript
- // is reset), so a stale target id bails without partial truncation.
- if (k >= userTurnStarts(agentEngine.history).size) return@launch
- val kept = truncateHistoryAtTurn(agentEngine.history, k, dropTurn = false)
- agentEngine.history.clear()
- agentEngine.history.addAll(kept)
- engineTranscript = transcript.take(transcript.indexOf(target) + 1)
- _uiState.update {
- it.copy(
- messages = engineTranscript,
- activity = null,
- subAgentRunning = null,
- subAgentCurrentAction = null
- )
- }
- // Re-derive the context readout from the truncated history, matching
- // the engine's own estimate units so the meter doesn't lie after revert.
- val tokens = kept.sumOf { it.textContent.length / 4 } + AgentEngine.PROMPT_OVERHEAD_TOKENS
- agentEngine.tokenCount = tokens
- applyContextUsage(tokens)
- // Keep the drawer's live overlay in sync so the reverted session's
- // row doesn't keep showing the pre-truncation count.
- agentEngine.sessionId?.let { sid ->
- _liveTokenBySession.update { it + (sid to tokens) }
- }
- // Never promote undone work on the share card.
- agentEngine.restoreRunSummaries(emptyList())
- agentEngine.saveCurrentSession()
- refreshSessions()
- }
- }
-
- /**
- * Buzz/chime as a reply lands. The chat screen has no spoken "I'm done"
- * unless auto-read is on, so without this a reply that arrives while the
- * user is elsewhere goes unnoticed.
- */
- private fun signalReplyArrived() {
- if (runHadError) {
- CompletionFeedback.error(getApplication())
- } else {
- CompletionFeedback.replyArrived(
- context = getApplication(),
- vibrate = settings.notifyVibrationEnabled,
- chime = settings.notifyChimeEnabled
- )
+ if (runner.revertTo(view, targetId)) refreshSessions()
}
}
/**
- * Point [agentEngine] at [viewedId] (the session being viewed) so a run
- * operates on it. Only called from [sendMessage], which is gated on nothing
- * else running, so re-pointing the engine here is safe. Reloads the session's
- * LLM history from disk when the engine had drifted to another session.
+ * Point the engine at [viewedId] (the session being viewed) so a run
+ * operates on it; see [ChatRunner.bindForSend].
*
- * `internal` rather than private so the handoff can be tested directly: it is
- * where a persona picked while another chat was running finally reaches the
- * engine, and [sendMessage] itself can't be driven from a JVM test.
+ * `internal` so the handoff can be tested directly: it is where a persona
+ * picked while another chat was running finally reaches the engine, and
+ * [sendMessage] itself can't be driven from a JVM test.
*/
internal suspend fun bindEngineToViewedSession(viewedId: String?) {
- if (viewedId != null && agentEngine.sessionId != viewedId) {
- val saved = historyRepository.loadSession(viewedId)
- agentEngine.history.clear()
- agentEngine.sessionId = viewedId
- if (saved != null) {
- agentEngine.history.addAll(saved.messages)
- agentEngine.tokenCount = saved.tokenCount
- agentEngine.restoreTitle(if (saved.isFallbackTitle()) null else saved.title)
- agentEngine.restoreRunSummaries(saved.runSummaries)
- agentEngine.sessionIsSample = saved.isSample
- } else {
- agentEngine.tokenCount = 0
- agentEngine.restoreTitle(null)
- agentEngine.restoreRunSummaries(emptyList())
- agentEngine.sessionIsSample = false
- }
- agentEngine.setupWorkingDir()
- }
- engineTranscript = _uiState.value.messages
- engineAgent = _uiState.value.activeAgent
- agentEngine.sessionPersonaId = _uiState.value.activePersonaId
+ runner.bindForSend(viewedChat().copy(sessionId = viewedId))
}
/** Load an image from a content:// URI, downscale, and return base64. */
@@ -1384,21 +733,11 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}?.takeIf { it.isNotBlank() } ?: fallback
}
- fun stopAgent() {
- agentJob?.cancel()
- }
+ fun stopAgent() = runner.stop()
- fun confirmPendingActions(approved: Boolean) {
- _uiState.update { it.copy(pendingConfirmation = null) }
- confirmationGate?.complete(approved)
- confirmationGate = null
- }
+ fun confirmPendingActions(approved: Boolean) = runner.confirmPendingActions(approved)
- fun submitAnswer(answer: String?) {
- _uiState.update { it.copy(pendingQuestion = null) }
- questionGate?.complete(answer ?: "")
- questionGate = null
- }
+ fun submitAnswer(answer: String?) = runner.submitAnswer(answer)
/**
* Called from the Activity's onStart/onStop so confirmations know if they'd be hidden.
@@ -1406,48 +745,17 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
* back within a moment and should not raise a notification.
*/
fun setForeground(foreground: Boolean, recreating: Boolean = false) {
- appInForeground = foreground
+ runner.setForeground(foreground, recreating)
// Back in the app on a chat whose task finished: that chat's
// notification has done its job.
if (foreground) _uiState.value.activeSessionId?.let(completionNotifier::cancel)
- // A question or confirmation left on screen is out of sight now; back
- // in the app, its dialog is showing again.
- when {
- foreground -> attentionNotifier.cancel()
- !recreating -> notifyNeedsInput()
- }
}
/** Speak the given text aloud using the configured TTS provider. */
- fun speak(text: String) {
- if (settings.ttsProvider == AudioProvider.NONE) return
- viewModelScope.launch {
- ttsEngine.stop()
- _uiState.update { it.copy(isSpeaking = true) }
- try {
- val language = settings.effectiveVoiceLanguage
- val defaultVoice = _uiState.value.ttsModels
- .firstOrNull { it.id == settings.ttsApiModel }
- ?.defaultVoiceFor(language) ?: "af_heart"
- val voice = settings.ttsVoice.ifBlank { defaultVoice }
- ttsEngine.speak(
- text = text,
- provider = settings.ttsProvider,
- apiModel = settings.ttsApiModel,
- voice = voice,
- language = language
- )
- } finally {
- _uiState.update { it.copy(isSpeaking = false) }
- }
- }
- }
+ fun speak(text: String) = runner.speak(text)
/** Stop any ongoing TTS speech output. */
- fun stopSpeaking() {
- ttsEngine.stop()
- _uiState.update { it.copy(isSpeaking = false) }
- }
+ fun stopSpeaking() = runner.stopSpeaking()
/**
* Speak [language]'s call-started phrase through the shared Android TTS
@@ -1460,6 +768,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
*/
suspend fun testAndroidTts(language: Language): Boolean? {
if (settings.ttsProvider == AudioProvider.NONE) return null
+ val ttsEngine = runner.ttsEngine
ttsEngine.stop()
ttsEngine.speak(
text = SpokenPhrases.callStarted(language),
@@ -1576,8 +885,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
/** Refresh available TTS/STT models from the API. */
fun refreshAudioModels() {
viewModelScope.launch {
- val ttsModels = ttsEngine.refreshApiModels()
+ val ttsModels = runner.ttsEngine.refreshApiModels()
val sttModels = sttEngine.refreshApiModels()
+ runner.ttsModels = ttsModels
_uiState.update { it.copy(ttsModels = ttsModels, sttModels = sttModels) }
}
}
@@ -1624,8 +934,8 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
// Only inject into the LLM history when the viewed chat is the engine's;
// otherwise the mode is applied at the next send (run(engineAgent)).
if (viewingEngineSession()) {
- agentEngine.history += ChatMessage(role = "system", content = JsonPrimitive(llmMsg))
- engineAgent = next
+ runner.engine.history += ChatMessage(role = "system", content = JsonPrimitive(llmMsg))
+ runner.engineAgent = next
}
appendUi(MessageKind.ASSISTANT, uiMsg)
}
@@ -1657,15 +967,19 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
// persona rides in UI state and reaches the engine at send time, through
// bindEngineToViewedSession.
if (_uiState.value.runningSessionId == null) {
- agentEngine.sessionPersonaId = persona?.id
+ runner.engine.sessionPersonaId = persona?.id
}
_uiState.update { it.copy(activePersonaId = persona?.id) }
persona?.let { setAgent(it.defaultAgent) }
}
+ /**
+ * The Activity is gone for good (issue #111). The run, if any, goes on in
+ * [runner]; only what needs this screen is let go.
+ */
override fun onCleared() {
- confirmationOverlay.dismiss()
- foregroundControlIndicator.dismiss()
+ runner.removeObserver(this)
+ runner.onUiDetached()
super.onCleared()
}
@@ -1679,75 +993,26 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
*/
fun clearChat(defaultAgent: AgentMode = AgentMode.MONITOR) {
lastInputWasVoice = false
- currentRunIsVoice = false
+ runner.clearVoiceFlag()
val newId = java.util.UUID.randomUUID().toString()
val runInProgress = _uiState.value.runningSessionId != null
nextId = 0
- if (!runInProgress) {
- agentEngine.history.clear()
- agentEngine.sessionId = newId
- agentEngine.tokenCount = 0
- agentEngine.restoreTitle(null)
- // Without this the previous chat's run summaries survive into the
- // new session: the share card would promote the old conversation
- // and saveCurrentSession() would persist the stale list into the
- // new chat file.
- agentEngine.restoreRunSummaries(emptyList())
- agentEngine.sessionIsSample = false
- // A new chat never inherits the previous one's persona: it is picked
- // per chat, on the home screen this call is about to show.
- agentEngine.sessionPersonaId = null
- agentEngine.setupWorkingDir(create = false)
- engineTranscript = emptyList()
- engineAgent = defaultAgent
- }
+ if (!runInProgress) runner.bindFresh(newId, defaultAgent)
_uiState.update {
it.copy(
messages = emptyList(),
activeSessionId = newId,
activeAgent = defaultAgent,
activePersonaId = null,
- viewingSample = false
+ viewingSample = false,
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
)
}
applyContextUsage(0)
}
- /**
- * One-time migration: rename any pre-existing UUID-named chat working dirs
- * to the readable "Slug_shortId" scheme. The "done" flag is only persisted
- * once every session was handled, so a partial migration (a rename blocked by
- * a missing storage grant, say) is retried on the next launch instead of
- * leaving UUID-named dirs stranded forever.
- */
- private suspend fun migrateChatDirsIfNeeded() {
- val prefs = settingsRepository.prefs
- if (prefs.getBoolean(MIGRATED_CHAT_DIRS_KEY, false)) return
- val sessions = historyRepository.listSessions()
- val chatsRoot = com.gotcha.data.GotchaStorage.chatsRoot()
- var allMigrated = true
- for (session in sessions) {
- try {
- val rawDir = java.io.File(chatsRoot, session.id)
- if (!rawDir.exists() || !rawDir.isDirectory) continue
- val target = java.io.File(
- chatsRoot,
- com.gotcha.data.GotchaStorage.chatDirName(session.title, session.id)
- )
- // An already-migrated target is not a failure: keep the raw dir
- // out of the way rather than clobbering the renamed one.
- if (!rawDir.renameTo(target) && !target.isDirectory) {
- allMigrated = false
- android.util.Log.w("Gotcha", "migrateChatDirsIfNeeded: rename failed for ${session.id}")
- }
- } catch (e: Exception) {
- allMigrated = false
- android.util.Log.w("Gotcha", "migrateChatDirsIfNeeded: failed for ${session.id}", e)
- }
- }
- if (allMigrated) prefs.edit().putBoolean(MIGRATED_CHAT_DIRS_KEY, true).apply()
- }
-
fun refreshSessions() {
viewModelScope.launch {
_sessions.value = historyRepository.listSessions()
@@ -1757,83 +1022,44 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
fun openSession(id: String?) {
id?.let(completionNotifier::cancel)
lastInputWasVoice = false
- currentRunIsVoice = false
+ runner.clearVoiceFlag()
viewModelScope.launch {
if (id == null) {
clearChat()
return@launch
}
- // Re-viewing the running session: just show its live engine transcript.
- if (id == agentEngine.sessionId && _uiState.value.runningSessionId == id) {
- nextId = (engineTranscript.maxOfOrNull { it.id }?.plus(1)) ?: 0
- _uiState.update {
- it.copy(
- activeSessionId = id,
- activeAgent = engineAgent,
- activePersonaId = agentEngine.sessionPersonaId,
- messages = engineTranscript,
- viewingSample = _sessions.value.firstOrNull { s -> s.id == id }?.isSample
- ?: agentEngine.sessionIsSample
- )
- }
- applyContextUsage(agentEngine.tokenCount)
+ // Re-viewing the running session: just show its live transcript.
+ val run = runner.state.value
+ if (id == run.boundSessionId && run.runningSessionId == id) {
+ showBoundSession(id)
return@launch
}
val session = historyRepository.loadSession(id) ?: return@launch
val restoredAgent = session.agentMode
?.let { runCatching { AgentMode.valueOf(it) }.getOrNull() }
?: AgentMode.MONITOR
- val runInProgress = _uiState.value.runningSessionId != null
- // Only rebind the engine when idle. While a run is active this is a
- // view-only switch so the running session is never disturbed.
- if (!runInProgress) {
- agentEngine.history.clear()
- agentEngine.history.addAll(session.messages)
- agentEngine.sessionId = session.id
- agentEngine.tokenCount = session.tokenCount
- agentEngine.restoreTitle(if (session.isFallbackTitle()) null else session.title)
- agentEngine.restoreRunSummaries(session.runSummaries)
- agentEngine.sessionIsSample = session.isSample
- agentEngine.sessionPersonaId = session.personaId
- agentEngine.setupWorkingDir()
- engineTranscript = session.displayMessages
- engineAgent = restoredAgent
- }
// Restore the verbatim on-screen transcript so what's shown on reopen
// matches exactly what was shown live. Fall back to a lossy rebuild
// only for legacy sessions saved before display messages existed.
- if (session.displayMessages.isNotEmpty()) {
- nextId = (session.displayMessages.maxOf { it.id } + 1)
- _uiState.update {
- it.copy(
- activeSessionId = session.id,
- activeAgent = restoredAgent,
- activePersonaId = session.personaId,
- viewingSample = session.isSample,
- messages = session.displayMessages,
- // Clear engine-scoped transient UI for the viewed (non-running) chat.
- activity = null,
- subAgentRunning = null,
- subAgentCurrentAction = null
- )
- }
- applyContextUsage(session.tokenCount)
- } else {
- nextId = 0
- _uiState.update {
- it.copy(
- activeSessionId = session.id,
- activeAgent = restoredAgent,
- activePersonaId = session.personaId,
- viewingSample = session.isSample,
- activity = null,
- subAgentRunning = null,
- subAgentCurrentAction = null
- )
- }
- applyContextUsage(session.tokenCount)
- rebuildUiMessagesFrom(session.messages)
+ val shown = session.displayMessages.ifEmpty { rebuildUiMessagesFrom(session.messages) }
+ // Only rebind the engine when idle. While a run is active this is a
+ // view-only switch so the running session is never disturbed.
+ if (_uiState.value.runningSessionId == null) runner.bindSaved(session, restoredAgent, shown)
+ nextId = shown.maxOfOrNull { it.id }?.plus(1) ?: 0
+ _uiState.update {
+ it.copy(
+ activeSessionId = session.id,
+ activeAgent = restoredAgent,
+ activePersonaId = session.personaId,
+ viewingSample = session.isSample,
+ messages = shown,
+ // Clear engine-scoped transient UI for the viewed (non-running) chat.
+ activity = null,
+ subAgentRunning = null,
+ subAgentCurrentAction = null
+ )
}
+ applyContextUsage(session.tokenCount)
}
}
@@ -1844,17 +1070,22 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}
historyRepository.deleteSession(id)
localNotificationStore.forgetChat(id)
- if (agentEngine.sessionId == id) {
+ if (runner.engine.sessionId == id) {
clearChat()
}
// Drop any live overlay entry for the deleted session so the
// drawer doesn't keep showing a token count for a chat that no
// longer exists.
- _liveTokenBySession.update { it - id }
+ runner.forgetLiveTokens(id)
refreshSessions()
}
}
+ /**
+ * Shows a notice from the screen itself. In the chat bound to the engine it
+ * joins the runner's transcript, so it is saved with the chat and outlives
+ * this ViewModel like the agent's own bubbles.
+ */
private fun appendUi(
kind: MessageKind,
text: String,
@@ -1862,6 +1093,16 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
subAgentSteps: List<String> = emptyList(),
reasoningContent: String? = null
) {
+ if (viewingEngineSession()) {
+ runner.appendTranscript(
+ kind = kind,
+ text = text,
+ imageBase64 = imageBase64,
+ subAgentSteps = subAgentSteps,
+ reasoningContent = reasoningContent
+ )
+ return
+ }
_uiState.update {
it.copy(
messages = it.messages + UiMessage(
@@ -1871,41 +1112,10 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}
}
- /**
- * Append a bubble authored by the ENGINE (agent output). Always records it on
- * [engineTranscript] so the running session's transcript stays complete even
- * while the user browses another chat; mirrors into the visible [_uiState]
- * only when that running session is the one being viewed.
- */
- private fun appendEngineUi(
- kind: MessageKind,
- text: String,
- imageBase64: String? = null,
- attachments: List<ComposerAttachment> = emptyList(),
- subAgentSteps: List<String> = emptyList(),
- reasoningContent: String? = null
- ) {
- val viewing = viewingEngineSession()
- val id = if (viewing) nextId++ else engineNextId++
- if (kind == MessageKind.ERROR) runHadError = true
- val message = UiMessage(
- id = id,
- kind = kind,
- text = text,
- imageBase64 = imageBase64,
- subAgentSteps = subAgentSteps,
- subAgentCollapsed = true,
- reasoningContent = reasoningContent,
- attachments = attachments
- )
- engineTranscript = engineTranscript + message
- if (viewing) {
- _uiState.update { it.copy(messages = it.messages + message) }
- }
- }
-
- private fun rebuildUiMessagesFrom(source: List<ChatMessage>) {
- val rebuilt = source.mapNotNull { msg ->
+ /** A legacy chat saved without its on-screen transcript, rebuilt from its history. */
+ private fun rebuildUiMessagesFrom(source: List<ChatMessage>): List<UiMessage> {
+ var rebuiltId = 0L
+ return source.mapNotNull { msg ->
val text = msg.textContent
when {
msg.role == "user" && (text.startsWith("[Screen State]") || text.startsWith("Screen text:")) -> {
@@ -1915,13 +1125,13 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
} else {
"[Screenshot captured for visual context]"
}
- UiMessage(nextId++, MessageKind.ASSISTANT, msgText)
+ UiMessage(rebuiltId++, MessageKind.ASSISTANT, msgText)
}
msg.role == "user" -> {
val text = msg.textContent
val docPrompt = documentPromptText(text)
UiMessage(
- nextId++,
+ rebuiltId++,
MessageKind.USER,
if (docPrompt != null) {
docPrompt.ifEmpty { "(document attached)" }
@@ -1931,9 +1141,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
)
}
msg.role == "assistant" && text.isNotBlank() ->
- UiMessage(nextId++, MessageKind.ASSISTANT, text, reasoningContent = msg.reasoningContent)
+ UiMessage(rebuiltId++, MessageKind.ASSISTANT, text, reasoningContent = msg.reasoningContent)
msg.role == "assistant" && !msg.reasoningContent.isNullOrBlank() ->
- UiMessage(nextId++, MessageKind.ASSISTANT, "", reasoningContent = msg.reasoningContent)
+ UiMessage(rebuiltId++, MessageKind.ASSISTANT, "", reasoningContent = msg.reasoningContent)
msg.role == "tool" && text.startsWith("SUBAGENT_STEPS:") -> {
val descEnd = text.indexOf('\n', "SUBAGENT_STEPS:".length)
val rest = if (descEnd > 0) {
@@ -1962,25 +1172,24 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
answer = rest
}
UiMessage(
- nextId++,
+ rebuiltId++,
MessageKind.SUBAGENT,
answer,
subAgentSteps = steps,
reasoningContent = msg.reasoningContent
)
}
- msg.role == "tool" -> UiMessage(nextId++, MessageKind.TOOL, text.ifEmpty { "(no result)" })
+ msg.role == "tool" -> UiMessage(rebuiltId++, MessageKind.TOOL, text.ifEmpty { "(no result)" })
else -> null
}
}
- _uiState.update { it.copy(messages = rebuilt) }
}
fun exportChat() {
val markdown = ChatMarkdown.export(
- history = agentEngine.history.toList(),
- sessionId = agentEngine.sessionId ?: "unknown",
- title = agentEngine.currentTitle()
+ history = runner.engine.history.toList(),
+ sessionId = runner.engine.sessionId ?: "unknown",
+ title = runner.engine.currentTitle()
)
_exportContent.tryEmit(markdown)
}
@@ -2153,30 +1362,17 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
* than fingerprinting the history (which would hash vision base64 payloads).
*/
fun activeSessionRunSummaries(): List<RunSummary> {
- val recorded = agentEngine.runSummaries
+ val recorded = runner.engine.runSummaries
if (recorded.isNotEmpty()) return recorded.toList()
// Snapshot the history before iterating: the engine coroutine mutates it
// as it runs, and this is read from the UI thread.
- val snapshot = agentEngine.history.toList()
- return synthesizeRunSummariesFromHistory(snapshot, settings.model, engineAgent.name)
+ val snapshot = runner.engine.history.toList()
+ return synthesizeRunSummariesFromHistory(snapshot, settings.model, runner.engineAgent.name)
}
private companion object {
- const val GATE_TIMEOUT_MS = 120_000L
-
- /** How long a question waits; see [awaitQuestionAnswer]. */
- const val QUESTION_TIMEOUT_MS = 600_000L
-
/** Set once the one-time notification-permission ask has been shown. */
const val KEY_NOTIFICATION_PERMISSION_ASKED = "chat_notification_permission_asked"
-
- const val MIGRATED_CHAT_DIRS_KEY = "migrated_chat_dirs_v1"
-
- /** Ends the chat of a run that died with the process; see [reportInterruptedRun]. */
- const val INTERRUPTED_NOTICE = "This task was interrupted: Android closed Gotcha while it was " +
- "working, usually to free memory. Send a message to pick up where it left off."
- const val INTERRUPTED_INBOX_BODY = "Android closed Gotcha while it was working on this task. " +
- "Tap to open the chat and pick up where it left off."
}
}
diff --git a/app/src/main/java/com/gotcha/agent/RunInProgressMarker.kt b/app/src/main/java/com/gotcha/agent/RunInProgressMarker.kt
index 9f7182d..a1cdc03 100644
--- a/app/src/main/java/com/gotcha/agent/RunInProgressMarker.kt
+++ b/app/src/main/java/com/gotcha/agent/RunInProgressMarker.kt
@@ -11,9 +11,9 @@ import kotlinx.serialization.json.JsonPrimitive
* with no explanation (issue #105). Written synchronously, because the process
* can die right after.
*
- * Tagged with the process that wrote it: a run whose ViewModel was cleared while
- * the process lives on is ended, and its marker cleared, by the run's own
- * cleanup, so only a marker left by another process means one was killed.
+ * Tagged with the process that wrote it: a run in this process clears its
+ * marker in its own cleanup, so only a marker left by another process means
+ * one was killed.
*/
class RunInProgressMarker(
private val prefs: SharedPreferences,
diff --git a/app/src/main/java/com/gotcha/service/ChatRunService.kt b/app/src/main/java/com/gotcha/service/ChatRunService.kt
index d40ba04..4fbc0c2 100644
--- a/app/src/main/java/com/gotcha/service/ChatRunService.kt
+++ b/app/src/main/java/com/gotcha/service/ChatRunService.kt
@@ -31,7 +31,7 @@ data class RunningChat(val sessionId: String, val chatTitle: String?)
/**
* Keeps Gotcha's process alive while a chat run is in progress (issue #105).
- * The run itself stays in `ChatViewModel`; this service only holds a foreground
+ * The run itself lives in `ChatRunner` (issue #111); this service only holds a foreground
* slot, with an ongoing "Gotcha is working…" notification, so Android does not
* reclaim the process once the user leaves the app mid-task.
*
diff --git a/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt b/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt
new file mode 100644
index 0000000..785b860
--- /dev/null
+++ b/app/src/test/java/com/gotcha/agent/ChatRunnerLifecycleTest.kt
@@ -0,0 +1,220 @@
+package com.gotcha.agent
+
+import android.Manifest
+import android.app.Application
+import android.app.NotificationManager
+import android.content.Intent
+import androidx.core.app.NotificationCompat
+import androidx.lifecycle.ViewModelProvider
+import androidx.lifecycle.ViewModelStore
+import androidx.test.core.app.ApplicationProvider
+import com.gotcha.GotchaApp
+import com.gotcha.data.ChatHistoryRepository
+import com.gotcha.data.LlmProvider
+import com.gotcha.data.Settings
+import com.gotcha.data.SettingsRepository
+import com.gotcha.service.ChatRunService
+import com.gotcha.testsupport.FakeAndroidKeyStore
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.async
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNotEquals
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertSame
+import org.junit.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Robolectric
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.Shadows.shadowOf
+import org.robolectric.annotation.Config
+import org.robolectric.shadows.ShadowLooper
+import java.net.ServerSocket
+import kotlin.concurrent.thread
+
+/**
+ * Issue #111: a chat run belongs to the app-scoped [ChatRunner], not to the
+ * ViewModel of the screen that started it, so it goes on when MainActivity
+ * finishes (swiped out of Recents, Back on the home screen) and a later screen
+ * picks it up. The LLM base URL refuses connections, so a run left alone ends
+ * as a failure; one cancelled with its ViewModel would end as "stopped".
+ */
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [34])
+class ChatRunnerLifecycleTest {
+
+ private val application: Application = ApplicationProvider.getApplicationContext()
+ private val manager = application.getSystemService(NotificationManager::class.java)
+ private val historyRepository = ChatHistoryRepository(application)
+
+ @Before
+ fun setUp() {
+ FakeAndroidKeyStore.setUp()
+ shadowOf(application).grantPermissions(Manifest.permission.POST_NOTIFICATIONS)
+ manager.cancelAll()
+ ChatRunService.stop()
+ SettingsRepository(application).save(
+ Settings(provider = LlmProvider.OPENAI_COMPATIBLE, apiKey = "test-key", baseUrl = "http://127.0.0.1:1/v1")
+ )
+ }
+
+ /** A chat screen whose ViewModel can be cleared as when its Activity finishes for good. */
+ private class Screen(application: Application) {
+ val store = ViewModelStore()
+ val viewModel: ChatViewModel = ViewModelProvider(
+ store,
+ // Not getInstance(): that factory is cached with the first test's Application.
+ ViewModelProvider.AndroidViewModelFactory(application)
+ )[ChatViewModel::class.java]
+
+ fun finish() {
+ viewModel.setForeground(false)
+ store.clear()
+ }
+ }
+
+ private fun runner() = ChatRunner.of(application)
+
+ private fun waitForRunToFinish() {
+ val deadline = System.currentTimeMillis() + 5_000
+ ShadowLooper.idleMainLooper()
+ while (System.currentTimeMillis() < deadline && runner().isRunning) {
+ ShadowLooper.idleMainLooper()
+ Thread.sleep(10)
+ }
+ ShadowLooper.idleMainLooper()
+ }
+
+ private fun savedTranscript(sessionId: String): List<UiMessage> =
+ runBlocking { historyRepository.loadSession(sessionId) }!!.displayMessages
+
+ @Test
+ fun `the runner is held by the app, one for every screen`() {
+ assertTrue(application is GotchaApp)
+ assertSame(Screen(application).viewModel.runner, Screen(application).viewModel.runner)
+ }
+
+ @Test
+ fun `a run goes on after its screen is gone and ends on its own`() {
+ val screen = Screen(application)
+ screen.viewModel.sendMessage("Hello")
+ val sessionId = screen.viewModel.uiState.value.runningSessionId!!
+
+ screen.finish()
+ assertTrue("clearing the screen stopped the run", runner().isRunning)
+ waitForRunToFinish()
+
+ val transcript = savedTranscript(sessionId)
+ assertEquals("Hello", transcript.first().text)
+ assertFalse(
+ "the run was cancelled with its ViewModel",
+ transcript.any { it.text == "Agent was interrupted by the user." }
+ )
+ val finished = shadowOf(manager).allNotifications.single()
+ assertTrue(finished.extras.getString(NotificationCompat.EXTRA_TITLE)!!.startsWith("Failed: "))
+ }
+
+ @Test
+ fun `a screen created mid-run shows the running chat`() {
+ val first = Screen(application)
+ first.viewModel.sendMessage("Hello")
+ val sessionId = first.viewModel.uiState.value.runningSessionId!!
+ first.finish()
+
+ val second = Screen(application).viewModel
+ val state = second.uiState.value
+
+ assertEquals(sessionId, state.activeSessionId)
+ assertTrue(state.isBusy)
+ assertEquals(listOf("Hello"), state.messages.map { it.text })
+ waitForRunToFinish()
+ // The run's last bubble reaches the screen now following it.
+ assertEquals(runner().state.value.transcript, second.uiState.value.messages)
+ assertFalse(second.uiState.value.isBusy)
+ }
+
+ @Test
+ fun `a screen created after the run starts a fresh chat`() {
+ val first = Screen(application)
+ first.viewModel.sendMessage("Hello")
+ val sessionId = first.viewModel.uiState.value.runningSessionId!!
+ first.finish()
+ waitForRunToFinish()
+
+ val state = Screen(application).viewModel.uiState.value
+
+ assertNotEquals(sessionId, state.activeSessionId)
+ assertTrue(state.messages.isEmpty())
+ }
+
+ @Test
+ fun `stop on the ongoing notification works with no screen`() {
+ // A server that takes the run's request and never answers, so the run is
+ // still waiting on the model when Stop arrives, whatever the timing. Later
+ // requests (the chat title, made as the stopped run saves) are dropped.
+ ServerSocket(0).use { silent ->
+ thread(isDaemon = true) {
+ val held = runCatching { silent.accept() }.getOrNull()
+ while (!silent.isClosed) runCatching { silent.accept().close() }
+ held?.close()
+ }
+ SettingsRepository(application).save(
+ Settings(
+ provider = LlmProvider.OPENAI_COMPATIBLE,
+ apiKey = "test-key",
+ baseUrl = "http://127.0.0.1:${silent.localPort}/v1"
+ )
+ )
+ val screen = Screen(application)
+ screen.viewModel.sendMessage("Hello")
+ val sessionId = screen.viewModel.uiState.value.runningSessionId!!
+ screen.finish()
+ ShadowLooper.idleMainLooper()
+ assertTrue(runner().isRunning)
+
+ Robolectric.buildService(ChatRunService::class.java, Intent().setAction(ChatRunService.ACTION_STOP_RUN))
+ .create()
+ .startCommand(0, 1)
+ waitForRunToFinish()
+
+ assertFalse(runner().isRunning)
+ assertEquals("Agent was interrupted by the user.", savedTranscript(sessionId).last().text)
+ }
+ }
+
+ @Test
+ fun `a permission ask is answered no once the screen is gone`() {
+ val screen = Screen(application)
+ val gate = CoroutineScope(Dispatchers.Unconfined).async {
+ runner().awaitPermissionGrant(Manifest.permission.CAMERA)
+ }
+ assertEquals(Manifest.permission.CAMERA, screen.viewModel.uiState.value.pendingPermission)
+
+ screen.store.clear()
+
+ assertTrue("the ask waited for its timeout", gate.isCompleted)
+ assertFalse(runBlocking { gate.await() })
+ assertNull(runner().state.value.pendingPermission)
+ }
+
+ @Test
+ fun `a question outlives the screen and is answered from the next one`() {
+ val first = Screen(application)
+ val gate = CoroutineScope(Dispatchers.Unconfined).async {
+ runner().awaitQuestionAnswer(PendingQuestion("Which folder?"))
+ }
+ first.finish()
+ assertFalse(gate.isCompleted)
+
+ val second = Screen(application).viewModel
+ assertEquals("Which folder?", second.uiState.value.pendingQuestion?.question)
+ second.submitAnswer("Downloads")
+
+ assertEquals("Downloads", runBlocking { gate.await() })
+ assertNull(second.uiState.value.pendingQuestion)
+ }
+}
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelAttachmentTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelAttachmentTest.kt
index a2ba03c..7824348 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelAttachmentTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelAttachmentTest.kt
@@ -92,9 +92,7 @@ class ChatViewModelAttachmentTest {
/** Reads the live engine history without hitting the network. */
private fun engineHistory(): List<ChatMessage> {
- val field = ChatViewModel::class.java.getDeclaredField("agentEngine")
- .apply { isAccessible = true }
- return (field.get(viewModel) as AgentEngine).history.toList()
+ return viewModel.runner.engine.history.toList()
}
/**
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelAutoReadTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelAutoReadTest.kt
index 1796bf3..e85eced 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelAutoReadTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelAutoReadTest.kt
@@ -41,25 +41,25 @@ class ChatViewModelAutoReadTest {
@Test
fun `typed message when autoReadReplies is false does not speak reply`() {
viewModel.sendMessage("Hello", isVoiceInput = false)
- viewModel.onAssistantReply("Hello back!")
+ viewModel.runner.onAssistantReply("Hello back!")
assertFalse(viewModel.uiState.value.isSpeaking)
}
@Test
fun `voice message when autoReadReplies is false speaks reply for that turn`() {
viewModel.sendMessage("Hello", isVoiceInput = true)
- viewModel.onAssistantReply("Hello back!")
+ viewModel.runner.onAssistantReply("Hello back!")
assertTrue(viewModel.uiState.value.isSpeaking)
}
@Test
fun `subsequent typed message after voice message does not speak reply when autoReadReplies is false`() {
viewModel.sendMessage("Hello", isVoiceInput = true)
- viewModel.onAssistantReply("Hello back!")
+ viewModel.runner.onAssistantReply("Hello back!")
viewModel.stopSpeaking()
viewModel.sendMessage("Second message", isVoiceInput = false)
- viewModel.onAssistantReply("Second reply")
+ viewModel.runner.onAssistantReply("Second reply")
assertFalse(viewModel.uiState.value.isSpeaking)
}
@@ -69,7 +69,7 @@ class ChatViewModelAutoReadTest {
viewModel.openSession("new-session-id")
viewModel.sendMessage("Session message", isVoiceInput = false)
- viewModel.onAssistantReply("Session reply")
+ viewModel.runner.onAssistantReply("Session reply")
assertFalse(viewModel.uiState.value.isSpeaking)
}
}
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt
index 12e2f22..c61e1a1 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelBackgroundHintTest.kt
@@ -74,9 +74,7 @@ class ChatViewModelBackgroundHintTest {
}
private fun engineHistory(): List<ChatMessage> {
- val field = ChatViewModel::class.java.getDeclaredField("agentEngine")
- .apply { isAccessible = true }
- return (field.get(viewModel) as AgentEngine).history.toList()
+ return viewModel.runner.engine.history.toList()
}
private val expectedHint = backgroundHintText(vibrate = true, chime = false)
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
index f5d6343..7593d52 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
@@ -26,7 +26,7 @@ import org.robolectric.shadows.ShadowLooper
* The init coroutine of [ChatViewModel] runs on Robolectric's main looper,
* which is drained in [setUp] so each test starts from a clean baseline.
* Tests deliberately exercise only the synchronous entry points
- * ([ChatViewModel.clearChat], [ChatViewModel.onTokenCount],
+ * ([ChatViewModel.clearChat], [ChatRunner.onTokenCount],
* [ChatViewModel.refreshSettings], [ChatViewModel.deleteSession]) because
* `viewModelScope.launch` posts continuations back to Main after a real
* `Dispatchers.IO` suspension, and that handoff is not observable from
@@ -39,7 +39,7 @@ import org.robolectric.shadows.ShadowLooper
* read [com.gotcha.agent.AgentEngine.tokenCount] unconditionally, so
* saving settings while a background run was in another chat would
* overwrite the viewed session's bar)
- * - [ChatViewModel.onTokenCount] publishes a live overlay so the drawer
+ * - [ChatRunner.onTokenCount] publishes a live overlay so the drawer
* updates in-frame (Bug 4)
* - [ChatViewModel.deleteSession] cleans up the overlay entry
*/
@@ -154,7 +154,7 @@ class ChatViewModelContextUsageTest {
val engineId = viewModel.uiState.value.activeSessionId
assertNotNull(engineId)
- viewModel.onTokenCount(555)
+ viewModel.runner.onTokenCount(555)
assertEquals(555, viewModel.liveTokenBySession.value[engineId])
}
@@ -166,7 +166,7 @@ class ChatViewModelContextUsageTest {
*/
@Test
fun `onTokenCount moves the meter of the viewed engine session`() {
- viewModel.onTokenCount(7_000)
+ viewModel.runner.onTokenCount(7_000)
val limit = settingsRepository.load().maxContextTokens.toFloat()
assertEquals(7_000, viewModel.uiState.value.tokenCount)
@@ -184,7 +184,7 @@ class ChatViewModelContextUsageTest {
flow.value = flow.value.copy(activeSessionId = "session-B")
val percent = viewModel.uiState.value.contextUsagePercent
- viewModel.onTokenCount(9_999)
+ viewModel.runner.onTokenCount(9_999)
assertEquals(3_000, viewModel.uiState.value.tokenCount)
assertEquals(percent, viewModel.uiState.value.contextUsagePercent, 0.0001f)
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
index e76898a..7a71a9e 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelPermissionGateTest.kt
@@ -24,7 +24,7 @@ import org.robolectric.annotation.Config
/**
* The chat host's half of asking for a permission when it is needed (issue #79):
- * the engine blocks on [ChatViewModel.awaitPermissionGrant] while the Activity
+ * the engine blocks on [ChatRunner.awaitPermissionGrant] while the Activity
* puts the reason on screen and raises the system dialog.
*
* Two things must hold or the agent hangs. The ask has to be visible in the UI
@@ -55,7 +55,7 @@ class ChatViewModelPermissionGateTest {
@Test
fun `the ask is published to the UI state until it is answered`() = runTest(dispatcher) {
- val gate = async { viewModel.awaitPermissionGrant(android.Manifest.permission.CAMERA) }
+ val gate = async { viewModel.runner.awaitPermissionGrant(android.Manifest.permission.CAMERA) }
runCurrent()
assertEquals(
@@ -73,7 +73,7 @@ class ChatViewModelPermissionGateTest {
@Test
fun `declining answers the engine without leaving the ask on screen`() = runTest(dispatcher) {
- val gate = async { viewModel.awaitPermissionGrant(android.Manifest.permission.SEND_SMS) }
+ val gate = async { viewModel.runner.awaitPermissionGrant(android.Manifest.permission.SEND_SMS) }
runCurrent()
viewModel.onPermissionResult(false)
@@ -90,7 +90,7 @@ class ChatViewModelPermissionGateTest {
// timeout with the agent frozen behind it.
viewModel.setForeground(false)
- val granted = viewModel.awaitPermissionGrant(android.Manifest.permission.READ_CONTACTS)
+ val granted = viewModel.runner.awaitPermissionGrant(android.Manifest.permission.READ_CONTACTS)
assertFalse(granted)
assertNull("nothing should be queued for a screen that isn't there", viewModel.uiState.value.pendingPermission)
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelPersonaTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelPersonaTest.kt
index 9020f93..c407974 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelPersonaTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelPersonaTest.kt
@@ -63,8 +63,7 @@ class ChatViewModelPersonaTest {
/** The engine is private; its persona is what the system prompt is built from. */
private fun engine(): AgentEngine {
- val field = ChatViewModel::class.java.getDeclaredField("agentEngine").apply { isAccessible = true }
- return field.get(viewModel) as AgentEngine
+ return viewModel.runner.engine
}
private fun enginePersonaId(): String? = engine().sessionPersonaId