Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Fix native timeline day transitions

Louis Beaumont5ff6ba379bde
diff --git a/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineCore.swift b/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineCore.swift
index 71aa5ce58..a1b37b946 100644
--- a/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineCore.swift
+++ b/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineCore.swift
@@ -68,6 +68,15 @@ enum TimelineAppTaxonomy {
]),
]
+ /// App names repeat for thousands of consecutive frames. Taxonomy uses
+ /// fuzzy bidirectional substring checks, so memoizing the small distinct
+ /// name set avoids re-running the keyword matrix during lane grouping.
+ private static let categoryCache: NSCache<NSString, NSString> = {
+ let cache = NSCache<NSString, NSString>()
+ cache.countLimit = 2_000
+ return cache
+ }()
+
/// `String.prototype.includes`: an empty needle always matches, where
/// Swift's `contains` returns false. That difference decides the category of
/// an empty app name, so it is reproduced rather than tidied away.
@@ -79,11 +88,18 @@ enum TimelineAppTaxonomy {
/// order: browser, dev, communication, media, productivity.
static func category(for appName: String) -> AppCategory {
let lower = appName.lowercased()
+ let key = lower as NSString
+ if let cached = categoryCache.object(forKey: key),
+ let category = AppCategory(rawValue: cached as String) {
+ return category
+ }
for (category, apps) in categories {
for app in apps where jsIncludes(lower, app) || jsIncludes(app, lower) {
+ categoryCache.setObject(category.rawValue as NSString, forKey: key)
return category
}
}
+ categoryCache.setObject(AppCategory.other.rawValue as NSString, forKey: key)
return .other
}
@@ -663,6 +679,58 @@ enum TimelineGrouping {
return groups
}
+ /// Clip already-computed full-window groups to the visible viewport.
+ /// Group identity, domain evidence and colour stay anchored to the entire
+ /// run instead of being re-derived from whichever fragment is on screen.
+ static func visibleGroups(
+ from groups: [TimelineAppGroup],
+ in range: Range<Int>
+ ) -> [TimelineAppGroup] {
+ guard !range.isEmpty, !groups.isEmpty else { return [] }
+
+ // Groups are ordered and non-overlapping. Seek to the first possible
+ // overlap instead of walking a full day for every scroll tick.
+ var lowerBound = 0
+ var upperBound = groups.count
+ while lowerBound < upperBound {
+ let middle = lowerBound + (upperBound - lowerBound) / 2
+ let last = groups[middle].frameIndices.last ?? -1
+ if last < range.lowerBound {
+ lowerBound = middle + 1
+ } else {
+ upperBound = middle
+ }
+ }
+
+ var result: [TimelineAppGroup] = []
+ var groupIndex = lowerBound
+ while groupIndex < groups.count {
+ let group = groups[groupIndex]
+ guard let first = group.frameIndices.first,
+ let last = group.frameIndices.last,
+ first < range.upperBound else { break }
+ guard last >= range.lowerBound else {
+ groupIndex += 1
+ continue
+ }
+ let lower = max(first, range.lowerBound)
+ let upper = min(last + 1, range.upperBound)
+ guard lower < upper else {
+ groupIndex += 1
+ continue
+ }
+
+ var visible = group
+ // Group indices are contiguous because a group flushes at every
+ // app, domain or day boundary.
+ visible.frameIndices = Array(lower..<upper)
+ if lower != first { visible.dayBoundaryLabel = nil }
+ result.append(visible)
+ groupIndex += 1
+ }
+ return result
+ }
+
/// Segment colour: the top site for a browser run, the app name otherwise.
static func barColor(for group: TimelineAppGroup) -> TimelineHSL {
TimelineColors.barColor(for: group.topDomains.first ?? group.appName)
@@ -935,11 +1003,17 @@ enum TimelineNavigation {
}
/// Closest frame to a wall-clock instant, used by search and deep links.
- static func indexNearest(_ target: Date, in frames: [StreamTimeSeriesResponse]) -> Int? {
+ static func indexNearest(
+ _ target: Date,
+ in frames: [StreamTimeSeriesResponse],
+ sameLocalDay: Bool = false
+ ) -> Int? {
var best: Int?
var bestDelta = Double.greatestFiniteMagnitude
+ let calendar = Calendar.current
for (i, frame) in frames.enumerated() {
guard let d = TimelineFrames.date(of: frame) else { continue }
+ if sameLocalDay, !calendar.isDate(d, inSameDayAs: target) { continue }
let delta = abs(d.timeIntervalSince(target))
if delta < bestDelta {
bestDelta = delta
diff --git a/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineViewModel.swift b/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineViewModel.swift
index be8a0e246..b7aeb5a3e 100644
--- a/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineViewModel.swift
+++ b/apps/screenpipe-app-tauri/src-tauri/swift/timeline/TimelineViewModel.swift
@@ -262,6 +262,13 @@ final class TimelineViewModel: ObservableObject {
/// for an explicit day change: treating it as a session-long history made
/// scrolling from yesterday back into an already visited today a no-op.
private var requestedDays = Set<String>()
+ /// Adjacent-day data is backfill, not a live-edge update. Preserve the
+ /// exact moment under the playhead while the neighbouring day is merged.
+ private var pendingAdjacentAnchor: (frameId: String?, timestamp: Date)?
+ /// Explicit day arrows/calendar target the beginning of that day, matching
+ /// the web Timeline. The request is ascending so its first batch contains
+ /// the midnight-side frame and later batches preserve that exact anchor.
+ private var pendingDayAnchor: Date?
private var tagFetchInFlight = Set<String>()
private var navigationGeneration = 0
private var pendingSearchNavigation: (frameId: String?, timestamp: Date)?
@@ -292,6 +299,8 @@ final class TimelineViewModel: ObservableObject {
private var cachedFacetsKey: String = ""
private var cachedMatching: [Int]??
private var cachedMatchingKey: String = ""
+ private var cachedAllGroups: [TimelineAppGroup] = []
+ private var cachedAllGroupsGeneration = -1
private var cachedGroups: [TimelineAppGroup] = []
private var cachedGroupsKey: String = ""
private var lastAdjacentLoad = Date.distantPast
@@ -348,6 +357,8 @@ final class TimelineViewModel: ObservableObject {
playbackTimer = nil
meetingDetectionWorkItem = nil
meetingDetectionDirtySince = nil
+ pendingAdjacentAnchor = nil
+ pendingDayAnchor = nil
isLoadingImage = false
}
@@ -389,6 +400,8 @@ final class TimelineViewModel: ObservableObject {
currentImage = nil
currentImageFrameId = nil
pendingSearchNavigation = searchToResume
+ pendingAdjacentAnchor = nil
+ pendingDayAnchor = nil
preferredFrameId = searchToResume?.frameId
imageLoadToken &+= 1
isLoadingImage = false
@@ -467,11 +480,11 @@ final class TimelineViewModel: ObservableObject {
// MARK: Requests
- func requestDay(_ date: Date) {
+ func requestDay(_ date: Date, order: String = "descending") {
let key = TimelineDateNavigation.dayKey(date)
let range = TimelineDateNavigation.dayRange(for: date)
requestedDays.insert(key)
- stream.request(FrameStreamRequest(start: range.start, end: range.end))
+ stream.request(FrameStreamRequest(start: range.start, end: range.end, order: order))
}
/// Search can return an exact frame that falls outside the day's capped
@@ -525,7 +538,27 @@ final class TimelineViewModel: ObservableObject {
let result = TimelineMerge.merge(existing: frames, incoming: incoming)
let previousDisplayFrameId = displayFrameId
frames = result.frames
- currentIndex = TimelineLiveEdge.shiftIndex(currentIndex, newFramesAtFront: result.newAtFront)
+ if let anchor = pendingAdjacentAnchor {
+ let anchoredIndex = anchor.frameId.flatMap {
+ TimelineNavigation.index(ofFrameId: $0, in: frames)
+ } ?? TimelineNavigation.indexNearest(anchor.timestamp, in: frames)
+ if let anchoredIndex { currentIndex = anchoredIndex }
+ pendingAdjacentAnchor = nil
+ } else if let anchor = pendingDayAnchor {
+ if let anchoredIndex = TimelineNavigation.indexNearest(
+ anchor,
+ in: frames,
+ sameLocalDay: true
+ ) {
+ currentIndex = anchoredIndex
+ pendingDayAnchor = nil
+ }
+ } else {
+ currentIndex = TimelineLiveEdge.shiftIndex(
+ currentIndex,
+ newFramesAtFront: result.newAtFront
+ )
+ }
var resolvedPendingSearch = false
if let pending = pendingSearchNavigation {
if let frameId = pending.frameId,
@@ -691,6 +724,11 @@ final class TimelineViewModel: ObservableObject {
guard !requestedDays.contains(key) else { return }
// Never request past today.
if !nearOldest, target > Date() { return }
+ let playheadFrameId = currentFrame?.devices.first?.frameId
+ pendingAdjacentAnchor = (
+ frameId: playheadFrameId,
+ timestamp: currentTimestamp ?? anchor
+ )
lastAdjacentLoad = Date()
requestDay(target)
}
@@ -718,7 +756,11 @@ final class TimelineViewModel: ObservableObject {
let v = viewport
let key = "\(framesGeneration)|\(v.start)|\(v.end)"
if key == cachedGroupsKey { return cachedGroups }
- let groups = TimelineGrouping.groups(for: visibleFrames, indexOffset: v.start)
+ if cachedAllGroupsGeneration != framesGeneration {
+ cachedAllGroups = TimelineGrouping.groups(for: frames)
+ cachedAllGroupsGeneration = framesGeneration
+ }
+ let groups = TimelineGrouping.visibleGroups(from: cachedAllGroups, in: v.range)
cachedGroups = groups
cachedGroupsKey = key
return groups
@@ -1061,6 +1103,8 @@ final class TimelineViewModel: ObservableObject {
isNavigating = false
currentDate = Date()
currentIndex = 0
+ pendingAdjacentAnchor = nil
+ pendingDayAnchor = nil
requestedDays.removeAll()
requestDay(currentDate)
loadCurrentImage()
@@ -1080,6 +1124,8 @@ final class TimelineViewModel: ObservableObject {
resetFilters()
currentDate = date
currentIndex = 0
+ pendingAdjacentAnchor = nil
+ pendingDayAnchor = Calendar.current.startOfDay(for: date)
requestedDays.removeAll()
frames = []
// Retain the last decoded pixels as a non-interactive transition
@@ -1087,7 +1133,7 @@ final class TimelineViewModel: ObservableObject {
// pixels cannot be mistaken for the new playhead or copied as its text.
preferredFrameId = nil
isLoading = true
- requestDay(date)
+ requestDay(date, order: "ascending")
// Never leave the spinner up forever if the day query stalls.
DispatchQueue.main.asyncAfter(deadline: .now() + TimelineDateNavigation.navigationTimeout) { [weak self] in
guard let self, self.navigationGeneration == generation else { return }
diff --git a/apps/screenpipe-app-tauri/src-tauri/swift/timeline_performance_tests.swift b/apps/screenpipe-app-tauri/src-tauri/swift/timeline_performance_tests.swift
index 621b62098..181e86f1a 100644
--- a/apps/screenpipe-app-tauri/src-tauri/swift/timeline_performance_tests.swift
+++ b/apps/screenpipe-app-tauri/src-tauri/swift/timeline_performance_tests.swift
@@ -187,6 +187,42 @@ struct TimelinePerformanceTests {
)
exit(1)
}
+
+ // Lane identity is computed from the full loaded window once, then
+ // clipped for each viewport. Exercise a deliberately fragmented day
+ // so stabilizing colours cannot reintroduce O(frames) work per scroll.
+ var fragmented = fixtureFrames()
+ for index in fragmented.indices {
+ fragmented[index].devices[0].metadata.appName = "Synthetic \(index % 20)"
+ }
+ let groupingModel = TimelineViewModel(
+ config: TimelineAPIConfig(host: "127.0.0.1", port: 0, apiKey: nil)
+ )
+ groupingModel.injectForTesting(frames: fragmented)
+ var groupingChecksum = 0
+ let coldGroupingMs = elapsedMilliseconds {
+ groupingChecksum += groupingModel.appGroups.count
+ }
+ let scrollGroupingMs = elapsedMilliseconds {
+ for iteration in 0..<500 {
+ groupingModel.currentIndex = (iteration * 5) % fragmented.count
+ groupingChecksum += groupingModel.appGroups.count
+ }
+ }
+ print(
+ String(
+ format: "timeline performance: cold_grouping_ms=%.3f repeated_grouping_ms=%.3f iterations=500 checksum=%d",
+ coldGroupingMs,
+ scrollGroupingMs,
+ groupingChecksum
+ )
+ )
+ guard coldGroupingMs < 100, scrollGroupingMs < 100 else {
+ FileHandle.standardError.write(
+ "FAIL stable lane grouping exceeded 100 ms\n".data(using: .utf8)!
+ )
+ exit(1)
+ }
model.stop()
}
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/swift/timeline_render_tests.swift b/apps/screenpipe-app-tauri/src-tauri/swift/timeline_render_tests.swift
index 24981b375..6b1b00d89 100644
--- a/apps/screenpipe-app-tauri/src-tauri/swift/timeline_render_tests.swift
+++ b/apps/screenpipe-app-tauri/src-tauri/swift/timeline_render_tests.swift
@@ -625,6 +625,81 @@ private func testDateNavigation() {
expectEqual(model.currentIndex, 0, "jump to now returns to the live edge")
}
+@MainActor
+private func testAdjacentDayLoadPreservesPlayhead() {
+ let calendar = Calendar.current
+ let today = calendar.startOfDay(for: Date())
+ let yesterday = calendar.date(byAdding: .day, value: -1, to: today)!
+ let yesterdayEnd = today.addingTimeInterval(-1)
+ let model = offlineModel()
+ model.setHealthForTesting(HealthStatus(status: "healthy", frameStatus: "ok"))
+ model.changeDate(to: yesterday)
+ model.injectForTesting(frames: fixtureFrames(count: 1_000, base: yesterdayEnd))
+
+ // Explicit day navigation starts near midnight. Move to yesterday's newest
+ // edge as a user scrolling across the day boundary would.
+ model.setIndex(0)
+ let heldFrameId = model.currentFrame?.devices.first?.frameId
+ let heldTimestamp = model.currentTimestamp
+ expect(model.hasRequestedDayForTesting(today), "today is requested at yesterday's newest edge")
+
+ // Simulate the first batch from today arriving hours after midnight. The
+ // old live-edge rule jumped straight to this batch's newest frame.
+ var todayFrames = fixtureFrames(count: 100, base: today.addingTimeInterval(4 * 3_600))
+ for index in todayFrames.indices {
+ todayFrames[index].devices[0].frameId = "today-\(index)"
+ }
+ model.injectForTesting(frames: todayFrames)
+ expectEqual(
+ model.currentFrame?.devices.first?.frameId,
+ heldFrameId,
+ "adjacent-day merge keeps the frame under the playhead"
+ )
+ expectEqual(model.currentTimestamp, heldTimestamp, "adjacent-day merge keeps its timestamp")
+}
+
+@MainActor
+private func testExplicitDayNavigationStartsAtDayBoundary() {
+ let calendar = Calendar.current
+ let today = calendar.startOfDay(for: Date())
+ let yesterday = calendar.date(byAdding: .day, value: -1, to: today)!
+ let model = populatedModel()
+ model.changeDate(to: yesterday)
+
+ // A day's batch is newest-first in model storage. Explicit date controls
+ // should still select the frame nearest midnight, as the web Timeline does.
+ let frames = fixtureFrames(count: 480, base: yesterday.addingTimeInterval(4 * 3_600))
+ let expected = frames.last?.timestamp
+ model.injectForTesting(frames: frames)
+ expectEqual(
+ model.currentFrame?.timestamp,
+ expected,
+ "previous/next day navigation lands near the target day's beginning"
+ )
+}
+
+@MainActor
+private func testLaneColorIsStableAcrossViewport() {
+ var frames = fixtureFrames(count: 1_000)
+ for index in frames.indices {
+ frames[index].devices[0].metadata.appName = "Google Chrome"
+ frames[index].devices[0].metadata.browserUrl = nil
+ }
+ frames[0].devices[0].metadata.browserUrl = "https://example.com/work"
+
+ let model = offlineModel()
+ model.setHealthForTesting(HealthStatus(status: "healthy", frameStatus: "ok"))
+ model.injectForTesting(frames: frames)
+ let liveColor = model.appGroups.first.map(TimelineGrouping.barColor)
+ let liveDomain = model.appGroups.first?.topDomains.first
+
+ model.setIndex(500)
+ let historyColor = model.appGroups.first.map(TimelineGrouping.barColor)
+ let historyDomain = model.appGroups.first?.topDomains.first
+ expectEqual(historyDomain, liveDomain, "the browser lane keeps its full-run domain while scrolling")
+ expectEqual(historyColor, liveColor, "the same browser lane keeps the same color while scrolling")
+}
+
@MainActor
private func testConnectionFailurePresentation() {
expect(
@@ -967,6 +1042,9 @@ struct TimelineRenderTests {
("selection and filters", testSelectionAndFilters),
("live edge", testLiveEdgeFollowing),
("date navigation", testDateNavigation),
+ ("adjacent day anchor", testAdjacentDayLoadPreservesPlayhead),
+ ("explicit day boundary", testExplicitDayNavigationStartsAtDayBoundary),
+ ("stable lane color", testLaneColorIsStableAcrossViewport),
("connection failure presentation", testConnectionFailurePresentation),
("playback", testPlayback),
("search review", { testSearchReview(shots: shots) }),