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、支持任意模型,记忆和密码绝不离开设备。
fix: 继续 after the step limit picks up where the task stopped
heychargingb3c78e664186
diff --git a/CLAUDE.md b/CLAUDE.mdindex becf281..560a7b9 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -80,7 +80,7 @@ Four things in `LocalBrain` are correctness, not style:`LocalPrompt.TEXT` is the system prompt, adapted from the *cloud* brain's text. Our mirror of that text (`brain/andee-system-prompt.md`) was deleted on 2026-09-29 — nothing loaded it and the two had already stopped needing to move together, so it was a file with no reader. Note what that did **not** do: the cloud mode is still in the code (`VoiceConfig.BRAIN_HUB` still exists; the local brain is the out-of-box default), and that side keeps its own copy of the prompt, which this repo never owned. The CRM sections and the commitment/playbook rituals are **gone rather than softened** from *this* prompt: `crm_*` does not exist here and nothing wakes this device up later, so a rule whose tools are absent just teaches the model to claim it did something it cannot do.-Guards: `MAX_ITERATIONS = 25` and an identical-`(name, arguments)`-three-times cut-off, both of which end with a spoken final answer — a ball that simply stops is the one failure the user cannot interpret. Neither replaces `ScreenController`'s per-location tap guard; that one counts coordinates, these count calls.+Guards: `MAX_ITERATIONS = 25` and an identical-`(name, arguments)`-three-times cut-off, both of which end with a spoken final answer — a ball that simply stops is the one failure the user cannot interpret. **A turn either guard stops is not over, and 继续 has to find it** (`LocalBrain.Unfinished`, `stopUnfinished`). The sentence the user heard goes into the history as well as to the TTS, every tool call the model already made gets a reply, and the task's messages are kept by `trimHistory` (`HistoryTrim.dropIndexes(keepFrom)`), the harvest and topic folds even after another turn becomes newest — measured 10-22: a todo fired between the stop and 继续, the trim kept only "the newest turn", and 104 messages became 52 with the task gone. Every user message carries an `(unfinished: …)` line until a later turn acts and finishes, or an hour passes. `answerDangling` separately answers tool calls a turn the user stopped left hanging, which otherwise make the next request a 400. `ContinueAfterStepLimitTest` replays the whole thing against a scripted model. Neither replaces `ScreenController`'s per-location tap guard; that one counts coordinates, these count calls.Not done on purpose: no streaming (an SSE parser plus incremental `tool_calls` reassembly, for a second or two of earlier TTS), no history on disk (service restart = new session), no context compaction, and `tools/boss.py` has no injection point in local mode because its whole mechanism is impersonating a device to the hub.diff --git a/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.kt b/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.ktindex 05aeedc..ea213ac 100644--- a/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.kt+++ b/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.kt@@ -13,12 +13,15 @@ object HistoryTrim {/*** @param boundary one flag per message, true for a plain `user` message (the only place a conversation* may be cut). Index 0 is the system message and is never dropped.+ * @param keepFrom nothing at or after this index is dropped either — the start of a task that stopped at+ * the step limit, which another turn may have pushed out of "newest" before the user says 继续.* @return indexes to remove. Whole turns from the front, oldest first, and never the newest turn: it* stays intact however long it has grown, because it is the one the conversation is still in.*/- fun dropIndexes(boundary: List<Boolean>, max: Int): List<Int> {+ fun dropIndexes(boundary: List<Boolean>, max: Int, keepFrom: Int = Int.MAX_VALUE): List<Int> {if (boundary.size <= max) return emptyList()- val newest = (boundary.lastIndex downTo 1).firstOrNull { boundary[it] } ?: return emptyList()+ val last = (boundary.lastIndex downTo 1).firstOrNull { boundary[it] } ?: return emptyList()+ val newest = minOf(last, keepFrom)val drop = ArrayList<Int>()var i = 1var size = boundary.sizediff --git a/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt b/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.ktindex c1afad7..a47d5fe 100644--- a/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt+++ b/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt@@ -584,6 +584,81 @@ class LocalBrain(*/private var turnAnswer: String? = null+ /**+ * Loop thread only. A task that stopped before it was done — at the step limit, or cut by the repeat+ * guard — and the user may yet say 继续 to.+ *+ * Without this the stop was invisible to the model and the task fragile. The sentence the user heard was+ * never in the history, so the model did not know it had been cut off; and the trim protects only the+ * newest turn, so the moment anything else became newest (a todo firing between the stop and 继续 —+ * measured, 10-22 11:30, 104 messages trimmed to 52) the whole task was dropped and 继续 meant nothing.+ * While this is set the task's messages are kept by the trim, the harvest and topic folds alike, and every+ * user message says what was stopped. Cleared by a later turn that acts and finishes, or by [UNFINISHED_TTL_MS].+ */+ private class Unfinished(val start: JSONObject, val request: String, val at: Long, val steps: Int)++ private var unfinished: Unfinished? = null++ /** Index of the unfinished task's first message, or [Int.MAX_VALUE] when there is none to keep. */+ private fun keepFrom(): Int {+ val u = unfinished ?: return Int.MAX_VALUE+ val i = history.indexOfFirst { it === u.start }+ if (i < 1) {+ unfinished = null+ return Int.MAX_VALUE+ }+ return i+ }++ /**+ * End a turn that is not done: the sentence goes into the history as well as to the user, every tool call+ * the model already made is answered (an unanswered `tool_call_id` is a 400 on the next request, which is+ * how 继续 used to fail outright after the repeat guard), and the task is marked [unfinished].+ */+ private fun stopUnfinished(start: JSONObject, request: String, steps: Int, said: String, pending: List<LlmClient.ToolCall>) {+ for (tc in pending) {+ history += JSONObject().put("role", "tool").put("tool_call_id", tc.id)+ .put("content", "Not run: the turn was stopped before this call (see your next message).")+ }+ history += JSONObject().put("role", "assistant").put("content", said)+ unfinished = Unfinished(start, request.lineSequence().firstOrNull().orEmpty().trim().take(160), System.currentTimeMillis(), steps)+ finish(said)+ }++ /**+ * Answer the tool calls a cut-off turn left hanging. A turn the user stopped (or talked over) returns+ * between one tool call and the next, so its last assistant message can name calls that never got a+ * `tool` reply — and the API refuses the whole next request for it, which is 继续 failing outright.+ * Each missing reply is placed right after that message's own replies, where the API expects it.+ */+ private fun answerDangling() {+ val a = history.indexOfLast { it.optString("role") == "assistant" && it.has("tool_calls") }+ if (a < 0) return+ val calls = history[a].optJSONArray("tool_calls") ?: return+ var end = a + 1+ val answered = HashSet<String>()+ while (end < history.size && history[end].optString("role") == "tool") {+ answered += history[end].optString("tool_call_id"); end+++ }+ var at = end+ for (i in 0 until calls.length()) {+ val id = calls.optJSONObject(i)?.optString("id").orEmpty()+ if (id.isEmpty() || id in answered) continue+ history.add(at++, JSONObject().put("role", "tool").put("tool_call_id", id).put("content", "Not run: the turn was stopped before this call."))+ }+ if (at > end) host.logInfo(TAG, "answered ${at - end} tool call(s) a stopped turn left hanging")+ }++ /** The line every user message carries while a task is unfinished. Empty otherwise. */+ private fun unfinishedNote(): String {+ val u = unfinished ?: return ""+ val at = java.text.SimpleDateFormat("HH:mm", java.util.Locale.US).format(java.util.Date(u.at))+ return "\n\n(unfinished: at $at you stopped a task after ${u.steps} steps before it was done — it began with " ++ "\"${u.request}\". Its steps are still above, and so is what you told the user when you stopped. If they want " ++ "you to go on (继续 / go on / keep going), carry on from where you stopped — do not start over, and do not " ++ "ask what they mean.)"+ }+/** A turn's final answer: the one place it is both said and remembered. Loop thread only. */private fun finish(text: String) {turnAnswer = text@@ -669,7 +744,9 @@ class LocalBrain(if (!internal) trimHistory()// Where this turn's own messages begin. The harvest uses it afterwards// to throw away its own scaffolding and keep only the summary.+ answerDangling()if (internal) internalFromIndex = history.size+ if (!internal) unfinished?.let { if (System.currentTimeMillis() - it.at > UNFINISHED_TTL_MS) unfinished = null }history += if (internal) {// A silent turn speaks as itself, never as the user. Hanging the// review prompt on a `user` message would forge an utterance the@@ -798,9 +875,13 @@ class LocalBrain(""} else {"\n\n(other Andees you can ask with ask_friend; list_friends has detail)\n$friends"- },+ } ++ // A task cut off at the step limit, so 继续 has something to continue. See [Unfinished].+ unfinishedNote(),)}+ val turnStart = history.last()+ var acted = falseif (!internal && images.isNotEmpty()) attachUserPhotos(images)@@ -849,6 +930,9 @@ class LocalBrain(// are about to drop.applyHarvest(answer)} else if (!internal) {+ // A turn that did something and finished means the user has moved on — or that 继续 just+ // finished the job. Either way the stopped task no longer needs keeping.+ if (acted) unfinished = null// A silent turn has nobody to answer; whatever it wrote// stays in the history as its own note and that is all.finish(@@ -866,7 +950,7 @@ class LocalBrain(if (reply.content.isNotBlank() && !internal) onProgress(reply.content.trim())val images = mutableListOf<String>()- for (tc in reply.toolCalls) {+ for ((ti, tc) in reply.toolCalls.withIndex()) {if (gen != generation.get()) return// The repeated-call guard, which is *not* the one in@@ -879,17 +963,18 @@ class LocalBrain(if (repeats >= MAX_REPEATS) {host.logWarn(TAG, "same call $MAX_REPEATS× in a row: $sig")BrainTrace.end(gen, "same call $MAX_REPEATS× in a row: ${tc.name}")+ val rest = reply.toolCalls.drop(ti)if (!internal) {- finish(- host.text(BrainText.REPEAT_STOP,- tc.name,)- )+ stopUnfinished(turnStart, userText, step, host.text(BrainText.REPEAT_STOP, tc.name), rest)+ } else {+ rest.forEach { history += JSONObject().put("role", "tool").put("tool_call_id", it.id).put("content", "Not run: stopped.") }}return}if (reply.content.isBlank() && !internal) onProgress(narrate(tc.name))val t0 = System.nanoTime() / 1_000_000L+ acted = trueval msg = runTool(tc, images, internal, kind.untrusted)BrainTrace.tool(gen, tc.name, host.methodOf(tc.name), tc.argumentsRaw,@@ -903,12 +988,7 @@ class LocalBrain(host.logWarn(TAG, "hit max steps ($maxSteps)")BrainTrace.end(gen, "hit max steps ($maxSteps)")- if (!internal) {- finish(- host.text(BrainText.MAX_STEPS,- maxSteps,)- )- }+ if (!internal) stopUnfinished(turnStart, userText, maxSteps, host.text(BrainText.MAX_STEPS, maxSteps), emptyList())}/**@@ -1216,6 +1296,11 @@ class LocalBrain(host.logWarn(TAG, "fold of 「${fold.title}」 finished with no live range; dropped")return}+ if (keepFrom() < until) {+ // The stretch reaches into a task stopped before it was done; putting it away would take 继续's context.+ host.logInfo(TAG, "fold of 「${fold.title}」 skipped: it reaches into an unfinished task")+ return+ }val firstSaid = TopicSplitter.anchorText(fold.first)val nextSaid = TopicSplitter.anchorText(fold.next)val all = host.scrollback()@@ -1434,7 +1519,7 @@ class LocalBrain(private fun trimHistory() {val before = history.sizeif (before <= MAX_HISTORY) return- for (gone in HistoryTrim.dropIndexes(history.map { isBoundary(it) }, MAX_HISTORY).sortedDescending()) drop(gone)+ for (gone in HistoryTrim.dropIndexes(history.map { isBoundary(it) }, MAX_HISTORY, keepFrom()).sortedDescending()) drop(gone)host.logInfo(TAG, "history trimmed to ${history.size} msgs")}@@ -1495,7 +1580,8 @@ class LocalBrain(host.logInfo(TAG, "harvest skipped: nothing changed since the last one")return}- val cut = boundaryAtOrBefore(history.size - KEEP_TAIL_MESSAGES)+ // Never into a task the user may still say 继续 to — see [Unfinished].+ val cut = minOf(boundaryAtOrBefore(history.size - KEEP_TAIL_MESSAGES), keepFrom())if (cut < MIN_HARVEST_MESSAGES) {host.logInfo(TAG, "harvest skipped: only ${cut - 1} messages old enough to fold in")return@@ -1559,7 +1645,7 @@ class LocalBrain(host.logWarn(TAG, "harvest produced no summary; nothing dropped")return}- val keepStart = boundaryAtOrBefore(from - KEEP_TAIL_MESSAGES)+ val keepStart = minOf(boundaryAtOrBefore(from - KEEP_TAIL_MESSAGES), keepFrom())if (keepStart < 2) {host.logInfo(TAG, "harvest has nothing old enough to replace; kept as is")return@@ -1699,6 +1785,9 @@ class LocalBrain(private const val MAX_HISTORY = 60+ /** How long a task stopped at the step limit is kept ready for 继续. See [Unfinished]. */+ private const val UNFINISHED_TTL_MS = 60 * 60_000L+/*** The other half of the context budget. See [historyChars] — 60* messages is a lot of chit-chat and very little screenshot, so thediff --git a/core/src/test/kotlin/net/kuafuai/andee/brain/ContinueAfterStepLimitTest.kt b/core/src/test/kotlin/net/kuafuai/andee/brain/ContinueAfterStepLimitTest.ktnew file mode 100644index 0000000..84afe3e--- /dev/null+++ b/core/src/test/kotlin/net/kuafuai/andee/brain/ContinueAfterStepLimitTest.kt@@ -0,0 +1,108 @@+package net.kuafuai.andee.brain++import com.sun.net.httpserver.HttpServer+import net.kuafuai.andee.config.BrainConfig+import org.json.JSONArray+import org.json.JSONObject+import org.junit.After+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test+import java.io.File+import java.lang.reflect.Proxy+import java.net.InetSocketAddress+import java.util.Locale+import java.util.concurrent.LinkedBlockingQueue+import java.util.concurrent.TimeUnit++/**+ * The 10-22 11:30 failure, replayed against a scripted model: a task stops at the step limit, a todo fires+ * before the user answers, and then the user says 继续. The request that 继续 sends must still carry the+ * task, what the user was told when it stopped, and a line saying it is unfinished.+ */+class ContinueAfterStepLimitTest {++ private val steps = 35+ private val requests = mutableListOf<JSONObject>()+ private val finals = LinkedBlockingQueue<String>()+ private val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0).apply {+ createContext("/chat/completions") { ex ->+ val body = JSONObject(ex.requestBody.readBytes().toString(Charsets.UTF_8))+ val n = synchronized(requests) { requests += body; requests.size }+ val msg = if (n <= steps) {+ JSONObject().put("role", "assistant").put("content", "").put(+ "tool_calls",+ JSONArray().put(+ JSONObject().put("id", "c$n").put("type", "function").put(+ "function", JSONObject().put("name", "tap").put("arguments", "{\"x\":$n}"),+ ),+ ),+ )+ } else {+ JSONObject().put("role", "assistant").put("content", "reply $n")+ }+ val out = JSONObject().put("choices", JSONArray().put(JSONObject().put("message", msg)))+ .put("usage", JSONObject().put("prompt_tokens", 1).put("completion_tokens", 1)).toString().toByteArray()+ ex.sendResponseHeaders(200, out.size.toLong())+ ex.responseBody.use { it.write(out) }+ }+ start()+ }++ private val trace = File.createTempFile("trace", "").also { it.delete(); it.mkdirs() }++ private val host = Proxy.newProxyInstance(javaClass.classLoader, arrayOf(BrainHost::class.java)) { _, m, args ->+ when (m.name) {+ "text" -> (args[0] as BrainText).name+ "locale" -> Locale.ENGLISH+ "maxSteps" -> steps+ "persona", "platformPrompt", "notebookIndex", "scenesIndex", "pagesIndex", "topicsIndex",+ "gearIndex", "planIndex", "friendsIndex" -> ""+ "scrollback" -> emptyList<ScrollRow>()+ "toolLists" -> listOf(+ JSONArray().put(+ JSONObject().put("name", "tap").put("method", "screen.tap").put("description", "tap")+ .put("parameters", JSONObject().put("type", "object").put("properties", JSONObject())),+ ),+ )+ "methodOf" -> "screen.tap"+ "dispatch" -> JSONObject().put("ok", true)+ "traceDir" -> trace+ else -> null+ }+ } as BrainHost++ private val brain = LocalBrain(+ BrainConfig(BrainConfig.BRAIN_LOCAL, "key", "http://127.0.0.1:${server.address.port}", "m", false, "low"),+ host, onProgress = {}, onFinal = { finals += it },+ )++ @After fun tearDown() {+ brain.stop()+ server.stop(0)+ }++ private fun final(): String = finals.poll(20, TimeUnit.SECONDS) ?: throw AssertionError("no answer")++ @Test fun continueStillSeesTheStoppedTask() {+ brain.submit("打开朋友圈,把动态整理成清单")+ assertEquals("MAX_STEPS", final())+ // A todo comes due before the user answers, and becomes the newest turn.+ brain.wake("(time's up) ask about the article")+ final()+ brain.submit("继续")+ final()++ val messages = requests.last().getJSONArray("messages")+ val all = (0 until messages.length()).map { messages.getJSONObject(it) }+ val user = all.filter { it.optString("role") == "user" && it.opt("content") is String }.map { it.getString("content") }+ assertTrue("the task's request is gone", user.any { it.contains("打开朋友圈,把动态整理成清单") })+ assertTrue("its steps are gone", all.count { it.optString("role") == "tool" } >= steps)+ assertTrue("what the user was told is missing", all.any { it.optString("role") == "assistant" && it.optString("content") == "MAX_STEPS" })+ assertTrue("继续 does not say what is unfinished", user.last().contains("(unfinished:") && user.last().contains("打开朋友圈"))+ // Every tool call has its reply, or the real API would refuse the request.+ val ids = all.flatMap { a -> a.optJSONArray("tool_calls")?.let { c -> (0 until c.length()).map { c.getJSONObject(it).getString("id") } }.orEmpty() }+ val answered = all.filter { it.optString("role") == "tool" }.map { it.getString("tool_call_id") }.toSet()+ assertTrue(ids.all { it in answered })+ }+}diff --git a/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.kt b/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.ktindex 3b5e8c6..77cb0f9 100644--- a/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.kt+++ b/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.kt@@ -34,4 +34,11 @@ class HistoryTrimTest {val d = HistoryTrim.dropIndexes(b, 60)assertEquals(30, d.size)}++ @Test fun aStoppedTaskIsKeptEvenWhenAnotherTurnIsNewer() {+ // system + a 70-message task stopped at the step limit + a todo's 2-message turn after it.+ val b = listOf(false) + turn(70) + turn(2)+ assertTrue("without the guard the task goes", 1 in HistoryTrim.dropIndexes(b, 60))+ assertEquals(emptyList<Int>(), HistoryTrim.dropIndexes(b, 60, keepFrom = 1))+ }}