Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

test: cover which turn Full conversation unfolds and when it follows new steps

leepokai4f030580d188
diff --git a/apps/android/app/src/main/kotlin/com/codync/android/TraceView.kt b/apps/android/app/src/main/kotlin/com/codync/android/TraceView.kt
index b6671af6..5e9e25a6 100644
--- a/apps/android/app/src/main/kotlin/com/codync/android/TraceView.kt
+++ b/apps/android/app/src/main/kotlin/com/codync/android/TraceView.kt
@@ -35,8 +35,8 @@ import kotlinx.serialization.json.jsonPrimitive
@Composable internal fun TraceView(bot: Bot, state: AppState) {
val all = state.entries.filter { it.botId == bot.id }
val turns = all.groupBy { it.turn }.toSortedMap().toList()
- // The turn running now (a queued message already holds the next one): its thoughts start unfolded.
- val running = if (bot.isWorking(bot.id)) all.filter { it.status != "queued" }.maxOfOrNull { it.turn } else null
+ // The running turn's thoughts start unfolded.
+ val running = runningTurn(all, bot.isWorking(bot.id))
val list = rememberLazyListState()
// Opens at the end and follows any new step there (in whichever turn) until the reader scrolls up.
rememberFollowing(list, turns.lastOrNull()?.first?.toString(), all.maxOfOrNull { it.rev }, working = false, bottom = 0.dp)
@@ -55,6 +55,10 @@ import kotlinx.serialization.json.jsonPrimitive
}
}
+/** The turn running now while the bot works in this chat; a queued message already holds the next turn. */
+internal fun runningTurn(entries: List<Entry>, working: Boolean): Long? =
+ if (working) entries.filter { it.status != "queued" }.maxOfOrNull { it.turn } else null
+
/** The running turn's thoughts start `unfolded`, so its reasoning reads as it comes in. */
@Composable private fun TraceRow(entry: Entry, unfolded: Boolean) {
val d = entry.data
diff --git a/apps/android/app/src/test/kotlin/com/codync/android/TraceTest.kt b/apps/android/app/src/test/kotlin/com/codync/android/TraceTest.kt
new file mode 100644
index 00000000..ac9a8a26
--- /dev/null
+++ b/apps/android/app/src/test/kotlin/com/codync/android/TraceTest.kt
@@ -0,0 +1,19 @@
+package com.codync.android
+
+import com.codync.android.core.Entry
+import kotlinx.serialization.json.buildJsonObject
+import kotlinx.serialization.json.put
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class TraceTest {
+ private fun entry(id: String, turn: Long, status: String? = null) =
+ Entry(id, 0, "bot", 0, "user", turn, buildJsonObject { put("text", id); if (status != null) put("status", status) })
+
+ @Test fun `the trace unfolds the running turn, not a queued message holding the next`() {
+ val entries = listOf(entry("u1", 1), entry("t1", 1), entry("q", 2, status = "queued"))
+ assertEquals(1L, runningTurn(entries, working = true))
+ assertNull(runningTurn(entries, working = false))
+ }
+}
diff --git a/apps/desktop/src/renderer/views/thread/TraceView.tsx b/apps/desktop/src/renderer/views/thread/TraceView.tsx
index d04839c3..aebf1a2d 100644
--- a/apps/desktop/src/renderer/views/thread/TraceView.tsx
+++ b/apps/desktop/src/renderer/views/thread/TraceView.tsx
@@ -11,6 +11,7 @@ import { useStore } from '../../store/context'
import { useScreenViewed } from '../../store/analytics'
import { MarkdownText } from './Markdown'
import { CodeBox, DiffView } from './PermissionCard'
+import { runningTurn, scrolledToEnd, traceTurns } from './trace-turns'
import './trace.css'
/**
@@ -21,16 +22,10 @@ export function TraceView({ botId }: { botId: string }) {
const store = useStore()
useScreenViewed('trace')
const all = store.allEntries(botId)
- const turns = new Map<number, Entry[]>()
- for (const e of all) {
- turns.set(e.turn, [...(turns.get(e.turn) ?? []), e])
- }
- const sorted = [...turns.entries()].sort((a, b) => a[0] - b[0])
- // The turn running now (a queued message already holds the next one): its thoughts start unfolded.
+ const sorted = traceTurns(all)
+ // The running turn's thoughts start unfolded.
const bot = store.bots.get(botId)
- const running = bot && isWorkingIn(bot, botId)
- ? all.reduce((t, e) => (e.data.status === 'queued' ? t : Math.max(t, e.turn)), -1)
- : undefined
+ const running = runningTurn(all, !!bot && isWorkingIn(bot, botId))
const scroller = useRef<HTMLDivElement>(null)
// Opens at the end and follows new steps there until the reader scrolls up.
const atEnd = useRef(true)
@@ -39,8 +34,7 @@ export function TraceView({ botId }: { botId: string }) {
if (el && atEnd.current) el.scrollTop = el.scrollHeight
})
const onScroll = (e: React.UIEvent<HTMLDivElement>) => {
- const el = e.currentTarget
- atEnd.current = el.scrollHeight - el.scrollTop - el.clientHeight < 8
+ atEnd.current = scrolledToEnd(e.currentTarget)
}
return (
<SheetPage>
diff --git a/apps/desktop/src/renderer/views/thread/trace-turns.test.ts b/apps/desktop/src/renderer/views/thread/trace-turns.test.ts
new file mode 100644
index 00000000..6b3f4d1c
--- /dev/null
+++ b/apps/desktop/src/renderer/views/thread/trace-turns.test.ts
@@ -0,0 +1,25 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { runningTurn, scrolledToEnd, traceTurns } from './trace-turns.ts'
+import type { Entry } from '../../../shared/models.ts'
+
+const entry = (id: string, turn: number, kind = 'agent', status?: string): Entry => ({
+ id, seq: 0, botId: 'b', rev: 1, kind, turn, data: { text: id, status }, createdAt: 0, updatedAt: 0,
+})
+
+test('turns group in turn order and keep their entries in order', () => {
+ const turns = traceTurns([entry('u2', 2, 'user'), entry('u1', 1, 'user'), entry('a1', 1), entry('a2', 2)])
+ assert.deepEqual(turns.map(([turn, es]) => [turn, es.map((e) => e.id)]), [[1, ['u1', 'a1']], [2, ['u2', 'a2']]])
+})
+
+test('the running turn is the newest one, not a queued message waiting for the next', () => {
+ const entries = [entry('u1', 1, 'user'), entry('t1', 1, 'thought'), entry('q', 2, 'user', 'queued')]
+ assert.equal(runningTurn(entries, true), 1)
+ assert.equal(runningTurn(entries, false), undefined)
+})
+
+test('the trace follows new steps only while scrolled to the end', () => {
+ assert.equal(scrolledToEnd({ scrollHeight: 1000, scrollTop: 600, clientHeight: 400 }), true)
+ assert.equal(scrolledToEnd({ scrollHeight: 1000, scrollTop: 595, clientHeight: 400 }), true)
+ assert.equal(scrolledToEnd({ scrollHeight: 1000, scrollTop: 500, clientHeight: 400 }), false)
+})
diff --git a/apps/desktop/src/renderer/views/thread/trace-turns.ts b/apps/desktop/src/renderer/views/thread/trace-turns.ts
new file mode 100644
index 00000000..b575de8f
--- /dev/null
+++ b/apps/desktop/src/renderer/views/thread/trace-turns.ts
@@ -0,0 +1,19 @@
+import type { Entry } from '../../../shared/models.ts'
+
+/** A bot's entries grouped by turn, oldest turn first, each turn's entries in their order. */
+export function traceTurns(entries: Entry[]): [number, Entry[]][] {
+ const turns = new Map<number, Entry[]>()
+ for (const e of entries) turns.set(e.turn, [...(turns.get(e.turn) ?? []), e])
+ return [...turns.entries()].sort((a, b) => a[0] - b[0])
+}
+
+/** The turn running now while the bot works in this chat; a queued message already holds the next turn. */
+export function runningTurn(entries: Entry[], working: boolean): number | undefined {
+ if (!working) return undefined
+ return entries.reduce((t, e) => (e.data.status === 'queued' ? t : Math.max(t, e.turn)), -1)
+}
+
+/** Scrolled to (within a few pixels of) the end: new steps keep it there. */
+export function scrolledToEnd(el: { scrollHeight: number; scrollTop: number; clientHeight: number }) {
+ return el.scrollHeight - el.scrollTop - el.clientHeight < 8
+}
diff --git a/apps/ios/Kit/Sources/CodyncUI/Thread/TraceView.swift b/apps/ios/Kit/Sources/CodyncUI/Thread/TraceView.swift
index 2a9327b0..a3d5a3d6 100644
--- a/apps/ios/Kit/Sources/CodyncUI/Thread/TraceView.swift
+++ b/apps/ios/Kit/Sources/CodyncUI/Thread/TraceView.swift
@@ -13,9 +13,8 @@ public struct TraceView: View {
let all = model.chat(botId)
let turns = Dictionary(grouping: all, by: \.turn)
.sorted { $0.key < $1.key }
- // The turn running now (a queued message already holds the next one): its thoughts start unfolded.
- let running = model.bots[botId]?.isWorking(in: botId) == true
- ? all.filter { $0.data.status != "queued" }.map(\.turn).max() : nil
+ // The running turn's thoughts start unfolded.
+ let running = Self.runningTurn(all, working: model.bots[botId]?.isWorking(in: botId) == true)
VStack(spacing: 0) {
ModalHeader("Full conversation")
ScrollView {
@@ -45,6 +44,11 @@ public struct TraceView: View {
.background(Palette.background)
.tracked(.screenViewed(.trace))
}
+
+ /// The turn running now while the bot works in this chat; a queued message already holds the next turn.
+ nonisolated static func runningTurn(_ entries: [Entry], working: Bool) -> Int64? {
+ working ? entries.filter { $0.data.status != "queued" }.map(\.turn).max() : nil
+ }
}
private struct TraceRow: View {
diff --git a/apps/ios/Kit/Tests/CodyncUITests/ChatItemTests.swift b/apps/ios/Kit/Tests/CodyncUITests/ChatItemTests.swift
index 75c2d21a..253aca19 100644
--- a/apps/ios/Kit/Tests/CodyncUITests/ChatItemTests.swift
+++ b/apps/ios/Kit/Tests/CodyncUITests/ChatItemTests.swift
@@ -65,3 +65,14 @@ import Testing
let deliveredItems = ChatItem.build([delivered])
#expect(pendingItems.map(\.id) == deliveredItems.map(\.id))
}
+
+/// The trace unfolds the turn running now, not a queued message that already holds the next turn.
+@Test func traceRunningTurnSkipsQueuedMessages() {
+ func entry(_ id: String, turn: Int64, status: String? = nil) -> Entry {
+ Entry(id: id, seq: 0, botId: "b", rev: 0, kind: "user", turn: turn,
+ data: EntryData(text: id, status: status), createdAt: 0, updatedAt: 0)
+ }
+ let entries = [entry("u1", turn: 1), entry("t1", turn: 1), entry("q", turn: 2, status: "queued")]
+ #expect(TraceView.runningTurn(entries, working: true) == 1)
+ #expect(TraceView.runningTurn(entries, working: false) == nil)
+}