-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Mark newly discovered completed threads unread #4501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
danieliser
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
danieliser:agent/first-seen-completed-thread-monitoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
apps/web/src/hooks/useMarkFirstSeenCompletedThreadsUnread.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; | ||
| import { EnvironmentId, ThreadId } from "@t3tools/contracts"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { resolveFirstSeenCompletedThreads } from "./useMarkFirstSeenCompletedThreadsUnread"; | ||
|
|
||
| const localEnvironmentId = EnvironmentId.make("environment-local"); | ||
| const remoteEnvironmentId = EnvironmentId.make("environment-remote"); | ||
|
|
||
| function thread( | ||
| id: string, | ||
| state: "completed" | "running" = "completed", | ||
| environmentId = localEnvironmentId, | ||
| ) { | ||
| return { | ||
| environmentId, | ||
| id: ThreadId.make(id), | ||
| latestTurn: { | ||
| state, | ||
| completedAt: "2026-06-18T09:00:00.000Z", | ||
| }, | ||
| } as const; | ||
| } | ||
|
|
||
| describe("resolveFirstSeenCompletedThreads", () => { | ||
| it("seeds initial snapshot history without marking it unread", () => { | ||
| const result = resolveFirstSeenCompletedThreads({ | ||
| threads: [thread("historical")], | ||
| environmentSnapshotIds: [localEnvironmentId], | ||
| previouslySeenThreadKeysByEnvironment: new Map(), | ||
| }); | ||
|
|
||
| expect(result.newlyUnreadThreads).toEqual([]); | ||
| expect(result.nextSeenThreadKeysByEnvironment.get(localEnvironmentId)).toEqual( | ||
| new Set([scopedThreadKey(scopeThreadRef(localEnvironmentId, ThreadId.make("historical")))]), | ||
| ); | ||
| }); | ||
|
|
||
| it("marks a completed thread that first appears after bootstrap unread", () => { | ||
| const historicalKey = scopedThreadKey( | ||
| scopeThreadRef(localEnvironmentId, ThreadId.make("historical")), | ||
| ); | ||
| const completedKey = scopedThreadKey( | ||
| scopeThreadRef(localEnvironmentId, ThreadId.make("completed")), | ||
| ); | ||
| const result = resolveFirstSeenCompletedThreads({ | ||
| threads: [thread("historical"), thread("completed")], | ||
| environmentSnapshotIds: [localEnvironmentId], | ||
| previouslySeenThreadKeysByEnvironment: new Map([ | ||
| [localEnvironmentId, new Set([historicalKey])], | ||
| ]), | ||
| }); | ||
|
|
||
| expect(result.newlyUnreadThreads).toEqual([ | ||
| { | ||
| threadKey: completedKey, | ||
| completedAt: "2026-06-18T09:00:00.000Z", | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("does not mark a new unfinished thread or a thread outside a snapshot environment", () => { | ||
| const result = resolveFirstSeenCompletedThreads({ | ||
| threads: [thread("running", "running"), thread("remote", "completed", remoteEnvironmentId)], | ||
| environmentSnapshotIds: [localEnvironmentId], | ||
| previouslySeenThreadKeysByEnvironment: new Map([[localEnvironmentId, new Set()]]), | ||
| }); | ||
|
|
||
| expect(result.newlyUnreadThreads).toEqual([]); | ||
| expect(result.nextSeenThreadKeysByEnvironment.has(remoteEnvironmentId)).toBe(false); | ||
| }); | ||
| }); |
97 changes: 97 additions & 0 deletions
97
apps/web/src/hooks/useMarkFirstSeenCompletedThreadsUnread.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { useAtomValue } from "@effect/atom-react"; | ||
| import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; | ||
| import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; | ||
| import * as Option from "effect/Option"; | ||
| import { Atom } from "effect/unstable/reactivity"; | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| import { environmentCatalog } from "../connection/catalog"; | ||
| import { useThreadShells } from "../state/entities"; | ||
| import { environmentShell } from "../state/shell"; | ||
| import { useUiStateStore } from "../uiStateStore"; | ||
|
|
||
| const environmentSnapshotIdsAtom = Atom.make((get): ReadonlyArray<EnvironmentId> => { | ||
| const environmentIds: EnvironmentId[] = []; | ||
| for (const environmentId of get(environmentCatalog.catalogValueAtom).entries.keys()) { | ||
| if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) { | ||
| environmentIds.push(environmentId); | ||
| } | ||
| } | ||
| return environmentIds; | ||
| }).pipe(Atom.withLabel("completed-thread-unread:snapshot-environments")); | ||
|
|
||
| interface FirstSeenThreadInput { | ||
| readonly environmentId: EnvironmentId; | ||
| readonly id: ThreadId; | ||
| readonly latestTurn: { | ||
| readonly state: string; | ||
| readonly completedAt: string | null; | ||
| } | null; | ||
| } | ||
|
|
||
| export function resolveFirstSeenCompletedThreads(input: { | ||
| readonly threads: ReadonlyArray<FirstSeenThreadInput>; | ||
| readonly environmentSnapshotIds: ReadonlyArray<EnvironmentId>; | ||
| readonly previouslySeenThreadKeysByEnvironment: ReadonlyMap<EnvironmentId, ReadonlySet<string>>; | ||
| }): { | ||
| readonly nextSeenThreadKeysByEnvironment: Map<EnvironmentId, Set<string>>; | ||
| readonly newlyUnreadThreads: ReadonlyArray<{ | ||
| readonly threadKey: string; | ||
| readonly completedAt: string | null; | ||
| }>; | ||
| } { | ||
| const snapshotEnvironmentIds = new Set(input.environmentSnapshotIds); | ||
| const nextSeenThreadKeysByEnvironment = new Map<EnvironmentId, Set<string>>(); | ||
| const newlyUnreadThreads: Array<{ | ||
| readonly threadKey: string; | ||
| readonly completedAt: string | null; | ||
| }> = []; | ||
| for (const environmentId of snapshotEnvironmentIds) { | ||
| nextSeenThreadKeysByEnvironment.set(environmentId, new Set()); | ||
| } | ||
|
|
||
| for (const thread of input.threads) { | ||
| if (!snapshotEnvironmentIds.has(thread.environmentId)) { | ||
| continue; | ||
| } | ||
|
|
||
| const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); | ||
| nextSeenThreadKeysByEnvironment.get(thread.environmentId)?.add(threadKey); | ||
|
|
||
| const previousThreadKeys = input.previouslySeenThreadKeysByEnvironment.get( | ||
| thread.environmentId, | ||
| ); | ||
| if ( | ||
| previousThreadKeys !== undefined && | ||
| !previousThreadKeys.has(threadKey) && | ||
| thread.latestTurn?.state === "completed" | ||
| ) { | ||
| newlyUnreadThreads.push({ | ||
| threadKey, | ||
| completedAt: thread.latestTurn.completedAt, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { nextSeenThreadKeysByEnvironment, newlyUnreadThreads }; | ||
| } | ||
|
|
||
| export function useMarkFirstSeenCompletedThreadsUnread(): void { | ||
| const threads = useThreadShells(); | ||
| const environmentSnapshotIds = useAtomValue(environmentSnapshotIdsAtom); | ||
| const seenThreadKeysByEnvironmentRef = useRef<Map<EnvironmentId, Set<string>>>(new Map()); | ||
|
|
||
| useEffect(() => { | ||
| const { nextSeenThreadKeysByEnvironment, newlyUnreadThreads } = | ||
| resolveFirstSeenCompletedThreads({ | ||
| threads, | ||
| environmentSnapshotIds, | ||
| previouslySeenThreadKeysByEnvironment: seenThreadKeysByEnvironmentRef.current, | ||
| }); | ||
| for (const thread of newlyUnreadThreads) { | ||
| useUiStateStore.getState().markThreadUnread(thread.threadKey, thread.completedAt); | ||
| } | ||
|
|
||
| seenThreadKeysByEnvironmentRef.current = nextSeenThreadKeysByEnvironment; | ||
| }, [environmentSnapshotIds, threads]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
hooks/useMarkFirstSeenCompletedThreadsUnread.ts:64resolveFirstSeenCompletedThreadsmarks a completed thread as seen and pushes it tonewlyUnreadThreadseven whenlatestTurn.completedAtisnull. SincemarkThreadUnreadignores null-timestamp threads, the thread is recorded as seen but never actually marked unread. When the realcompletedAtarrives in a later snapshot, the thread key is already in the previous-seen set, so it is never retried — the thread stays read forever. Consider deferring the push tonewlyUnreadThreadsuntilcompletedAtis non-null, while still tracking the key as seen.if ( previousThreadKeys !== undefined && !previousThreadKeys.has(threadKey) && thread.latestTurn?.state === "completed" && + thread.latestTurn.completedAt != null ) {🤖 Copy this AI Prompt to have your agent fix this: