Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。
feat(sweep): recover and rebuild the quiet-hour review after a kill
booboosui23af24cad095
diff --git a/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt b/app/src/main/java/net/kuafuai/andee/BodyCoreService.ktindex 53f4531..a136c09 100644--- a/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt+++ b/app/src/main/java/net/kuafuai/andee/BodyCoreService.kt@@ -2634,6 +2634,10 @@ class BodyCoreService : Service() {// here, on the first start that can hear about it. This runs in hub// mode too — see the early return above.catchUpPromises()+ // The sweep is the other thing that only fires if the service is alive+ // when its timer lands, so a process killed mid-quiet leaves a+ // conversation that was never looked back at. Recover it here.+ catchUpSweep()}private fun stopBrain() {@@ -2692,6 +2696,45 @@ class BodyCoreService : Service() {brain.runSweep()}+ /**+ * Recover the quiet-hour sweep after the process was gone.+ *+ * The sweep timer is an AlarmManager alarm, so it survives a killed process+ * — but when it lands, [AlarmReceiver] finds no service and drops it, and+ * after a reboot the timer is not re-armed at all. The result is a+ * conversation that was finished but never looked back at. This is the+ * repair, run once on [startBrain]:+ *+ * * no recent activity → nothing to review, out;+ * * the quiet window has not elapsed → wait out the remainder, exactly as+ * a live timer would have;+ * * the window has elapsed but [Notebook.lastSweepAt] is *after* the last+ * activity → already swept, out;+ * * otherwise the last words were never swept → [onSweepTimer], which+ * re-checks the budget and runs (or, if the brain is busy or the history+ * too short, declines — its own refusal is not a lost sweep, because the+ * next start asks again).+ *+ * [Notebook.lastSweepAt] is the load-bearing fact: it is written only by+ * [Notebook.noteSweep], i.e. only when a sweep turn actually completed, so+ * comparing it against [Notebook.lastActivityAt] is the difference between+ * "already reviewed" and "died before it could".+ */+ private fun catchUpSweep() {+ val sweep = net.kuafuai.andee.config.VoiceConfig.sweepConfig(this)+ if (!sweep.enabled) return+ val last = net.kuafuai.andee.config.Notebook.lastActivityAt(this)+ if (last <= 0L) return+ val quietMs = sweep.quietMinutes * 60_000L+ val idle = System.currentTimeMillis() - last+ if (idle < quietMs) {+ net.kuafuai.andee.config.Scheduler.armSweepIn(this, quietMs - idle)+ return+ }+ if (last <= net.kuafuai.andee.config.Notebook.lastSweepAt(this)) return+ onSweepTimer()+ }+/*** A promise came due and the alarm got through — wake the brain up.*diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.ktindex 05604f5..371415a 100644--- a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt@@ -355,6 +355,12 @@ class LocalBrain(*/fun runSweep() {loop.execute {+ // [history] is in-memory and dies with the process, so after a kill+ // the sweep has nothing to review — the "recover the sweep" path+ // would trigger and then skip on "0 messages". Rebuild the dialogue+ // from the persisted scrollback first; a fresh conversation with no+ // rows still comes back empty and hits the usual gate below.+ if (history.isEmpty()) rebuildHistoryFromScrollback()when {cfg.apiKey.isEmpty() -> Log.i(TAG, "sweep skipped: no API key")busy.get() -> Log.i(TAG, "sweep skipped: a turn is running")@@ -365,6 +371,44 @@ class LocalBrain(}}+ /**+ * Rebuild the dialogue half of [history] from the persisted scrollback.+ *+ * The sweep reviews "this conversation", but the only record that survives+ * a killed process is [net.kuafuai.andee.ui.ChatHistory] — [history] itself+ * is a memory-only list. This walks the rows and rebuilds the two roles the+ * sweep cares about (the person's words and the assistant's), each user+ * message re-stamped with the row's own clock so [TopicSplitter] can still+ * find the turns and their silences. Pages and errors are skipped: they are+ * artifacts, not conversation.+ *+ * Capped at the tail so a weeks-old scrollback does not blow the context+ * window — the sweep is about what was *just* said, and the rest is the+ * notebook's business. Caller runs on the loop thread, so touching+ * [history] here is safe.+ */+ private fun rebuildHistoryFromScrollback() {+ val rows = net.kuafuai.andee.ui.ChatHistory.snapshot()+ .filter {+ it.role == net.kuafuai.andee.ui.ChatHistory.Role.USER ||+ it.role == net.kuafuai.andee.ui.ChatHistory.Role.ASSISTANT+ }+ .filter { it.text.isNotBlank() }+ .takeLast(REBUILD_MAX_MESSAGES)+ if (rows.isEmpty()) return+ history += JSONObject().put("role", "system").put("content", systemPrompt())+ val fmt = SimpleDateFormat("yyyy-MM-dd EEE HH:mm", AppLocale.localeOf(appContext))+ for (r in rows) {+ history += if (r.role == net.kuafuai.andee.ui.ChatHistory.Role.USER) {+ JSONObject().put("role", "user")+ .put("content", "[now ${fmt.format(Date(r.ts))}] ${r.text}")+ } else {+ JSONObject().put("role", "assistant").put("content", r.text)+ }+ }+ Log.i(TAG, "sweep rebuilt ${rows.size} messages from the scrollback")+ }+/*** The quiet-hour prompt with this conversation's topic candidates folded in.*@@ -1567,6 +1611,14 @@ class LocalBrain(/** A conversation shorter than this has nothing to review. */private const val MIN_MESSAGES_TO_REVIEW = 6+ /**+ * How much of the persisted scrollback a post-restart sweep rebuilds.+ * The sweep is about the conversation that just went quiet, so the tail+ * is enough; anything older is the notebook's business, not a context+ * window to re-fill.+ */+ private const val REBUILD_MAX_MESSAGES = 40+/** Caps what a silent turn leaves behind in the history. */private const val MAX_INTERNAL_NOTE_CHARS = 900diff --git a/app/src/main/java/net/kuafuai/andee/config/Notebook.kt b/app/src/main/java/net/kuafuai/andee/config/Notebook.ktindex 5096034..8e1358f 100644--- a/app/src/main/java/net/kuafuai/andee/config/Notebook.kt+++ b/app/src/main/java/net/kuafuai/andee/config/Notebook.kt@@ -54,6 +54,7 @@ object Notebook {private const val KEY_BLOB = "book"private const val KEY_ALIAS = "body_notebook_key"private const val KEY_LAST_ACTIVITY = "last_activity_at"+ private const val KEY_LAST_SWEEP = "last_sweep_at"private const val KEY_SWEEP_DAY = "sweep_day"private const val KEY_SWEEP_COUNT = "sweep_count"private const val KEY_SWEEP_TOKENS = "sweep_tokens"@@ -806,6 +807,14 @@ object Notebook {fun lastActivityAt(context: Context): Long = metaPrefs(context).getLong(KEY_LAST_ACTIVITY, 0L)+ /**+ * When the quiet-hour sweep last actually ran. Written by [noteSweep], so it+ * is the one fact that separates "the user spoke and the sweep already+ * looked back at it" from "the user spoke and the process died before the+ * sweep could". See `BodyCoreService.catchUpSweep`.+ */+ fun lastSweepAt(context: Context): Long = metaPrefs(context).getLong(KEY_LAST_SWEEP, 0L)+private fun today(): String =SimpleDateFormat("yyyy-MM-dd", Locale.US).format(Date())@@ -830,6 +839,7 @@ object Notebook {.putString(KEY_SWEEP_DAY, day).putInt(KEY_SWEEP_COUNT, count + 1).putInt(KEY_SWEEP_TOKENS, toks + tokens)+ .putLong(KEY_LAST_SWEEP, System.currentTimeMillis()).apply()}