Multiplayer agent harness for work.
Make the admin session listing cheap and bounded (#2076)
Regan Belldf06097f0b2a
diff --git a/plugins/admin/public/index.html b/plugins/admin/public/index.htmlindex 04354b5c3..87e966617 100644--- a/plugins/admin/public/index.html+++ b/plugins/admin/public/index.html@@ -6844,6 +6844,7 @@for (const el of document.querySelectorAll("[data-brand-product]")) el.textContent = brandSelfLabel();document.title = `${brandSelfLabel()} Admin`;let page = 1;+ let historyCursor = null;const FOCUS_STALE_MS = 60000;const viewLoadedAt = {};@@ -7264,7 +7265,7 @@if (st.own) p.set("own", st.own);if (st.cron) p.set("cron", st.cron);if (st.historyKind && st.historyKind !== "conversation") p.set("kind", st.historyKind);- if (st.page > 1) p.set("page", st.page);+ if (st.cursor) p.set("cursor", st.cursor);const q = p.toString();const path = scopeInPath ? API_BASE + "/history/scopes/" + scopeSegment : API_BASE + "/history";return q ? path + "?" + q : path;@@ -7347,6 +7348,7 @@range: p.get("range") || null,historyKind: normalizedHistoryKind,page: Math.max(1, parseInt(p.get("page"), 10) || 1),+ cursor: p.get("cursor") || null,container: p.get("container") || null,ts: p.get("ts") || null,decision: p.get("decision") || null,@@ -7365,6 +7367,7 @@view = st.view;scope = st.scope;page = st.page || 1;+ historyCursor = st.cursor || null;historyKind = st.historyKind || "conversation";$("app-view").classList.toggle("history-view", view === "history");$("app-view").classList.toggle("slack-view", view === "slack");@@ -9189,7 +9192,8 @@if (view === "memory" && orgWideView() && urlToState().mem !== "edit") path = "/api/memory/scopes";if (view === "history") {- path += `&limit=${HISTORY_PAGE_SIZE}&offset=${(page - 1) * HISTORY_PAGE_SIZE}&category=${encodeURIComponent(historyCategory(historyKind))}`;+ path += `&limit=${HISTORY_PAGE_SIZE}&category=${encodeURIComponent(historyCategory(historyKind))}`;+ if (historyCursor) path += `&cursor=${encodeURIComponent(historyCursor)}`;if (historyKind === "cron") {path += `&origin=${encodeURIComponent(historyKind)}`;const cronSel = urlToState().cron;@@ -10404,7 +10408,7 @@historyKind,historyKindMatches,cron: urlToState().cron,- pageSize: HISTORY_PAGE_SIZE,+ cursor: historyCursor,scope,orgScope: "org:" + orgId,environments: environmentDir,@@ -10425,12 +10429,6 @@errorStripBox,adminErrorCount,adminErrors,- correctPage: (next, cron) => {- if (next === page) return;- page = next;- const st = { view: "history", scope, session: null, page, historyKind, cron };- history.replaceState(st, "", stateToUrl(st));- },});}diff --git a/plugins/admin/test/history-urls.test.ts b/plugins/admin/test/history-urls.test.tsindex 13882befd..713ec9e9e 100644--- a/plugins/admin/test/history-urls.test.ts+++ b/plugins/admin/test/history-urls.test.ts@@ -63,6 +63,7 @@ function routerAt(pathname: string, search = "", base = "/admin") {turn: string | null;range: string | null;page: number;+ cursor: string | null;};};}@@ -87,8 +88,13 @@ test("scoped history addresses the scope as a path segment; kind stays a query p`/admin/history/scopes/${SCOPE_ENC}?kind=cron`,);assert.equal(- stateToUrl({ view: "history", scope: SCOPE, session: null, historyKind: "cron", cron: "c1", page: 2 }),- `/admin/history/scopes/${SCOPE_ENC}?cron=c1&kind=cron&page=2`,+ stateToUrl({ view: "history", scope: SCOPE, session: null, historyKind: "cron", cron: "c1", cursor: "17~s9" }),+ `/admin/history/scopes/${SCOPE_ENC}?cron=c1&kind=cron&cursor=17%7Es9`,+ );+ assert.equal(+ routerAt(`/admin/history/scopes/${SCOPE_ENC}`, "?kind=cron&cursor=17%7Es9").urlToState().cursor,+ "17~s9",+ "the keyset cursor round-trips through the URL",);assert.equal(stateToUrl({ view: "history", scope: "org:acme", session: null }), "/admin/history");});@@ -183,8 +189,6 @@ test("cron fire rows surface the result digest and retain silent styling", () =>orgScope: "org:acme",environments: [],historyKindMatches: () => true,- pageSize: 50,- correctPage() {},historyModeLabel: () => "Crons",kindLabels: { conversation: "Conversations", cron: "Crons" },pageShell() {},@@ -201,6 +205,50 @@ test("cron fire rows surface the result digest and retain silent styling", () =>f.dom.window.close();});+test("history pages older rows by keyset cursor, never by offset", () => {+ const f = litFixture();+ const went: Record<string, unknown>[] = [];+ const render = (cursor: string | null, nextCursor?: string) => {+ f.root.textContent = "";+ f.ui.history.history(+ f.root,+ {+ sessions: [{ id: "s1", category: "conversation", firstMessage: "hello" }],+ total: 120,+ ...(nextCursor ? { nextCursor } : {}),+ },+ {+ historyKind: "conversation",+ cron: null,+ cursor,+ scope: "org:acme",+ orgScope: "org:acme",+ environments: [],+ historyKindMatches: () => true,+ historyModeLabel: () => "Conversations",+ kindLabels: { conversation: "Conversations", cron: "Crons" },+ pageShell() {},+ cronName: () => "Job",+ scopeKind: () => "org",+ plural: (n: number, w: string) => `${n} ${w}s`,+ stateToUrl: () => "/x",+ go: (st: Record<string, unknown>) => went.push(st),+ },+ );+ return [...f.root.querySelectorAll<HTMLButtonElement>(".pager .page")];+ };+ const [newest, older] = render(null, "50~s50");+ assert.equal(newest!.disabled, true, "the first page has nothing newer");+ older!.click();+ assert.equal(went.at(-1)!.cursor, "50~s50", "Older follows the server's nextCursor");+ const [newestLater, olderLast] = render("50~s50");+ assert.equal(olderLast!.disabled, true, "the last page has no nextCursor");+ newestLater!.click();+ assert.equal(went.at(-1)!.cursor, null, "Newest drops the cursor");+ assert.equal(render(null).length, 0, "a single page renders no pager");+ f.dom.window.close();+});+test("a bare history URL is the org scope, not whatever scope was viewed last", () => {const st = routerAt("/admin/history", "?kind=cron").urlToState();assert.equal(st.scope, "org:acme");diff --git a/plugins/admin/ui/artifacts.ts b/plugins/admin/ui/artifacts.tsindex 8f6ec25e8..8e27e20e0 100644--- a/plugins/admin/ui/artifacts.ts+++ b/plugins/admin/ui/artifacts.ts@@ -889,7 +889,7 @@ function cronDetail(root: HTMLElement, cron: Data | undefined, id: string, c: Covoid c.api("GET",- `/api/sessions?scope=${encodeURIComponent(c.scope)}&limit=25&offset=0&category=background&origin=cron&cron=${encodeURIComponent(id)}`,+ `/api/sessions?scope=${encodeURIComponent(c.scope)}&limit=25&category=background&origin=cron&cron=${encodeURIComponent(id)}`,).then((response: Data) => {if (!root.isConnected) return;diff --git a/plugins/admin/ui/history.ts b/plugins/admin/ui/history.tsindex 1cf608522..e3c94aac6 100644--- a/plugins/admin/ui/history.ts+++ b/plugins/admin/ui/history.ts@@ -1,5 +1,5 @@import { html, nothing } from "lit";-import { bareCard, card, denseList, pager, renderer, node } from "./shared.ts";+import { bareCard, card, denseList, renderer, node } from "./shared.ts";export function history(root: HTMLElement, d: any, s: Record<string, any>) {const paint = renderer(root);@@ -15,17 +15,30 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {};const cron = s.historyKind === "cron" ? s.cron : null;const total = (cron ? d.total : count(s.historyKind)) || sessions.length;- const limit = d.limit || s.pageSize;- const offset = d.offset || 0;- s.correctPage(Math.floor(offset / limit) + 1, cron);const route = (extra: any) => ({view: "history",scope: s.scope,session: null,- page: 1,+ cursor: null,historyKind: s.historyKind,...extra,});+ const pages =+ s.cursor || d.nextCursor+ ? html`<div class="pager">+ <button type="button" class="page" ?disabled=${!s.cursor} @click=${() => s.go(route({ cron }))}>+ ← Newest</button+ ><span class="pageinfo">${s.plural(total, "session")}</span+ ><button+ type="button"+ class="page"+ ?disabled=${!d.nextCursor}+ @click=${() => s.go(route({ cron, cursor: d.nextCursor }))}+ >+ Older →+ </button>+ </div>`+ : nothing;const mode = s.historyModeLabel(s.historyKind);const tabs = ["conversation",@@ -111,7 +124,7 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {(row) => s.go(route({ session: row.id })),"No " + mode.toLowerCase() + " in this scope.",),- )}${pager(total, limit, offset, (page) => s.go(route({ page, cron })))}${+ )}${pages}${s.scopeKind(s.scope) !== "org" && !cron? html`<div>${diff --git a/skills-seed/admin/SKILL.md b/skills-seed/admin/SKILL.mdindex a1180b5d8..a6df9538c 100644--- a/skills-seed/admin/SKILL.md+++ b/skills-seed/admin/SKILL.md@@ -150,7 +150,7 @@ on the memory self-API is the lighter path; see the memory skill.)## Inspect activity & content```bash-GET /v1/admin/sessions?scope=&limit=&offset= → conversation listing (turns, last activity)+GET /v1/admin/sessions?scope=&limit=&cursor= → conversation listing (turns, last activity); pass nextCursor for older rowsGET /v1/admin/sessions/<id>?scope= → a transcriptGET /v1/admin/sessions/<id>/llm?scope= → captured provider requests — what the model actually saw (debugging "why did it do X")GET /v1/admin/runs?scope= → queued/in-flight/recent runsdiff --git a/src/api/routes/admin/sessions.ts b/src/api/routes/admin/sessions.tsindex 26eb0b1e9..0c4e1e8ad 100644--- a/src/api/routes/admin/sessions.ts+++ b/src/api/routes/admin/sessions.ts@@ -12,6 +12,7 @@ import {transcriptEntries,type LlmRequestRecord,type SessionCategory,+ type SessionStore,type SessionSummary,} from "../../../sessions/session-store.ts";import { createTranscriptSource } from "../../../harness/tape-projection.ts";@@ -33,6 +34,25 @@ const SESSIONS_PAGE_LIMIT_MAX = 200;const SESSIONS_PAGE_LIMIT_DEFAULT = 50;const TRANSCRIPT_LIMIT_MAX = 50000;const TRANSCRIPT_LIMIT_DEFAULT = 500;+const SCOPE_AGGREGATE_TTL_MS = 15_000;++const scopeAggregates = new WeakMap<SessionStore, Map<string, { expiresAt: number; value: Promise<unknown> }>>();++function sharedScopeAggregate<T>(store: SessionStore, key: string, read: () => Promise<T>): Promise<T> {+ const now = Date.now();+ let cache = scopeAggregates.get(store);+ if (!cache) scopeAggregates.set(store, (cache = new Map()));+ const hit = cache.get(key);+ if (hit && hit.expiresAt > now) return hit.value as Promise<T>;+ for (const [k, entry] of cache) if (entry.expiresAt <= now) cache.delete(k);+ const value = read();+ const entries = cache;+ entries.set(key, { expiresAt: now + SCOPE_AGGREGATE_TTL_MS, value });+ value.catch(() => {+ if (entries.get(key)?.value === value) entries.delete(key);+ });+ return value;+}async function displayNameForPrincipal(app: App, deps: ServerDeps, principalId: string): Promise<string> {return (await app.directoryMember(principalId))?.displayName?.trim() || principalId;@@ -171,7 +191,16 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {const originFilter: AdminSessionOrigin | undefined =originParam === "cron" || originParam === "other_background" ? originParam : undefined;const cronId = url.searchParams.get("cron") || undefined;- const stats = (await deps.sessions?.scopeSessionStats(scope, orgWide, categoryFilter, originFilter, cronId)) ?? {+ const disconnected = new AbortController();+ res.once("close", () => disconnected.abort());+ const sessionStore = deps.sessions;+ const stats = (sessionStore+ ? await sharedScopeAggregate(+ sessionStore,+ JSON.stringify(["stats", scope, categoryFilter, originFilter, cronId]),+ () => sessionStore.scopeSessionStats(scope, orgWide, categoryFilter, originFilter, cronId),+ )+ : undefined) ?? {total: 0,turns: 0,byType: {},@@ -222,26 +251,27 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {turns: stats.turns,byType: stats.byType,limit,- offset: 0,});}const total = stats.total;- const lastOffset = total ? Math.floor((total - 1) / limit) * limit : 0;const cursorParam = url.searchParams.get("cursor");const cursorMatch = cursorParam ? /^(\d+)~(.+)$/.exec(cursorParam) : null;const before = cursorMatch ? { lastActivity: Number(cursorMatch[1]), id: cursorMatch[2]! } : undefined;- const offset = before- ? 0- : Math.min(Math.max(0, Math.floor(Number(url.searchParams.get("offset"))) || 0), lastOffset);const summaries =- (await deps.sessions?.scopeSessionSummaries(scope, orgWide, {- limit,- offset,- ...(before ? { before } : {}),- ...(categoryFilter ? { category: categoryFilter } : {}),- ...(originFilter ? { origin: originFilter } : {}),- ...(cronId ? { cronId } : {}),- })) ?? [];+ (await deps.sessions+ ?.scopeSessionSummaries(scope, orgWide, {+ limit,+ signal: disconnected.signal,+ ...(before ? { before } : {}),+ ...(categoryFilter ? { category: categoryFilter } : {}),+ ...(originFilter ? { origin: originFilter } : {}),+ ...(cronId ? { cronId } : {}),+ })+ .catch((error: unknown) => {+ if (disconnected.signal.aborted) return null;+ throw error;+ })) ?? [];+ if (disconnected.signal.aborted) return;const categories = new Map(summaries.map((s) => [s.id, sessionCategory(s.origin)]));const background = summaries.filter((s) => categories.get(s.id) === "background");const sentCounts =@@ -293,7 +323,6 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {turns: stats.turns,byType: stats.byType,limit,- offset,...(summaries.length === limit? { nextCursor: `${summaries[summaries.length - 1]!.lastActivity}~${summaries[summaries.length - 1]!.id}` }: {}),diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.tsindex 06ec59641..b6ac75b9d 100644--- a/src/sessions/memory-session-store.ts+++ b/src/sessions/memory-session-store.ts@@ -742,21 +742,19 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStorereturn [...byScope].map(([scopeId, channelName]) => ({ scopeId, ...(channelName ? { channelName } : {}) }));},- async scopeSessionSummaries(scope, orgWide, page?: SessionPage, sessionIds?: string[]): Promise<SessionSummary[]> {- const idSet = sessionIds ? new Set(sessionIds) : null;+ async scopeSessionSummaries(scope, orgWide, page: SessionPage): Promise<SessionSummary[]> {const out: SessionSummary[] = [];for (const s of sessions.values()) {if (!orgWide && s.scopeId !== scope) continue;- if (idSet && !idSet.has(s.id)) continue;const origin = sessionOrigin(s.threadRef);- if (page?.category && sessionCategory(origin) !== page.category) continue;+ if (page.category && sessionCategory(origin) !== page.category) continue;if (- page?.origin === "other_background"+ page.origin === "other_background"? origin === "conversation" || origin === "cron"- : page?.origin && origin !== page.origin+ : page.origin && origin !== page.origin)continue;- if (page?.cronId && cronIdOf(s.threadRef) !== page.cronId) continue;+ if (page.cronId && cronIdOf(s.threadRef) !== page.cronId) continue;const log = entries.get(s.id) ?? [];const userEntries = log.filter((e) => e.type === "user" && !isOverheardEntry(e));out.push({@@ -774,16 +772,15 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore});}out.sort((a, b) => b.lastActivity - a.lastActivity || idDesc(a.id, b.id));- if (!page) return out;const before = page.before;- if (before) {- return out- .filter(- (r) => r.lastActivity < before.lastActivity || (r.lastActivity === before.lastActivity && r.id < before.id),- )- .slice(0, page.limit);- }- return out.slice(page.offset, page.offset + page.limit);+ return (+ before+ ? out.filter(+ (r) =>+ r.lastActivity < before.lastActivity || (r.lastActivity === before.lastActivity && r.id < before.id),+ )+ : out+ ).slice(0, page.limit);},async lastUserMessages(sessionIds): Promise<Map<string, string>> {diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.tsindex ac405a98e..51d64d0d0 100644--- a/src/sessions/postgres-session-store.ts+++ b/src/sessions/postgres-session-store.ts@@ -1,8 +1,15 @@import { randomUUID } from "node:crypto";-import { createPgPool, type PgPool, type PoolClient, withPgTransaction } from "../persistence/pg-pool.ts";+import {+ createPgPool,+ type PgPool,+ type PoolClient,+ type PgQueryOptions,+ type Rows,+ withPgTransaction,+} from "../persistence/pg-pool.ts";import { jsonbStringify } from "../persistence/durable-map.ts";-import { jsonbSafeStringify } from "../util/text.ts";-import { reportFailure } from "../util/errors.ts";+import { jsonbSafeStringify, pgTextSafe } from "../util/text.ts";+import { reportFailure, reportFailureAs } from "../util/errors.ts";import type { Session, SessionEntry, SessionType, ScopeId } from "../types.ts";import type {NewSessionPin,@@ -195,6 +202,21 @@ const SEARCH_TIMEOUT_MS = 10_000;const SPEND_INDEXABLE = `(spend_usage_json(usage_json) IS NOT NULLAND octet_length(session_id) + octet_length(model) + octet_length(usage_json) <= 2000)`;+const ADMIN_READ_TIMEOUT_MS = 10_000;+const FIRST_PREVIEW_LEN = 160;+const LAST_PREVIEW_LEN = 100;++const storedPreview = (payload: unknown, maxLen: number): string => pgTextSafe(userMessagePreview(payload, maxLen));++function parsedPayload(raw: unknown): unknown {+ if (typeof raw !== "string") return null;+ try {+ return JSON.parse(raw);+ } catch {+ return null;+ }+}+export function createPostgresSessionStore(connectionString: string, opts: StoreOptions = {}): SessionStore {const now = opts.now ?? (() => Date.now());const leaseTtlMs = opts.leaseTtlMs ?? 5 * 60_000;@@ -759,6 +781,21 @@ export function createPostgresSessionStore(connectionString: string, opts: StoreON CONFLICT DO NOTHING`,],},+ {+ id: "sessions/store/0024-user-previews",+ statements: [+ `SET LOCAL lock_timeout = '3s'`,+ `ALTER TABLE sessions ADD COLUMN IF NOT EXISTS first_user_preview TEXT`,+ `ALTER TABLE sessions ADD COLUMN IF NOT EXISTS last_user_preview TEXT`,+ ],+ },+ {+ id: "sessions/store/0025-org-activity-index",+ statements: [+ `CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_by_activity+ ON sessions((COALESCE(last_activity, created_at)) DESC, id DESC)`,+ ],+ },],[{@@ -854,6 +891,39 @@ export function createPostgresSessionStore(connectionString: string, opts: Storereturn full;};+ type UserPreviews = { first: string; last: string };+ const userPreviews = async (rows: Rows, options: PgQueryOptions): Promise<Map<string, UserPreviews>> => {+ const out = new Map<string, UserPreviews>();+ const missing: string[] = [];+ for (const r of rows) {+ const first = r.first_user_preview as string | null;+ const last = r.last_user_preview as string | null;+ out.set(r.id as string, { first: first ?? "", last: last ?? "" });+ if ((first === null || last === null) && (r.turns == null || Number(r.turns) > 0)) missing.push(r.id as string);+ }+ if (!missing.length) return out;+ const derived = await q(+ `SELECT s.id, s.first_user_preview, s.last_user_preview,+ (SELECT fe.payload FROM session_entries fe+ WHERE fe.session_id = s.id AND ${userTurn("fe")} ORDER BY fe.seq ASC LIMIT 1) AS first_user,+ (SELECT le.payload FROM session_entries le+ WHERE le.session_id = s.id AND ${userTurn("le")} ORDER BY le.seq DESC LIMIT 1) AS last_user+ FROM sessions s WHERE s.id = ANY($1)`,+ [missing],+ options,+ ).catch((error: unknown) => {+ if (options.signal?.aborted) throw error;+ return reportFailureAs("sessions: derive legacy user previews", [] as Rows)(error);+ });+ for (const r of derived) {+ out.set(r.id as string, {+ first: (r.first_user_preview as string | null) ?? storedPreview(parsedPayload(r.first_user), FIRST_PREVIEW_LEN),+ last: (r.last_user_preview as string | null) ?? storedPreview(parsedPayload(r.last_user), LAST_PREVIEW_LEN),+ });+ }+ return out;+ };+return {leaseTtlMs,async getOrCreateByThread(threadRef, type, scopeId, channelName, surface): Promise<Session> {@@ -1038,6 +1108,11 @@ export function createPostgresSessionStore(connectionString: string, opts: Storetape_payload: JSON.stringify(tapeTranscriptEntryRecord(entry).payload),}));const encoded = JSON.stringify(rows);+ const userTurns = full.filter(+ (entry, index) => entry.type === "user" && !rows[index]!.payload.includes('"overheard":true'),+ );+ const firstUser = userTurns[0];+ const lastUser = userTurns[userTurns.length - 1];await client.query(`INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at)SELECT $1, e.seq, e.parent_seq, e.type, e.payload, e.scope_label, e.created_at@@ -1062,14 +1137,19 @@ export function createPostgresSessionStore(connectionString: string, opts: Storemessages = $3,turns = CASE WHEN turns IS NULL OR messages IS DISTINCT FROM $5THEN (SELECT COUNT(*) FROM session_entries t WHERE t.session_id = $1 AND ${userTurn("t")})- ELSE turns + $4 END+ ELSE turns + $4 END,+ first_user_preview = CASE WHEN first_user_preview IS NULL AND turns = 0 AND messages = $5+ THEN $6 ELSE first_user_preview END,+ last_user_preview = COALESCE($7, last_user_preview)WHERE id = $1`,[lease.sessionId,createdAt,startSeq + full.length,- rows.filter((entry) => entry.type === "user" && !entry.payload.includes('"overheard":true')).length,+ userTurns.length,startSeq,+ firstUser ? storedPreview(firstUser.payload, FIRST_PREVIEW_LEN) : null,+ lastUser ? storedPreview(lastUser.payload, LAST_PREVIEW_LEN) : null,],);return full;@@ -1680,65 +1760,47 @@ export function createPostgresSessionStore(connectionString: string, opts: Store}));},- async scopeSessionSummaries(scope, orgWide, page?: SessionPage, sessionIds?: string[]): Promise<SessionSummary[]> {+ async scopeSessionSummaries(scope, orgWide, page: SessionPage): Promise<SessionSummary[]> {+ const options = { timeoutMs: ADMIN_READ_TIMEOUT_MS, ...(page.signal ? { signal: page.signal } : {}) };const params: unknown[] = [orgWide, scope];let categoryClause = "";- if (page?.category === "background") categoryClause = ` AND ${isBackground("s")}`;- else if (page?.category) categoryClause = ` AND NOT ${isBackground("s")}`;- const originClause = page?.origin ? ` AND ${originFilterClause("s", page.origin)}` : "";- let idsClause = "";- if (sessionIds) {- params.push(sessionIds);- idsClause = ` AND s.id = ANY($${params.length})`;- }+ if (page.category === "background") categoryClause = ` AND ${isBackground("s")}`;+ else if (page.category) categoryClause = ` AND NOT ${isBackground("s")}`;+ const originClause = page.origin ? ` AND ${originFilterClause("s", page.origin)}` : "";let cronClause = "";- if (page?.cronId) {+ if (page.cronId) {params.push(page.cronId);cronClause = ` AND ${cronIdExpr("s")} = $${params.length}`;}let keysetClause = "";- if (page?.before) {+ if (page.before) {params.push(page.before.lastActivity, page.before.id);keysetClause = ` AND (${lastActivityExpr("s")}, s.id) < ($${params.length - 1}::bigint, $${params.length}::text)`;}- let pageClause = "";- if (page) {- params.push(page.limit);- pageClause = ` LIMIT $${params.length}`;- if (!page.before) {- params.push(page.offset);- pageClause += ` OFFSET $${params.length}`;- }- }+ params.push(page.limit);const rows = await q(- `SELECT s.id, s.type, s.scope_id, s.thread_ref, s.created_at,- COALESCE(s.messages, 0) AS messages,- COALESCE(s.turns, 0) AS turns,- ${lastActivityExpr("s")} AS last_activity,- (SELECT ${previewExpr("fe.payload")} FROM session_entries fe- WHERE fe.session_id = s.id AND ${userTurn("fe")}- ORDER BY fe.seq ASC LIMIT 1) AS first_user,- (SELECT ${previewExpr("le.payload")} FROM session_entries le- WHERE le.session_id = s.id AND ${userTurn("le")}- ORDER BY le.seq DESC LIMIT 1) AS last_user+ `SELECT s.id, s.type, s.scope_id, s.thread_ref, s.created_at, s.messages, s.turns,+ ${lastActivityExpr("s")} AS last_activity, s.first_user_preview, s.last_user_previewFROM sessions s- WHERE ($1::boolean OR s.scope_id = $2)${categoryClause}${originClause}${idsClause}${cronClause}${keysetClause}- ORDER BY last_activity DESC, s.id DESC${pageClause}`,+ WHERE ($1::boolean OR s.scope_id = $2)${categoryClause}${originClause}${cronClause}${keysetClause}+ ORDER BY ${lastActivityExpr("s")} DESC, s.id DESC+ LIMIT $${params.length}`,params,+ options,);- const parse = (v: unknown, maxLen?: number): string => userMessagePreview(v ?? null, maxLen);+ const previews = await userPreviews(rows, options);return rows.map((r) => ({id: r.id as string,type: r.type as Session["type"],origin: sessionOrigin(r.thread_ref as string | null),scopeId: r.scope_id as Session["scopeId"],threadRef: r.thread_ref as Session["threadRef"],- turns: Number(r.turns),- messages: Number(r.messages),+ turns: Number(r.turns ?? 0),+ messages: Number(r.messages ?? 0),lastActivity: Number(r.last_activity),createdAt: Number(r.created_at),- firstMessage: parse(r.first_user),- lastMessage: parse(r.last_user, 100),+ firstMessage: previews.get(r.id as string)!.first,+ lastMessage: previews.get(r.id as string)!.last,}));},@@ -1840,6 +1902,7 @@ export function createPostgresSessionStore(connectionString: string, opts: StoreWHERE ($1::boolean OR s.scope_id = $2)) tGROUP BY origin, bucket, matched`,params,+ { timeoutMs: ADMIN_READ_TIMEOUT_MS },);const byType: Record<string, number> = {};const byTypeAll: Record<string, number> = {};diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.tsindex cc529058a..f08e7654e 100644--- a/src/sessions/session-store.ts+++ b/src/sessions/session-store.ts@@ -306,11 +306,11 @@ interface ListLlmRequestsOptions {export interface SessionPage {limit: number;- offset: number;before?: { lastActivity: number; id: string };category?: SessionCategory;origin?: SessionOriginFilter;cronId?: string;+ signal?: AbortSignal;}export interface CronGroupSummary {@@ -786,12 +786,7 @@ export interface SessionStore {distinctScopes(): Promise<DistinctScope[]>;- scopeSessionSummaries(- scope: ScopeId,- orgWide: boolean,- page?: SessionPage,- sessionIds?: string[],- ): Promise<SessionSummary[]>;+ scopeSessionSummaries(scope: ScopeId, orgWide: boolean, page: SessionPage): Promise<SessionSummary[]>;lastUserMessages(sessionIds: string[]): Promise<Map<string, string>>;diff --git a/test/admin-observability.test.ts b/test/admin-observability.test.tsindex b3425079b..c46417fda 100644--- a/test/admin-observability.test.ts+++ b/test/admin-observability.test.ts@@ -739,7 +739,7 @@ test("monitor delivery origin context is scope-gated in admin", async () => {}});-test("conversation listing pages by limit/offset; aggregates span the whole scope; offset clamps", async () => {+test("conversation listing pages by keyset cursor; aggregates span the whole scope; offset is ignored", async () => {const s = start();try {for (let i = 0; i < 5; i++) {@@ -752,22 +752,27 @@ test("conversation listing pages by limit/offset; aggregates span the whole scopassert.equal((await s.built.app.turn(dm)).status, "ok");}- const p1 = await getJson(s.base, "/v1/admin/sessions?scope=org:default-org&limit=2&offset=0");+ const path = "/v1/admin/sessions?scope=org:default-org&limit=2";+ const p1 = await getJson(s.base, path);assert.equal(p1.sessions.length, 2, "limit bounds the returned slice");assert.equal(p1.total, 5, "total spans the whole scope, not the page");assert.equal(p1.byType.dm, 5, "by-type aggregate spans the whole scope");assert.equal(p1.limit, 2);- assert.equal(p1.offset, 0);+ assert.equal(p1.offset, undefined, "the listing no longer reports an offset");- const p2 = await getJson(s.base, "/v1/admin/sessions?scope=org:default-org&limit=2&offset=2");- const p3 = await getJson(s.base, "/v1/admin/sessions?scope=org:default-org&limit=2&offset=4");+ const p2 = await getJson(s.base, `${path}&cursor=${encodeURIComponent(p1.nextCursor)}`);+ const p3 = await getJson(s.base, `${path}&cursor=${encodeURIComponent(p2.nextCursor)}`);assert.equal(p3.sessions.length, 1, "the final page holds the remainder");+ assert.equal(p3.nextCursor, undefined, "a short page ends the walk");const ids = new Set([...p1.sessions, ...p2.sessions, ...p3.sessions].map((x: { id: string }) => x.id));assert.equal(ids.size, 5, "the three pages cover every session exactly once");- const over = await getJson(s.base, "/v1/admin/sessions?scope=org:default-org&limit=2&offset=999");- assert.equal(over.offset, 4, "offset clamps to the last page");- assert.equal(over.sessions.length, 1, "clamped page returns the last page's rows");+ const ignored = await getJson(s.base, `${path}&offset=4`);+ assert.deepEqual(+ ignored.sessions.map((x: { id: string }) => x.id),+ p1.sessions.map((x: { id: string }) => x.id),+ "an offset parameter cannot push the database into skipping rows",+ );} finally {await s.close();}@@ -903,13 +908,13 @@ test("the Crons history lists one row per cron; ?cron= paginates that cron's firconst p1 = await getJson(s.base,- `/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron&cron=${cron.id}&limit=2&offset=0`,+ `/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron&cron=${cron.id}&limit=2`,);assert.equal(p1.total, 3, "drill-down total counts this cron's fires only");assert.equal(p1.sessions.length, 2);const p2 = await getJson(s.base,- `/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron&cron=${cron.id}&limit=2&offset=2`,+ `/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron&cron=${cron.id}&limit=2&cursor=${encodeURIComponent(p1.nextCursor)}`,);assert.equal(p2.sessions.length, 1, "last page holds the remainder");const ids = new Set([...p1.sessions, ...p2.sessions].map((x: { id: string }) => x.id));diff --git a/test/admin-paging-params.test.ts b/test/admin-paging-params.test.tsindex 5b20d0dc8..6c68ff408 100644--- a/test/admin-paging-params.test.ts+++ b/test/admin-paging-params.test.ts@@ -12,7 +12,7 @@ import { testConfig } from "./support/test-config.ts";const URL = process.env.DATABASE_URL;-test("admin list endpoints accept fractional limit and offset values", { skip: !URL }, async () => {+test("admin list endpoints accept fractional limit values", { skip: !URL }, async () => {const config = testConfig({dataDir: mkdtempSync(join(tmpdir(), "admin-paging-")),databaseUrl: URL,@@ -44,7 +44,7 @@ test("admin list endpoints accept fractional limit and offset values", { skip: !assert.equal((await built.app.turn(dm)).status, "ok");for (const path of ["/v1/admin/sessions?scope=org:default-org&limit=1.5",- "/v1/admin/sessions?scope=org:default-org&offset=0.5",+ "/v1/admin/sessions?scope=org:default-org&cursor=not-a-cursor","/v1/admin/audit?scope=org:default-org&limit=2.5",]) {const res = await fetch(base + path, { headers });diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.tsindex 6fab6443b..14fb85208 100644--- a/test/postgres-store.test.ts+++ b/test/postgres-store.test.ts@@ -894,7 +894,7 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",const cron = await s.getOrCreateByThread("agent:main:cron:c1", "channel", team);const webhook = await s.getOrCreateByThread("agent:main:webhook:wh1", "channel", team);- const scoped = await s.scopeSessionSummaries(team, false);+ const scoped = await s.scopeSessionSummaries(team, false, { limit: 500 });assert.deepEqual(scoped.map((r) => r.id).sort(),[a.id, b.id, cron.id, webhook.id].sort(),@@ -917,7 +917,7 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",assert.equal(rb.lastActivity, rb.createdAt, "entry-less last activity falls back to created_at");assert.deepEqual([rb.firstMessage, rb.lastMessage], ["", ""], "entry-less session has no message preview");- const all = await s.scopeSessionSummaries(team, true);+ const all = await s.scopeSessionSummaries(team, true, { limit: 500 });assert.equal(all.some((r) => r.id === c.id),true,@@ -929,13 +929,13 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",assert.equal(previews.has(b.id), false, "entry-less session absent from the preview map");assert.equal((await s.lastUserMessages([])).size, 0, "empty id set → empty map");- const conversations = await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, category: "conversation" });+ const conversations = await s.scopeSessionSummaries(team, false, { limit: 10, category: "conversation" });assert.deepEqual(conversations.map((r) => r.id).sort(),[a.id, b.id].sort(),"conversation page excludes cron monologues before pagination",);- const background = await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, category: "background" });+ const background = await s.scopeSessionSummaries(team, false, { limit: 10, category: "background" });assert.deepEqual(background.map((r) => r.id).sort(),[cron.id, webhook.id].sort(),@@ -943,7 +943,6 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",);const cronPage = await s.scopeSessionSummaries(team, false, {limit: 1,- offset: 0,category: "background",origin: "cron",});@@ -954,7 +953,6 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",);const otherPage = await s.scopeSessionSummaries(team, false, {limit: 1,- offset: 0,category: "background",origin: "other_background",});@@ -1000,13 +998,17 @@ test("pg scopeCronGroups: one aggregated row per cron; cronId page filters one cconst cronStats = await s.scopeSessionStats(team, false, "background", "cron", "g1");assert.equal(cronStats.total, 3, "stats cronId filter counts one cron's fires");- const fires = await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, cronId: "g1" });+ const fires = await s.scopeSessionSummaries(team, false, { limit: 10, cronId: "g1" });assert.deepEqual(fires.map((r) => r.id).sort(),[c1a.id, c1b.id, fresh.id].sort(),"cronId page returns only that cron's fires",);- const page = await s.scopeSessionSummaries(team, false, { limit: 1, offset: 1, cronId: "g1" });+ const page = await s.scopeSessionSummaries(team, false, {+ limit: 1,+ cronId: "g1",+ before: { lastActivity: fires[0]!.lastActivity, id: fires[0]!.id },+ });assert.equal(page.length, 1, "cronId filter applies before pagination");const stats = await s.scopeSessionStats(team, false);@@ -1054,7 +1056,7 @@ test(const orgWide = byScopeId(await s.scopeSessionRollups(a, true));assert.deepEqual(orgWide,- rollupsFromSummaries(await s.scopeSessionSummaries(a, true)),+ rollupsFromSummaries(await s.scopeSessionSummaries(a, true, { limit: 500 })),"org-wide aggregate matches a row-by-row pass over every scope's summaries",);const mine = orgWide.filter((r) => [a, b, c].includes(r.scopeId));@@ -1117,22 +1119,20 @@ test(]),);const classify = async () => ({- summaries: (await s.scopeSessionSummaries(team, false)).map((r) => [r.threadRef, r.origin]).sort(),- background: (await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, category: "background" }))- .map((r) => r.threadRef)+ summaries: (await s.scopeSessionSummaries(team, false, { limit: 500 }))+ .map((r) => [r.threadRef, r.origin]).sort(),- conversation: (- await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, category: "conversation" })- ).map((r) => r.threadRef),- cron: (await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, origin: "cron" }))+ background: (await s.scopeSessionSummaries(team, false, { limit: 10, category: "background" })).map((r) => r.threadRef).sort(),- other: (await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, origin: "other_background" }))- .map((r) => r.threadRef)- .sort(),- oc2: (await s.scopeSessionSummaries(team, false, { limit: 10, offset: 0, cronId: "oc2" })).map(+ conversation: (await s.scopeSessionSummaries(team, false, { limit: 10, category: "conversation" })).map((r) => r.threadRef,),+ cron: (await s.scopeSessionSummaries(team, false, { limit: 10, origin: "cron" })).map((r) => r.threadRef).sort(),+ other: (await s.scopeSessionSummaries(team, false, { limit: 10, origin: "other_background" }))+ .map((r) => r.threadRef)+ .sort(),+ oc2: (await s.scopeSessionSummaries(team, false, { limit: 10, cronId: "oc2" })).map((r) => r.threadRef),groups: (await s.scopeCronGroups(team, false)).map((g) => g.cronId).sort(),stats: await s.scopeSessionStats(team, false),cronStats: await s.scopeSessionStats(team, false, "background", "cron", "oc1"),@@ -1171,7 +1171,7 @@ test(},);-test("pg scopeSessionSummaries: keyset cursor pages stitch into the offset listing", { skip }, async () => {+test("pg scopeSessionSummaries: keyset cursor pages stitch into the full listing", { skip }, async () => {let clock = Date.now() + 1_000_000;const s = createPostgresSessionStore(URL!, { now: () => ++clock });const scope = scopeId("channel", "keyset");@@ -1181,18 +1181,16 @@ test("pg scopeSessionSummaries: keyset cursor pages stitch into the offset listiawait s.append(lease!, { type: "user", payload: { text: `m${i}` }, scopeLabel: scope });await s.releaseLease(lease!);}- const all = await s.scopeSessionSummaries(scope, false, { limit: 10, offset: 0 });- const page1 = await s.scopeSessionSummaries(scope, false, { limit: 2, offset: 0 });+ const all = await s.scopeSessionSummaries(scope, false, { limit: 10 });+ const page1 = await s.scopeSessionSummaries(scope, false, { limit: 2 });const c1 = page1[page1.length - 1]!;const page2 = await s.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: c1.lastActivity, id: c1.id },});const c2 = page2[page2.length - 1]!;const page3 = await s.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: c2.lastActivity, id: c2.id },});assert.deepEqual(@@ -1211,16 +1209,14 @@ test("pg scopeSessionSummaries: keyset cursor pages stitch into the offset listi} finally {await raw.end();}- const rerun = await s.scopeSessionSummaries(scope, false, { limit: 10, offset: 0 });- const p1 = await s.scopeSessionSummaries(scope, false, { limit: 2, offset: 0 });+ const rerun = await s.scopeSessionSummaries(scope, false, { limit: 10 });+ const p1 = await s.scopeSessionSummaries(scope, false, { limit: 2 });const p2 = await s.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: p1[1]!.lastActivity, id: p1[1]!.id },});const p3 = await s.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: p2[1]!.lastActivity, id: p2[1]!.id },});assert.deepEqual(@@ -1234,7 +1230,7 @@ test("pg scopeSessionSummaries: keyset cursor pages stitch into the offset listi);});-test("pg scopeSessionSummaries: previews extracted in SQL match the JS extraction", { skip }, async () => {+test("pg scopeSessionSummaries: previews are stored on append and match the JS extraction", { skip }, async () => {const s = createPostgresSessionStore(URL!);const scope = scopeId("channel", "pvw");const obj = await s.getOrCreateByThread("pvwA", "channel", scope);@@ -1249,63 +1245,249 @@ test("pg scopeSessionSummaries: previews extracted in SQL match the JS extractioconst strp = await s.getOrCreateByThread("pvwB", "channel", scope);const { lease: lease2 } = await s.acquireLease(strp.id);await s.append(lease2!, { type: "user", payload: "a bare string payload", scopeLabel: scope });+ await s.append(lease2!, { type: "user", payload: { text: "nul\u0000byte" }, scopeLabel: scope });await s.releaseLease(lease2!);const nontext = await s.getOrCreateByThread("pvwC", "channel", scope);const { lease: lease3 } = await s.acquireLease(nontext.id);await s.append(lease3!, { type: "user", payload: { text: 42 }, scopeLabel: scope });await s.releaseLease(lease3!);- const rows = await s.scopeSessionSummaries(scope, false);+ const rows = await s.scopeSessionSummaries(scope, false, { limit: 500 });const ra = rows.find((r) => r.id === obj.id)!;- assert.equal(- ra.firstMessage,- "the real opener",- "bracketed boilerplate stripped (even a >2KB block), like the JS preview",- );+ assert.equal(ra.firstMessage, "the real opener", "bracketed boilerplate stripped (even a >2KB block)");assert.equal(ra.lastMessage, "x".repeat(99) + "…", "last-message preview truncated to 100 like before");const rb = rows.find((r) => r.id === strp.id)!;assert.equal(rb.firstMessage, "a bare string payload", "a bare-string payload still previews");- assert.equal(- rows.find((r) => r.id === nontext.id)!.firstMessage,- "",- "a non-string text field has no preview, like the JS path",- );+ assert.equal(rb.lastMessage, "nulbyte", "a null byte never reaches the text column");+ assert.equal(rows.find((r) => r.id === nontext.id)!.firstMessage, "", "a non-string text field has no preview");const pg = (await import("pg")).default;const raw = new pg.Pool({ connectionString: URL });try {- await raw.query(- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",- [strp.id, 1, 0, "user", '{"text":"poisoned\\u0000tail"}', scope, Date.now()],+ const stored = await raw.query(+ "SELECT id, first_user_preview, last_user_preview FROM sessions WHERE id = ANY($1) ORDER BY thread_ref",+ [[obj.id, strp.id, nontext.id]],+ );+ assert.deepEqual(+ stored.rows.map((r) => [r.first_user_preview, r.last_user_preview]),+ [+ ["the real opener", "x".repeat(99) + "…"],+ ["a bare string payload", "nulbyte"],+ ["", ""],+ ],+ "the append path persists bounded previews on the session row",);} finally {await raw.end();}- const again = (await s.scopeSessionSummaries(scope, false)).find((r) => r.id === strp.id)!;- assert.equal(again.lastMessage, "poisonedtail", "the null-byte escape is dropped, not fatal");+});- const junk = await s.getOrCreateByThread("pvwD", "channel", scope);- const pg2 = (await import("pg")).default;- const raw2 = new pg2.Pool({ connectionString: URL });- try {- for (const [seq, bad] of [- [1, '{"text":"truncated'],- [2, '"unterminated'],- ] as const) {- await raw2.query(- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",- [junk.id, seq, seq - 1, "user", bad, scope, Date.now()],+test("pg appendMany maintains first/last user previews across batches", { skip }, async () => {+ const s = createPostgresSessionStore(URL!);+ const scope = scopeId("channel", "pvw-batch");+ const session = await s.getOrCreateByThread("pvwBatch", "channel", scope);+ const preview = async () => {+ const row = (await s.scopeSessionSummaries(scope, false, { limit: 10 })).find((r) => r.id === session.id)!;+ return [row.firstMessage, row.lastMessage, row.turns];+ };+ assert.deepEqual(await preview(), ["", "", 0], "a fresh session has no preview");+ const { lease } = await s.acquireLease(session.id);+ await s.appendMany(lease!, [+ { type: "assistant", payload: { text: "greeting" }, scopeLabel: scope },+ { type: "user", payload: { text: "an overheard aside", overheard: true }, scopeLabel: scope },+ { type: "user", payload: { text: "first ask" }, scopeLabel: scope },+ { type: "user", payload: { text: "second ask" }, scopeLabel: scope },+ ]);+ assert.deepEqual(await preview(), ["first ask", "second ask", 2], "one batch sets both ends, skipping overheard");+ await s.appendMany(lease!, [{ type: "assistant", payload: { text: "reply" }, scopeLabel: scope }]);+ assert.deepEqual(await preview(), ["first ask", "second ask", 2], "a batch without user turns keeps both");+ await s.append(lease!, { type: "user", payload: { text: "third ask" }, scopeLabel: scope });+ assert.deepEqual(await preview(), ["first ask", "third ask", 3], "later user turns move only the last preview");+ await s.append(lease!, { type: "user", payload: { text: "eavesdrop", overheard: true }, scopeLabel: scope });+ assert.deepEqual(await preview(), ["first ask", "third ask", 3], "an overheard turn is not the last message");+ await s.releaseLease(lease!);+});++test(+ "pg scopeSessionSummaries: legacy rows derive previews read-only, only for the requested page",+ { skip },+ async () => {+ let clock = Date.now() + 5_000_000;+ const s = createPostgresSessionStore(URL!, { now: () => (clock += 120_000) });+ const scope = scopeId("channel", "pvw-legacy");+ const ids: string[] = [];+ for (let i = 0; i < 4; i++) {+ const session = await s.getOrCreateByThread(`pvwLegacy${i}`, "channel", scope);+ const { lease } = await s.acquireLease(session.id);+ await s.append(lease!, { type: "user", payload: { text: `open ${i}` }, scopeLabel: scope });+ await s.append(lease!, { type: "user", payload: { text: `close ${i}` }, scopeLabel: scope });+ await s.releaseLease(lease!);+ ids.push(session.id);+ }+ const empty = await s.getOrCreateByThread("pvwLegacyEmpty", "channel", scope);+ const junk = await s.getOrCreateByThread("pvwLegacyJunk", "channel", scope);+ const pg = (await import("pg")).default;+ const raw = new pg.Pool({ connectionString: URL });+ const columns = async (sessionIds: string[]) =>+ (+ await raw.query(+ "SELECT id, first_user_preview AS f, last_user_preview AS l, turns FROM sessions WHERE id = ANY($1)",+ [sessionIds],+ )+ ).rows.reduce((m, r) => m.set(r.id, [r.f, r.l, r.turns]), new Map<string, unknown[]>());+ try {+ for (const [seq, payload] of [+ [0, '{"text":"poisoned\\u0000tail"}'],+ [1, '{"text":"truncated'],+ ] as const) {+ await raw.query(+ "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",+ [junk.id, seq, seq - 1, "user", payload, scope, 1],+ );+ }+ await raw.query(+ "UPDATE sessions SET first_user_preview = NULL, last_user_preview = NULL, turns = NULL WHERE id = ANY($1)",+ [[...ids, empty.id, junk.id]],+ );+ await raw.query("UPDATE sessions SET last_activity = 1 WHERE id = ANY($1)", [[empty.id, junk.id]]);++ const page = await s.scopeSessionSummaries(scope, false, { limit: 2 });+ assert.deepEqual(+ page.map((r) => [r.firstMessage, r.lastMessage]),+ [+ ["open 3", "close 3"],+ ["open 2", "close 2"],+ ],+ "a page of legacy rows reads back the same previews the append path would store",);+ const xmin = async () =>+ (await raw.query("SELECT id, xmin::text AS v FROM sessions WHERE id = ANY($1) ORDER BY id", [ids])).rows;+ const versions = await xmin();+ await s.scopeSessionSummaries(scope, false, { limit: 2 });+ assert.deepEqual(await xmin(), versions, "a listing read never writes session rows");+ assert.deepEqual(+ [...(await columns(ids)).values()].map((v) => v.slice(0, 2)),+ ids.map(() => [null, null]),+ "legacy previews are derived per read, not persisted from a GET",+ );++ const all = await s.scopeSessionSummaries(scope, false, { limit: 50 });+ assert.deepEqual(+ all.filter((r) => ids.includes(r.id)).map((r) => r.lastMessage),+ ["close 3", "close 2", "close 1", "close 0"],+ );+ const junkRow = all.find((r) => r.id === junk.id)!;+ assert.deepEqual(+ [junkRow.firstMessage, junkRow.lastMessage],+ ["poisonedtail", ""],+ "a null-byte escape is dropped and malformed JSON previews as empty, never a fatal cast",+ );+ const emptyRow = all.find((r) => r.id === empty.id)!;+ assert.deepEqual([emptyRow.firstMessage, emptyRow.lastMessage], ["", ""], "an entry-less legacy row is blank");++ const { lease } = await s.acquireLease(ids[0]!);+ await s.append(lease!, { type: "user", payload: { text: "fresh" }, scopeLabel: scope });+ await s.releaseLease(lease!);+ const resumed = (await s.scopeSessionSummaries(scope, false, { limit: 50 })).find((r) => r.id === ids[0])!;+ assert.deepEqual(+ [resumed.firstMessage, resumed.lastMessage],+ ["open 0", "fresh"],+ "a legacy row's first preview stays its opener after new appends",+ );+ } finally {+ await raw.end();}- } finally {- await raw2.end();+ },+);++test("pg scopeSessionSummaries: keyset pages break last_activity ties by id without gaps", { skip }, async () => {+ const at = Date.now() + 9_000_000;+ const s = createPostgresSessionStore(URL!, { now: () => at });+ const scope = scopeId("channel", `ties-${randomUUID()}`);+ for (let i = 0; i < 7; i++) await s.getOrCreateByThread(`ties:${scope}:${i}`, "channel", scope);+ const all = await s.scopeSessionSummaries(scope, false, { limit: 50 });+ assert.equal(new Set(all.map((r) => r.lastActivity)).size, 1, "every row ties on last_activity");+ assert.deepEqual(+ all.map((r) => r.id),+ all+ .map((r) => r.id)+ .sort()+ .reverse(),+ "ties order by id descending",+ );+ for (const orgWide of [false, true]) {+ const walked: string[] = [];+ let before = { lastActivity: at + 1, id: "" };+ while (walked.length < all.length) {+ const page = await s.scopeSessionSummaries(scope, orgWide, { limit: 3, before });+ const mine = page.filter((r) => r.scopeId === scope);+ walked.push(...mine.map((r) => r.id));+ if (mine.length < 3) break;+ before = { lastActivity: page[2]!.lastActivity, id: page[2]!.id };+ }+ assert.deepEqual(+ walked,+ all.map((r) => r.id),+ `${orgWide ? "org-wide" : "scoped"} cursor walk visits each tie once`,+ );}- const orgWide = await s.scopeSessionSummaries(scope, true);- assert.equal(- orgWide.find((r) => r.id === junk.id)!.firstMessage,- "",- "a malformed-JSON payload previews as empty, not a fatal cast",+});++test("pg scopeSessionSummaries: a cancelled caller releases the listing query", { skip }, async () => {+ const s = createPostgresSessionStore(URL!);+ const scope = scopeId("channel", "listing-abort");+ await s.getOrCreateByThread("listing-abort", "channel", scope);+ const controller = new AbortController();+ controller.abort();+ await assert.rejects(+ s.scopeSessionSummaries(scope, false, { limit: 10, signal: controller.signal }),+ { name: "AbortError" },+ "a disconnected admin request does not keep a pool connection busy",);+ assert.equal((await s.scopeSessionSummaries(scope, false, { limit: 10 })).length, 1);+});++test("pg scopeSessionSummaries: the listing plan is an index walk with no per-row subplans", { skip }, async (t) => {+ const s = createPostgresSessionStore(URL!);+ const scope = scopeId("channel", "plan-shape");+ const session = await s.getOrCreateByThread("plan-shape", "channel", scope);+ const { lease } = await s.acquireLease(session.id);+ await s.append(lease!, { type: "user", payload: { text: "hi" }, scopeLabel: scope });+ await s.releaseLease(lease!);+ const pg = (await import("pg")).default;+ const execute = pg.Client.prototype.query;+ const captured: { text: string; values: unknown[] }[] = [];+ t.mock.method(pg.Client.prototype, "query", function (this: InstanceType<typeof pg.Client>, ...args: unknown[]) {+ const config = args[0] as { text?: string; values?: unknown[] } | string;+ if (typeof config === "object" && config.text?.includes("AS last_activity, s.first_user_preview")) {+ captured.push({ text: config.text, values: config.values ?? [] });+ }+ return Reflect.apply(execute, this, args);+ });+ const before = { lastActivity: Date.now() + 1e9, id: "~" };+ await s.scopeSessionSummaries(scope, false, { limit: 50, before, category: "conversation" });+ await s.scopeSessionSummaries(scope, true, { limit: 50, before, category: "conversation" });+ t.mock.restoreAll();+ assert.equal(captured.length, 2);+ const raw = await new pg.Pool({ connectionString: URL }).connect();+ try {+ await raw.query("BEGIN");+ await raw.query("SET LOCAL enable_seqscan = off");+ await raw.query("SET LOCAL enable_bitmapscan = off");+ await raw.query("SET LOCAL enable_sort = off");+ for (const [query, index] of captured.map(+ (c, i) => [c, i ? "sessions_by_activity" : "sessions_by_scope_activity"] as const,+ )) {+ const explained = await raw.query(`EXPLAIN (FORMAT JSON) ${query.text}`, query.values);+ const plan = JSON.stringify(explained.rows[0]["QUERY PLAN"]);+ assert.doesNotMatch(plan, /SubPlan|session_entries/, "no correlated subquery over session_entries");+ assert.doesNotMatch(plan, /"Node Type":"Sort"/, "the index supplies the order; nothing is sorted");+ assert.match(plan, new RegExp(`"Index Name":"${index}"`), `${index} serves the listing`);+ }+ await raw.query("ROLLBACK");+ } finally {+ raw.release();+ }});test("pg sessions table indexes scoped activity pages", { skip }, async () => {@@ -1323,7 +1505,11 @@ test("pg sessions table indexes scoped activity pages", { skip }, async () => {indexes.get("sessions_by_scope_activity") ?? "",/scope_id, COALESCE\(last_activity, created_at\) DESC, id DESC/,);- assert.equal(indexes.has("sessions_by_activity"), false, "the org-wide activity index is dropped");+ assert.match(+ indexes.get("sessions_by_activity") ?? "",+ /COALESCE\(last_activity, created_at\) DESC, id DESC/,+ "the org-wide listing walks its own activity index",+ );const relopts = await raw.query("SELECT reloptions FROM pg_class WHERE relname = 'sessions'");assert.ok(((relopts.rows[0]?.reloptions ?? []) as string[]).includes("fillfactor=70"),@@ -1342,7 +1528,8 @@ test("pg append debounces last_activity by 60s but keeps counters exact", { skipconst session = await s.getOrCreateByThread("debounceA", "channel", scope);const { lease } = await s.acquireLease(session.id);assert.ok(lease);- const activityOf = async () => (await s.scopeSessionSummaries(scope, false)).find((r) => r.id === session.id)!;+ const activityOf = async () =>+ (await s.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === session.id)!;nowRef.v = t0 + 30_000;await s.append(lease, { type: "user", payload: { text: "within the window" }, scopeLabel: scope });@@ -1405,7 +1592,7 @@ test("pg store startup never rewrites or waits on session rows", { skip }, async);await boots[0]!.releaseLease(lease!);assert.deepEqual(await rowVersions(), before, "startup writes no session rows");- const row = (await boots[1]!.scopeSessionSummaries(scope, false)).find((r) => r.id === writing)!;+ const row = (await boots[1]!.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === writing)!;assert.equal(row.messages, 2, "normal appends keep exact counters");assert.equal(row.turns, 1);} finally {@@ -1576,7 +1763,7 @@ test(});await s.releaseLease(lease!);- const row = (await s.scopeSessionSummaries(scope, false)).find((r) => r.id === a.id)!;+ const row = (await s.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;assert.equal(row.turns, 1, "summary turns excludes overheard");assert.equal(row.messages, 4, "messages still counts every durable entry");assert.equal(row.firstMessage, "the real question", "overheard is not the first message");@@ -1629,7 +1816,7 @@ test("pg null-byte payloads: stripped on write, tolerated on read (no jsonb castconst { lease: lease2 } = await s.acquireLease(a.id);await s.append(lease2!, { type: "user", payload: { text: "z" }, scopeLabel: scope });await s.releaseLease(lease2!);- const row = (await s.scopeSessionSummaries(scope, false)).find((r) => r.id === a.id)!;+ const row = (await s.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;assert.equal(row.turns, 3, "append recounts NULL turns; the poisoned row is counted, not crashed");assert.equal(row.messages, 3, "messages self-heal from the entry seq");});@@ -1655,7 +1842,7 @@ test([a.id, b.id],]);const s2 = createPostgresSessionStore(URL!);- const rows = await s2.scopeSessionSummaries(scope, false);+ const rows = await s2.scopeSessionSummaries(scope, false, { limit: 500 });const ra = rows.find((r) => r.id === a.id)!;assert.equal(ra.messages, 0, "startup no longer backfills: an idle legacy row reads as zero until it is written");assert.equal(ra.lastActivity, ra.createdAt, "…and sorts by created_at");@@ -1672,7 +1859,7 @@ test(});await s2.releaseLease(legacyLease!);const entries = await s2.getEntries(a.id);- const legacy = (await s2.scopeSessionSummaries(scope, false)).find((r) => r.id === a.id)!;+ const legacy = (await s2.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;assert.equal(legacy.messages, 4, "append counts every entry of a legacy row");assert.equal(legacy.turns, 1, "append recount excludes overheard user rows");assert.equal(legacy.lastActivity, entries[entries.length - 1]!.createdAt, "last-activity = newest entry");@@ -1684,7 +1871,7 @@ test(const { lease: lease2 } = await s2.acquireLease(a.id);await s2.append(lease2!, { type: "user", payload: { text: "back on new code" }, scopeLabel: scope });await s2.releaseLease(lease2!);- const healed = (await s2.scopeSessionSummaries(scope, false)).find((r) => r.id === a.id)!;+ const healed = (await s2.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;assert.equal(healed.messages, 6, "messages self-heal from the entry seq");assert.equal(healed.turns, 3, "seq/messages mismatch triggers a full turns recount");@@ -1701,12 +1888,12 @@ test();await raw.query("UPDATE sessions SET messages = 0, turns = 0 WHERE id = $1", [b.id]);const s3 = createPostgresSessionStore(URL!);- const stale = (await s3.scopeSessionSummaries(scope, false)).find((r) => r.id === b.id)!;+ const stale = (await s3.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === b.id)!;assert.equal(stale.messages, 0, "a fresh store leaves stale non-NULL counters alone");const { lease: leaseB } = await s3.acquireLease(b.id);await s3.append(leaseB!, { type: "assistant", payload: { text: "next" }, scopeLabel: scope });await s3.releaseLease(leaseB!);- const rb2 = (await s3.scopeSessionSummaries(scope, false)).find((r) => r.id === b.id)!;+ const rb2 = (await s3.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === b.id)!;assert.equal(rb2.messages, 3, "the next append repairs stale non-NULL counters");assert.equal(rb2.turns, 1, "…and restores turns the old writer never counted");@@ -1715,9 +1902,9 @@ test(const { lease: lease3 } = await s3.acquireLease(a.id);await s3.append(lease3!, { type: "assistant", payload: { text: "late clock" }, scopeLabel: scope });await s3.releaseLease(lease3!);- const ra2 = (await s3.scopeSessionSummaries(scope, false)).find((r) => r.id === a.id)!;+ const ra2 = (await s3.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;assert.equal(ra2.lastActivity, future, "an older-clocked append never moves last_activity backwards");- const ra3 = (await createPostgresSessionStore(URL!).scopeSessionSummaries(scope, false)).find(+ const ra3 = (await createPostgresSessionStore(URL!).scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id,)!;assert.equal(ra3.lastActivity, future, "a fresh store never moves last_activity either");diff --git a/test/session-store.test.ts b/test/session-store.test.tsindex e552a72f0..b9ff2558f 100644--- a/test/session-store.test.ts+++ b/test/session-store.test.ts@@ -220,7 +220,7 @@ for (const [name, make] of backends) {const scope = scopeId("personal", "U1");await store.getOrCreateByThread("dm:D1", "dm", scope);await store.getOrCreateByThread("agent:main:cron:c1", "dm", scope);- const rows = await store.scopeSessionSummaries(scope, false);+ const rows = await store.scopeSessionSummaries(scope, false, { limit: 500 });const byThreadOrigin = new Map(rows.map((r) => [r.id, r.origin]));const conv = await store.getByThread("dm:D1");const cron = await store.getByThread("agent:main:cron:c1");@@ -278,7 +278,7 @@ for (const [name, make] of backends) {const stats = await store.scopeSessionStats(scope, false);assert.equal(stats.turns, 1, "only the real user turn counts; the two overheard rows do not");- const rows = await store.scopeSessionSummaries(scope, false);+ const rows = await store.scopeSessionSummaries(scope, false, { limit: 500 });const row = rows.find((r) => r.id === s.id)!;assert.equal(row.turns, 1, "summary turns excludes overheard");assert.equal(row.firstMessage, "the real question", "the overheard row is not the first message");@@ -315,13 +315,20 @@ for (const [name, make] of backends) {await store.append(lb!, { type: "user", payload: { text: "b" }, scopeLabel: scope });await store.releaseLease(lb!);- const page1 = await store.scopeSessionSummaries(scope, false, { limit: 2, offset: 0 });+ const page1 = await store.scopeSessionSummaries(scope, false, { limit: 2 });assert.equal(page1.length, 2);assert.equal(page1[0]!.id, b.id, "most recent activity first");assert.equal(page1[1]!.id, a.id);- const page2 = await store.scopeSessionSummaries(scope, false, { limit: 2, offset: 2 });+ const page2 = await store.scopeSessionSummaries(scope, false, {+ limit: 2,+ before: { lastActivity: page1[1]!.lastActivity, id: page1[1]!.id },+ });assert.equal(page2.length, 1, "third session on the next page");- assert.equal((await store.scopeSessionSummaries(scope, false)).length, 3, "unpaginated still returns all");+ assert.equal(+ (await store.scopeSessionSummaries(scope, false, { limit: 500 })).length,+ 3,+ "a wide page returns all",+ );});test(`${name}: scopeSessionSummaries filters category before paginating`, async () => {@@ -333,7 +340,6 @@ for (const [name, make] of backends) {const conversations = await store.scopeSessionSummaries(scope, false, {limit: 10,- offset: 0,category: "conversation",});assert.deepEqual(@@ -341,7 +347,7 @@ for (const [name, make] of backends) {[convo.id],);- const background = await store.scopeSessionSummaries(scope, false, { limit: 1, offset: 0, category: "background" });+ const background = await store.scopeSessionSummaries(scope, false, { limit: 1, category: "background" });assert.equal(background.length, 1);assert.notEqual(background[0]!.id, convo.id);assert.equal(background[0]!.origin === "cron" || background[0]!.origin === "webhook", true);@@ -355,7 +361,6 @@ for (const [name, make] of backends) {const cronRows = await store.scopeSessionSummaries(scope, false, {limit: 1,- offset: 0,category: "background",origin: "cron",});@@ -366,7 +371,6 @@ for (const [name, make] of backends) {const otherRows = await store.scopeSessionSummaries(scope, false, {limit: 1,- offset: 0,category: "background",origin: "other_background",});@@ -423,13 +427,17 @@ for (const [name, make] of backends) {const cronStats = await store.scopeSessionStats(scope, false, "background", "cron", "c1");assert.equal(cronStats.total, 3, "stats cronId filter counts one cron's fires");- const fires = await store.scopeSessionSummaries(scope, false, { limit: 10, offset: 0, cronId: "c1" });+ const fires = await store.scopeSessionSummaries(scope, false, { limit: 10, cronId: "c1" });assert.deepEqual(fires.map((r) => r.id).sort(),[c1a.id, c1b.id, fresh.id].sort(),"cronId page returns only that cron's fires",);- const page = await store.scopeSessionSummaries(scope, false, { limit: 1, offset: 1, cronId: "c1" });+ const page = await store.scopeSessionSummaries(scope, false, {+ limit: 1,+ cronId: "c1",+ before: { lastActivity: fires[0]!.lastActivity, id: fires[0]!.id },+ });assert.equal(page.length, 1, "cronId filter applies before pagination");assert.deepEqual(await store.scopeCronGroups(scopeId("channel", "other"), false), [], "scope filter applies");});@@ -490,7 +498,7 @@ for (const [name, make] of backends) {]);assert.deepEqual(rollups,- rollupsFromSummaries(await store.scopeSessionSummaries(a, true)),+ rollupsFromSummaries(await store.scopeSessionSummaries(a, true, { limit: 500 })),"the aggregate matches a row-by-row pass over the summaries",);assert.deepEqual(@@ -980,7 +988,7 @@ for (const [name, make] of backends) {);});- test(`${name}: keyset cursor pages match offset pages and skip nothing`, async () => {+ test(`${name}: keyset cursor pages stitch into the full listing and skip nothing`, async () => {let clock = Date.now() + 1_000_000;const store = createMemorySessionStore({ now: () => ++clock });const scope = scopeId("personal", "U1");@@ -990,18 +998,16 @@ for (const [name, make] of backends) {await store.append(lease!, { type: "user", payload: { text: `m${i}` }, scopeLabel: scope });await store.releaseLease(lease!);}- const all = await store.scopeSessionSummaries(scope, false, { limit: 10, offset: 0 });- const page1 = await store.scopeSessionSummaries(scope, false, { limit: 2, offset: 0 });+ const all = await store.scopeSessionSummaries(scope, false, { limit: 10 });+ const page1 = await store.scopeSessionSummaries(scope, false, { limit: 2 });const cursor1 = page1[page1.length - 1]!;const page2 = await store.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: cursor1.lastActivity, id: cursor1.id },});const cursor2 = page2[page2.length - 1]!;const page3 = await store.scopeSessionSummaries(scope, false, {limit: 2,- offset: 0,before: { lastActivity: cursor2.lastActivity, id: cursor2.id },});assert.deepEqual(