Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(agent-platform): sort session lists by recent activity (#69114)

Dylan Martin70d1305a806c
diff --git a/products/agent_platform/backend/presentation/views.py b/products/agent_platform/backend/presentation/views.py
index 4e87ce7e5b04..3bfc31b0e563 100644
--- a/products/agent_platform/backend/presentation/views.py
+++ b/products/agent_platform/backend/presentation/views.py
@@ -1160,7 +1160,7 @@ class AgentApplicationViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
)
@action(detail=True, methods=["get"], url_path="sessions")
def sessions_list(self, request: Request, **kwargs) -> Response:
- """List sessions for this application, newest first. Strips the
+ """List sessions for this application, most recently active first. Strips the
conversation transcript from each summary, but includes a `preview`
(last assistant text, ~120 chars) and `usage_total` (token + cost
aggregate). Use `agent-applications-sessions-retrieve` for the full
diff --git a/products/agent_platform/frontend/generated/api.ts b/products/agent_platform/frontend/generated/api.ts
index 283ea28b7f92..2d4aa72d3df7 100644
--- a/products/agent_platform/frontend/generated/api.ts
+++ b/products/agent_platform/frontend/generated/api.ts
@@ -1904,7 +1904,7 @@ export const getAgentApplicationsSessionsListUrl = (
}
/**
- * List sessions for this application, newest first. Strips the
+ * List sessions for this application, most recently active first. Strips the
* conversation transcript from each summary, but includes a `preview`
* (last assistant text, ~120 chars) and `usage_total` (token + cost
* aggregate). Use `agent-applications-sessions-retrieve` for the full
diff --git a/products/agent_platform/services/agent-janitor/src/server.test.ts b/products/agent_platform/services/agent-janitor/src/server.test.ts
index 7a3c931a616d..f7f98f3a420d 100644
--- a/products/agent_platform/services/agent-janitor/src/server.test.ts
+++ b/products/agent_platform/services/agent-janitor/src/server.test.ts
@@ -202,10 +202,22 @@ describe('janitor HTTP', () => {
expect(res.body.sections).toContain('models')
})
- it('GET /sessions?application_id= returns summaries, newest first', async () => {
+ it('GET /sessions?application_id= returns summaries, most recently active first', async () => {
const { queue, app } = mk()
- const a = { ...session('s-a'), application_id: uuidFor('app-1'), created_at: '2026-05-01T00:00:00Z' }
- const b = { ...session('s-b'), application_id: uuidFor('app-1'), created_at: '2026-05-02T00:00:00Z' }
+ // s-a was created first but touched most recently — activity ordering
+ // must put it ahead of the newer-created s-b.
+ const a = {
+ ...session('s-a'),
+ application_id: uuidFor('app-1'),
+ created_at: '2026-05-01T00:00:00Z',
+ updated_at: '2026-05-04T00:00:00Z',
+ }
+ const b = {
+ ...session('s-b'),
+ application_id: uuidFor('app-1'),
+ created_at: '2026-05-02T00:00:00Z',
+ updated_at: '2026-05-02T00:00:00Z',
+ }
const c = { ...session('s-c'), application_id: uuidFor('other'), created_at: '2026-05-03T00:00:00Z' }
await queue.enqueue(a)
await queue.enqueue(b)
@@ -215,7 +227,7 @@ describe('janitor HTTP', () => {
.query({ application_id: uuidFor('app-1') })
expect(res.status).toBe(200)
const ids = (res.body.results as Array<{ id: string }>).map((s) => s.id)
- expect(ids).toEqual([uuidFor('s-b'), uuidFor('s-a')])
+ expect(ids).toEqual([uuidFor('s-a'), uuidFor('s-b')])
expect(res.body.count).toBe(2)
// Summaries strip the heavy conversation body.
expect(Object.keys(res.body.results[0])).not.toContain('conversation')
diff --git a/products/agent_platform/services/agent-janitor/src/server.ts b/products/agent_platform/services/agent-janitor/src/server.ts
index cef46553c8a4..8febbaa8ebb6 100644
--- a/products/agent_platform/services/agent-janitor/src/server.ts
+++ b/products/agent_platform/services/agent-janitor/src/server.ts
@@ -6,7 +6,7 @@
* Endpoints (grouped):
*
* Session lifecycle (existing):
- * GET /sessions?application_id= list sessions for one application (newest first)
+ * GET /sessions?application_id= list sessions for one application (most recently active first)
* GET /sessions/:id full session state
* POST /sessions/:id/cancel mark failed
* POST /sweep trigger a sweep (tests / debug)
diff --git a/products/agent_platform/services/agent-shared/src/persistence/pg-queue.ts b/products/agent_platform/services/agent-shared/src/persistence/pg-queue.ts
index 1ef50c4d6f7c..114121931c7d 100644
--- a/products/agent_platform/services/agent-shared/src/persistence/pg-queue.ts
+++ b/products/agent_platform/services/agent-shared/src/persistence/pg-queue.ts
@@ -447,7 +447,7 @@ export class PgSessionQueue implements SessionQueue {
`SELECT ${SELECT_COLS}
FROM agent_session
WHERE ${where.join(' AND ')}
- ORDER BY created_at DESC
+ ORDER BY updated_at DESC
LIMIT $${params.length - 1} OFFSET $${params.length}`,
params
)
@@ -475,7 +475,7 @@ export class PgSessionQueue implements SessionQueue {
`SELECT ${SUMMARY_COLS}
FROM agent_session
WHERE ${where.join(' AND ')}
- ORDER BY created_at DESC
+ ORDER BY updated_at DESC
LIMIT $${params.length - 1} OFFSET $${params.length}`,
params
)
diff --git a/products/agent_platform/services/agent-shared/src/persistence/queue.ts b/products/agent_platform/services/agent-shared/src/persistence/queue.ts
index 223fc589917d..73b13f5ba007 100644
--- a/products/agent_platform/services/agent-shared/src/persistence/queue.ts
+++ b/products/agent_platform/services/agent-shared/src/persistence/queue.ts
@@ -181,10 +181,12 @@ export interface SessionQueue extends SessionInputsStore {
*/
clearStaleIdempotencyKeys(cutoff: Date): Promise<number>
/**
- * List sessions for one application, newest first. `limit` defaults to 100
- * so a buggy caller can't accidentally page through every session in the
- * project; supply an explicit larger value if needed (capped at 500
- * server-side). Filters compose with AND semantics.
+ * List sessions for one application, most recently active first
+ * (`updated_at DESC` — the runner stamps `updated_at` on every
+ * conversation write). `limit` defaults to 100 so a buggy caller can't
+ * accidentally page through every session in the project; supply an
+ * explicit larger value if needed (capped at 500 server-side). Filters
+ * compose with AND semantics.
*/
listByApplication(applicationId: string, opts?: ListSessionsOpts): Promise<AgentSession[]>
/**
diff --git a/services/mcp/src/generated/agent_platform/api.ts b/services/mcp/src/generated/agent_platform/api.ts
index a143bdebcc6e..3eed1fdf9eb0 100644
--- a/services/mcp/src/generated/agent_platform/api.ts
+++ b/services/mcp/src/generated/agent_platform/api.ts
@@ -972,7 +972,7 @@ export const AgentApplicationsPreviewProxyBody = /* @__PURE__ */ zod
)
/**
- * List sessions for this application, newest first. Strips the
+ * List sessions for this application, most recently active first. Strips the
* conversation transcript from each summary, but includes a `preview`
* (last assistant text, ~120 chars) and `usage_total` (token + cost
* aggregate). Use `agent-applications-sessions-retrieve` for the full