mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 14:26:00 +00:00
feat(agent): show and remove what the agent stores in the Linux userland
This commit is contained in:
1 parent
53cdf00b43
commit
fc46d3c658
4 files changed
+205
-1
No files matched your search
@@ -0,0 +1,95 @@
|
||||
package com.droiddeck.launcher.agent
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.file.FileVisitResult
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.SimpleFileVisitor
|
||||
import java.nio.file.attribute.BasicFileAttributes
|
||||
|
||||
/**
|
||||
* What the agent adds to the Linux userland, measured and removable piece by piece. Sign-ins
|
||||
* (~/.claude, ~/.codex, gh's config) are left alone, so removing a CLI and installing it again
|
||||
* does not sign out.
|
||||
*/
|
||||
object AgentStorage {
|
||||
enum class Item(val paths: List<String>) {
|
||||
CLAUDE_CODE(listOf("/root/.local/share/claude", "/root/.local/bin/claude", "/root/cc")),
|
||||
CODEX(listOf("/root/.local/bin/codex")),
|
||||
BUILD_TOOLS(listOf("/root/android-sdk", "/root/aapt2", "/root/x86root", "/root/.gradle")),
|
||||
SOURCE(listOf("/root/DroidDeck")),
|
||||
CACHES(listOf("/root/.cache", "/root/.npm")),
|
||||
}
|
||||
|
||||
/** Bytes per item; missing while it is being measured. */
|
||||
val sizes = mutableStateMapOf<Item, Long>()
|
||||
val removing = mutableStateMapOf<Item, Boolean>()
|
||||
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
|
||||
fun measure(context: Context) {
|
||||
val root = LinuxRuntime.rootDir(context.applicationContext)
|
||||
Thread({
|
||||
for (item in Item.entries) {
|
||||
val bytes = item.paths.sumOf { size(File(root, it.removePrefix("/")).toPath()) }
|
||||
main.post { sizes[item] = bytes }
|
||||
}
|
||||
}, "agent-storage").start()
|
||||
}
|
||||
|
||||
fun remove(context: Context, item: Item) {
|
||||
val app = context.applicationContext
|
||||
val root = LinuxRuntime.rootDir(app)
|
||||
removing[item] = true
|
||||
Thread({
|
||||
for (path in item.paths) runCatching { delete(File(root, path.removePrefix("/")).toPath()) }
|
||||
main.post {
|
||||
removing.remove(item)
|
||||
AgentHost.refresh(app)
|
||||
AgentSetup.check(app)
|
||||
measure(app)
|
||||
}
|
||||
}, "agent-storage-remove").start()
|
||||
}
|
||||
|
||||
/** Walks without following links, so a link out of the rootfs (into /sdcard, say) counts and removes only itself. */
|
||||
@VisibleForTesting
|
||||
internal fun size(path: Path): Long {
|
||||
if (!Files.exists(path, java.nio.file.LinkOption.NOFOLLOW_LINKS)) return 0
|
||||
var total = 0L
|
||||
Files.walkFileTree(path, object : SimpleFileVisitor<Path>() {
|
||||
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||
total += attrs.size()
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
override fun visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE
|
||||
})
|
||||
return total
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun delete(path: Path) {
|
||||
if (!Files.exists(path, java.nio.file.LinkOption.NOFOLLOW_LINKS)) return
|
||||
Files.walkFileTree(path, object : SimpleFileVisitor<Path>() {
|
||||
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||
Files.deleteIfExists(file)
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
|
||||
Files.deleteIfExists(dir)
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
override fun visitFileFailed(file: Path, exc: IOException): FileVisitResult {
|
||||
runCatching { Files.deleteIfExists(file) }
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,16 @@ import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.agent.AgentHarness
|
||||
import com.droiddeck.launcher.agent.AgentHost
|
||||
import com.droiddeck.launcher.agent.AgentSetup
|
||||
import com.droiddeck.launcher.agent.AgentStorage
|
||||
import android.text.format.Formatter
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import com.droiddeck.launcher.agent.AgentSetup.Status
|
||||
import com.droiddeck.launcher.agent.AgentSetup.Step
|
||||
import com.droiddeck.launcher.core.WirelessAdbPairingService
|
||||
@@ -34,7 +44,10 @@ internal fun AgentSetupView(modifier: Modifier, onDone: () -> Unit = {}) {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
LaunchedEffect(AgentHost.harness) { AgentSetup.check(context) }
|
||||
LaunchedEffect(AgentHost.harness) {
|
||||
AgentSetup.check(context)
|
||||
AgentStorage.measure(context)
|
||||
}
|
||||
Column(modifier.verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
Text(stringResource(R.string.agent_setup_title), fontSize = 22.sp, fontWeight = FontWeight.Bold, color = colors.onBackground)
|
||||
if (!AgentSetup.runtimeInstalled) {
|
||||
@@ -71,9 +84,66 @@ internal fun AgentSetupView(modifier: Modifier, onDone: () -> Unit = {}) {
|
||||
}
|
||||
}
|
||||
if (AgentSetup.ready) PrimaryButton(stringResource(R.string.agent_setup_done), main = true, onClick = onDone)
|
||||
StorageCard()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StorageCard() {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val items = AgentStorage.Item.entries.filter { (AgentStorage.sizes[it] ?: 0L) > 0L }
|
||||
if (items.isEmpty()) return
|
||||
val total = items.sumOf { AgentStorage.sizes[it] ?: 0L }
|
||||
Row(verticalAlignment = Alignment.Bottom, modifier = Modifier.fillMaxWidth().padding(top = 10.dp)) {
|
||||
Text(stringResource(R.string.agent_storage_title), fontSize = 17.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, modifier = Modifier.weight(1f))
|
||||
Text(Formatter.formatShortFileSize(context, total), fontSize = 14.sp, color = pal.onSurfaceVariant)
|
||||
}
|
||||
Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) {
|
||||
items.forEachIndexed { index, item ->
|
||||
var armed by remember(item) { mutableStateOf(false) }
|
||||
val busy = AgentStorage.removing[item] == true
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().heightIn(min = 60.dp).padding(horizontal = 14.dp, vertical = 10.dp),
|
||||
) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(stringResource(storageTitle(item)), fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground)
|
||||
Text(
|
||||
if (armed) stringResource(storageWarning(item)) else Formatter.formatShortFileSize(context, AgentStorage.sizes[item] ?: 0L),
|
||||
fontSize = 13.sp, color = if (armed) AttentionAmber else pal.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
SecondaryButton(
|
||||
stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_storage_remove),
|
||||
enabled = !busy && AgentHost.state != AgentHost.State.WORKING, compact = true,
|
||||
) {
|
||||
if (armed) {
|
||||
armed = false
|
||||
AgentStorage.remove(context, item)
|
||||
} else armed = true
|
||||
}
|
||||
}
|
||||
if (index < items.lastIndex) Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun storageTitle(item: AgentStorage.Item) = when (item) {
|
||||
AgentStorage.Item.CLAUDE_CODE -> R.string.agent_storage_claude
|
||||
AgentStorage.Item.CODEX -> R.string.agent_storage_codex
|
||||
AgentStorage.Item.BUILD_TOOLS -> R.string.agent_step_build
|
||||
AgentStorage.Item.SOURCE -> R.string.agent_storage_source
|
||||
AgentStorage.Item.CACHES -> R.string.agent_storage_caches
|
||||
}
|
||||
|
||||
private fun storageWarning(item: AgentStorage.Item) = when (item) {
|
||||
AgentStorage.Item.SOURCE -> R.string.agent_storage_source_warning
|
||||
AgentStorage.Item.CACHES -> R.string.agent_storage_caches_warning
|
||||
else -> R.string.agent_storage_reinstall_warning
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun labels(step: Step, harness: String): Pair<String, String> = when (step) {
|
||||
Step.CONNECTION -> stringResource(R.string.agent_step_connection) to stringResource(R.string.agent_step_connection_hint)
|
||||
|
||||
@@ -1149,6 +1149,16 @@
|
||||
<string name="agent_send">Send</string>
|
||||
<string name="agent_new">New conversation</string>
|
||||
<string name="agent_stop">Stop</string>
|
||||
<string name="agent_storage_title">Storage</string>
|
||||
<string name="agent_storage_remove">Remove</string>
|
||||
<string name="agent_storage_confirm">Remove now</string>
|
||||
<string name="agent_storage_claude">Claude Code</string>
|
||||
<string name="agent_storage_codex">Codex</string>
|
||||
<string name="agent_storage_source">DroidDeck source</string>
|
||||
<string name="agent_storage_caches">Download caches</string>
|
||||
<string name="agent_storage_source_warning">Changes that are not pushed are lost</string>
|
||||
<string name="agent_storage_caches_warning">Builds download these again</string>
|
||||
<string name="agent_storage_reinstall_warning">Setup can install it again; sign-ins are kept</string>
|
||||
<string name="agent_resumed">DroidDeck restarted on your Lab build. Continue where you left off.</string>
|
||||
<string name="agent_empty">Ask it why a game won\'t start, to explain a log, or to change DroidDeck and try the change on this device.</string>
|
||||
<string name="agent_failed">Stopped: %1$s</string>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.droiddeck.launcher.agent
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
import java.nio.file.Files
|
||||
|
||||
class AgentStorageTest {
|
||||
@Test fun removingAFolderKeepsWhatItsLinksPointAt() {
|
||||
val base = Files.createTempDirectory("agent-storage")
|
||||
val outside = Files.createDirectories(base.resolve("sdcard"))
|
||||
Files.write(outside.resolve("save.dat"), ByteArray(100))
|
||||
val item = Files.createDirectories(base.resolve("rootfs/android-sdk"))
|
||||
Files.write(item.resolve("a.jar"), ByteArray(10))
|
||||
val linked = runCatching { Files.createSymbolicLink(item.resolve("saves"), outside) }.isSuccess
|
||||
assumeTrue("symbolic links need privileges on this OS", linked)
|
||||
|
||||
assertTrue(AgentStorage.size(item) < 100)
|
||||
AgentStorage.delete(item)
|
||||
assertFalse(Files.exists(item))
|
||||
assertTrue(Files.exists(outside.resolve("save.dat")))
|
||||
}
|
||||
|
||||
@Test fun aMissingPathHasNoSize() {
|
||||
assertEquals(0L, AgentStorage.size(Files.createTempDirectory("agent-storage").resolve("gone")))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user