mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(activity): roll subagent activity up to its root and accept only selectable windows
- List child sessions in the activity query and credit each child's latest update to its root, so Recent includes roots whose subagents were active inside the window; children never become rows, and only roots of in-window children are fetched - Show and sort activity rows by that rolled-up time - Restrict activityWindowMinutes to the selectable options on write, and coerce a stored unknown value to the default on read so it cannot reset every preference
This commit is contained in:
1 parent
8b6251d83b
commit
84cdffa261
22 files changed
+343
-48
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
buildRepoActivityIndex,
|
||||
collectActiveRoots,
|
||||
repoActivityFromIndex,
|
||||
rootSessionID,
|
||||
type ActiveWorkGroups,
|
||||
} from '@/lib/activeWork'
|
||||
import {
|
||||
@@ -103,15 +104,27 @@ export function useActiveWork(windowMs: number): {
|
||||
} = useSessionsAcrossDirectories(repoDirectories, {
|
||||
allDirectories: true,
|
||||
limit: ACTIVE_WORK_SESSION_LIMIT,
|
||||
includeChildren: true,
|
||||
})
|
||||
|
||||
const statuses = useSessionStatus((state) => state.statuses)
|
||||
const parents = useSessionStatus((state) => state.parents)
|
||||
const storeParents = useSessionStatus((state) => state.parents)
|
||||
const runningShells = useSessionStatus((state) => state.runningShells)
|
||||
const outcomes = useSessionStatus((state) => state.outcomes)
|
||||
const sessionDirectories = useSessionStatus((state) => state.directories)
|
||||
const pendingInputSessionIDs = usePendingInputSessionIDs()
|
||||
|
||||
const parents = useMemo(() => {
|
||||
const merged = new Map<string, string | null>()
|
||||
for (const session of sessions) {
|
||||
merged.set(session.id, session.parentID ?? null)
|
||||
}
|
||||
for (const [sessionID, parentID] of storeParents) {
|
||||
if (!merged.has(sessionID)) merged.set(sessionID, parentID)
|
||||
}
|
||||
return merged
|
||||
}, [sessions, storeParents])
|
||||
|
||||
const roots = useMemo(
|
||||
() => collectActiveRoots({ statuses, parents, runningShells, pendingInputSessionIDs }),
|
||||
[statuses, parents, runningShells, pendingInputSessionIDs],
|
||||
@@ -124,6 +137,12 @@ export function useActiveWork(windowMs: number): {
|
||||
|
||||
const missingRootIDs = useMemo(() => {
|
||||
const ids = new Set<string>()
|
||||
const windowStart = Date.now() - windowMs
|
||||
for (const session of sessions) {
|
||||
if (session.time.updated < windowStart) continue
|
||||
const rootID = rootSessionID(session.id, parents)
|
||||
if (!listedSessionIDs.has(rootID)) ids.add(rootID)
|
||||
}
|
||||
for (const sessionID of roots.needsInput) {
|
||||
if (!listedSessionIDs.has(sessionID)) ids.add(sessionID)
|
||||
}
|
||||
@@ -131,7 +150,7 @@ export function useActiveWork(windowMs: number): {
|
||||
if (!listedSessionIDs.has(sessionID)) ids.add(sessionID)
|
||||
}
|
||||
return [...ids]
|
||||
}, [roots, listedSessionIDs])
|
||||
}, [sessions, parents, listedSessionIDs, roots, windowMs])
|
||||
|
||||
const fetchedSessions = useQueries({
|
||||
queries: missingRootIDs.map((sessionID) =>
|
||||
@@ -152,10 +171,11 @@ export function useActiveWork(windowMs: number): {
|
||||
windowMs,
|
||||
repos,
|
||||
sessions: mergedSessions,
|
||||
parents,
|
||||
roots,
|
||||
outcomes,
|
||||
}),
|
||||
[windowMs, repos, mergedSessions, roots, outcomes],
|
||||
[windowMs, repos, mergedSessions, parents, roots, outcomes],
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
@@ -174,6 +174,61 @@ describe('useSessionsAcrossDirectories', () => {
|
||||
expect(result.current.data.map((session) => session.id)).toEqual(['ses_all1', 'ses_all2'])
|
||||
})
|
||||
|
||||
it('requests child sessions and records includeChildren in the query key', async () => {
|
||||
mocks.listSessionPage.mockResolvedValue({ items: [sessionInfo('ses_child', '/w/a')] })
|
||||
|
||||
const queryClient = createQueryClient()
|
||||
const { result } = renderHook(
|
||||
() => useSessionsAcrossDirectories(['/w/a'], { includeChildren: true }),
|
||||
{ wrapper: createWrapper(queryClient) },
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(mocks.listSessionPage).toHaveBeenCalledWith({
|
||||
directory: '/w/a',
|
||||
limit: 25,
|
||||
order: 'desc',
|
||||
search: undefined,
|
||||
includeChildren: true,
|
||||
})
|
||||
expect(queryClient.getQueryCache().getAll()[0]?.queryKey).toEqual([
|
||||
'opencode',
|
||||
'sessions',
|
||||
'/w/a',
|
||||
{
|
||||
search: undefined,
|
||||
limit: 25,
|
||||
allDirectories: false,
|
||||
project: undefined,
|
||||
subpath: undefined,
|
||||
includeChildren: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps root-only and include-children lists in distinct cache entries', async () => {
|
||||
mocks.listSessionPage.mockResolvedValue({ items: [] })
|
||||
|
||||
const queryClient = createQueryClient()
|
||||
const wrapper = createWrapper(queryClient)
|
||||
renderHook(() => useSessionsAcrossDirectories(['/w/a']), { wrapper })
|
||||
renderHook(() => useSessionsAcrossDirectories(['/w/a'], { includeChildren: true }), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.listSessionPage).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
const optionObjects = queryClient
|
||||
.getQueryCache()
|
||||
.getAll()
|
||||
.map((query) => query.queryKey[3] as { includeChildren?: boolean })
|
||||
expect(optionObjects.some((options) => options.includeChildren === true)).toBe(true)
|
||||
expect(optionObjects.some((options) => options.includeChildren === false)).toBe(true)
|
||||
})
|
||||
|
||||
it('pages a project scope through one cursor query with project and subpath', async () => {
|
||||
mocks.listSessionPage.mockImplementation(async ({ cursor }: { cursor?: string }) => {
|
||||
if (cursor === 'cursor_project') {
|
||||
|
||||
@@ -44,6 +44,7 @@ interface UseSessionsAcrossDirectoriesOptions {
|
||||
keepPreviousResults?: boolean
|
||||
allDirectories?: boolean
|
||||
project?: { id: string; subpath: string }
|
||||
includeChildren?: boolean
|
||||
}
|
||||
|
||||
type SessionDirectoryCursors = Record<string, string>
|
||||
@@ -73,7 +74,9 @@ export const useSessionsAcrossDirectories = (
|
||||
const projectSubpath = options?.project?.subpath;
|
||||
const projectScoped = !allDirectories && projectId !== undefined;
|
||||
const singleCursor = allDirectories || projectScoped;
|
||||
const includeChildren = options?.includeChildren ?? false;
|
||||
const directoryKey = uniqueDirectories.join('|');
|
||||
const childSessionsParam = includeChildren ? { includeChildren: true } : {};
|
||||
|
||||
const query = useInfiniteQuery<
|
||||
SessionListPage,
|
||||
@@ -86,7 +89,7 @@ export const useSessionsAcrossDirectories = (
|
||||
'opencode',
|
||||
'sessions',
|
||||
directoryKey,
|
||||
{ search: normalizedSearch, limit, allDirectories, project: projectId, subpath: projectSubpath },
|
||||
{ search: normalizedSearch, limit, allDirectories, project: projectId, subpath: projectSubpath, includeChildren },
|
||||
],
|
||||
queryFn: async ({ pageParam }) => {
|
||||
if (singleCursor) {
|
||||
@@ -98,8 +101,9 @@ export const useSessionsAcrossDirectories = (
|
||||
order: 'desc',
|
||||
search: normalizedSearch,
|
||||
...(projectScoped ? { project: projectId, subpath: projectSubpath } : {}),
|
||||
...childSessionsParam,
|
||||
}
|
||||
: { cursor },
|
||||
: { cursor, ...childSessionsParam },
|
||||
);
|
||||
return {
|
||||
items: page.items,
|
||||
@@ -115,6 +119,7 @@ export const useSessionsAcrossDirectories = (
|
||||
limit,
|
||||
order: 'desc',
|
||||
search: normalizedSearch,
|
||||
...childSessionsParam,
|
||||
}),
|
||||
),
|
||||
);
|
||||
@@ -134,7 +139,9 @@ export const useSessionsAcrossDirectories = (
|
||||
|
||||
const entries = Object.entries(pageParam.cursors);
|
||||
const pages = await Promise.all(
|
||||
entries.map(([directory, cursor]) => listSessionPage({ directory, cursor })),
|
||||
entries.map(([directory, cursor]) =>
|
||||
listSessionPage({ directory, cursor, ...childSessionsParam }),
|
||||
),
|
||||
);
|
||||
const cursors: SessionDirectoryCursors = {};
|
||||
const items: SessionInfo[] = [];
|
||||
|
||||
Reference in new issue
Block a user