Skip to content

armgit / andee

Public Imported from GitHub
0

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(brain): keep a trace of each local turn, and let a computer say a line to it

booboosui51f7096ae72d
diff --git a/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt b/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
index 5d72da3..f73d280 100644
--- a/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
+++ b/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
@@ -1665,6 +1665,7 @@ class ScreenBodyService : AccessibilityService() {
// [CommandDispatcher.dispatch].
dispatcher.dispatch(method, params, driving = false)
},
+ onSay = { text -> ui.post { submitUserTurn(text, "debug.say") } },
)
srv.start()
wsServer = srv
diff --git a/app/src/main/java/net/kuafuai/andee/brain/BrainTrace.kt b/app/src/main/java/net/kuafuai/andee/brain/BrainTrace.kt
new file mode 100644
index 0000000..3d7d8b8
--- /dev/null
+++ b/app/src/main/java/net/kuafuai/andee/brain/BrainTrace.kt
@@ -0,0 +1,77 @@
+package net.kuafuai.andee.brain
+
+import android.content.Context
+import android.util.Log
+import org.json.JSONObject
+import java.io.File
+import java.util.concurrent.Executors
+
+/**
+ * Every step of the local brain, appended to `files/brain_trace.jsonl`.
+ *
+ * logcat is not enough: some ROMs (Honor's MagicOS, measured) drop every
+ * `Log.*` line a third-party app writes, so a 25-step task that failed left no
+ * trace anywhere. This file is app-private, read back with `run-as`, bounded
+ * at two generations of [MAX_BYTES], and wiped with the chat history.
+ *
+ * Vault results are never written: `get_vault` returns phone numbers and
+ * emails in the clear, and a debug file is the last place they should land.
+ */
+object BrainTrace {
+
+ private const val TAG = "Body"
+ private const val FILE = "brain_trace.jsonl"
+ private const val MAX_BYTES = 1_000_000L
+ private const val MAX_FIELD = 4_000
+
+ private val exec = Executors.newSingleThreadExecutor { r ->
+ Thread(r, "brain-trace").apply { isDaemon = true }
+ }
+
+ @Volatile private var dir: File? = null
+
+ fun init(context: Context) {
+ dir = context.applicationContext.filesDir
+ }
+
+ fun turn(gen: Int, kind: String, text: String) =
+ append(JSONObject().put("ev", "turn").put("gen", gen).put("kind", kind).put("text", clip(text)))
+
+ fun step(gen: Int, step: Int, content: String, reasoning: String, calls: Int, promptTokens: Int) =
+ append(
+ JSONObject().put("ev", "step").put("gen", gen).put("step", step)
+ .put("content", clip(content)).put("reasoning", clip(reasoning))
+ .put("calls", calls).put("prompt_tokens", promptTokens),
+ )
+
+ fun tool(gen: Int, name: String, method: String?, args: String, result: String, ms: Long) {
+ val secret = method?.startsWith("device.vault.") == true
+ append(
+ JSONObject().put("ev", "tool").put("gen", gen).put("name", name)
+ .put("args", clip(args)).put("ms", ms)
+ .put("result", if (secret) "<vault result not traced>" else clip(result)),
+ )
+ }
+
+ fun end(gen: Int, why: String) = append(JSONObject().put("ev", "end").put("gen", gen).put("why", why))
+
+ fun wipe(context: Context) {
+ val d = context.applicationContext.filesDir
+ File(d, FILE).delete()
+ File(d, "$FILE.1").delete()
+ }
+
+ private fun clip(s: String) = if (s.length <= MAX_FIELD) s else s.take(MAX_FIELD) + "…(${s.length})"
+
+ private fun append(o: JSONObject) {
+ val d = dir ?: return
+ o.put("ts", System.currentTimeMillis())
+ exec.execute {
+ runCatching {
+ val f = File(d, FILE)
+ if (f.length() > MAX_BYTES) f.renameTo(File(d, "$FILE.1"))
+ f.appendText(o.toString() + "\n")
+ }.onFailure { Log.w(TAG, "brain trace: ${it.message}") }
+ }
+ }
+}
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
index 58575f9..b8d6094 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
@@ -99,6 +99,10 @@ class LocalBrain(
Thread(r, "LocalBrain-loop").apply { isDaemon = true }
}
+ init {
+ BrainTrace.init(appContext)
+ }
+
/**
* Only here so the loop thread can walk away from a tool that overran. A
* cached pool rather than a single thread because an abandoned call keeps
@@ -316,8 +320,10 @@ class LocalBrain(
currentKind = kind
turnTokens = 0
try {
+ BrainTrace.turn(gen, kind.name, text)
runTurn(text, gen, kind, images)
} catch (t: Throwable) {
+ BrainTrace.end(gen, "threw: ${t.message ?: t.javaClass.simpleName}")
if (gen == generation.get()) {
Log.e(TAG, "turn $gen ($kind) failed", t)
// A silent turn has nobody to apologise to; it just ends.
@@ -516,6 +522,7 @@ class LocalBrain(
"(cached ${reply.cachedTokens}), history ${history.size} msgs",
)
+ BrainTrace.step(gen, step, reply.content, reply.reasoning, reply.toolCalls.size, reply.promptTokens)
history += reply.assistantMessage
if (reply.toolCalls.isEmpty()) {
@@ -555,6 +562,7 @@ class LocalBrain(
lastSignature = sig
if (repeats >= MAX_REPEATS) {
Log.w(TAG, "same call $MAX_REPEATS× in a row: $sig")
+ BrainTrace.end(gen, "same call $MAX_REPEATS× in a row: ${tc.name}")
if (!internal) {
onFinal(
AppLocale.str(
@@ -568,13 +576,20 @@ class LocalBrain(
}
if (reply.content.isBlank() && !internal) onProgress(narrate(tc.name))
- history += runTool(tc, images, internal, kind.untrusted)
+ val t0 = android.os.SystemClock.uptimeMillis()
+ val msg = runTool(tc, images, internal, kind.untrusted)
+ BrainTrace.tool(
+ gen, tc.name, ToolSchemas.methodOf(tc.name), tc.argumentsRaw,
+ msg.optString("content"), android.os.SystemClock.uptimeMillis() - t0,
+ )
+ history += msg
}
if (images.isNotEmpty()) attachImages(images)
}
Log.w(TAG, "hit MAX_ITERATIONS")
+ BrainTrace.end(gen, "hit MAX_ITERATIONS")
if (!internal) {
onFinal(
AppLocale.str(
diff --git a/app/src/main/java/net/kuafuai/andee/config/FactoryReset.kt b/app/src/main/java/net/kuafuai/andee/config/FactoryReset.kt
index fb94c1e..a77dc95 100644
--- a/app/src/main/java/net/kuafuai/andee/config/FactoryReset.kt
+++ b/app/src/main/java/net/kuafuai/andee/config/FactoryReset.kt
@@ -65,7 +65,10 @@ object FactoryReset {
WakeTemplates.invalidate()
},
- Target(R.string.factory_reset_target_history) { ChatHistory.wipe(it) },
+ Target(R.string.factory_reset_target_history) {
+ ChatHistory.wipe(it)
+ net.kuafuai.andee.brain.BrainTrace.wipe(it)
+ },
Target(R.string.factory_reset_target_web) { webviewData() },
diff --git a/app/src/main/java/net/kuafuai/andee/net/BodyWsServer.kt b/app/src/main/java/net/kuafuai/andee/net/BodyWsServer.kt
index f6aca05..5d6ce7f 100644
--- a/app/src/main/java/net/kuafuai/andee/net/BodyWsServer.kt
+++ b/app/src/main/java/net/kuafuai/andee/net/BodyWsServer.kt
@@ -59,6 +59,14 @@ class BodyWsServer(
/** Extension-pushed event; the service turns these into hub broadcasts + UI updates. */
private val onExtensionEvent: (kind: String, data: JSONObject) -> Unit = { _, _ -> },
private val onCommand: (method: String, params: JSONObject?) -> Any?,
+ /**
+ * `debug.say {"text"}` — hand a sentence to the brain as if the user typed
+ * it, so a task can be reproduced from a computer. Loopback only (that is
+ * what `adb forward` arrives as): a LAN peer could already drive the
+ * screen through this socket, but it should not get to spend the user's
+ * API key.
+ */
+ private val onSay: (text: String) -> Unit = {},
) : WebSocketServer(InetSocketAddress(bindHost, port)) {
/** Driver connections only — the ones broadcasts go to. Extensions never join. */
@@ -119,7 +127,17 @@ class BodyWsServer(
// otherwise stall ping/pong handling and the client drops us.
dispatchPool.execute {
val response = try {
- val result = onCommand(method, params)
+ val result = if (method == "debug.say") {
+ if (conn.remoteSocketAddress?.address?.isLoopbackAddress != true) {
+ throw IllegalStateException("debug.say is loopback only — use adb forward")
+ }
+ val text = params?.optString("text").orEmpty().trim()
+ require(text.isNotEmpty()) { "debug.say needs {\"text\": …}" }
+ onSay(text)
+ JSONObject().put("submitted", text)
+ } else {
+ onCommand(method, params)
+ }
JSONObject()
.put("type", "response")
.put("id", id)