Skip to content

Commit c127454

Browse files
fix(ui): fork active sessions from stable history (#123718)
* fix(ui): fork active sessions from stable history * fix(protocol): align active fork types * test(ui): match main-session fork routing * fix(sessions): unify stable fork admission * fix(native): fork active sessions from stable history * fix(android): fork active sessions from stable history * style(android): simplify active fork result handling * fix(native): preserve legacy fork transport API * test(native): complete legacy fork transport stub --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 4a6edc0 commit c127454

49 files changed

Lines changed: 540 additions & 66 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/.i18n/native-source.json‎

Lines changed: 36 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/android/app/src/main/java/ai/openclaw/app/MainViewModel.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,8 @@ class MainViewModel private constructor(
16861686
suspend fun forkChatSession(
16871687
parentKey: String,
16881688
ownerAgentId: String? = null,
1689-
): String? = ensureRuntime().forkChatSession(parentKey, ownerAgentId)
1689+
fromLastCompleted: Boolean = false,
1690+
): String? = ensureRuntime().forkChatSession(parentKey, ownerAgentId, fromLastCompleted)
16901691

16911692
suspend fun rewindChatAtEntry(entryId: String): SessionRewindResult? = ensureRuntime().rewindChatAtEntry(entryId)
16921693

‎apps/android/app/src/main/java/ai/openclaw/app/NodeRuntime.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5207,7 +5207,8 @@ class NodeRuntime private constructor(
52075207
suspend fun forkChatSession(
52085208
parentKey: String,
52095209
ownerAgentId: String? = null,
5210-
): String? = chat.forkSession(parentKey, ownerAgentId)
5210+
fromLastCompleted: Boolean = false,
5211+
): String? = chat.forkSession(parentKey, ownerAgentId, fromLastCompleted)
52115212

52125213
suspend fun rewindChatAtEntry(entryId: String): SessionRewindResult? = chat.rewindSessionAtEntryResult(chatSessionKey.value, entryId)
52135214

‎apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ class ChatController internal constructor(
12321232
suspend fun forkSession(
12331233
parentKey: String,
12341234
ownerAgentId: String? = null,
1235+
fromLastCompleted: Boolean = false,
12351236
): String? {
12361237
val sessionKey = parentKey.trim().takeIf { it.isNotEmpty() } ?: return null
12371238
val capturedOwnerAgentId =
@@ -1243,6 +1244,7 @@ class ChatController internal constructor(
12431244
buildJsonObject {
12441245
put("parentSessionKey", JsonPrimitive(sessionKey))
12451246
put("fork", JsonPrimitive(true))
1247+
if (fromLastCompleted) put("forkFrom", JsonPrimitive("last-completed"))
12461248
// Keep the fork under the selected row's captured agent; omitting agentId can
12471249
// create the child under a newer gateway default for unscoped parent keys.
12481250
capturedOwnerAgentId?.let { put("agentId", JsonPrimitive(it)) }

‎apps/android/app/src/main/java/ai/openclaw/app/ui/SessionsScreen.kt‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,13 @@ internal fun SessionsScreen(
376376
onRename = { renameSessionTarget = session.toActionTarget(activeGatewayStableId) },
377377
onFork = {
378378
coroutineScope.launch {
379-
viewModel.forkChatSession(session.key, session.ownerAgentId)?.let { newKey ->
379+
val newKey =
380+
viewModel.forkChatSession(
381+
session.key,
382+
session.ownerAgentId,
383+
fromLastCompleted = session.hasActiveRun == true,
384+
)
385+
if (newKey != null) {
380386
viewModel.switchChatSession(newKey, session.ownerAgentId)
381387
onOpenChat()
382388
}
@@ -727,7 +733,11 @@ private fun SessionRow(
727733
menuExpanded = false
728734
onRename()
729735
}
730-
SessionMenuItem(nativeString("Fork")) {
736+
SessionMenuItem(
737+
nativeString(
738+
if (session.hasActiveRun == true) "Fork from last completed message" else "Fork",
739+
),
740+
) {
731741
menuExpanded = false
732742
onFork()
733743
}

‎apps/android/app/src/test/java/ai/openclaw/app/chat/ChatControllerCommandControlsTest.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ class ChatControllerCommandControlsTest {
413413
val create = requests.first { it.first == "sessions.create" }.second.orEmpty()
414414
assertTrue(create.contains("\"parentSessionKey\":\"main\""))
415415
assertTrue(create.contains("\"fork\":true"))
416+
assertFalse(create.contains("\"forkFrom\""))
416417
// The active unqualified parent keeps the captured default-agent owner.
417418
assertTrue(create.contains("\"agentId\":\"main\""))
418419

@@ -427,6 +428,10 @@ class ChatControllerCommandControlsTest {
427428
val capturedOwnerCreate = requests.last { it.first == "sessions.create" }.second.orEmpty()
428429
assertTrue(capturedOwnerCreate.contains("\"parentSessionKey\":\"custom\""))
429430
assertTrue(capturedOwnerCreate.contains("\"agentId\":\"owner-a\""))
431+
432+
controller.forkSession("main", fromLastCompleted = true)
433+
val activeCreate = requests.last { it.first == "sessions.create" }.second.orEmpty()
434+
assertTrue(activeCreate.contains("\"forkFrom\":\"last-completed\""))
430435
assertTrue(requests.any { it.first == "sessions.list" })
431436
assertEquals(
432437
false,

‎apps/ios/Sources/Chat/IOSGatewayChatTransport.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,16 @@ struct IOSGatewayChatTransport: OpenClawChatTransport {
461461
}
462462

463463
func forkSession(parentKey: String) async throws -> String {
464+
try await self.forkSession(parentKey: parentKey, fromLastCompleted: false)
465+
}
466+
467+
func forkSession(parentKey: String, fromLastCompleted: Bool) async throws -> String {
464468
let target = self.sessionTarget(for: parentKey)
465469
let childAgentID = target.agentID ?? OpenClawChatSessionKey.agentID(from: target.sessionKey)
466470
let request = OpenClawChatGatewayRequests.forkSession(
467471
parentSessionKey: target.sessionKey,
468-
agentID: childAgentID)
472+
agentID: childAgentID,
473+
fromLastCompleted: fromLastCompleted)
469474
let response = try await requestSessionMutation(request)
470475
return try JSONDecoder().decode(OpenClawChatCreateSessionResponse.self, from: response).key
471476
}

‎apps/ios/Sources/Design/CommandCenterSupport.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ struct CommandSessionActionsModifier: ViewModifier {
183183
self.actionButton("Rename…", systemImage: "pencil") {
184184
self.beginRename()
185185
}
186-
self.actionButton("Fork", systemImage: "arrow.triangle.branch") {
186+
self.actionButton(
187+
self.session.hasActiveRun == true
188+
? OpenClawTextValue.localized("Fork from last completed message")
189+
: OpenClawTextValue.localized("Fork"),
190+
systemImage: "arrow.triangle.branch")
191+
{
187192
self.actions.fork()
188193
}
189194
self.groupMenu

‎apps/ios/Sources/Design/CommandCenterTab.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,9 @@ struct CommandCenterTab: View {
668668
private func forkSession(_ session: OpenClawChatSessionEntry) {
669669
Task {
670670
do {
671-
let key = try await self.appModel.makeChatTransport().forkSession(parentKey: session.key)
671+
let key = try await self.appModel.makeChatTransport().forkSession(
672+
parentKey: session.key,
673+
fromLastCompleted: session.hasActiveRun == true)
672674
if let dashboardModel {
673675
await dashboardModel.refreshSessions(appModel: self.appModel)
674676
} else {
@@ -1346,7 +1348,9 @@ struct CommandSessionsScreen: View {
13461348
private func forkSession(_ session: OpenClawChatSessionEntry) {
13471349
Task {
13481350
do {
1349-
let key = try await self.appModel.makeChatTransport().forkSession(parentKey: session.key)
1351+
let key = try await self.appModel.makeChatTransport().forkSession(
1352+
parentKey: session.key,
1353+
fromLastCompleted: session.hasActiveRun == true)
13501354
await self.refreshSessions()
13511355
self.openSessionKey(key)
13521356
} catch {

‎apps/ios/Sources/RootSidebar.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,9 @@ struct RootSidebar: View {
849849
private func forkSession(_ session: OpenClawChatSessionEntry) {
850850
Task {
851851
do {
852-
let key = try await self.appModel.makeChatTransport().forkSession(parentKey: session.key)
852+
let key = try await self.appModel.makeChatTransport().forkSession(
853+
parentKey: session.key,
854+
fromLastCompleted: session.hasActiveRun == true)
853855
self.appModel.openChat(sessionKey: key)
854856
self.selectSidebarDestination(.chat)
855857
await self.model.refreshSessions(appModel: self.appModel)

0 commit comments

Comments
 (0)