File size: 46,114 Bytes
b8370f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 | import Foundation
import OSLog
private let chatSessionActionsLogger = Logger(
subsystem: "ai.openclaw",
category: "OpenClawChat")
extension OpenClawChatViewModel {
public func deleteSession(_ sessionKey: String, agentID: String? = nil) {
let target = self.sessionMutationTarget(key: sessionKey, agentID: agentID)
let transport = self.transport
Task {
do {
guard let routeLease = await transport.acquireSessionMutationRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
try await routeLease.deleteSession(key: sessionKey, agentID: target.agentID)
} catch {
self.errorText = error.localizedDescription
return
}
self.sessions.removeAll { self.sessionMatchesTarget($0, target: target) }
if self.matchesCurrentSessionKey(incoming: sessionKey, agentId: target.agentID, current: self.sessionKey) {
// The active transcript just disappeared server-side; fall
// back to the main session instead of a dead key.
let fallback = self.resolvedMainSessionKey
if fallback != self.sessionKey {
self.switchSession(to: fallback)
} else {
// Deleting the active main session: the key stays the
// address, so clear local state and re-bootstrap in place.
self.advanceSessionGeneration()
self.clearSessionOwnedState()
self.errorText = nil
self.load()
}
}
await self.fetchSessions(limit: nil, sessionSnapshot: self.currentSessionSnapshot())
}
}
func sessionMutationTarget(key: String, agentID: String? = nil) -> OpenClawChatSessionTarget {
let owner = agentID ?? self.sessions.first(where: { $0.key == key })?.agentId ??
self.explicitSessionAgentID
return OpenClawChatSessionTarget(
sessionKey: key,
agentID: OpenClawChatSessionKey.agentID(from: key) == nil ? owner : nil)
}
func sessionMatchesTarget(_ entry: OpenClawChatSessionEntry, target: OpenClawChatSessionTarget) -> Bool {
guard entry.key == target.sessionKey else { return false }
return target.agentID == nil ||
(entry.agentId ?? self.currentSessionSnapshot().deliveryAgentID) == target.agentID
}
var canRequestSessionCompact: Bool {
!self.isCompacting &&
!self.isSending &&
!self.hasBlockingRunActivity &&
!self.isAborting
}
struct SessionBranchSwitchActivity: Equatable {
let session: SessionSnapshot
let generation: UInt64
}
var isSwitchingSessionBranch: Bool {
self.sessionBranchSwitchActivity != nil
}
private enum SessionBranchesRefreshPurpose {
case readOnly
case reconcile
case finalizeMutation
}
public func refreshSessions(limit: Int? = nil) {
let context = self.currentSessionSnapshot()
Task { await self.fetchSessions(limit: limit, sessionSnapshot: context) }
}
func generatedNewSessionKey(agentID explicitAgentID: String? = nil) -> String {
let baseKey = "ios-\(UUID().uuidString.lowercased())"
guard let agentID = explicitAgentID ??
OpenClawChatSessionKey.agentID(from: sessionKey) ??
explicitSessionAgentID ??
activeAgentId ??
OpenClawChatSessionKey.agentID(from: resolvedMainSessionKey) ??
sessions.lazy.compactMap({ OpenClawChatSessionKey.agentID(from: $0.key) }).first
else {
return baseKey
}
return "agent:\(agentID):\(baseKey)"
}
/// Returns true only when a session switch happened (create or reset
/// fallback); callers keep UI like the new-session popover open on failure.
@discardableResult
func performStartNewSession(
agentID: String? = nil,
worktree: Bool,
worktreeBaseRef: String? = nil,
routeLease: OpenClawChatNewSessionRouteLease? = nil) async -> Bool
{
guard !self.isCreatingSession, self.canCreateSessionForImmediateSwitch() else { return false }
self.isCreatingSession = true
defer { self.isCreatingSession = false }
let initiatingSession = self.currentSessionSnapshot()
let normalizedAgentID = agentID?
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
let requestedAgentID = normalizedAgentID?.isEmpty == false ? normalizedAgentID : nil
let requested = self.generatedNewSessionKey(agentID: requestedAgentID)
// Only authoritative identities decide agent ownership; scanning the roster
// could adopt an unrelated agent and hand sessions.create a cross-agent parent.
let currentAgentID = (
OpenClawChatSessionKey.agentID(from: self.sessionKey) ??
self.explicitSessionAgentID ??
self.activeAgentId ??
OpenClawChatSessionKey.agentID(from: self.resolvedMainSessionKey))?
.lowercased()
let parentSessionKey = requestedAgentID == nil || requestedAgentID == currentAgentID
? self.sessionKey
: nil
let next: String
do {
let created = if let routeLease {
try await routeLease.createSession(
key: requested,
label: nil,
agentID: requestedAgentID,
parentSessionKey: parentSessionKey,
worktree: worktree ? true : nil,
worktreeBaseRef: worktree ? worktreeBaseRef : nil)
} else {
try await self.transport.createSession(
key: requested,
label: nil,
agentID: requestedAgentID,
parentSessionKey: parentSessionKey,
worktree: worktree ? true : nil,
worktreeBaseRef: worktree ? worktreeBaseRef : nil)
}
let createdKey = created.key.trimmingCharacters(in: .whitespacesAndNewlines)
next = createdKey.isEmpty ? requested : createdKey
} catch {
guard self.isCurrentSession(initiatingSession) else { return false }
if Self.isUnsupportedCreateSessionError(error) {
// Reset only mimics a plain new chat; agent/worktree selections were
// not honored, so advanced requests surface the error instead of
// silently resetting the current session.
guard requestedAgentID == nil, !worktree else {
chatUILogger.error("sessions.create unsupported; advanced options not honored")
self.errorText = error.localizedDescription
return false
}
guard self.canCreateSessionForImmediateSwitch() else { return false }
chatUILogger.info("sessions.create unsupported; falling back to sessions.reset")
await self.performReset()
return self.isCurrentSession(initiatingSession)
}
chatUILogger.error("sessions.create failed \(error.localizedDescription, privacy: .public)")
self.errorText = error.localizedDescription
return false
}
guard self.isCurrentSession(initiatingSession), self.canCreateSessionForImmediateSwitch() else {
if !self.sessions.contains(where: { $0.key == next }) { self.refreshSessions() }
return false
}
self.adoptCreatedSession(next)
return true
}
static func isUnsupportedCreateSessionError(_ error: Error) -> Bool {
let nsError = error as NSError
return nsError.domain == "OpenClawChatTransport"
&& nsError.localizedDescription == "sessions.create not supported by this transport"
}
@discardableResult
public func startNewSession(
agentID: String? = nil,
worktree: Bool = false,
worktreeBaseRef: String? = nil) async -> Bool
{
await self.performStartNewSession(
agentID: agentID,
worktree: worktree,
worktreeBaseRef: worktreeBaseRef)
}
@discardableResult
func startNewSession(
agentID: String,
worktree: Bool,
worktreeBaseRef: String?,
using routeLease: OpenClawChatNewSessionRouteLease) async -> Bool
{
await self.performStartNewSession(
agentID: agentID,
worktree: worktree,
worktreeBaseRef: worktreeBaseRef,
routeLease: routeLease)
}
func newSessionRouteLease() async throws -> OpenClawChatNewSessionRouteLease {
guard let routeLease = await self.transport.acquireNewSessionRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
return routeLease
}
public func fetchSessionGroups() async throws -> [OpenClawChatSessionGroup] {
let routeLease = try await self.sessionGroupsRouteLease()
return try await self.fetchSessionGroups(using: routeLease)
}
func sessionGroupsRouteLease() async throws -> OpenClawChatSessionGroupsRouteLease {
guard let routeLease = await self.transport.acquireSessionGroupsRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
return routeLease
}
func fetchSessionGroups(
using routeLease: OpenClawChatSessionGroupsRouteLease) async throws -> [OpenClawChatSessionGroup]
{
let response = try await routeLease.listGroups()
return (response?.groups ?? []).sorted { lhs, rhs in
lhs.position == rhs.position ? lhs.name < rhs.name : lhs.position < rhs.position
}
}
@discardableResult
func createSessionGroup(
named rawName: String,
using routeLease: OpenClawChatSessionGroupsRouteLease) async throws -> [OpenClawChatSessionGroup]
{
let name = rawName.trimmingCharacters(in: .whitespacesAndNewlines)
guard !name.isEmpty else { return try await self.fetchSessionGroups(using: routeLease) }
// Read-modify-write matches web group creation (app-sidebar-session-groups,
// custom-groups); the gateway has no atomic add/CAS. A concurrent edit can
// lose catalog names/order only — session categories are untouched by
// sessions.groups.put, so memberships survive. Accepted tradeoff until the
// gateway grows a revisioned groups API.
let current = try await self.fetchSessionGroups(using: routeLease)
let response = try await routeLease.putGroups(names: current.map(\.name) + [name])
self.sessionGroupsRevision += 1
return response.groups
}
@discardableResult
func renameSessionGroup(
_ name: String,
to rawName: String,
using routeLease: OpenClawChatSessionGroupsRouteLease) async throws -> [OpenClawChatSessionGroup]
{
let nextName = rawName.trimmingCharacters(in: .whitespacesAndNewlines)
guard !nextName.isEmpty else { return try await self.fetchSessionGroups(using: routeLease) }
let response = try await routeLease.renameGroup(name: name, to: nextName)
self.sessionGroupsRevision += 1
self.refreshSessions(limit: Self.sessionListFetchLimit)
return response.groups
}
@discardableResult
func deleteSessionGroup(
_ name: String,
using routeLease: OpenClawChatSessionGroupsRouteLease) async throws -> [OpenClawChatSessionGroup]
{
let response = try await routeLease.deleteGroup(name: name)
self.sessionGroupsRevision += 1
self.refreshSessions(limit: Self.sessionListFetchLimit)
return response.groups
}
public func setSessionGroup(key: String, group: String?, agentID: String? = nil) async throws {
let target = self.sessionMutationTarget(key: key, agentID: agentID)
let normalized = group?.trimmingCharacters(in: .whitespacesAndNewlines)
let nextGroup = normalized?.isEmpty == false ? normalized : nil
let routeLease = await self.transport.acquireSessionMutationRouteLease()
guard let routeLease else { throw OpenClawChatTransportSendError.notDispatched }
try await routeLease.patchSession(
key: key,
agentID: target.agentID,
label: nil,
category: .some(nextGroup),
pinned: nil,
archived: nil,
unread: nil)
if let index = self.sessions.firstIndex(where: { self.sessionMatchesTarget($0, target: target) }) {
self.sessions[index].category = nextGroup
}
self.refreshSessions(limit: Self.sessionListFetchLimit)
}
func performSessionBatch(
sessions selectedSessions: [OpenClawChatSessionEntry],
action: ChatSessionBatchAction) async -> ChatSessionBatchResult
{
let orderedKeys = selectedSessions.map(\.key)
let presentation = self.currentSessionSnapshot()
let entries = Dictionary(uniqueKeysWithValues: selectedSessions.map { ($0.key, $0) })
let targets = Dictionary(uniqueKeysWithValues: selectedSessions.map {
($0.key, self.sessionMutationTarget(key: $0.key, agentID: $0.agentId))
})
let mainSessionKey = self.resolvedMainSessionKey
let attachmentBlockedKeys = self.isAttachmentOwnerPinned
? Set(selectedSessions.filter {
self.matchesCurrentSessionKey(incoming: $0.key, current: self.sessionKey)
}.map(\.key))
: []
let routeLease = await self.transport.acquireSessionMutationRouteLease()
guard let routeLease else {
return ChatSessionBatchResult(
succeededKeys: [],
errorsByKey: Dictionary(uniqueKeysWithValues: orderedKeys.map {
($0, String(localized: "Gateway changed before the thread operation started."))
}))
}
let result = await ChatSessionBatchMutationRunner.run(keys: orderedKeys) { key in
if let entry = entries[key] {
switch action {
case .archive where !ChatSessionSidebarModel.canArchiveSession(
entry,
mainSessionKey: mainSessionKey):
throw ChatSessionBatchValidationError.cannotArchive
case .delete where !ChatSessionSidebarModel.canDeleteSession(
key: key,
mainSessionKey: mainSessionKey):
throw ChatSessionBatchValidationError.cannotDelete
case .archive where attachmentBlockedKeys.contains(key),
.delete where attachmentBlockedKeys.contains(key):
throw ChatSessionBatchValidationError.attachmentOwnerPinned
default:
break
}
}
switch action {
case .pin:
try await routeLease.patchSession(
key: key,
agentID: targets[key]?.agentID,
label: nil,
category: nil,
pinned: true,
archived: nil,
unread: nil)
case .unpin:
try await routeLease.patchSession(
key: key,
agentID: targets[key]?.agentID,
label: nil,
category: nil,
pinned: false,
archived: nil,
unread: nil)
case .archive:
guard let expectedSessionID = entries[key]?.sessionId?
.trimmingCharacters(in: .whitespacesAndNewlines),
!expectedSessionID.isEmpty
else {
throw ChatSessionBatchValidationError.cannotArchive
}
try await routeLease.patchSession(
key: key,
agentID: targets[key]?.agentID,
expectedSessionID: expectedSessionID,
label: nil,
category: nil,
pinned: nil,
archived: true,
unread: nil)
case .delete:
try await routeLease.deleteSession(key: key, agentID: targets[key]?.agentID)
}
}
let succeeded = Set(result.succeededKeys)
guard self.isCurrentSession(presentation) else {
self.refreshSessions(limit: Self.sessionListFetchLimit)
return result
}
switch action {
case .pin, .unpin:
let pinned = action == .pin
for index in self.sessions.indices {
let entry = self.sessions[index]
guard succeeded.contains(entry.key), let target = targets[entry.key],
self.sessionMatchesTarget(entry, target: target) else { continue }
self.sessions[index].pinned = pinned
self.sessions[index].pinnedAt = pinned ? Date().timeIntervalSince1970 * 1000 : nil
}
self.sessions = OpenClawChatSessionListOrganizer.organize(self.sessions)
case .archive, .delete:
self.sessions.removeAll {
guard succeeded.contains($0.key), let target = targets[$0.key] else { return false }
return self.sessionMatchesTarget($0, target: target)
}
if succeeded.contains(where: {
self.matchesCurrentSessionKey(incoming: $0, agentId: targets[$0]?.agentID, current: self.sessionKey)
}) {
self.switchSession(to: self.resolvedMainSessionKey)
}
}
self.refreshSessions(limit: Self.sessionListFetchLimit)
return result
}
public func requestSessionReset() {
Task { await self.performReset() }
}
public func requestSessionCompact() {
Task { await self.performCompact() }
}
public func fetchSessionList(search: String?, archived: Bool) async -> [OpenClawChatSessionEntry] {
let session = self.currentSessionSnapshot()
let normalizedSearch = search?.trimmingCharacters(in: .whitespacesAndNewlines)
let query = normalizedSearch?.isEmpty == false ? normalizedSearch : nil
do {
let res = try await self.transport.listSessions(
limit: Self.sessionListFetchLimit,
search: query,
archived: archived,
agentID: session.deliveryAgentID)
guard self.isCurrentSession(session) else { return [] }
return OpenClawChatSessionListOrganizer.organize(res.sessions)
} catch {
// A superseded (cancelled) fetch must not produce fallback rows;
// the newer task owns the scoped list. Callers also guard on
// Task.isCancelled before applying results.
guard self.isCurrentSession(session), !(error is CancellationError), !Task.isCancelled else { return [] }
guard !archived else { return [] }
guard let query else { return self.sessions }
return OpenClawChatSessionListOrganizer.filter(self.sessions, search: query)
}
}
public func renameSession(key: String, label: String, agentID: String? = nil) {
let target = self.sessionMutationTarget(key: key, agentID: agentID)
let transport = self.transport
let presentation = self.currentSessionSnapshot()
let trimmed = label.trimmingCharacters(in: .whitespacesAndNewlines)
let nextLabel: String? = trimmed.isEmpty ? nil : trimmed
let previous = self.sessions
if let index = self.sessions.firstIndex(where: { self.sessionMatchesTarget($0, target: target) }) {
self.sessions[index].label = nextLabel
self.sessions[index].displayName = nextLabel
}
Task {
do {
guard let routeLease = await transport.acquireSessionMutationRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
try await routeLease.patchSession(
key: key,
agentID: target.agentID,
expectedSessionID: nil,
label: .some(nextLabel),
category: nil,
color: nil,
pinned: nil,
archived: nil,
unread: nil)
self.refreshSessions()
} catch {
guard self.isCurrentSession(presentation) else { return }
self.sessions = self.applyingLocalUnreadOverrides(to: previous)
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.patch(label) failed \(error.localizedDescription, privacy: .public)")
}
}
}
public func forkSession(key: String, fromLastCompleted: Bool? = nil, agentID: String? = nil) async {
guard self.canCreateSessionForImmediateSwitch() else { return }
let target = self.sessionMutationTarget(key: key, agentID: agentID)
let initiatingSession = self.currentSessionSnapshot()
do {
let stableBoundary = fromLastCompleted ??
(self.sessions.first(where: { $0.key == key })?.hasActiveRun == true)
let createdKey = try await self.transport.forkSession(
parentKey: key,
fromLastCompleted: stableBoundary,
agentID: target.agentID)
.trimmingCharacters(in: .whitespacesAndNewlines)
guard !createdKey.isEmpty else { return }
guard self.isCurrentSession(initiatingSession), self.canCreateSessionForImmediateSwitch() else {
self.refreshSessions(limit: Self.sessionListFetchLimit)
return
}
self.switchSession(to: createdKey)
} catch {
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.create(fork) failed \(error.localizedDescription, privacy: .public)")
}
}
public func rewindToMessage(_ message: OpenClawChatMessage) async {
guard let entryID = Self.sessionMutationEntryID(for: message) else { return }
guard self.canPerformMessageSessionAction else { return }
let initiatingSession = self.currentSessionSnapshot()
guard await self.beginOutboxSessionMutation(initiatingSession) else { return }
guard self.isCurrentSession(initiatingSession) else {
await self.cancelOutboxSessionMutation(initiatingSession)
return
}
do {
let result = try await self.transport.rewindSession(
sessionKey: initiatingSession.key,
entryId: entryID)
guard self.isCurrentSession(initiatingSession) else {
await self.recoverOutboxAfterSessionMutationRefreshFailure(
initiatingSession,
branchingUnsupported: false)
return
}
self.replyTarget = nil
self.runMessageScopesByRunID.removeAll()
self.provisionalFinalMessagesByID.removeAll()
self.input = result.editorText ?? ""
self.restoreEditorAttachments(result.editorAttachments)
let historyRequest = self.beginHistoryRequest(for: initiatingSession)
_ = await self.refreshHistoryAfterRun(historyRequest: historyRequest)
guard self.isCurrentSession(initiatingSession) else {
await self.recoverOutboxAfterSessionMutationRefreshFailure(
initiatingSession,
branchingUnsupported: false)
return
}
await self.refreshSessionBranches(confirmingBranchChange: true)
} catch {
await self.cancelOutboxSessionMutation(initiatingSession)
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.rewind failed \(error.localizedDescription, privacy: .public)")
}
}
@discardableResult
public func refreshSessionBranches(confirmingBranchChange: Bool = false) async -> Bool {
let session = self.currentSessionSnapshot()
let refreshGeneration = self.beginSessionBranchesRefresh()
let previousState = await self.captureOutboxBranchState(for: session)
return await self.performSessionBranchesRefresh(
for: session,
refreshGeneration: refreshGeneration,
previousState: previousState,
purpose: confirmingBranchChange ? .finalizeMutation : .readOnly)
}
func refreshSessionBranches(
for session: SessionSnapshot,
preBootstrapBranchState: OpenClawChatOutboxBranchState?) async -> Bool
{
let refreshGeneration = self.beginSessionBranchesRefresh()
return await self.performSessionBranchesRefresh(
for: session,
refreshGeneration: refreshGeneration,
previousState: preBootstrapBranchState,
purpose: .reconcile)
}
private func beginSessionBranchesRefresh() -> UInt64 {
self.sessionBranchesRefreshGeneration &+= 1
self.isLoadingSessionBranches = true
return self.sessionBranchesRefreshGeneration
}
private func performSessionBranchesRefresh(
for session: SessionSnapshot,
refreshGeneration: UInt64,
previousState: OpenClawChatOutboxBranchState?,
purpose: SessionBranchesRefreshPurpose) async -> Bool
{
let connectionGeneration = self.outboxBranchConnectionGeneration
defer {
if self.isCurrentSession(session),
refreshGeneration == self.sessionBranchesRefreshGeneration
{
self.isLoadingSessionBranches = false
}
}
do {
let response = try await self.requestSessionBranchListing(
sessionKey: session.key,
agentID: self.outboxAgentID(for: session))
guard self.isCurrentSession(session),
refreshGeneration == self.sessionBranchesRefreshGeneration,
connectionGeneration == self.outboxBranchConnectionGeneration
else {
if case .finalizeMutation = purpose {
await self.recoverOutboxAfterSessionMutationRefreshFailure(
session,
branchingUnsupported: false)
}
return false
}
switch purpose {
case .readOnly:
if let outbox = self.outbox,
let scope = self.outboxBranchScope(for: session),
let expectedEpoch = previousState?.epoch,
let activeLeafEntryID = Self.activeBranchLeafEntryID(in: response.branches)
{
_ = await outbox.updateLastActiveLeafEntryID(
activeLeafEntryID,
expectedEpoch: expectedEpoch,
for: scope)
}
case .reconcile:
guard await self.reconcileOutboxBranchScope(
session,
branches: response.branches,
previousState: previousState,
connectionGeneration: connectionGeneration)
else {
self.pauseOutboxBranchScope(session)
return false
}
case .finalizeMutation:
guard let activeLeafEntryID = Self.activeBranchLeafEntryID(in: response.branches),
await self.confirmOutboxBranchChange(
session,
activeLeafEntryID: activeLeafEntryID)
else {
await self.recoverOutboxAfterSessionMutationRefreshFailure(
session,
branchingUnsupported: false)
return false
}
}
guard self.isCurrentSession(session),
refreshGeneration == self.sessionBranchesRefreshGeneration
else { return false }
self.sessionBranches = response.branches
self.flushOutboxIfNeeded()
return true
} catch {
guard self.isCurrentSession(session),
refreshGeneration == self.sessionBranchesRefreshGeneration,
connectionGeneration == self.outboxBranchConnectionGeneration
else {
if case .finalizeMutation = purpose {
await self.recoverOutboxAfterSessionMutationRefreshFailure(
session,
branchingUnsupported: false)
}
return false
}
chatSessionActionsLogger.debug(
"sessions.branches.list failed \(error.localizedDescription, privacy: .public)")
let branchingUnsupported = Self.branchListingIsUnsupported(error)
switch purpose {
case .readOnly:
break
case .reconcile where branchingUnsupported:
self.allowOutboxReplayWithoutBranching(session)
case .reconcile:
self.pauseOutboxBranchScope(session)
case .finalizeMutation:
await self.recoverOutboxAfterSessionMutationRefreshFailure(
session,
branchingUnsupported: branchingUnsupported)
}
return false
}
}
func refreshSessionBranchesForMenuPresentation() async {
await self.refreshSessionBranches()
}
var canSwitchSessionBranch: Bool {
!self.hasBlockingRunActivity &&
!self.isSending &&
!self.isAborting &&
!self.hasUnresolvedOutboxCommandsForCurrentSession
}
var canPerformMessageSessionAction: Bool {
!self.hasBlockingRunActivity &&
!self.isSending &&
!self.isAborting &&
!self.hasPendingOutboxCommandsForCurrentSession
}
public func switchToBranch(_ leafEntryId: String) async {
let normalizedLeafEntryID = leafEntryId.trimmingCharacters(in: .whitespacesAndNewlines)
guard !normalizedLeafEntryID.isEmpty else { return }
guard self.canSwitchSessionBranch else { return }
guard !self.sessionBranches.contains(where: {
$0.leafEntryId == normalizedLeafEntryID && $0.active
}) else { return }
let initiatingSession = self.currentSessionSnapshot()
let switchActivity = self.beginSessionBranchSwitchActivity(for: initiatingSession)
defer { self.endSessionBranchSwitchActivity(switchActivity) }
guard await self.beginOutboxSessionMutation(initiatingSession) else {
return
}
guard self.isCurrentSessionBranchSwitchActivity(switchActivity) else {
await self.cancelOutboxSessionMutation(initiatingSession)
return
}
do {
try await self.transport.switchSessionBranch(
sessionKey: initiatingSession.key,
agentID: self.outboxAgentID(for: initiatingSession),
leafEntryId: normalizedLeafEntryID)
guard self.isCurrentSessionBranchSwitchActivity(switchActivity) else {
if await self.confirmOutboxBranchChange(
initiatingSession,
activeLeafEntryID: normalizedLeafEntryID) == false
{
await self.recoverOutboxAfterSessionMutationRefreshFailure(
initiatingSession,
branchingUnsupported: false)
}
return
}
self.replyTarget = nil
self.runMessageScopesByRunID.removeAll()
self.provisionalFinalMessagesByID.removeAll()
await self.reconcileSessionBranchChange(
switchActivity,
confirmedLeafEntryID: normalizedLeafEntryID)
} catch {
await self.cancelOutboxSessionMutation(initiatingSession)
guard self.isCurrentSessionBranchSwitchActivity(switchActivity) else { return }
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.branches.switch failed \(error.localizedDescription, privacy: .public)")
}
}
/// Dispatch parity with forkSession(key:): per-request server-lease guards in the
/// transport plus post-RPC staleness re-checks, not the patch-flow route lease,
/// which does not expose fork/rewind and would widen the lease API for no sibling.
public func forkAtMessage(_ message: OpenClawChatMessage) async {
guard let entryID = Self.sessionMutationEntryID(for: message) else { return }
guard self.canPerformMessageSessionAction else { return }
guard self.canCreateSessionForImmediateSwitch() else { return }
let initiatingSession = self.currentSessionSnapshot()
guard await self.beginOutboxSessionMutation(initiatingSession) else { return }
guard self.isCurrentSession(initiatingSession), self.canCreateSessionForImmediateSwitch() else {
await self.cancelOutboxSessionMutation(initiatingSession)
return
}
do {
let result = try await self.transport.forkSessionAtMessage(
sessionKey: initiatingSession.key,
entryId: entryID)
// Fork leaves the source transcript unchanged, so its lease is only an entry gate.
// Rewind repoints the source scope and confirms an epoch change instead.
await self.cancelOutboxSessionMutation(initiatingSession)
let createdKey = result.sessionKey.trimmingCharacters(in: .whitespacesAndNewlines)
guard !createdKey.isEmpty else { return }
guard self.isCurrentSession(initiatingSession),
!self.hasBlockingRunActivity,
!self.isSending,
!self.isAborting,
self.canCreateSessionForImmediateSwitch()
else {
self.refreshSessions(limit: Self.sessionListFetchLimit)
return
}
self.switchSession(to: createdKey)
guard self.sessionKey == createdKey else { return }
self.input = result.editorText ?? ""
self.restoreEditorAttachments(result.editorAttachments)
} catch {
await self.cancelOutboxSessionMutation(initiatingSession)
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.fork failed \(error.localizedDescription, privacy: .public)")
}
}
private static func sessionMutationEntryID(for message: OpenClawChatMessage) -> String? {
guard message.role.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() == "user"
else { return nil }
let entryID = message.transcriptMessageID?.trimmingCharacters(in: .whitespacesAndNewlines)
return entryID?.isEmpty == false ? entryID : nil
}
public func setSessionUnread(key: String, unread: Bool, agentID: String? = nil) {
let target = self.sessionMutationTarget(key: key, agentID: agentID)
let identityKey = self.sessionMutationIdentity(for: key, agentID: target.agentID)
let transport = self.transport
let previousEntry = self.sessions.first(where: { self.sessionMatchesTarget($0, target: target) })
let rollbackUnread = self.unreadPatchGuard.confirmedUnread(key: identityKey) ?? previousEntry?.unread
let revision = self.unreadPatchGuard.beginExplicitPatch(
key: identityKey,
unread: unread,
isActive: self.matchesCurrentSessionKey(incoming: key, agentId: target.agentID, current: self.sessionKey))
if let index = self.sessions.firstIndex(where: { self.sessionMatchesTarget($0, target: target) }) {
self.sessions[index].unread = unread
}
let routeLease = Task { await transport.acquireSessionMutationRouteLease() }
let operation = self.unreadMutationQueue.reserve(
routeLease: routeLease,
queueKey: identityKey,
routeKey: key,
agentID: target.agentID,
unread: unread)
Task {
do {
try await operation.value
guard self.unreadPatchGuard.patchSucceeded(
key: identityKey,
unread: unread,
revision: revision)
else { return }
self.refreshSessions()
} catch {
guard self.unreadPatchGuard.patchFailed(key: identityKey, revision: revision) else { return }
if let index = self.sessions.firstIndex(where: { self.sessionMatchesTarget($0, target: target) }),
self.sessions[index].unread == unread
{
self.sessions[index].unread = rollbackUnread
}
self.refreshSessions()
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.patch(unread) failed \(error.localizedDescription, privacy: .public)")
}
}
}
public func setSessionColor(key: String, color: String?, agentID: String? = nil) async {
let target = self.sessionMutationTarget(key: key, agentID: agentID)
do {
let routeLease = await self.transport.acquireSessionMutationRouteLease()
guard let routeLease else { throw OpenClawChatTransportSendError.notDispatched }
try await routeLease.patchSession(
key: key,
agentID: target.agentID,
label: nil,
category: nil,
color: .some(color),
pinned: nil,
archived: nil,
unread: nil)
self.refreshSessions(limit: Self.sessionListFetchLimit)
} catch {
self.errorText = error.localizedDescription
}
}
public func setSessionPinned(key: String, pinned: Bool, agentID: String? = nil) {
let target = self.sessionMutationTarget(key: key, agentID: agentID)
let transport = self.transport
let presentation = self.currentSessionSnapshot()
let previous = self.sessions
if let index = self.sessions.firstIndex(where: { self.sessionMatchesTarget($0, target: target) }) {
self.sessions[index].pinned = pinned
self.sessions[index].pinnedAt = pinned ? Date().timeIntervalSince1970 * 1000 : nil
self.sessions = OpenClawChatSessionListOrganizer.organize(self.sessions)
}
Task {
do {
guard let routeLease = await transport.acquireSessionMutationRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
try await routeLease.patchSession(
key: key,
agentID: target.agentID,
expectedSessionID: nil,
label: nil,
category: nil,
color: nil,
pinned: pinned,
archived: nil,
unread: nil)
self.refreshSessions()
} catch {
guard self.isCurrentSession(presentation) else { return }
self.sessions = self.applyingLocalUnreadOverrides(to: previous)
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.patch(pinned) failed \(error.localizedDescription, privacy: .public)")
}
}
}
public func setSessionArchived(_ session: OpenClawChatSessionEntry, archived: Bool) {
let key = session.key
let target = self.sessionMutationTarget(key: key, agentID: session.agentId)
let transport = self.transport
let presentation = self.currentSessionSnapshot()
guard archived else {
Task { await self.restoreSession(session) }
return
}
guard let expectedSessionID = session.sessionId?
.trimmingCharacters(in: .whitespacesAndNewlines),
!expectedSessionID.isEmpty
else {
self.errorText = "Session lifecycle action requires a durable session identity."
return
}
let previous = self.sessions
self.sessions.removeAll { self.sessionMatchesTarget($0, target: target) }
Task {
do {
guard let routeLease = await transport.acquireSessionMutationRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
try await routeLease.patchSession(
key: key,
agentID: target.agentID,
expectedSessionID: expectedSessionID,
label: nil,
category: nil,
color: nil,
pinned: nil,
archived: true,
unread: nil)
if self.matchesCurrentSessionKey(incoming: key, agentId: target.agentID, current: self.sessionKey) {
// The archived session rejects new sends; move the user back
// to the main session instead of leaving a dead composer.
self.switchSession(to: self.resolvedMainSessionKey)
}
self.refreshSessions()
} catch {
guard self.isCurrentSession(presentation) else { return }
self.sessions = self.applyingLocalUnreadOverrides(to: previous)
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.patch(archived) failed \(error.localizedDescription, privacy: .public)")
}
}
}
/// Restores an archived session. Returns false (with `errorText` set) on
/// failure so open-flows can avoid switching into a still-archived session.
@discardableResult
public func restoreSession(_ session: OpenClawChatSessionEntry) async -> Bool {
let target = self.sessionMutationTarget(key: session.key, agentID: session.agentId)
guard let expectedSessionID = session.sessionId?
.trimmingCharacters(in: .whitespacesAndNewlines),
!expectedSessionID.isEmpty
else {
self.errorText = "Session lifecycle action requires a durable session identity."
return false
}
do {
guard let routeLease = await self.transport.acquireSessionMutationRouteLease() else {
throw OpenClawChatTransportSendError.notDispatched
}
try await routeLease.patchSession(
key: session.key,
agentID: target.agentID,
expectedSessionID: expectedSessionID,
label: nil,
category: nil,
color: nil,
pinned: nil,
archived: false,
unread: nil)
self.refreshSessions()
return true
} catch {
self.errorText = error.localizedDescription
chatSessionActionsLogger.error(
"sessions.patch(archived=false) failed \(error.localizedDescription, privacy: .public)")
return false
}
}
func markCurrentSessionReadAfterActivation(
_ session: SessionSnapshot,
fallbackEntry: OpenClawChatSessionEntry?) async
{
guard self.isCurrentSession(session), self.hasAppliedLiveHistory,
let entry = self.currentSessionEntry() ?? fallbackEntry,
let revision = self.unreadPatchGuard.shouldPatch(
key: self.sessionMutationIdentity(for: entry.key, listedKey: entry.key, agentID: entry.agentId),
unread: entry.unread,
markedUnreadAt: entry.markedUnreadAt)
else { return }
let identityKey = self.sessionMutationIdentity(for: entry.key, listedKey: entry.key, agentID: entry.agentId)
let target = self.sessionMutationTarget(key: entry.key, agentID: entry.agentId)
let transport = self.transport
let routeLease = Task { await transport.acquireSessionMutationRouteLease() }
let operation = self.unreadMutationQueue.reserve(
routeLease: routeLease,
queueKey: identityKey,
routeKey: entry.key,
agentID: target.agentID,
expectedMarkedUnreadAt: .some(entry.markedUnreadAt),
unread: false)
do {
try await operation.value
guard self.unreadPatchGuard.patchSucceeded(
key: identityKey,
unread: false,
revision: revision)
else { return }
self.refreshSessions()
} catch {
guard self.unreadPatchGuard.patchFailed(key: identityKey, revision: revision) else { return }
chatSessionActionsLogger.error(
"sessions.patch(unread=false) failed \(error.localizedDescription, privacy: .public)")
}
}
}
|