Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions packages/app/src/pages/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { createShellNavigation } from "./layout/shell-navigation"
import {
buildPawworkSessionWindow,
nextPawworkSessionWindowLimit,
type PawworkWindowSession,
PAWWORK_SESSION_WINDOW_INITIAL,
sortPawworkSessionWindowSessions,
} from "./layout/pawwork-session-window"
Expand Down Expand Up @@ -524,9 +525,9 @@ export default function Layout(props: ParentProps) {

const [pawworkSessionWindowState, setPawworkSessionWindowState] = createStore({
limit: PAWWORK_SESSION_WINDOW_INITIAL,
normal: [] as Session[],
pinned: [] as Session[],
active: undefined as Session | undefined,
normal: [] as PawworkWindowSession[],
pinned: [] as PawworkWindowSession[],
active: undefined as PawworkWindowSession | undefined,
hasMore: false,
loading: false,
})
Expand Down Expand Up @@ -581,10 +582,26 @@ export default function Layout(props: ParentProps) {
const [store] = globalSync.child(session.directory, { bootstrap: false, pin: false })
return store.message[session.id]
},
partsForMessage: (session, messageID) => {
const [store] = globalSync.child(session.directory, { bootstrap: false, pin: false })
return store.part[messageID]
},
})
return sortPawworkSidebarSessions(rows.map((item) => ({ ...item, id: item.session.id }))).map(({ id: _, ...item }) => item)
})

const mergePawworkWindowSessionMetadata = (
session: Session | PawworkWindowSession,
existing?: PawworkWindowSession,
): PawworkWindowSession => {
const next = session as PawworkWindowSession
return {
...session,
activityAt: next.activityAt ?? existing?.activityAt,
lastUserMessageAt: next.lastUserMessageAt ?? existing?.lastUserMessageAt,
}
}

async function loadPawworkSessionWindow() {
if (!pageReady()) return
if (!layoutReady()) return
Expand All @@ -595,17 +612,33 @@ export default function Layout(props: ParentProps) {
const response = await globalSDK.client.experimental.session.list({
roots: true,
limit: pawworkSessionWindowState.limit,
sort: "created",
sort: "activity",
})
if (rev !== pawworkSessionWindowRev) return
const normal = ((response.data ?? []) as Session[]).filter((session) => !session.time?.archived)
const normal = ((response.data ?? []) as PawworkWindowSession[]).filter((session) => !session.time?.archived)
const loaded = new Map(normal.map((session) => [session.id, session]))
const existing = new Map(
[
...pawworkSessionWindowState.normal,
...pawworkSessionWindowState.pinned,
...(pawworkSessionWindowState.active ? [pawworkSessionWindowState.active] : []),
].map((session) => [session.id, session] as const),
)
const pinned = (
await Promise.all(
store.pawworkPinnedSessions.map(async (id) => loaded.get(id) ?? (await loadSessionByID(id))),
store.pawworkPinnedSessions.map(async (id) => {
const session = loaded.get(id) ?? (await loadSessionByID(id))
return session ? mergePawworkWindowSessionMetadata(session, existing.get(id)) : undefined
}),
Comment thread
Astro-Han marked this conversation as resolved.
Resolved
)
).filter((session): session is Session => !!session)
const active = params.id ? (loaded.get(params.id) ?? (await loadSessionByID(params.id))) : undefined
).filter((session): session is PawworkWindowSession => !!session)
const activeID = params.id
const active = activeID
? await (async () => {
const session = loaded.get(activeID) ?? (await loadSessionByID(activeID))
return session ? mergePawworkWindowSessionMetadata(session, existing.get(activeID)) : undefined
})()
: undefined

if (rev !== pawworkSessionWindowRev) return
batch(() => {
Expand Down Expand Up @@ -644,17 +677,20 @@ export default function Layout(props: ParentProps) {

const upsertPawworkWindowSession = (info: Session) => {
if (info.parentID || info.time?.archived) return
const mergeWindowSession = (current: PawworkWindowSession[]) => {
const existing = current.find((session) => session.id === info.id)
const next = mergePawworkWindowSessionMetadata(info, existing)
return sortPawworkSessionWindowSessions([...current.filter((session) => session.id !== info.id), next])
}
batch(() => {
setPawworkSessionWindowState("normal", (current) =>
sortPawworkSessionWindowSessions([...current.filter((session) => session.id !== info.id), info]),
)
setPawworkSessionWindowState("normal", mergeWindowSession)
if (store.pawworkPinnedSessions.includes(info.id)) {
setPawworkSessionWindowState("pinned", (current) =>
sortPawworkSessionWindowSessions([...current.filter((session) => session.id !== info.id), info]),
)
setPawworkSessionWindowState("pinned", mergeWindowSession)
}
if (params.id === info.id) {
setPawworkSessionWindowState("active", info)
setPawworkSessionWindowState("active", (current) =>
current?.id === info.id ? mergePawworkWindowSessionMetadata(info, current) : mergePawworkWindowSessionMetadata(info),
)
}
})
}
Expand Down
172 changes: 172 additions & 0 deletions packages/app/src/pages/layout/pawwork-session-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,89 @@ describe("sortPawworkSidebarSessions", () => {
})

describe("buildPawworkSidebarSessionRows", () => {
test("uses API activity time before loaded message cache for sidebar rows", () => {
const result = buildPawworkSidebarSessionRows(
[
{
id: "session-old",
directory: "/repo",
activityAt: 900,
time: { created: 100, updated: 950 },
},
],
{
slugForDirectory: (directory) => `slug:${directory}`,
projectLabelForSession: () => "PawWork",
messagesForSession: () => [{ id: "msg_1", role: "user", time: { created: 800 } }],
},
)

expect(result[0].created).toBe(900)
})

test("does not let unqualified loaded user message cache override API activity time", () => {
const result = buildPawworkSidebarSessionRows(
[
{
id: "session-old",
directory: "/repo",
activityAt: 700,
time: { created: 100, updated: 950 },
},
],
{
slugForDirectory: (directory) => `slug:${directory}`,
projectLabelForSession: () => "PawWork",
messagesForSession: () => [{ id: "msg_1", role: "user", time: { created: 900 } }],
},
)

expect(result[0].created).toBe(700)
})

test("uses fresher loaded real user message parts over stale API activity time", () => {
const result = buildPawworkSidebarSessionRows(
[
{
id: "session-old",
directory: "/repo",
activityAt: 700,
time: { created: 100, updated: 950 },
},
],
{
slugForDirectory: (directory) => `slug:${directory}`,
projectLabelForSession: () => "PawWork",
messagesForSession: () => [{ id: "msg_1", role: "user", time: { created: 900 } }],
partsForMessage: (_session, messageID) =>
messageID === "msg_1" ? [{ type: "text", synthetic: false }] : undefined,
},
)

expect(result[0].created).toBe(900)
})

test("does not let loaded synthetic-only user message parts override API activity time", () => {
const result = buildPawworkSidebarSessionRows(
[
{
id: "session-old",
directory: "/repo",
activityAt: 700,
time: { created: 100, updated: 950 },
},
],
{
slugForDirectory: (directory) => `slug:${directory}`,
projectLabelForSession: () => "PawWork",
messagesForSession: () => [{ id: "msg_1", role: "user", time: { created: 900 } }],
partsForMessage: (_session, messageID) => (messageID === "msg_1" ? [{ type: "text", synthetic: true }] : undefined),
},
)

expect(result[0].created).toBe(700)
})

test("uses loaded user message time for sidebar rows", () => {
const result = buildPawworkSidebarSessionRows(
[
Expand Down Expand Up @@ -203,6 +286,95 @@ describe("pawworkSidebarSessionTime", () => {
).toBe(500)
})

test("uses API activity time before loaded user messages when activity is available", () => {
expect(
pawworkSidebarSessionTime(
{
activityAt: 400,
time: {
created: 100,
updated: 600,
},
},
[{ id: "msg_1", role: "user", time: { created: 500 } }],
),
).toBe(400)
expect(
pawworkSidebarSessionTime(
{
activityAt: 700,
time: {
created: 100,
updated: 800,
},
},
[{ id: "msg_2", role: "user", time: { created: 500 } }],
),
).toBe(700)
})

test("uses newer eligible loaded user time when activity is stale", () => {
expect(
pawworkSidebarSessionTime(
{
activityAt: 400,
time: {
created: 100,
updated: 600,
},
},
[{ id: "msg_1", role: "user", time: { created: 500 } }],
(messageID) => (messageID === "msg_1" ? [{ type: "text" }] : undefined),
),
).toBe(500)
})

test("does not use synthetic-only or compaction loaded user messages when activity is available", () => {
expect(
pawworkSidebarSessionTime(
{
activityAt: 400,
time: {
created: 100,
updated: 600,
},
},
[
{ id: "msg_1", role: "user", time: { created: 600 } },
{ id: "msg_2", role: "user", time: { created: 500 } },
],
(messageID) =>
messageID === "msg_1"
? [{ type: "text", synthetic: true }]
: messageID === "msg_2"
? [{ type: "compaction" }]
: undefined,
),
).toBe(400)
})

test("uses real user messages that also include synthetic reminder parts", () => {
expect(
pawworkSidebarSessionTime(
{
activityAt: 400,
time: {
created: 100,
updated: 600,
},
},
[{ id: "msg_1", role: "user", time: { created: 500 } }],
(messageID) =>
messageID === "msg_1"
? [
{ type: "text" },
{ type: "text", synthetic: true },
]
: undefined,
),
).toBe(500)
})

test("ignores user messages without a valid created time", () => {
expect(
pawworkSidebarSessionTime(
Expand Down
53 changes: 50 additions & 3 deletions packages/app/src/pages/layout/pawwork-session-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type SessionLike = {
}

type SessionTimeLike = {
activityAt?: number
time?: {
created?: number
updated?: number
Expand All @@ -29,6 +30,11 @@ type MessageTimeLike = {
}
}

type PartTimeLike = {
type?: string
synthetic?: boolean
}

type SidebarRowSessionLike = SessionTimeLike & {
id: string
directory: string
Expand Down Expand Up @@ -68,13 +74,49 @@ export function sortPawworkSidebarSessions<T extends SessionLike>(sessions: T[])
})
}

export function pawworkSidebarSessionTime(session: SessionTimeLike, messages?: MessageTimeLike[]) {
const isActivityEligibleUserMessage = (parts: PartTimeLike[] | undefined) => {
if (!parts) return false
if (parts.some((part) => part.type === "compaction")) return false
const hasSynthetic = parts.some((part) => part.synthetic === true)
if (!hasSynthetic) return true
return parts.some((part) => part.synthetic !== true)
}

const latestLoadedUserMessageTime = (
messages: MessageTimeLike[] | undefined,
partsForMessage: ((messageID: string) => PartTimeLike[] | undefined) | undefined,
requireEligibility: boolean,
) => {
let latestLoadedUserAt: number | undefined
for (let i = (messages?.length ?? 0) - 1; i >= 0; i--) {
const message = messages?.[i]
if (message?.role !== "user") continue
if (requireEligibility) {
if (!message.id) continue
if (!isActivityEligibleUserMessage(partsForMessage?.(message.id))) continue
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const created = message.time?.created
if (isFiniteNumber(created)) return created
if (isFiniteNumber(created)) {
latestLoadedUserAt = created
break
}
}
return latestLoadedUserAt
}

export function pawworkSidebarSessionTime(
session: SessionTimeLike,
messages?: MessageTimeLike[],
partsForMessage?: (messageID: string) => PartTimeLike[] | undefined,
) {
if (isFiniteNumber(session.activityAt)) {
const latestEligibleLoadedUserAt = latestLoadedUserMessageTime(messages, partsForMessage, true)
return latestEligibleLoadedUserAt === undefined
? session.activityAt
: Math.max(session.activityAt, latestEligibleLoadedUserAt)
}
const latestLoadedUserAt = latestLoadedUserMessageTime(messages, partsForMessage, false)
if (latestLoadedUserAt !== undefined) return latestLoadedUserAt
const sessionCreated = session.time?.created
return isFiniteNumber(sessionCreated) ? sessionCreated : 0
}
Expand All @@ -85,13 +127,18 @@ export function buildPawworkSidebarSessionRows<T extends SidebarRowSessionLike>(
slugForDirectory: (directory: string) => string
projectLabelForSession: (session: T) => string
messagesForSession?: (session: T) => MessageTimeLike[] | undefined
partsForMessage?: (session: T, messageID: string) => PartTimeLike[] | undefined
},
) {
return sessions.map((session) => ({
session,
slug: input.slugForDirectory(session.directory),
projectLabel: input.projectLabelForSession(session),
created: pawworkSidebarSessionTime(session, input.messagesForSession?.(session)),
created: pawworkSidebarSessionTime(
session,
input.messagesForSession?.(session),
input.partsForMessage ? (messageID) => input.partsForMessage?.(session, messageID) : undefined,
),
}))
}

Expand Down
Loading
Loading