|
| 1 | +package com.itsaky.androidide.plugins.aicore.tool.handlers |
| 2 | + |
| 3 | +import com.itsaky.androidide.plugins.aicore.tool.AgentLoop |
| 4 | +import com.itsaky.androidide.plugins.services.LogEntry |
| 5 | +import com.itsaky.androidide.plugins.services.LogLevel |
| 6 | + |
| 7 | +/** |
| 8 | + * Selects the slice of a log the model needs, in whole lines and within [MAX_OUTPUT_CHARS] |
| 9 | + * including the truncation markers. Only the kept lines are ever joined into a string. |
| 10 | + */ |
| 11 | +internal object LogWindowCalculator { |
| 12 | + /** Maximum characters of log handed to the model: AgentLoop's cap, less room for the message. */ |
| 13 | + const val MAX_OUTPUT_CHARS = AgentLoop.DEFAULT_TOOL_OUTPUT_CHAR_LIMIT - 100 |
| 14 | + |
| 15 | + /** Markers of an app crash; the anchor prefers these over a later error when the crash fits. */ |
| 16 | + private val CRASH_MARKERS = listOf("FATAL EXCEPTION", "Fatal signal") |
| 17 | + |
| 18 | + /** |
| 19 | + * A read that fits is returned whole. Logs span runs, so otherwise the window starts at the newest |
| 20 | + * crash if it and all after it fit, else at whichever is newer, that crash or the start of the |
| 21 | + * newest run of error lines. With neither, or a log of nothing but errors, it keeps the newest lines. |
| 22 | + */ |
| 23 | + fun windowFor(entries: List<LogEntry>, hostTruncated: Boolean): OutputWindow { |
| 24 | + if (!hostTruncated && fits(entries, MAX_OUTPUT_CHARS)) return OutputWindow(joined(entries), false) |
| 25 | + val crash = entries.indexOfLast { isCrashLine(it) }.takeIf { it >= 0 } |
| 26 | + val markedRoom = MAX_OUTPUT_CHARS - TRUNCATION_MARKER.length - 1 |
| 27 | + val anchor = crash?.takeIf { fits(entries.subList(it, entries.size), markedRoom) } |
| 28 | + ?: listOfNotNull(crash, newestErrorRunStart(entries)).maxOrNull() |
| 29 | + val text = if (anchor != null) { |
| 30 | + fromAnchor(entries.subList(anchor, entries.size), droppedBefore = hostTruncated || anchor > 0) |
| 31 | + } else { |
| 32 | + tail(entries) |
| 33 | + } |
| 34 | + return OutputWindow(text = text, anchoredOnError = anchor != null) |
| 35 | + } |
| 36 | + |
| 37 | + /** [entries] whole if they fit, else their head and their newest lines, half the room each. */ |
| 38 | + private fun fromAnchor(entries: List<LogEntry>, droppedBefore: Boolean): String { |
| 39 | + val prefix = if (droppedBefore) "$TRUNCATION_MARKER\n" else "" |
| 40 | + val room = MAX_OUTPUT_CHARS - prefix.length |
| 41 | + if (fits(entries, room)) return prefix + joined(entries) |
| 42 | + |
| 43 | + val separator = "\n$TRUNCATION_MARKER\n" |
| 44 | + val half = (room - separator.length) / 2 |
| 45 | + return prefix + headBody(entries, half) + separator + tailBody(entries, half) |
| 46 | + } |
| 47 | + |
| 48 | + /** The newest lines of [entries] that fit, marking what was dropped before them. */ |
| 49 | + private fun tail(entries: List<LogEntry>): String { |
| 50 | + val prefix = "$TRUNCATION_MARKER\n" |
| 51 | + return prefix + tailBody(entries, MAX_OUTPUT_CHARS - prefix.length) |
| 52 | + } |
| 53 | + |
| 54 | + /** The oldest whole lines of [entries] that fit in [room], else the start of the first. */ |
| 55 | + private fun headBody(entries: List<LogEntry>, room: Int): String { |
| 56 | + val kept = fitCount(entries, room) |
| 57 | + return if (kept > 0) joined(entries.subList(0, kept)) else keepStart(entries.first().text, room) |
| 58 | + } |
| 59 | + |
| 60 | + /** The newest whole lines of [entries] that fit in [room], else the end of the last. */ |
| 61 | + private fun tailBody(entries: List<LogEntry>, room: Int): String { |
| 62 | + val kept = fitCount(entries.asReversed(), room) |
| 63 | + return if (kept > 0) { |
| 64 | + joined(entries.subList(entries.size - kept, entries.size)) |
| 65 | + } else { |
| 66 | + keepEnd(entries.last().text, room) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** How many of [entries], taken in order, fit in [room] characters once joined by newlines. */ |
| 71 | + private fun fitCount(entries: List<LogEntry>, room: Int): Int { |
| 72 | + var used = -1 // the first line needs no separator |
| 73 | + var count = 0 |
| 74 | + for (entry in entries) { |
| 75 | + used += entry.text.length + 1 |
| 76 | + if (used > room) break |
| 77 | + count++ |
| 78 | + } |
| 79 | + return count |
| 80 | + } |
| 81 | + |
| 82 | + private fun fits(entries: List<LogEntry>, room: Int): Boolean = fitCount(entries, room) == entries.size |
| 83 | + |
| 84 | + private fun joined(entries: List<LogEntry>): String = entries.joinToString("\n") { it.text } |
| 85 | + |
| 86 | + /** The start of one oversized line, never splitting a surrogate pair. */ |
| 87 | + private fun keepStart(text: String, room: Int): String { |
| 88 | + val end = if (text[room - 1].isHighSurrogate()) room - 1 else room |
| 89 | + return text.substring(0, end) |
| 90 | + } |
| 91 | + |
| 92 | + /** The end of one oversized line, never splitting a surrogate pair. */ |
| 93 | + private fun keepEnd(text: String, room: Int): String { |
| 94 | + val start = text.length - room |
| 95 | + return text.substring(if (text[start].isLowSurrogate()) start + 1 else start) |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * The first line of the newest contiguous run of ERROR lines, so its trace keeps its head; null |
| 100 | + * when every line is an ERROR, where a head would keep the oldest errors. |
| 101 | + */ |
| 102 | + private fun newestErrorRunStart(entries: List<LogEntry>): Int? { |
| 103 | + var start = entries.indexOfLast { it.level == LogLevel.ERROR }.takeIf { it >= 0 } ?: return null |
| 104 | + while (start > 0 && entries[start - 1].level == LogLevel.ERROR) start-- |
| 105 | + return start.takeUnless { it == 0 && entries.last().level == LogLevel.ERROR } |
| 106 | + } |
| 107 | + |
| 108 | + private fun isCrashLine(entry: LogEntry): Boolean = |
| 109 | + CRASH_MARKERS.any { entry.text.contains(it, ignoreCase = true) } |
| 110 | +} |
0 commit comments