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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat(server): add info-level logging to diagnose timeline refresh issue
Louis Beaumont6ada59f23a07
diff --git a/screenpipe-app-tauri/lib/hooks/__tests__/server-poll-logic.test.ts b/screenpipe-app-tauri/lib/hooks/__tests__/server-poll-logic.test.tsnew file mode 100644index 000000000..133ef094e--- /dev/null+++ b/screenpipe-app-tauri/lib/hooks/__tests__/server-poll-logic.test.ts@@ -0,0 +1,400 @@+/**+ * Tests to reproduce server-side poll_timer issue+ *+ * The bug: New frames are captured to DB but not pushed to client in real-time.+ * User must right-click refresh to see new frames.+ *+ * This test simulates the server-side logic to identify the root cause.+ */++import { describe, it, expect } from "bun:test";++// Simulate the server-side data structures+interface FrameChunk {+ frame_id: number;+ timestamp: Date;+ data: string;+}++interface ActiveRequest {+ start_time: Date;+ end_time: Date;+ is_descending: boolean;+ last_polled: Date;+}++// Simulate database+function createMockDb(frames: FrameChunk[]) {+ return {+ find_video_chunks: (since: Date, until: Date): FrameChunk[] => {+ return frames.filter(f => f.timestamp >= since && f.timestamp <= until);+ }+ };+}++describe("Server Poll Logic - Reproducing Real-Time Push Bug", () => {++ /**+ * TEST 1: Simulate initial fetch marking frames as sent+ */+ it("initial fetch should mark all fetched frame IDs as sent", () => {+ const sentFrameIds = new Set<number>();++ // Simulate initial frames in DB (frames from today, 00:00 to now)+ const initialFrames: FrameChunk[] = [+ { frame_id: 1, timestamp: new Date("2024-01-15T10:00:00Z"), data: "frame1" },+ { frame_id: 2, timestamp: new Date("2024-01-15T10:01:00Z"), data: "frame2" },+ { frame_id: 3, timestamp: new Date("2024-01-15T10:02:00Z"), data: "frame3" },+ ];++ // Simulate fetch_and_process_frames_with_tracking+ for (const frame of initialFrames) {+ sentFrameIds.add(frame.frame_id);+ }++ expect(sentFrameIds.size).toBe(3);+ expect(sentFrameIds.has(1)).toBe(true);+ expect(sentFrameIds.has(2)).toBe(true);+ expect(sentFrameIds.has(3)).toBe(true);+ });++ /**+ * TEST 2: Poll should find NEW frames (not in sentFrameIds)+ */+ it("poll should find new frames not in sentFrameIds", () => {+ const sentFrameIds = new Set<number>([1, 2, 3]); // Already sent++ // New frame captured after initial fetch+ const allFrames: FrameChunk[] = [+ { frame_id: 1, timestamp: new Date("2024-01-15T10:00:00Z"), data: "frame1" },+ { frame_id: 2, timestamp: new Date("2024-01-15T10:01:00Z"), data: "frame2" },+ { frame_id: 3, timestamp: new Date("2024-01-15T10:02:00Z"), data: "frame3" },+ { frame_id: 4, timestamp: new Date("2024-01-15T10:03:00Z"), data: "frame4" }, // NEW+ ];++ const db = createMockDb(allFrames);++ // Simulate poll: look for frames from last_polled to now+ const lastPolled = new Date("2024-01-15T10:02:00Z");+ const now = new Date("2024-01-15T10:04:00Z");++ const chunks = db.find_video_chunks(lastPolled, now);+ const newFrames = chunks.filter(f => !sentFrameIds.has(f.frame_id));++ expect(newFrames.length).toBe(1);+ expect(newFrames[0].frame_id).toBe(4);+ });++ /**+ * TEST 3: BUG REPRODUCTION - last_polled equals latest frame timestamp+ *+ * If last_polled is set to the timestamp of the latest frame (not the current time),+ * and new frames have timestamps AFTER that, they should be found.+ *+ * But what if find_video_chunks uses > instead of >= ?+ */+ it("should find frames with timestamp > last_polled", () => {+ const sentFrameIds = new Set<number>([1, 2, 3]);++ const allFrames: FrameChunk[] = [+ { frame_id: 3, timestamp: new Date("2024-01-15T10:02:00Z"), data: "frame3" },+ { frame_id: 4, timestamp: new Date("2024-01-15T10:02:01Z"), data: "frame4" }, // 1 second later+ ];++ // last_polled is exactly the timestamp of frame 3+ const lastPolled = new Date("2024-01-15T10:02:00Z");+ const now = new Date("2024-01-15T10:04:00Z");++ // Simulate find_video_chunks with >= (inclusive)+ const chunksInclusive = allFrames.filter(f => f.timestamp >= lastPolled && f.timestamp <= now);+ const newFramesInclusive = chunksInclusive.filter(f => !sentFrameIds.has(f.frame_id));++ // Frame 4 should be found+ expect(newFramesInclusive.length).toBe(1);+ expect(newFramesInclusive[0].frame_id).toBe(4);+ });++ /**+ * TEST 4: BUG - What if poll_start >= poll_end?+ *+ * Server code: if poll_start < poll_end { ... }+ * If this fails, no poll happens!+ */+ it("POTENTIAL BUG: poll_start < poll_end check", () => {+ const lastPolled = new Date("2024-01-15T10:02:00.000Z");+ const now = new Date("2024-01-15T10:02:00.500Z"); // Only 500ms later+ const endTime = new Date("2024-01-15T23:59:59.999Z");++ const pollStart = lastPolled;+ const pollEnd = now < endTime ? now : endTime;++ // This should pass - 500ms gap+ expect(pollStart < pollEnd).toBe(true);++ // But what if last_polled was set to a future timestamp by mistake?+ const badLastPolled = new Date("2024-01-15T10:05:00.000Z"); // In the future!+ const badPollStart = badLastPolled;++ // This would FAIL - no poll would happen+ expect(badPollStart < pollEnd).toBe(false);+ });++ /**+ * TEST 5: BUG - last_polled set to latest frame, but frames captured in same second+ *+ * If multiple frames have the same timestamp, and last_polled is set to that timestamp,+ * subsequent frames in the same second might be missed.+ */+ it("POTENTIAL BUG: frames with same timestamp as last_polled", () => {+ const sameTimestamp = new Date("2024-01-15T10:02:00Z");++ const allFrames: FrameChunk[] = [+ { frame_id: 3, timestamp: sameTimestamp, data: "frame3" },+ { frame_id: 4, timestamp: sameTimestamp, data: "frame4" }, // Same timestamp, different ID+ { frame_id: 5, timestamp: sameTimestamp, data: "frame5" }, // Same timestamp, different ID+ ];++ // Initial fetch gets frame 3, sets last_polled to its timestamp+ const sentFrameIds = new Set<number>([3]);+ const lastPolled = sameTimestamp;++ // Poll looks for frames >= lastPolled+ const now = new Date("2024-01-15T10:04:00Z");+ const chunks = allFrames.filter(f => f.timestamp >= lastPolled && f.timestamp <= now);+ const newFrames = chunks.filter(f => !sentFrameIds.has(f.frame_id));++ // Frames 4 and 5 should be found (same timestamp but different IDs)+ expect(newFrames.length).toBe(2);+ });++ /**+ * TEST 6: BUG - end_time check with timezone issues+ *+ * Client sends end_time as local 23:59:59, but server compares with UTC now+ */+ it("POTENTIAL BUG: timezone mismatch in end_time check", () => {+ // Client in UTC+8 sends end_time as "2024-01-15T23:59:59" (local)+ // This might be interpreted as UTC on server+ const endTimeFromClient = new Date("2024-01-15T23:59:59Z"); // UTC++ // Server's "now" in UTC+ const serverNowUtc = new Date("2024-01-16T06:00:00Z"); // Next day in UTC++ // Check: now <= end_time+ const shouldPoll = serverNowUtc <= endTimeFromClient;++ // This would FAIL - server thinks end_time has passed+ expect(shouldPoll).toBe(false);++ // This is a bug if client is in a timezone ahead of UTC+ console.log("BUG: If client timezone > UTC, end_time check fails prematurely");+ });++ /**+ * TEST 7: Simulate full flow - initial fetch then poll+ */+ it("FULL FLOW: initial fetch marks frames, poll should find new ones", () => {+ const sentFrameIds = new Set<number>();+ let activeRequest: ActiveRequest | null = null;++ // === STEP 1: Initial frames in DB ===+ let dbFrames: FrameChunk[] = [+ { frame_id: 1, timestamp: new Date("2024-01-15T10:00:00Z"), data: "frame1" },+ { frame_id: 2, timestamp: new Date("2024-01-15T10:01:00Z"), data: "frame2" },+ { frame_id: 3, timestamp: new Date("2024-01-15T10:02:00Z"), data: "frame3" },+ ];++ // === STEP 2: Client sends request ===+ const requestStartTime = new Date("2024-01-15T00:00:00Z");+ const requestEndTime = new Date("2024-01-15T23:59:59.999Z");++ // === STEP 3: Initial fetch (fetch_and_process_frames_with_tracking) ===+ const db = createMockDb(dbFrames);+ const initialChunks = db.find_video_chunks(requestStartTime, requestEndTime);++ let latestTimestamp: Date | null = null;+ for (const chunk of initialChunks) {+ sentFrameIds.add(chunk.frame_id);+ if (!latestTimestamp || chunk.timestamp > latestTimestamp) {+ latestTimestamp = chunk.timestamp;+ }+ }++ // Set active request with last_polled = latestTimestamp+ activeRequest = {+ start_time: requestStartTime,+ end_time: requestEndTime,+ is_descending: true,+ last_polled: latestTimestamp!,+ };++ expect(sentFrameIds.size).toBe(3);+ expect(activeRequest.last_polled).toEqual(new Date("2024-01-15T10:02:00Z"));++ // === STEP 4: New frame captured ===+ dbFrames.push({ frame_id: 4, timestamp: new Date("2024-01-15T10:03:00Z"), data: "frame4" });++ // === STEP 5: Poll timer fires ===+ const now = new Date("2024-01-15T10:04:00Z");++ // Check: now <= end_time+ expect(now <= activeRequest.end_time).toBe(true);++ const pollStart = activeRequest.last_polled;+ const pollEnd = now < activeRequest.end_time ? now : activeRequest.end_time;++ // Check: poll_start < poll_end+ expect(pollStart < pollEnd).toBe(true);++ // Fetch new frames+ const newDb = createMockDb(dbFrames);+ const pollChunks = newDb.find_video_chunks(pollStart, pollEnd);+ const newFrames = pollChunks.filter(f => !sentFrameIds.has(f.frame_id));++ // === EXPECTED: Frame 4 should be found ===+ expect(newFrames.length).toBe(1);+ expect(newFrames[0].frame_id).toBe(4);++ console.log("✓ Full flow works in test - new frame found");+ console.log(" If this passes but real server fails, issue is elsewhere:");+ console.log(" - DB query implementation differs from mock");+ console.log(" - Timezone issues");+ console.log(" - tokio::select! starvation");+ console.log(" - WebSocket sender issues");+ });++ /**+ * TEST 8: The tokio::select! starvation issue+ *+ * When frame_rx channel closes, it returns None immediately.+ * In tokio::select!, a branch that completes immediately can starve other branches.+ */+ it("DOCUMENTED BUG: channel closure can starve poll_timer in tokio::select!", () => {+ // This test documents the bug we identified earlier+ // The server code has this structure:+ //+ // loop {+ // tokio::select! {+ // frame = frame_rx.recv() => { ... } // Branch 1: receive from channel+ // _ = poll_timer.tick() => { ... } // Branch 2: poll for new frames+ // _ = keepalive.tick() => { ... } // Branch 3: keepalive+ // }+ // }+ //+ // When frame_rx channel closes:+ // - recv() returns None IMMEDIATELY+ // - select! keeps picking this branch+ // - poll_timer and keepalive never run++ // Simulate this:+ let pollTimerRan = 0;+ let channelRecvRan = 0;+ const channelClosed = true;++ // Simulate 10 iterations of select!+ for (let i = 0; i < 10; i++) {+ // In real tokio::select!, the first ready branch wins+ // A closed channel is ALWAYS ready (returns None)+ if (channelClosed) {+ channelRecvRan++;+ // In buggy code, this branch always wins+ continue; // Simulates the bug - never reaches poll_timer+ }++ // This never runs if channel is closed (in buggy code)+ pollTimerRan++;+ }++ expect(channelRecvRan).toBe(10);+ expect(pollTimerRan).toBe(0);++ console.log("BUG CONFIRMED: Closed channel starves poll_timer");+ console.log("FIX: Server code should track channel closure and skip that branch");+ console.log("Current code at server.rs:3324-3328 DOES handle this correctly:");+ console.log(" frame_rx = None; // Sets to None so select! skips this branch");+ });++ /**+ * TEST 9: Check if the fix for channel starvation is correct+ */+ it("FIX VERIFICATION: server code handles channel closure correctly", () => {+ // The server code does this when channel closes:+ // None => {+ // debug!("frame channel closed, switching to live polling only");+ // frame_rx = None;+ // }+ //+ // And the select! branch is:+ // frame = async {+ // match &mut frame_rx {+ // Some(rx) => rx.recv().await,+ // None => std::future::pending().await, // Never completes!+ // }+ // } => { ... }+ //+ // When frame_rx is None, this branch returns a pending future+ // which NEVER completes, so select! picks other branches.++ let frameRx: boolean | null = true; // Some(channel)+ let pollTimerRan = 0;+ let channelRecvRan = 0;++ for (let i = 0; i < 10; i++) {+ if (frameRx === null) {+ // Channel is None - this branch "never completes" (pending)+ // So we fall through to poll_timer+ pollTimerRan++;+ } else {+ // Channel receives something or closes+ channelRecvRan++;+ if (i === 2) {+ // Simulate channel closing on iteration 2+ frameRx = null;+ }+ }+ }++ expect(channelRecvRan).toBe(3); // Iterations 0, 1, 2+ expect(pollTimerRan).toBe(7); // Iterations 3-9++ console.log("✓ Fix is correct - after channel closes, poll_timer runs");+ });+});++describe("Server Poll Logic - Database Query Investigation", () => {+ /**+ * TEST 10: Check if find_video_chunks might return empty+ */+ it("find_video_chunks returns empty if time range has no frames", () => {+ const frames: FrameChunk[] = [+ { frame_id: 1, timestamp: new Date("2024-01-15T10:00:00Z"), data: "frame1" },+ ];++ const db = createMockDb(frames);++ // Query for time range AFTER the frame+ const since = new Date("2024-01-15T10:01:00Z");+ const until = new Date("2024-01-15T10:02:00Z");++ const result = db.find_video_chunks(since, until);+ expect(result.length).toBe(0);++ console.log("If poll_start is after latest frame, no frames found");+ });++ /**+ * TEST 11: Frames captured but not committed to DB yet+ */+ it("POTENTIAL BUG: frame captured but DB transaction not committed", () => {+ // This can't be tested in JS, but documents a potential issue:+ // 1. Screen capture creates frame+ // 2. Frame written to DB but transaction not committed+ // 3. Poll queries DB - doesn't see uncommitted frame+ // 4. Transaction commits+ // 5. Next poll might see it, or might miss if last_polled advanced++ console.log("POTENTIAL BUG: DB transaction timing");+ console.log("Frame might be captured but not visible to poll query");+ expect(true).toBe(true); // Placeholder+ });+});diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rsindex 835abad56..f06da0aba 100644--- a/screenpipe-server/src/server.rs+++ b/screenpipe-server/src/server.rs@@ -3217,8 +3217,8 @@ async fn handle_stream_frames_socket(socket: WebSocket, state: Arc<AppState>, _gif let Message::Text(text) = msg {match serde_json::from_str::<StreamFramesRequest>(&text) {Ok(request) => {- debug!(- "streaming frames from {} to {}",+ info!(+ "WebSocket stream request: {} to {} (live polling enabled)",request.start_time, request.end_time);@@ -3364,9 +3364,9 @@ async fn handle_stream_frames_socket(socket: WebSocket, state: Arc<AppState>, _g{Ok((new_frames, latest_ts)) => {if !new_frames.is_empty() {- debug!(- "Live push: sending {} new frames to client",- new_frames.len()+ info!(+ "Live push: sending {} new frames (poll_start={}, poll_end={})",+ new_frames.len(), poll_start, poll_end);// Sort frames based on client's preference@@ -3405,11 +3405,17 @@ async fn handle_stream_frames_socket(socket: WebSocket, state: Arc<AppState>, _g}}Err(e) => {- debug!("Error polling for new frames: {}", e);+ info!("Poll error: {}", e);}}+ } else {+ debug!("Poll skipped: poll_start >= poll_end ({} >= {})", poll_start, poll_end);}+ } else {+ debug!("Poll skipped: now > end_time ({} > {})", now, end_time);}+ } else {+ debug!("Poll skipped: no active request");}}// Send keep-alive message to prevent connection timeout