feat(swift-ios): add server-backed thread pinning

This commit is contained in:
Theo Browne committed 2026-08-09 20:52:53 -07:00
1 parent a4a0871a0b
commit cb12d183ce
15 files changed
+237 -15

No files matched your search

@@ -1011,6 +1011,12 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging,
try? await refresh(client: route.client)
}
func setThreadPinned(id: String, pinned: Bool) async throws {
let route = try threadRoute(for: id)
_ = try await route.client.pin(threadID: route.wireID, pinned: pinned)
try? await refresh(client: route.client)
}
func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws {
let route = try threadRoute(for: id)
_ = try await route.client.setRuntimeMode(
@@ -1737,6 +1743,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging,
settledAt: thread.settledAt,
snoozedUntil: thread.snoozedUntil,
snoozedAt: thread.snoozedAt,
pinnedAt: thread.pinnedAt,
session: thread.session,
latestUserMessageAt: thread.latestUserMessageAt,
hasPendingApprovals: thread.hasPendingApprovals,
@@ -3151,6 +3158,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging,
),
snoozedUntil: thread.snoozedUntil.map(parseDate),
snoozedAt: thread.snoozedAt.map(parseDate),
pinnedAt: thread.pinnedAt.map(parseDate),
supportsPinning: environment.descriptor?.capabilities.threadPinning,
attentionAt: failureDate(
latestTurn: thread.latestTurn,
session: thread.session
@@ -3207,6 +3216,8 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging,
),
snoozedUntil: thread.snoozedUntil.map(parseDate),
snoozedAt: thread.snoozedAt.map(parseDate),
pinnedAt: thread.pinnedAt.map(parseDate),
supportsPinning: environment.descriptor?.capabilities.threadPinning,
attentionAt: failureDate(
latestTurn: thread.latestTurn,
session: thread.session
@@ -4713,6 +4724,7 @@ enum NativeThreadDetailReducer {
settledAt: thread.settledAt,
snoozedUntil: thread.snoozedUntil,
snoozedAt: thread.snoozedAt,
pinnedAt: thread.pinnedAt,
deletedAt: thread.deletedAt,
messages: messages ?? thread.messages,
activities: activities ?? thread.activities,
+5
View File
@@ -42,6 +42,7 @@ public struct EnvironmentDescriptor: Codable, Equatable, Sendable {
public let connectionProbe: Bool?
public let threadSettlement: Bool?
public let threadSnooze: Bool?
public let threadPinning: Bool?
public let threadTitleRegeneration: Bool?
public let serverSelfUpdate: String?
public let serverSelfUpdateProgress: Bool?
@@ -51,6 +52,7 @@ public struct EnvironmentDescriptor: Codable, Equatable, Sendable {
case connectionProbe
case threadSettlement
case threadSnooze
case threadPinning
case threadTitleRegeneration
case serverSelfUpdate
case serverSelfUpdateProgress
@@ -63,6 +65,7 @@ public struct EnvironmentDescriptor: Codable, Equatable, Sendable {
connectionProbe = try container.decodeIfPresent(Bool.self, forKey: .connectionProbe)
threadSettlement = try container.decodeIfPresent(Bool.self, forKey: .threadSettlement)
threadSnooze = try container.decodeIfPresent(Bool.self, forKey: .threadSnooze)
threadPinning = try container.decodeIfPresent(Bool.self, forKey: .threadPinning)
threadTitleRegeneration = try container.decodeIfPresent(
Bool.self,
forKey: .threadTitleRegeneration
@@ -333,6 +336,7 @@ public struct OrchestrationThreadShell: Codable, Identifiable, Equatable, Sendab
public let settledAt: String?
public let snoozedUntil: String?
public let snoozedAt: String?
public let pinnedAt: String?
public let session: OrchestrationSession?
public let latestUserMessageAt: String?
public let hasPendingApprovals: Bool
@@ -404,6 +408,7 @@ public struct OrchestrationThread: Codable, Identifiable, Equatable, Sendable {
public let settledAt: String?
public let snoozedUntil: String?
public let snoozedAt: String?
public let pinnedAt: String?
public let deletedAt: String?
public let messages: [OrchestrationMessage]
public let activities: [OrchestrationActivity]
+17
View File
@@ -359,6 +359,11 @@ public actor T3Client {
)
}
@discardableResult
public func pin(threadID: String, pinned: Bool) async throws -> DispatchResult {
try await dispatch(OrchestrationCommands.pin(threadID: threadID, pinned: pinned))
}
@discardableResult
public func setRuntimeMode(
threadID: String,
@@ -1403,6 +1408,18 @@ public enum OrchestrationCommands {
])
}
public static func pin(
threadID: String,
pinned: Bool,
commandID: String = UUID().uuidString
) -> JSONValue {
basic(
type: pinned ? "thread.pin" : "thread.unpin",
threadID: threadID,
commandID: commandID
)
}
public static func setRuntimeMode(
threadID: String,
mode: RuntimeMode,
@@ -228,6 +228,21 @@ public struct ThreadDetailView: View {
}
}
Section {
if currentThread.supportsPinning == true, !currentThread.isArchived {
Button {
Task {
await model.setPinned(
thread.id,
pinned: currentThread.pinnedAt == nil
)
}
} label: {
Label(
currentThread.pinnedAt == nil ? "Pin" : "Unpin",
systemImage: currentThread.pinnedAt == nil ? "pin" : "pin.slash"
)
}
}
Button {
Task { _ = await model.detail(for: thread.id, force: true) }
} label: {
@@ -274,6 +274,9 @@ public final class FeatureRootModel {
$0.isSettled = settled
$0.keepsActive = !settled
$0.settledAt = settledAt
if settled {
$0.pinnedAt = nil
}
}
}
}
@@ -291,6 +294,24 @@ public final class FeatureRootModel {
}
}
public func setPinned(_ id: String, pinned: Bool) async {
let environment = currentEnvironmentIdentity
await perform {
try await client.setThreadPinned(id: id, pinned: pinned)
guard currentEnvironmentIdentity == environment else { return }
mutateThread(id: id) {
$0.pinnedAt = pinned ? Date.now : nil
if pinned {
$0.isSettled = false
$0.keepsActive = true
$0.settledAt = nil
$0.snoozedUntil = nil
$0.snoozedAt = nil
}
}
}
}
public func setRuntimeMode(_ id: String, mode: FeatureRuntimeMode) async {
let mode = mode.mobileNormalized
let environment = currentEnvironmentIdentity
@@ -55,6 +55,7 @@ public protocol FeatureClient: AnyObject {
func setThreadArchived(id: String, archived: Bool) async throws
func setThreadSettled(id: String, settled: Bool) async throws
func setThreadSnoozed(id: String, until: Date?) async throws
func setThreadPinned(id: String, pinned: Bool) async throws
func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws
func setInteractionMode(id: String, mode: FeatureInteractionMode) async throws
func deleteThread(id: String) async throws
@@ -132,6 +133,7 @@ public extension FeatureClient {
}
func setThreadSettled(id: String, settled: Bool) async throws {}
func setThreadSnoozed(id: String, until: Date?) async throws {}
func setThreadPinned(id: String, pinned: Bool) async throws {}
func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws {}
func setInteractionMode(id: String, mode: FeatureInteractionMode) async throws {}
@@ -144,6 +144,8 @@ public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codabl
public var lastActivityAt: Date?
public var snoozedUntil: Date?
public var snoozedAt: Date?
public var pinnedAt: Date?
public var supportsPinning: Bool?
public var attentionAt: Date?
public var workingStartedAt: Date?
public var latestTurnCompletedAt: Date?
@@ -174,6 +176,8 @@ public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codabl
lastActivityAt: Date? = nil,
snoozedUntil: Date? = nil,
snoozedAt: Date? = nil,
pinnedAt: Date? = nil,
supportsPinning: Bool? = nil,
attentionAt: Date? = nil,
workingStartedAt: Date? = nil,
latestTurnCompletedAt: Date? = nil,
@@ -203,6 +207,8 @@ public struct FeatureThread: Identifiable, Sendable, Equatable, Hashable, Codabl
self.lastActivityAt = lastActivityAt
self.snoozedUntil = snoozedUntil
self.snoozedAt = snoozedAt
self.pinnedAt = pinnedAt
self.supportsPinning = supportsPinning
self.attentionAt = attentionAt
self.workingStartedAt = workingStartedAt
self.latestTurnCompletedAt = latestTurnCompletedAt
@@ -168,6 +168,7 @@ enum DailyUXCreationContext {
}
struct DailyUXSidebarIndex {
let pinned: [FeatureThread]
let active: [FeatureThread]
let snoozed: [FeatureThread]
let settled: [FeatureThread]
@@ -195,14 +196,13 @@ struct DailyUXSidebarIndex {
}
let available = visible.filter { !$0.isEffectivelySnoozed(at: now) }
pinned = available
.filter { $0.pinnedAt != nil }
.sorted(by: Self.creationOrder)
active = available
.filter { !$0.isEffectivelySettled(at: now) }
.sorted { lhs, rhs in
if lhs.createdAt != rhs.createdAt {
return lhs.createdAt > rhs.createdAt
}
return lhs.id < rhs.id
}
.filter { $0.pinnedAt == nil && !$0.isEffectivelySettled(at: now) }
.sorted(by: Self.creationOrder)
snoozed = visible
.filter { $0.isEffectivelySnoozed(at: now) }
@@ -216,7 +216,7 @@ struct DailyUXSidebarIndex {
}
settled = available
.filter { $0.isEffectivelySettled(at: now) }
.filter { $0.pinnedAt == nil && $0.isEffectivelySettled(at: now) }
.sorted { lhs, rhs in
if lhs.settledSortDate != rhs.settledSortDate {
return lhs.settledSortDate > rhs.settledSortDate
@@ -225,12 +225,19 @@ struct DailyUXSidebarIndex {
}
searchResults = Self.matchingThreads(
active + snoozed + settled,
pinned + active + snoozed + settled,
snapshot: snapshot,
query: query
)
}
private static func creationOrder(_ lhs: FeatureThread, _ rhs: FeatureThread) -> Bool {
if lhs.createdAt != rhs.createdAt {
return lhs.createdAt > rhs.createdAt
}
return lhs.id < rhs.id
}
static func matchingThreads(
_ candidates: [FeatureThread],
snapshot: FeatureSnapshot,
@@ -283,6 +290,7 @@ enum DailyUXSidebarRefresh {
) -> Date? {
guard !thread.isArchived,
!thread.isSettled,
thread.pinnedAt == nil,
!thread.keepsActive,
let lastActivityAt = thread.lastActivityAt else {
return nil
@@ -21,6 +21,7 @@ struct HomeThreadCollectionView: UIViewRepresentable {
let onArchive: (FeatureThread, Bool) -> Void
let onSettle: (FeatureThread, Bool) -> Void
let onSnooze: (FeatureThread, Date?) -> Void
let onPin: (FeatureThread, Bool) -> Void
let onDelete: (FeatureThread) -> Void
func makeCoordinator() -> Coordinator {
@@ -163,7 +164,7 @@ struct HomeThreadCollectionView: UIViewRepresentable {
case .showMoreSettled:
collectionView.deselectItem(at: indexPath, animated: false)
parent.onShowMoreSettled()
case .empty, .searchEmpty:
case .empty, .searchEmpty, .pinnedDivider:
collectionView.deselectItem(at: indexPath, animated: false)
}
}
@@ -203,6 +204,14 @@ struct HomeThreadCollectionView: UIViewRepresentable {
}
primaryAction.image = UIImage(systemName: "arrow.uturn.backward")
primaryAction.backgroundColor = .systemBlue
} else if thread.pinnedAt != nil, thread.supportsPinning == true {
primaryAction = UIContextualAction(style: .normal, title: "Unpin") {
[weak self] _, _, finish in
self?.parent.onPin(thread, false)
finish(true)
}
primaryAction.image = UIImage(systemName: "pin.slash")
primaryAction.backgroundColor = .systemBlue
} else {
let isSettled = thread.isEffectivelySettled(at: .now)
primaryAction = UIContextualAction(
@@ -297,6 +306,9 @@ struct HomeThreadCollectionView: UIViewRepresentable {
cell.accessibilityValue = nil
cell.accessibilityHint = nil
cell.onAccessibilityActivate = nil
case .pinnedDivider:
cell.isAccessibilityElement = false
cell.onAccessibilityActivate = nil
}
}
@@ -367,6 +379,17 @@ struct HomeThreadCollectionView: UIViewRepresentable {
var actions: [UIMenuElement] = [rename, archive]
if !isArchived {
if thread.supportsPinning == true {
let isPinned = thread.pinnedAt != nil
actions.append(
UIAction(
title: isPinned ? "Unpin" : "Pin",
image: UIImage(systemName: isPinned ? "pin.slash" : "pin")
) { [weak self] _ in
self?.parent.onPin(thread, !isPinned)
}
)
}
let isSettled = thread.isEffectivelySettled(at: .now)
actions.append(
UIAction(
@@ -449,9 +472,22 @@ struct HomeThreadCollectionView: UIViewRepresentable {
}
}
var items: [HomeCollectionItem] = presentation.active.isEmpty
? [.empty(.active)]
: presentation.active.map {
var items = presentation.pinned.map {
HomeCollectionItem.thread(
$0,
presentation.rowContexts[$0.id] ?? .fallback,
.rich,
false,
forceRichRows
)
}
if !presentation.pinned.isEmpty, !presentation.active.isEmpty {
items.append(.pinnedDivider)
}
if presentation.active.isEmpty, presentation.pinned.isEmpty {
items.append(.empty(.active))
} else {
items.append(contentsOf: presentation.active.map {
.thread(
$0,
presentation.rowContexts[$0.id] ?? .fallback,
@@ -459,7 +495,8 @@ struct HomeThreadCollectionView: UIViewRepresentable {
false,
forceRichRows
)
}
})
}
items.append(.shelfHeader(.snoozed, presentation.snoozed.count, isSnoozedExpanded))
if isSnoozedExpanded {
@@ -543,6 +580,7 @@ private enum HomeCollectionItem: Equatable {
case empty(HomeShelf)
case showMoreSettled
case searchEmpty
case pinnedDivider
var threadID: String? {
guard case let .thread(id) = self else { return nil }
@@ -555,6 +593,7 @@ private enum HomeCollectionItem: Equatable {
case empty(HomeShelf)
case showMoreSettled(Int)
case searchEmpty(String)
case pinnedDivider
var id: ID {
switch self {
@@ -563,6 +602,7 @@ private enum HomeCollectionItem: Equatable {
case let .empty(shelf): .empty(shelf)
case .showMoreSettled: .showMoreSettled
case .searchEmpty: .searchEmpty
case .pinnedDivider: .pinnedDivider
}
}
}
@@ -613,6 +653,12 @@ private struct HomeCollectionCellContent: View {
ContentUnavailableView("No matching tasks", systemImage: "magnifyingglass")
.foregroundStyle(T3Colors.textSecondary)
.frame(maxWidth: .infinity, minHeight: 160)
case .pinnedDivider:
Rectangle()
.fill(T3Colors.textTertiary.opacity(0.18))
.frame(height: 1)
.padding(.horizontal, 18)
.padding(.vertical, 3)
}
}
}
@@ -241,6 +241,9 @@ public struct WorkspaceView: View {
onSnooze: { thread, until in
Task { await model.setSnoozed(thread.id, until: until) }
},
onPin: { thread, pinned in
Task { await model.setPinned(thread.id, pinned: pinned) }
},
onDelete: { thread in
Task { await model.deleteThread(thread.id) }
}
@@ -657,6 +660,7 @@ private extension FeatureDraftAttachment {
}
struct HomePresentation {
let pinned: [FeatureThread]
let active: [FeatureThread]
let snoozed: [FeatureThread]
let settled: [FeatureThread]
@@ -680,6 +684,7 @@ struct HomePresentation {
return $0.id < $1.id
}
pinned = index.pinned
active = index.active
snoozed = index.snoozed
settled = index.settled
@@ -688,7 +693,7 @@ struct HomePresentation {
searchResults = normalizedQuery.isEmpty
? []
: DailyUXSidebarIndex.matchingThreads(
index.active + index.snoozed + index.settled + archived,
index.pinned + index.active + index.snoozed + index.settled + archived,
snapshot: snapshot,
query: normalizedQuery
)
@@ -928,6 +933,11 @@ struct FeatureThreadRow: View, Equatable {
}
.foregroundStyle(environmentColor)
}
if thread.pinnedAt != nil {
Image(systemName: "pin.fill")
.font(.system(size: 9, weight: .semibold))
.foregroundStyle(T3Colors.textSecondary)
}
providerIcon(size: 16)
}
.font(T3Typography.homeMetadata)
@@ -955,6 +965,11 @@ struct FeatureThreadRow: View, Equatable {
.foregroundStyle(T3Colors.textSecondary)
.lineLimit(allowsMultilineTitle ? 2 : 1)
Spacer(minLength: 8)
if thread.pinnedAt != nil {
Image(systemName: "pin.fill")
.font(.system(size: 9, weight: .semibold))
.foregroundStyle(T3Colors.textSecondary)
}
providerIcon(size: 15)
Text(SidebarRelativeAge.compact(since: thread.updatedAt, now: now))
.font(T3Typography.homeMetadata.monospacedDigit())
@@ -74,6 +74,21 @@ final class CoreContractTests: XCTestCase {
XCTAssertEqual(command["threadId"]?.stringValue, "thread-1")
XCTAssertEqual(command["modelSelection"]?["instanceId"]?.stringValue, "codex")
XCTAssertEqual(command["runtimeMode"]?.stringValue, "full-access")
let pin = OrchestrationCommands.pin(
threadID: "thread-1",
pinned: true,
commandID: "command-pin"
)
XCTAssertEqual(pin["type"]?.stringValue, "thread.pin")
XCTAssertEqual(pin["threadId"]?.stringValue, "thread-1")
let unpin = OrchestrationCommands.pin(
threadID: "thread-1",
pinned: false,
commandID: "command-unpin"
)
XCTAssertEqual(unpin["type"]?.stringValue, "thread.unpin")
}
func testFirstSendCommandCarriesCanonicalBootstrapMetadata() throws {
@@ -89,6 +89,34 @@ struct DailyUXSidebarTests {
#expect(index.settled.isEmpty)
}
@Test
func pinPromotesSettledThreadsButSnoozeStillWins() {
var pinnedSettled = thread(
id: "pinned-settled",
created: -100,
updated: -400_000,
state: .idle,
isSettled: true
)
pinnedSettled.pinnedAt = now.addingTimeInterval(-20)
var pinnedSnoozed = thread(
id: "pinned-snoozed",
created: -50,
updated: -10
)
pinnedSnoozed.pinnedAt = now.addingTimeInterval(-10)
pinnedSnoozed.snoozedUntil = now.addingTimeInterval(3_600)
let index = makeIndex([pinnedSettled, pinnedSnoozed])
#expect(index.pinned.map(\.id) == ["pinned-settled"])
#expect(index.snoozed.map(\.id) == ["pinned-snoozed"])
#expect(index.active.isEmpty)
#expect(index.settled.isEmpty)
#expect(DailyUXSidebarRefresh.nextBoundary(for: [pinnedSettled], after: now) == nil)
}
@Test
func snoozedThreadsHaveAReachableReverseState() {
var snoozed = thread(id: "snoozed", created: -20, updated: -10)
@@ -610,6 +610,33 @@ struct FeatureRootModelTests {
#expect(updated.snoozedAt! > updated.attentionAt!)
}
@Test
func testPinOptimisticallyPromotesAParkedThread() async {
let client = FeatureClientStub()
var thread = FeatureThread(
id: "thread-1",
projectID: "project-1",
title: "Thread",
isSettled: true,
settledAt: .now,
snoozedUntil: Date.now.addingTimeInterval(3_600),
snoozedAt: .now
)
thread.supportsPinning = true
client.createdThread = thread
let model = testRootModel(client: client)
_ = await model.createThread(projectID: thread.projectID, title: nil, selection: nil)
await model.setPinned(thread.id, pinned: true)
let updated = model.snapshot.threads[0]
#expect(updated.pinnedAt != nil)
#expect(!updated.isSettled)
#expect(updated.keepsActive)
#expect(updated.settledAt == nil)
#expect(updated.snoozedUntil == nil)
}
@Test
func testResolveUserInputForwardsTypedAnswersAndClearsTheRequest() async {
let client = FeatureClientStub()
@@ -954,6 +981,7 @@ private func orchestrationThread(
settledAt: nil,
snoozedUntil: nil,
snoozedAt: nil,
pinnedAt: nil,
deletedAt: nil,
messages: messages,
activities: activities,
@@ -671,6 +671,7 @@ private func multiEnvironmentShell(
settledAt: nil,
snoozedUntil: nil,
snoozedAt: nil,
pinnedAt: nil,
session: nil,
latestUserMessageAt: nil,
hasPendingApprovals: false,
@@ -706,6 +707,7 @@ private func multiEnvironmentDetail(
settledAt: nil,
snoozedUntil: nil,
snoozedAt: nil,
pinnedAt: nil,
deletedAt: nil,
messages: [],
activities: [],
@@ -430,6 +430,7 @@ private func retryShellSnapshot() -> OrchestrationShellSnapshot {
settledAt: nil,
snoozedUntil: nil,
snoozedAt: nil,
pinnedAt: nil,
session: nil,
latestUserMessageAt: nil,
hasPendingApprovals: false,
@@ -713,6 +714,7 @@ private func retryEmptyThreadDetail(id: String) -> OrchestrationThreadDetailSnap
settledAt: nil,
snoozedUntil: nil,
snoozedAt: nil,
pinnedAt: nil,
deletedAt: nil,
messages: [],
activities: [],