mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
feat(agent): compact composer, plan go or revise, usage limits, diffs and send now (#2531)
* fix(runtime): the userland resolver survives a crash mid-write * feat(agent): compact composer, plan go or revise, usage limits, diffs and send now
This commit is contained in:
1 parent
76f0c51235
commit
dacfee64ab
11 files changed
+874
-166
No files matched your search
@@ -74,7 +74,11 @@ object AgentChats {
|
||||
is Entry.Used -> JSONObject().put("used", entry.tool).put("detail", entry.detail).put("id", entry.id).put("input", entry.input)
|
||||
.apply { entry.output?.let { put("output", it).put("failed", entry.failed) } }
|
||||
is Entry.Finished -> JSONObject().put("finished", true).put("error", entry.error ?: JSONObject.NULL).put("stopped", entry.stopped).apply {
|
||||
entry.usage?.let { put("ms", it.durationMs).put("cost", it.costUsd).put("in", it.inputTokens).put("out", it.outputTokens) }
|
||||
if (entry.planned) put("planned", true)
|
||||
entry.usage?.let {
|
||||
put("ms", it.durationMs).put("cost", it.costUsd).put("in", it.inputTokens).put("out", it.outputTokens)
|
||||
if (it.context > 0) put("ctx", it.context).put("win", it.window)
|
||||
}
|
||||
}
|
||||
}.toString()
|
||||
|
||||
@@ -105,7 +109,8 @@ object AgentChats {
|
||||
o.has("result") -> Entry.Result(o.getString("result"), o.optString("output"), o.optBoolean("failed"))
|
||||
o.has("finished") -> Entry.Finished(
|
||||
if (o.isNull("error")) null else o.optString("error"), o.optBoolean("stopped"),
|
||||
if (o.has("ms")) AgentHost.Usage(o.optLong("ms"), o.optDouble("cost", 0.0), o.optLong("in"), o.optLong("out")) else null,
|
||||
if (o.has("ms")) AgentHost.Usage(o.optLong("ms"), o.optDouble("cost", 0.0), o.optLong("in"), o.optLong("out"), o.optLong("ctx"), o.optLong("win")) else null,
|
||||
o.optBoolean("planned"),
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ interface AgentHarness {
|
||||
fun parse(line: String): AgentEvent?
|
||||
/** Sets the CLI's own defaults in the userland before it runs; [rootfs] is the userland's root. */
|
||||
fun prepare(rootfs: File) {}
|
||||
/** Context and plan limits the CLI keeps on disk rather than printing, read once a turn of [conversation] is done. */
|
||||
fun usage(rootfs: File, conversation: String?): AgentEvent? = null
|
||||
|
||||
data class Model(val id: String, val label: String)
|
||||
|
||||
@@ -50,6 +52,9 @@ data class AgentEvent(
|
||||
val thinking: String? = null,
|
||||
/** Tokens the conversation's context holds after this step. */
|
||||
val context: Long? = null,
|
||||
/** Tokens the model's context can hold. */
|
||||
val window: Long? = null,
|
||||
val limits: List<AgentHost.Limit>? = null,
|
||||
val usage: AgentHost.Usage? = null,
|
||||
)
|
||||
|
||||
@@ -139,10 +144,12 @@ object ClaudeCode : AgentHarness {
|
||||
}
|
||||
AgentEvent(conversation, results)
|
||||
}
|
||||
"rate_limit_event" -> AgentEvent(conversation, limits = limits(o.optJSONObject("rate_limit_info")))
|
||||
// A resumed session first answers background tasks the last run left behind; that result is not the turn's.
|
||||
"result" -> if (o.optJSONObject("origin")?.optString("kind") == "task-notification") conversation?.let { AgentEvent(it) } else AgentEvent(
|
||||
conversation,
|
||||
listOf(AgentHost.Entry.Finished(if (o.optBoolean("is_error")) o.optString("result").ifEmpty { o.optString("subtype") } else null)),
|
||||
window = window(o.optJSONObject("modelUsage")),
|
||||
usage = AgentHost.Usage(
|
||||
o.optLong("duration_ms"), o.optDouble("total_cost_usd", 0.0),
|
||||
o.optJSONObject("usage")?.let { it.optLong("input_tokens") + it.optLong("cache_read_input_tokens") + it.optLong("cache_creation_input_tokens") } ?: 0,
|
||||
@@ -153,6 +160,24 @@ object ClaudeCode : AgentHarness {
|
||||
}
|
||||
}
|
||||
|
||||
private val WINDOW_MINUTES = mapOf("five_hour" to 300, "seven_day" to 10_080)
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun limits(info: JSONObject?): List<AgentHost.Limit>? {
|
||||
val windows = info?.optJSONObject("unifiedWindows") ?: return null
|
||||
return WINDOW_MINUTES.mapNotNull { (key, minutes) ->
|
||||
val w = windows.optJSONObject(key) ?: return@mapNotNull null
|
||||
AgentHost.Limit(minutes, (w.optDouble("utilization", 0.0) * 100).toInt(), w.optLong("resetsAt"))
|
||||
}.takeIf { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
/** The context window of the model that did most of the turn; subagents may run a smaller one. */
|
||||
private fun window(models: JSONObject?): Long? {
|
||||
models ?: return null
|
||||
return models.keys().asSequence().mapNotNull { models.optJSONObject(it) }
|
||||
.maxByOrNull { it.optDouble("costUSD", 0.0) }?.optLong("contextWindow")?.takeIf { it > 0 }
|
||||
}
|
||||
|
||||
private fun resultText(content: Any?): String = when (content) {
|
||||
is String -> content
|
||||
is JSONArray -> (0 until content.length()).mapNotNull { i ->
|
||||
@@ -180,8 +205,8 @@ object ClaudeCode : AgentHarness {
|
||||
input ?: return ""
|
||||
return when {
|
||||
tool == "Bash" -> input.optString("command")
|
||||
tool == "Edit" -> "${input.optString("file_path")}\n\n- ${input.optString("old_string").replace("\n", "\n- ")}\n\n+ ${input.optString("new_string").replace("\n", "\n+ ")}"
|
||||
tool == "Write" -> "${input.optString("file_path")}\n\n${input.optString("content")}"
|
||||
tool == "Edit" -> input.optString("file_path") + "\n" + lineDiff(input.optString("old_string"), input.optString("new_string"))
|
||||
tool == "Write" -> input.optString("file_path") + "\n" + lineDiff("", input.optString("content"))
|
||||
tool == "TodoWrite" -> todos(input.optJSONArray("todos"))
|
||||
else -> input.toString(2)
|
||||
}
|
||||
@@ -287,6 +312,33 @@ object Codex : AgentHarness {
|
||||
return if (entries.isEmpty()) null else AgentEvent(entries = entries)
|
||||
}
|
||||
|
||||
private const val TOKEN_COUNT = "\"payload\":{\"type\":\"token_count\""
|
||||
|
||||
/** Codex prints no limits with --json; its session log, named after the thread, has a token_count event each step. */
|
||||
override fun usage(rootfs: File, conversation: String?): AgentEvent? {
|
||||
conversation ?: return null
|
||||
val log = File(rootfs, "root/.codex/sessions").walkBottomUp().filter { it.isFile && it.name.endsWith("-$conversation.jsonl") }
|
||||
.maxByOrNull { it.lastModified() } ?: return null
|
||||
val last = runCatching { log.readLines().lastOrNull { it.contains(TOKEN_COUNT) } }.getOrNull() ?: return null
|
||||
return tokenCount(last)
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun tokenCount(line: String): AgentEvent? {
|
||||
val payload = runCatching { JSONObject(line).optJSONObject("payload") }.getOrNull()?.takeIf { it.optString("type") == "token_count" } ?: return null
|
||||
val info = payload.optJSONObject("info")
|
||||
val rates = payload.optJSONObject("rate_limits")
|
||||
val limits = listOf("primary", "secondary").mapNotNull { key ->
|
||||
val w = rates?.optJSONObject(key) ?: return@mapNotNull null
|
||||
AgentHost.Limit(w.optInt("window_minutes"), w.optDouble("used_percent", 0.0).toInt(), w.optLong("resets_at"))
|
||||
}
|
||||
return AgentEvent(
|
||||
context = info?.optJSONObject("last_token_usage")?.optLong("total_tokens")?.takeIf { it > 0 },
|
||||
window = info?.optLong("model_context_window")?.takeIf { it > 0 },
|
||||
limits = limits.takeIf { it.isNotEmpty() },
|
||||
)
|
||||
}
|
||||
|
||||
private fun todoEntry(id: String, item: JSONObject): AgentHost.Entry.Used {
|
||||
val items = item.optJSONArray("items") ?: JSONArray()
|
||||
val text = (0 until items.length()).mapNotNull { i ->
|
||||
@@ -305,6 +357,28 @@ object Codex : AgentHarness {
|
||||
@VisibleForTesting
|
||||
internal fun firstLine(text: String) = text.lineSequence().first().take(160)
|
||||
|
||||
private const val DIFF_CONTEXT = 2
|
||||
|
||||
/**
|
||||
* [old] against [new], one line per changed or nearby line, each led by ' ', '-' or '+'. Edits
|
||||
* replace a short span, so the lines both share at the start and end are cut to [DIFF_CONTEXT].
|
||||
*/
|
||||
@VisibleForTesting
|
||||
internal fun lineDiff(old: String, new: String): String {
|
||||
val a = if (old.isEmpty()) emptyList() else old.removeSuffix("\n").lines()
|
||||
val b = if (new.isEmpty()) emptyList() else new.removeSuffix("\n").lines()
|
||||
var head = 0
|
||||
while (head < a.size && head < b.size && a[head] == b[head]) head++
|
||||
var tail = 0
|
||||
while (tail < a.size - head && tail < b.size - head && a[a.size - 1 - tail] == b[b.size - 1 - tail]) tail++
|
||||
return buildList {
|
||||
a.subList(maxOf(0, head - DIFF_CONTEXT), head).forEach { add(" $it") }
|
||||
a.subList(head, a.size - tail).forEach { add("-$it") }
|
||||
b.subList(head, b.size - tail).forEach { add("+$it") }
|
||||
a.subList(a.size - tail, minOf(a.size, a.size - tail + DIFF_CONTEXT)).forEach { add(" $it") }
|
||||
}.joinToString("\n")
|
||||
}
|
||||
|
||||
private val SHELL_WRAPPER = Regex("^/bin/(?:ba)?sh -l?c '(.*)'$", RegexOption.DOT_MATCHES_ALL)
|
||||
|
||||
/** Codex runs each command as `/bin/bash -lc '<command>'`; the command is what the user wants to read. */
|
||||
|
||||
@@ -32,10 +32,18 @@ object AgentHost {
|
||||
/** A tool call; [output] stays null until its result arrives. */
|
||||
data class Used(val id: String, val tool: String, val detail: String, val input: String = "", val output: String? = null, val failed: Boolean = false) : Entry
|
||||
data class Result(val id: String, val output: String, val failed: Boolean) : Entry
|
||||
data class Finished(val error: String?, val stopped: Boolean = false, val usage: Usage? = null) : Entry
|
||||
/** [planned]: the turn ran in plan mode, so it ends on a plan that nothing has carried out yet. */
|
||||
data class Finished(val error: String?, val stopped: Boolean = false, val usage: Usage? = null, val planned: Boolean = false) : Entry
|
||||
}
|
||||
|
||||
data class Usage(val durationMs: Long, val costUsd: Double, val inputTokens: Long, val outputTokens: Long)
|
||||
/** [context] of [window] tokens is what the conversation held when the turn ended. */
|
||||
data class Usage(
|
||||
val durationMs: Long, val costUsd: Double, val inputTokens: Long, val outputTokens: Long,
|
||||
val context: Long = 0, val window: Long = 0,
|
||||
)
|
||||
|
||||
/** A plan usage limit over a rolling window of [minutes]; [resetsAt] is in epoch seconds. */
|
||||
data class Limit(val minutes: Int, val usedPercent: Int, val resetsAt: Long)
|
||||
|
||||
/** A message typed while the agent was working, sent when the turn ends. */
|
||||
data class Queued(val text: String, val files: List<String>)
|
||||
@@ -103,6 +111,12 @@ object AgentHost {
|
||||
/** Tokens in the conversation's context after its last step. */
|
||||
var context by mutableLongStateOf(0L)
|
||||
private set
|
||||
/** Tokens the model's context can hold; 0 until a turn has told. */
|
||||
var window by mutableLongStateOf(0L)
|
||||
private set
|
||||
/** The account's plan limits as [harness] last reported them. */
|
||||
var limits by mutableStateOf<List<Limit>>(emptyList())
|
||||
private set
|
||||
var lab by mutableStateOf<LabProgress?>(null)
|
||||
private set
|
||||
|
||||
@@ -119,6 +133,7 @@ object AgentHost {
|
||||
/** The running Lab build was asked for by the current turn, so stopping the turn stops it. */
|
||||
private var labFromTurn = false
|
||||
private var conversation: String? = null
|
||||
private var turnPlanned = false
|
||||
private var loaded = false
|
||||
private var appContext: Context? = null
|
||||
|
||||
@@ -136,6 +151,7 @@ object AgentHost {
|
||||
model = prefs(app).getString(modelKey(harness), null)
|
||||
effort = prefs(app).getString(effortKey(harness), null)?.takeIf { it in harness.efforts }
|
||||
plan = prefs(app).getBoolean(PLAN_KEY, false)
|
||||
limits = decodeLimits(prefs(app).getString(limitsKey(harness), null))
|
||||
available = LinuxRuntime.hasRootfs(app)
|
||||
installed = harness.files.all { guestFileExists(app, it) }
|
||||
if (!installed) state = State.MISSING else if (state == State.MISSING) state = State.IDLE
|
||||
@@ -158,7 +174,9 @@ object AgentHost {
|
||||
conversation = loaded.conversation
|
||||
entries.clear()
|
||||
entries.addAll(loaded.entries)
|
||||
context = 0
|
||||
val last = loaded.entries.lastOrNull { it is Entry.Finished && (it.usage?.context ?: 0) > 0 } as? Entry.Finished
|
||||
context = last?.usage?.context ?: 0
|
||||
window = last?.usage?.window ?: 0
|
||||
}
|
||||
|
||||
/** Opens a kept conversation, switching to the CLI it belongs to. */
|
||||
@@ -263,11 +281,15 @@ object AgentHost {
|
||||
AgentHostService.start(app)
|
||||
val current = harness
|
||||
val prompt = AgentAttachments.prompt(message, attached)
|
||||
val command = current.turn(prompt, conversation, AgentHarness.Options(model, plan && current.plans, BRIEF, effort))
|
||||
turnPlanned = plan && current.plans
|
||||
val command = current.turn(prompt, conversation, AgentHarness.Options(model, turnPlanned, BRIEF, effort))
|
||||
val workdir = workspace(app)
|
||||
var threadConversation = conversation
|
||||
Thread({
|
||||
runCatching { current.prepare(LinuxRuntime.rootDir(app)) }
|
||||
runCatching { AgentSetup.installHelpers(app) }
|
||||
// Without a session nothing else keeps the userland's resolver current.
|
||||
runCatching { com.droiddeck.launcher.runtime.LinuxNetworkLinkComponent(app, LinuxRuntime.rootDir(app)).publish() }
|
||||
val child = try {
|
||||
guest(app, command, workdir).start()
|
||||
} catch (e: Exception) {
|
||||
@@ -288,8 +310,12 @@ object AgentHost {
|
||||
runCatching {
|
||||
child.inputStream.bufferedReader().forEachLine { line ->
|
||||
runCatching { raw?.apply { write(line); newLine(); flush() } }
|
||||
val event = current.parse(line) ?: return@forEachLine
|
||||
if (event.entries.any { it is Entry.Finished }) ended = true
|
||||
val parsed = current.parse(line) ?: return@forEachLine
|
||||
parsed.conversation?.let { threadConversation = it }
|
||||
val done = parsed.entries.any { it is Entry.Finished }
|
||||
if (done) ended = true
|
||||
val event = if (!done) parsed else runCatching { current.usage(LinuxRuntime.rootDir(app), threadConversation) }.getOrNull()
|
||||
?.let { parsed.copy(context = it.context ?: parsed.context, window = it.window ?: parsed.window, limits = it.limits ?: parsed.limits) } ?: parsed
|
||||
main.post { if (turn === started) apply(app, event) }
|
||||
}
|
||||
}
|
||||
@@ -323,6 +349,16 @@ object AgentHost {
|
||||
queue.remove(item)
|
||||
}
|
||||
|
||||
/** Sends [item] now: the running turn stops where it is, and the rest of the queue waits for the next one. */
|
||||
fun sendNow(context: Context, item: Queued) {
|
||||
val app = context.applicationContext
|
||||
if (!queue.remove(item)) return
|
||||
val rest = queue.toList()
|
||||
stop(app)
|
||||
queue.addAll(rest)
|
||||
send(app, item.text, item.files)
|
||||
}
|
||||
|
||||
/** Starts a new conversation; the current one stays in the history. */
|
||||
fun forget(context: Context) {
|
||||
stop(context)
|
||||
@@ -331,6 +367,7 @@ object AgentHost {
|
||||
chat = null
|
||||
conversation = null
|
||||
this.context = 0
|
||||
window = 0
|
||||
prefs(context).edit().remove(CHAT_KEY).apply()
|
||||
state = if (installed) State.IDLE else State.MISSING
|
||||
}
|
||||
@@ -380,6 +417,11 @@ object AgentHost {
|
||||
event.text?.let { live += it }
|
||||
event.thinking?.let { liveThinking += it }
|
||||
event.context?.let { context = it }
|
||||
event.window?.let { window = it }
|
||||
event.limits?.let { reported ->
|
||||
limits = (limits.filter { old -> reported.none { it.minutes == old.minutes } } + reported).sortedBy { it.minutes }
|
||||
prefs(app).edit().putString(limitsKey(harness), encodeLimits(limits)).apply()
|
||||
}
|
||||
for (entry in event.entries) when (entry) {
|
||||
is Entry.Finished -> finish(app, entry.error, usage = event.usage)
|
||||
else -> {
|
||||
@@ -397,7 +439,8 @@ object AgentHost {
|
||||
if (!stopped) labFromTurn = false
|
||||
live = ""
|
||||
liveThinking = ""
|
||||
record(app, Entry.Finished(error, stopped, usage))
|
||||
val held = if (context > 0) (usage ?: Usage(0, 0.0, 0, 0)).copy(context = context, window = window) else usage
|
||||
record(app, Entry.Finished(error, stopped, held, planned = turnPlanned && !stopped && error == null))
|
||||
state = State.IDLE
|
||||
unseen = true
|
||||
failed = error != null
|
||||
@@ -555,6 +598,18 @@ object AgentHost {
|
||||
private const val RETRY_MS = 3_000L
|
||||
|
||||
private fun modelKey(harness: AgentHarness) = "model_${harness.id}"
|
||||
private fun limitsKey(harness: AgentHarness) = "limits_${harness.id}"
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun encodeLimits(list: List<Limit>) = org.json.JSONArray(list.map {
|
||||
org.json.JSONObject().put("minutes", it.minutes).put("used", it.usedPercent).put("resets", it.resetsAt)
|
||||
}).toString()
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun decodeLimits(text: String?): List<Limit> = runCatching {
|
||||
val a = org.json.JSONArray(text ?: return emptyList())
|
||||
(0 until a.length()).map { a.getJSONObject(it) }.map { Limit(it.optInt("minutes"), it.optInt("used"), it.optLong("resets")) }
|
||||
}.getOrDefault(emptyList())
|
||||
private fun effortKey(harness: AgentHarness) = "effort_${harness.id}"
|
||||
|
||||
internal fun prefs(context: Context) = context.getSharedPreferences("agent", Context.MODE_PRIVATE)
|
||||
|
||||
Reference in new issue
Block a user