Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Admin Sessions tab: one newest-first list, no live totals (#2487)

Regan Bellfe11f3ed9a4e
diff --git a/knip.json b/knip.json
index 22ac41bb0..3564f74bc 100644
--- a/knip.json
+++ b/knip.json
@@ -13,7 +13,6 @@
"src/sandbox/local-sandbox.ts",
"scripts/aws-deploy-smoke.ts",
"scripts/aws-litestream-smoke.ts",
- "scripts/backfill-delivery-source-cron-id.ts",
"scripts/e2b-live-smoke.mjs",
"scripts/modal-live-smoke.mjs",
"scripts/dev/cli.ts",
diff --git a/plugins/admin/README.md b/plugins/admin/README.md
index 6b57ee7e8..953d63b6e 100644
--- a/plugins/admin/README.md
+++ b/plugins/admin/README.md
@@ -24,8 +24,8 @@ Invitation email uses Resend when configured. Teammate invitations contain a sin
The existing external-user API still requires an expiry and rejects org members.
Teammate invitations and revocations are restricted to the Admin surface.
(`org_admin` is the only supported role for now; `team_admin` was removed — team-scoped admin
-observability is future work. See `src/admin/admin-service.ts`.) History (conversation listing with a
-by-type usage rollup, drilling into transcripts with per-turn model-context breakdowns), Files
+observability is future work. See `src/admin/admin-service.ts`.) History (newest sessions, org-wide or narrowed
+to one scope, by origin, drilling into transcripts with per-turn model-context breakdowns), Files
(workspace contents), and Live (ongoing/recent runs) are **top-down content** views: an
org-scope query spans the whole org; a narrower scope is limited to that scope. Every
action is authorized in the core and audited.
diff --git a/plugins/admin/public/index.html b/plugins/admin/public/index.html
index d248d755f..46d820b82 100644
--- a/plugins/admin/public/index.html
+++ b/plugins/admin/public/index.html
@@ -1222,16 +1222,6 @@
.history-empty-scope:hover {
color: var(--text);
}
- details.history-quiet {
- margin: -4px 0 14px;
- color: var(--muted);
- font-size: 12px;
- }
- details.history-quiet summary {
- cursor: pointer;
- color: var(--muted);
- font-weight: 500;
- }
.segmented {
display: flex;
align-items: center;
@@ -1249,22 +1239,6 @@
background: color-mix(in srgb, var(--text) 4%, transparent);
font-weight: 500;
}
- .dense-preview .delivery-chip {
- margin: 0 8px 0 0;
- }
- .delivery-chip {
- display: inline-flex;
- align-items: center;
- flex: none;
- margin-left: 8px;
- font-size: 11px;
- font-weight: 500;
- white-space: nowrap;
- color: color-mix(in srgb, var(--ok) 75%, var(--muted));
- }
- .dense-row.history-silent {
- opacity: 0.45;
- }
.list-root {
display: grid;
gap: 0;
@@ -7065,12 +7039,6 @@
}
return d;
}
- function sessionCountsLabel(sessions, background) {
- const parts = [];
- if (sessions) parts.push(plural(sessions, "conversation"));
- if (background) parts.push(Number(background).toLocaleString() + " background");
- return parts.join(" · ") || "no conversations";
- }
function deploymentHref(dep) {
return dep.publicUrl || "";
}
@@ -7424,20 +7392,13 @@
if (view === "ackemoji") return void renderAckEmojiPicks(st);
if (view === "history" && st.session && st.turn != null) return void showContextPage(st.session, st.turn);
if (view === "history" && st.session) return void showTranscript(st.session);
- renderCurrentData();
+ renderData();
}
const GOV_LIKE = new Set(["governance", "models", "credentials", "customize", "slack-settings"]);
function isGovLike(v) {
return GOV_LIKE.has(v);
}
- function historyIsIndex() {
- return view === "history" && orgWideView() && !urlToState().session;
- }
- function renderCurrentData() {
- if (historyIsIndex()) return void renderScopeIndex();
- renderData();
- }
function hasGovernanceDraft() {
return isGovLike(view) && !!$("view-governance").querySelector(".card.dirty");
}
@@ -7573,7 +7534,6 @@
if (!r.ok) {
scopeDirNote =
r.status === 403 ? "Only an org admin can list scopes." : r.data?.message || "Failed to load scopes.";
- if (historyIsIndex()) renderScopeIndex();
return;
}
viewLoadedAt.history = Date.now();
@@ -7584,7 +7544,7 @@
const memoryEditor = view === "memory" && !(orgWideView() && urlToState().mem !== "edit");
if (!(memoryEditor && orgWideView())) defaultShell();
- if (view !== "governance" && !memoryEditor) renderCurrentData();
+ if (view !== "governance" && !memoryEditor) renderData();
}
}
function dirLabel(id) {
@@ -7829,8 +7789,7 @@
return;
}
if (view === "crons" && cronDestinationEditId) return;
- if (historyIsIndex()) loadScopeDirectory();
- else renderData();
+ renderData();
});
let loadedGovernanceScope = null;
@@ -9161,7 +9120,6 @@
let dataReq = 0;
let historyKind = "conversation";
- let scopeSort = "human";
const dataCache = new Map();
function paintData(root, data) {
@@ -9433,7 +9391,7 @@
return kind === "cron" || kind === "background" ? "background" : kind;
}
- const KIND_LABEL = { conversation: "Conversations", cron: "Crons", background: "Background" };
+ const KIND_LABEL = { conversation: "Conversations", cron: "Cron runs", background: "Background" };
function historyModeLabel(kind) {
return KIND_LABEL[kind] || KIND_LABEL.conversation;
}
@@ -10279,42 +10237,6 @@
return { block, sync: () => {} };
}
- function renderScopeIndex() {
- ++dataReq;
- const root = $("view-data");
- root.textContent = "";
- governanceUI.history.scopeIndex(root, {
- ready: !!scopeDir,
- note: scopeDirNote,
- title: VIEW_TITLE.history,
- rows: scopeDir ? scopeRows() : [],
- sort: scopeSort,
- pageShell,
- plural,
- environments: environmentDir,
- historyKind,
- shortName,
- selectScope,
- stateToUrl,
- relTime,
- sessionCountsLabel,
- scopeTitle,
- sortControl: () =>
- twoWayToggle({
- label: "Sort sessions",
- options: [
- { value: "human", label: "Human activity" },
- { value: "all", label: "All activity" },
- ],
- value: scopeSort,
- onChange: (value) => {
- scopeSort = value;
- renderScopeIndex();
- $("shellbar").querySelector(".two-way-toggle input:checked")?.focus({ preventScroll: true });
- },
- }),
- });
- }
function renderHistory(root, d) {
governanceUI.history.history(root, d, {
historyKind,
@@ -10341,6 +10263,7 @@
errorStripBox,
adminErrorCount,
adminErrors,
+ scopeRows,
});
}
diff --git a/plugins/admin/test/history-urls.test.ts b/plugins/admin/test/history-urls.test.ts
index ad88d8514..9aef3c7ab 100644
--- a/plugins/admin/test/history-urls.test.ts
+++ b/plugins/admin/test/history-urls.test.ts
@@ -205,15 +205,12 @@ test("an undecodable scope segment falls back instead of throwing", () => {
assert.equal(st.scope, "org:acme");
});
-test("cron fire rows surface the result digest and retain silent styling", () => {
+test("cron fire rows surface the result digest", () => {
const f = litFixture();
f.ui.history.history(
f.root,
{
- sessions: [
- { id: "fire1", category: "background", result: "Digest result", lastMessage: "Tool chatter", delivered: 0 },
- ],
- total: 1,
+ sessions: [{ id: "fire1", category: "background", result: "Digest result", lastMessage: "Tool chatter" }],
},
{
historyKind: "cron",
@@ -230,11 +227,74 @@ test("cron fire rows surface the result digest and retain silent styling", () =>
plural: String,
stateToUrl: () => "/session/fire1",
go() {},
+ scopeRows: () => [],
+ shortName: String,
},
);
assert.equal(f.root.querySelector(".dense-name")!.textContent, "Digest result");
assert.equal(f.root.querySelector(".dense-preview")!.textContent, "Tool chatter");
- assert.ok(f.root.querySelector(".history-silent"));
+ f.dom.window.close();
+});
+
+test("the org-wide landing lists sessions with a scope tag, a scope filter, and only precomputed tab counts", () => {
+ const f = litFixture();
+ const went: Record<string, unknown>[] = [];
+ let shell: any = null;
+ const render = (d: Record<string, unknown>) => {
+ f.root.textContent = "";
+ f.ui.history.history(
+ f.root,
+ { sessions: [{ id: "s1", category: "conversation", scopeId: "channel:C1", firstMessage: "hello" }], ...d },
+ {
+ historyKind: "conversation",
+ cron: null,
+ cursor: null,
+ scope: "org:acme",
+ orgScope: "org:acme",
+ title: "Sessions",
+ environments: [],
+ historyKindMatches: () => true,
+ historyModeLabel: () => "Conversations",
+ kindLabels: { conversation: "Conversations", cron: "Cron runs", background: "Background" },
+ pageShell: (opts: any) => (shell = opts),
+ cronName: () => "Job",
+ scopeKind: (id: string) => id.split(":")[0],
+ shortName: (id: string) => (id === "channel:C1" ? "#eng" : id),
+ plural: (n: number, w: string) => `${n} ${w}s`,
+ stateToUrl: () => "/x",
+ relTime: () => "now",
+ go: (st: Record<string, unknown>) => went.push(st),
+ scopeRows: () => [{ scopeId: "channel:C1" }, { scopeId: "personal:U1" }],
+ },
+ );
+ };
+ render({});
+ assert.equal(shell.title, "Sessions");
+ assert.deepEqual(
+ JSON.parse(JSON.stringify(shell.tabs.map((t: any) => [t.label, t.count ?? null]))),
+ [
+ ["Conversations", null],
+ ["Cron runs", null],
+ ["Background", null],
+ ],
+ "no counts until the background snapshot exists",
+ );
+ assert.equal(f.root.querySelector(".dense-tag")!.textContent, "#eng", "org-wide rows name their scope");
+ assert.equal(f.root.querySelector(".pager"), null, "a single page shows no pager and no total");
+ const select = shell.actions[0].querySelector("select") as HTMLSelectElement;
+ assert.deepEqual(
+ [...select.options].map((o) => [o.value, o.textContent!.trim()]),
+ [
+ ["org:acme", "All scopes"],
+ ["channel:C1", "#eng"],
+ ["personal:U1", "personal:U1"],
+ ],
+ );
+ select.value = "channel:C1";
+ select.dispatchEvent(new f.dom.window.Event("change"));
+ assert.equal(went.at(-1)!.scope, "channel:C1", "choosing a scope narrows the same list");
+ render({ counts: { conversation: 12, cron: 3400, background: 7 } });
+ assert.deepEqual(JSON.parse(JSON.stringify(shell.tabs.map((t: any) => t.count))), [12, 3400, 7]);
f.dom.window.close();
});
@@ -247,7 +307,6 @@ test("history pages older rows by keyset cursor, never by offset", () => {
f.root,
{
sessions: [{ id: "s1", category: "conversation", firstMessage: "hello" }],
- total: 120,
...(nextCursor ? { nextCursor } : {}),
},
{
@@ -266,6 +325,8 @@ test("history pages older rows by keyset cursor, never by offset", () => {
plural: (n: number, w: string) => `${n} ${w}s`,
stateToUrl: () => "/x",
go: (st: Record<string, unknown>) => went.push(st),
+ scopeRows: () => [],
+ shortName: String,
},
);
return [...f.root.querySelectorAll<HTMLButtonElement>(".pager .page")];
diff --git a/plugins/admin/test/settings-load.test.ts b/plugins/admin/test/settings-load.test.ts
index 72affc237..de383a568 100644
--- a/plugins/admin/test/settings-load.test.ts
+++ b/plugins/admin/test/settings-load.test.ts
@@ -44,7 +44,7 @@ test("settings navigation never starts the all-scopes history scan", () => {
},
renderTabs() {},
defaultShell() {},
- renderCurrentData() {},
+ renderData() {},
});
vm.runInContext(source, context);
vm.runInContext(`render({view:${JSON.stringify(view)}, scope:"org:example"})`, context);
diff --git a/plugins/admin/ui/history.ts b/plugins/admin/ui/history.ts
index e3c94aac6..d16b4f340 100644
--- a/plugins/admin/ui/history.ts
+++ b/plugins/admin/ui/history.ts
@@ -4,17 +4,8 @@ import { bareCard, card, denseList, renderer, node } from "./shared.ts";
export function history(root: HTMLElement, d: any, s: Record<string, any>) {
const paint = renderer(root);
const sessions = (d.sessions || []).filter((row: any) => s.historyKindMatches(row, s.historyKind));
- const counts = d.totalByCategory || {};
- const types = d.totalByType || d.byType || {};
- const cronCount = types.cron ?? types.cron_wake ?? types.cron_monologue ?? 0;
- const count = (kind: string) => {
- if (kind === "conversation") return counts.conversation ?? 0;
- if (kind === "cron") return d.distinctCrons ?? cronCount;
- if (kind === "background") return Math.max(0, (counts.background ?? 0) - cronCount);
- return d.total ?? sessions.length;
- };
+ const orgWide = s.scopeKind(s.scope) === "org";
const cron = s.historyKind === "cron" ? s.cron : null;
- const total = (cron ? d.total : count(s.historyKind)) || sessions.length;
const route = (extra: any) => ({
view: "history",
scope: s.scope,
@@ -28,7 +19,6 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {
? 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"
@@ -40,32 +30,38 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {
</div>`
: nothing;
const mode = s.historyModeLabel(s.historyKind);
- const tabs = [
- "conversation",
- "cron",
- ...(count("background") > 0 || s.historyKind === "background" ? ["background"] : []),
- ].map((kind) => ({
+ const tabs = ["conversation", "cron", "background"].map((kind) => ({
label: s.kindLabels[kind],
- count: count(kind) || 0,
+ count: d.counts?.[kind],
active: s.historyKind === kind,
onClick: () => s.go(route({ historyKind: kind })),
}));
+ const scopes = s.scopeRows();
+ const scopeFilter = node(
+ html`<div class="shell-sort">
+ <span class="shell-sort-label">Scope:</span
+ ><select
+ aria-label="Filter by scope"
+ @change=${(e: Event) => s.go(route({ scope: (e.target as HTMLSelectElement).value }))}
+ >
+ <option value=${s.orgScope} ?selected=${orgWide}>All scopes</option>
+ ${scopes.map(
+ (row: any) =>
+ html`<option value=${row.scopeId} ?selected=${row.scopeId === s.scope}>
+ ${s.shortName(row.scopeId)}
+ </option>`,
+ )}
+ </select>
+ </div>`,
+ );
s.pageShell(
cron
? {
- back: { label: "← Crons", onClick: () => s.go(route({})) },
+ back: { label: "← Cron runs", onClick: () => s.go(route({})) },
title: s.cronName(d.cron?.cron || sessions[0]?.origin?.cron, true),
- context: s.scopeKind(s.scope) !== "org" ? s.shortName(s.scope) : "",
- count: s.plural(total, "fire"),
+ context: orgWide ? "" : s.shortName(s.scope),
}
- : {
- back:
- s.scopeKind(s.scope) !== "org"
- ? { label: "← Sessions", onClick: () => s.go(route({ scope: s.orgScope })) }
- : undefined,
- title: s.scopeKind(s.scope) !== "org" ? s.shortName(s.scope) : s.title,
- tabs,
- },
+ : { title: orgWide ? s.title : s.shortName(s.scope), tabs, actions: [scopeFilter] },
);
const environment = (s.environments || []).find((e: any) => e.id === s.scope);
const attached = environment?.attachedScopes || [];
@@ -74,82 +70,84 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {
let errorsOpen = false;
const draw = () =>
paint(
- html`${environment && !cron ? card("Attached scopes", s.plural(attached.length, "scope") + " share this environment's computer and working memory.", html`<div class="history-empty-scopes">${attached.length ? attached.map((id: string) => html`<button type="button" class="history-empty-scope" title=${id} @click=${() => s.selectScope(id)}>${s.shortName(id)}</button>`) : html`<p class="empty">No scopes attached.</p>`}</div>`) : nothing}${
- s.historyKind === "cron" && !cron
- ? bareCard(
- denseList(
- d.crons || [],
- (c: any) => ({
- name: s.cronName(c.origin?.cron, true),
- preview: html`<span
- >${s.plural(c.sessions || 0, "fire") + (typeof c.deliveredRuns === "number" ? " · " + Number(c.deliveredRuns).toLocaleString() + " delivered" : "") + " · first fired " + s.fmtHistoryCreated(c.createdAt, c.lastActivity)}</span
- >`,
- time: c.lastActivity ? s.relTime(c.lastActivity) : "—",
- cls: typeof c.deliveredRuns === "number" && !c.deliveredRuns ? "history-silent" : "",
- href: s.stateToUrl(route({ cron: c.cronId })),
- }),
- (c) => s.go(route({ cron: c.cronId })),
- "No crons in this scope.",
- ),
- )
- : html`${bareCard(
+ html`${
+ orgWide && !cron && !s.cursor && s.environments?.length
+ ? card(
+ "Named environments",
+ "Named computers and working memory that scopes can share.",
denseList(
- sessions,
- (row: any) => {
- const origin = row.origin?.kind === "cron" ? row.origin.cron : null;
- const background = row.category === "background";
- let name = row.firstMessage || row.lastMessage || "(no messages yet)";
- let preview = "";
- if (background && !cron) {
- const kind = row.kind || row.type;
- let typeLabel = s.titleCase(String(kind).replaceAll("_", " "));
- if (kind === "dm") typeLabel = "DM";
- if (["cron_monologue", "cron_wake", "cron"].includes(kind)) typeLabel = "Cron";
- name = origin ? s.cronName(origin, true) : row.origin?.label || typeLabel;
- preview = row.result || row.lastMessage || row.firstMessage || "";
- } else if (background && row.result) {
- name = row.result;
- if (row.lastMessage && row.lastMessage !== row.result) preview = row.lastMessage;
- } else if (row.lastMessage && row.lastMessage !== name) preview = row.lastMessage;
+ s.environments,
+ (e: any) => {
+ const names = (e.attachedScopes || []).map((id: string) => s.shortName(id));
return {
- name,
- preview: html`<span
- >${background && typeof row.delivered === "number" && row.delivered > 0 ? html`<span class="delivery-chip">${row.delivered > 1 ? "✓ delivered ×" + row.delivered : "✓ delivered"}</span>` : nothing}${preview}</span
+ name: e.name || s.shortName(e.id),
+ preview: html`<span title=${names.join(", ")}
+ >${names.length ? names.slice(0, 4).join(", ") + (names.length > 4 ? " +" + s.plural(names.length - 4, "more scope") : "") : "no scopes attached"}</span
>`,
- time: row.lastActivity ? s.relTime(row.lastActivity) : "—",
- cls: background && typeof row.delivered === "number" && !row.delivered ? "history-silent" : "",
- href: s.stateToUrl(route({ session: row.id })),
+ href: s.stateToUrl(route({ scope: e.id })),
};
},
- (row) => s.go(route({ session: row.id })),
- "No " + mode.toLowerCase() + " in this scope.",
+ (e) => s.selectScope(e.id),
+ "No named environments.",
),
- )}${pages}${
- s.scopeKind(s.scope) !== "org" && !cron
- ? html`<div>
- ${
- errorCount && errors.length
- ? html`<div class="statline">
- <a
- href="#"
- @click=${(e: Event) => {
- e.preventDefault();
- errorsOpen = !errorsOpen;
- draw();
- }}
- >${s.plural(errorCount, "error")} logged in this scope</a
- >
- </div>
- ${errorsOpen ? s.errorStripBox(errors, "Most recent errors", 20, (e: any) => (e.sessionId ? { href: s.stateToUrl(route({ session: e.sessionId })), go: () => s.go(route({ session: e.sessionId })) } : null)) : nothing}`
- : nothing
- }
- </div>`
- : nothing
- }`
+ )
+ : nothing
+ }${environment && !cron ? card("Attached scopes", s.plural(attached.length, "scope") + " share this environment's computer and working memory.", html`<div class="history-empty-scopes">${attached.length ? attached.map((id: string) => html`<button type="button" class="history-empty-scope" title=${id} @click=${() => s.selectScope(id)}>${s.shortName(id)}</button>`) : html`<p class="empty">No scopes attached.</p>`}</div>`) : nothing}${bareCard(
+ denseList(
+ sessions,
+ (row: any) => {
+ const origin = row.origin?.kind === "cron" ? row.origin.cron : null;
+ const background = row.category === "background";
+ let name = row.firstMessage || row.lastMessage || "(no messages yet)";
+ let preview = "";
+ if (background && !cron) {
+ const kind = row.kind || row.type;
+ let typeLabel = s.titleCase(String(kind).replaceAll("_", " "));
+ if (kind === "dm") typeLabel = "DM";
+ if (["cron_monologue", "cron_wake", "cron"].includes(kind)) typeLabel = "Cron";
+ name = origin ? s.cronName(origin, true) : row.origin?.label || typeLabel;
+ preview = row.result || row.lastMessage || row.firstMessage || "";
+ } else if (background && row.result) {
+ name = row.result;
+ if (row.lastMessage && row.lastMessage !== row.result) preview = row.lastMessage;
+ } else if (row.lastMessage && row.lastMessage !== name) preview = row.lastMessage;
+ return {
+ name,
+ preview: html`<span
+ >${orgWide && row.scopeId ? html`<span class="dense-tag">${s.shortName(row.scopeId)}</span>` : nothing}${preview}</span
+ >`,
+ time: row.lastActivity ? s.relTime(row.lastActivity) : "—",
+ href: s.stateToUrl(route({ session: row.id })),
+ };
+ },
+ (row) => s.go(route({ session: row.id })),
+ "No " + mode.toLowerCase() + (orgWide ? " yet." : " in this scope."),
+ ),
+ )}${pages}${
+ !orgWide && !cron
+ ? html`<div>
+ ${
+ errorCount && errors.length
+ ? html`<div class="statline">
+ <a
+ href="#"
+ @click=${(e: Event) => {
+ e.preventDefault();
+ errorsOpen = !errorsOpen;
+ draw();
+ }}
+ >${s.plural(errorCount, "error")} logged in this scope</a
+ >
+ </div>
+ ${errorsOpen ? s.errorStripBox(errors, "Most recent errors", 20, (e: any) => (e.sessionId ? { href: s.stateToUrl(route({ session: e.sessionId })), go: () => s.go(route({ session: e.sessionId })) } : null)) : nothing}`
+ : nothing
+ }
+ </div>`
+ : nothing
}`,
);
draw();
- if (s.scopeKind(s.scope) !== "org" && !cron) {
+ if (!orgWide && !cron) {
const query = "scope=" + encodeURIComponent(s.scope);
void Promise.all([s.adminErrorCount(query), s.adminErrors(query)])
.then(([count, rows]) => {
@@ -160,85 +158,3 @@ export function history(root: HTMLElement, d: any, s: Record<string, any>) {
.catch(() => {});
}
}
-
-export function scopeIndex(root: HTMLElement, s: Record<string, any>) {
- const paint = renderer(root);
- if (!s.ready) {
- s.pageShell({ title: s.title });
- paint(html`<div class="loadingline">${s.note}</div>`);
- return;
- }
- const active = s.rows.filter((r: any) => (r.sessions || 0) > 0 || (r.backgroundSessions || 0) > 0);
- const quiet = s.rows.filter((r: any) => !(r.sessions || 0) && !(r.backgroundSessions || 0));
- if (s.sort === "human")
- active.sort(
- (a: any, b: any) =>
- (b.lastConversationActivity || 0) - (a.lastConversationActivity || 0) ||
- (b.sessions || 0) - (a.sessions || 0) ||
- (b.lastActivity || 0) - (a.lastActivity || 0) ||
- a.scopeId.localeCompare(b.scopeId),
- );
- s.pageShell({
- title: s.title,
- count: s.plural(active.length, "active scope"),
- actions: [node(html`<div class="shell-sort"><span class="shell-sort-label">Sort:</span>${s.sortControl()}</div>`)],
- });
- const route = (scope: string) => ({ view: "history", scope, historyKind: s.historyKind });
- paint(
- html`${
- s.environments.length
- ? card(
- "Named environments",
- "Named computers and working memory that scopes can share.",
- denseList(
- s.environments,
- (e: any) => {
- const attached = e.attachedScopes || [];
- const names = attached.map((id: string) => s.shortName(id));
- const label = !names.length
- ? "no scopes attached"
- : names.slice(0, 4).join(", ") +
- (names.length > 4 ? " +" + s.plural(names.length - 4, "more scope") : "");
- return {
- name: e.name || s.shortName(e.id),
- preview: html`<span title=${attached.length ? names.join(", ") : ""}>${label}</span>`,
- href: s.stateToUrl(route(e.id)),
- };
- },
- (e) => s.selectScope(e.id),
- "No named environments.",
- ),
- )
- : nothing
- }${card(
- "Conversations by person or channel",
- "",
- denseList(
- active,
- (row: any) => {
- const time = s.sort === "human" ? row.lastConversationActivity || 0 : row.lastActivity || 0;
- return {
- name: s.shortName(row.scopeId),
- preview: html`<span
- ><span class="dense-tag">${s.sessionCountsLabel(row.sessions || 0, row.backgroundSessions || 0)}</span
- >${row.environmentAttachment ? html`<span class="dense-tag" title="This scope is attached to a named environment.">${"env: " + (row.environmentAttachment.environmentName || s.shortName(row.environmentAttachment.environmentId))}</span>` : nothing}${row.lastMessage || ""}</span
- >`,
- time: time ? s.relTime(time) : "Never",
- href: s.stateToUrl(route(row.scopeId)),
- };
- },
- (row) => s.selectScope(row.scopeId),
- s.rows.length ? "No conversations yet." : "No scopes yet. Nobody has talked to the agent.",
- ),
- )}${
- quiet.length
- ? html`<details class="history-quiet">
- <summary>${quiet.length} known scope${quiet.length === 1 ? "" : "s"} without conversations</summary>
- <div class="history-empty-scopes">
- ${quiet.map((row: any) => html`<button type="button" class="history-empty-scope" @click=${() => s.selectScope(row.scopeId)}>${s.scopeTitle(row.scopeId)}</button>`)}
- </div>
- </details>`
- : nothing
- }`,
- );
-}
diff --git a/scripts/backfill-delivery-source-cron-id.ts b/scripts/backfill-delivery-source-cron-id.ts
deleted file mode 100644
index 5e6905ae3..000000000
--- a/scripts/backfill-delivery-source-cron-id.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import { createPgPool } from "../src/persistence/pg-pool.ts";
-import { backfillDeliverySourceCronIdBatch } from "../src/delivery/postgres-delivery-store.ts";
-import { sleep } from "../src/util/async.ts";
-import { databaseUrl } from "./lib/backfill-runner.ts";
-
-const BATCH = 5000;
-const PAUSE_MS = 250;
-
-const db = createPgPool(databaseUrl());
-let total = 0;
-for (;;) {
- const updated = await backfillDeliverySourceCronIdBatch(db.q, BATCH);
- if (updated === 0) break;
- total += updated;
- console.log(`backfilled ${total} deliveries`);
- await sleep(PAUSE_MS);
-}
-console.log(`done: ${total} deliveries backfilled, none left to derive`);
-await db.close();
diff --git a/scripts/lib/backfill-runner.ts b/scripts/lib/backfill-runner.ts
deleted file mode 100644
index 080026ba3..000000000
--- a/scripts/lib/backfill-runner.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-export function databaseUrl(): string {
- const url = process.env.DATABASE_URL;
- if (!url) {
- console.error("DATABASE_URL is required");
- process.exit(1);
- }
- return url;
-}
diff --git a/src/api/routes/admin/origins.ts b/src/api/routes/admin/origins.ts
index e7d312d61..511dd0ec7 100644
--- a/src/api/routes/admin/origins.ts
+++ b/src/api/routes/admin/origins.ts
@@ -99,7 +99,7 @@ function wakeSessionLabel(trigger: string, monologue?: boolean): string {
}
}
-export async function cronWakeOrigin(
+async function cronWakeOrigin(
app: App,
parsed: AdminWakeRef,
requestedScope: string,
diff --git a/src/api/routes/admin/scope-activity.ts b/src/api/routes/admin/scope-activity.ts
new file mode 100644
index 000000000..c150a7b34
--- /dev/null
+++ b/src/api/routes/admin/scope-activity.ts
@@ -0,0 +1,45 @@
+import type { ScopeActivity, SessionStore } from "../../../sessions/session-store.ts";
+import { reportFailure } from "../../../util/errors.ts";
+
+const REFRESH_MS = 5 * 60_000;
+
+export interface ScopeActivitySnapshot {
+ asOf: number;
+ byScope: Map<string, ScopeActivity>;
+}
+
+const snapshots = new WeakMap<SessionStore, { snapshot?: ScopeActivitySnapshot; refreshing?: Promise<void> }>();
+
+export function scopeActivity(store: SessionStore | undefined): ScopeActivitySnapshot | null {
+ if (!store) return null;
+ let entry = snapshots.get(store);
+ if (!entry) snapshots.set(store, (entry = {}));
+ const state = entry;
+ if (!state.refreshing && (!state.snapshot || Date.now() - state.snapshot.asOf > REFRESH_MS)) {
+ state.refreshing = store
+ .scopeActivity()
+ .then((rows) => {
+ state.snapshot = { asOf: Date.now(), byScope: new Map(rows.map((r) => [r.scopeId, r])) };
+ })
+ .catch((error: unknown) => reportFailure("admin: scope activity refresh", error))
+ .finally(() => {
+ state.refreshing = undefined;
+ });
+ }
+ return state.snapshot ?? null;
+}
+
+export function activityCounts(
+ snapshot: ScopeActivitySnapshot,
+ scope: string,
+ orgWide: boolean,
+): { conversation: number; cron: number; background: number } {
+ const rows = orgWide ? [...snapshot.byScope.values()] : [snapshot.byScope.get(scope)].filter((r) => !!r);
+ const counts = { conversation: 0, cron: 0, background: 0 };
+ for (const r of rows) {
+ counts.conversation += r.conversations;
+ counts.cron += r.crons;
+ counts.background += r.background;
+ }
+ return counts;
+}
diff --git a/src/api/routes/admin/scope-config.ts b/src/api/routes/admin/scope-config.ts
index 45898526b..489aafd38 100644
--- a/src/api/routes/admin/scope-config.ts
+++ b/src/api/routes/admin/scope-config.ts
@@ -37,6 +37,7 @@ import {
} from "../../../policy/command-policy.ts";
import { isHostDenied } from "../../../resolution/egress-policy.ts";
import { discoverScopes } from "./common.ts";
+import { scopeActivity } from "./scope-activity.ts";
export async function putScopeConfig(ctx: ApiCtx): Promise<void> {
const { res, deps, params, body } = ctx;
@@ -151,12 +152,11 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {
const actor = await authorizeAdmin(ctx, scope);
if (!actor) return;
audit(deps, { principalId: actor.id, action: "scopes.read", resource: "scopes", scopeLabel: scope });
- const [crons, deployments, skills, environmentRows, rollups] = await Promise.all([
+ const [crons, deployments, skills, environmentRows] = await Promise.all([
app.listCrons(),
app.listDeployments(),
app.listSkills(),
app.listEnvironments(),
- deps.sessions?.scopeSessionRollups(scope, true) ?? [],
]);
const environments = environmentRows.map(({ environment, attachments }) => ({
id: environment.id,
@@ -177,22 +177,18 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {
...environments.map((environment) => environment.id),
...environments.flatMap((environment) => environment.attachedScopes),
];
- const previewIds = rollups.flatMap((r) => (r.previewSessionId ? [r.previewSessionId] : []));
- const [labels, previews] = await Promise.all([
- discoverScopes(app, deps, owners),
- deps.sessions?.lastUserMessages(previewIds) ?? new Map<string, string>(),
- ]);
+ const labels = await discoverScopes(app, deps, owners);
+ const activity = scopeActivity(deps.sessions);
const countBy = (ids: string[]): Map<string, number> => {
const m = new Map<string, number>();
for (const id of ids) m.set(id, (m.get(id) ?? 0) + 1);
return m;
};
- const rollupBy = new Map(rollups.map((r) => [r.scopeId, r]));
const cronN = countBy(crons.map((c) => c.ownerScopeId));
const deployN = countBy(deployments.map((d) => d.ownerScopeId));
const skillN = countBy(skills.map((s) => s.scopeId));
const scopes = [...labels].map(([id, label]) => {
- const rollup = rollupBy.get(id);
+ const counts = activity?.byScope.get(id);
return {
scopeId: id,
...(label ? { label } : {}),
@@ -205,24 +201,25 @@ export async function listAdminScopes(ctx: ApiCtx): Promise<void> {
},
}
: {}),
- sessions: rollup?.sessions ?? 0,
- backgroundSessions: rollup?.backgroundSessions ?? 0,
- lastActivity: rollup?.lastActivity ?? 0,
- lastConversationActivity: rollup?.lastConversationActivity ?? 0,
- lastMessage: (rollup?.previewSessionId && previews.get(rollup.previewSessionId)) || "",
+ ...(activity
+ ? {
+ sessions: counts?.conversations ?? 0,
+ backgroundSessions: (counts?.crons ?? 0) + (counts?.background ?? 0),
+ lastActivity: counts?.lastActivity ?? 0,
+ }
+ : {}),
crons: cronN.get(id) ?? 0,
deployments: deployN.get(id) ?? 0,
skills: skillN.get(id) ?? 0,
};
});
- scopes.sort(
- (a, b) =>
- b.lastActivity - a.lastActivity ||
- b.sessions - a.sessions ||
- b.backgroundSessions - a.backgroundSessions ||
- a.scopeId.localeCompare(b.scopeId),
- );
- return sendJson(res, 200, { scopeId: scope, scopes, environments });
+ scopes.sort((a, b) => (b.lastActivity ?? 0) - (a.lastActivity ?? 0) || a.scopeId.localeCompare(b.scopeId));
+ return sendJson(res, 200, {
+ scopeId: scope,
+ scopes,
+ environments,
+ ...(activity ? { activityAsOf: activity.asOf } : {}),
+ });
}
interface ScopeEnvironmentMetadata {
diff --git a/src/api/routes/admin/sessions.ts b/src/api/routes/admin/sessions.ts
index b83446bb9..54650f18f 100644
--- a/src/api/routes/admin/sessions.ts
+++ b/src/api/routes/admin/sessions.ts
@@ -11,48 +11,20 @@ import {
sessionCategory,
type LlmRequestRecord,
type SessionCategory,
- type SessionStore,
type SessionSummary,
} from "../../../sessions/session-store.ts";
import { createTranscriptSource } from "../../../sessions/transcript-source.ts";
-import { swallowAs } from "../../../util/errors.ts";
import { sendJson } from "../../http.ts";
import { audit, requireScopedAdmin } from "../shared.ts";
import { type ApiCtx } from "../route.ts";
import { requireScopedResource } from "./common.ts";
-import {
- cronWakeOrigin,
- deliveryOrigin,
- parseSessionWakeRef,
- sessionKind,
- sessionOrigin,
- type AdminSessionOrigin,
-} from "./origins.ts";
+import { activityCounts, scopeActivity } from "./scope-activity.ts";
+import { deliveryOrigin, parseSessionWakeRef, sessionKind, sessionOrigin, type AdminSessionOrigin } from "./origins.ts";
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;
}
@@ -190,76 +162,19 @@ 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 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: {},
- byTypeAll: {},
- totalByCategory: { conversation: 0, background: 0, all: 0 },
- crons: 0,
- };
const limit = Math.min(
SESSIONS_PAGE_LIMIT_MAX,
Math.max(1, Math.floor(Number(url.searchParams.get("limit"))) || SESSIONS_PAGE_LIMIT_DEFAULT),
);
- if (originFilter === "cron" && !cronId) {
- const groups = (await deps.sessions?.scopeCronGroups(scope, orgWide)) ?? [];
- const deliveredByCron =
- deps.deliveries && groups.length
- ? await deps.deliveries
- .sentRunCountsByCron(groups.map((g) => g.cronId))
- .catch(swallowAs("api: admin cron delivered counts failed", null))
- : null;
- const cronLookup = groups.length ? new Map((await app.listCrons()).map((c) => [c.id, c])) : undefined;
- const crons = await Promise.all(
- groups.map(async (g) => ({
- ...g,
- ...(deliveredByCron ? { deliveredRuns: deliveredByCron.get(g.cronId) ?? 0 } : {}),
- origin: await cronWakeOrigin(
- app,
- {
- trigger: "cron",
- sourceId: g.cronId,
- fireKey: `agent:main:cron:${g.cronId}`,
- fireSlot: null,
- monologue: true,
- },
- scope,
- g.scopeId,
- cronLookup,
- ),
- })),
- );
- return sendJson(res, 200, {
- scopeId: scope,
- crons,
- total: groups.length,
- totalByCategory: stats.totalByCategory,
- totalByType: stats.byTypeAll,
- distinctCrons: stats.crons,
- category,
- turns: stats.turns,
- byType: stats.byType,
- limit,
- });
- }
- const total = stats.total;
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 summaries =
+ const disconnected = new AbortController();
+ res.once("close", () => disconnected.abort());
+ const fetched =
(await deps.sessions
?.scopeSessionSummaries(scope, orgWide, {
- limit,
+ limit: limit + 1,
signal: disconnected.signal,
...(before ? { before } : {}),
...(categoryFilter ? { category: categoryFilter } : {}),
@@ -271,14 +186,8 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {
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 =
- deps.deliveries && background.length
- ? await deps.deliveries
- .sentCountsBySourceSessions(background.map((s) => ({ sessionId: s.id, threadRef: s.threadRef })))
- .catch(swallowAs("api: admin sessions sent counts failed", null))
- : null;
+ const summaries = fetched.slice(0, limit);
+ const last = fetched.length > limit ? summaries[summaries.length - 1] : undefined;
const fireResults = await cronFireResults(app, summaries);
const pageCronIds = new Set<string>(cronId ? [cronId] : []);
for (const s of summaries) {
@@ -293,14 +202,14 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {
const result = cronRow ? fireResults.get(s.threadRef) || s.lastMessage || s.firstMessage : undefined;
return {
...s,
- category: categories.get(s.id)!,
+ category: sessionCategory(s.origin),
kind: sessionKind(s),
origin: await sessionOrigin(app, s, scope, cronLookup),
- ...(sentCounts && categories.get(s.id) === "background" ? { delivered: sentCounts.get(s.id) ?? 0 } : {}),
...(result ? { result } : {}),
};
}),
);
+ const activity = scopeActivity(deps.sessions);
return sendJson(res, 200, {
scopeId: scope,
sessions,
@@ -314,17 +223,10 @@ export async function listAdminSessions(ctx: ApiCtx): Promise<void> {
),
}
: {}),
- total,
- totalByCategory: stats.totalByCategory,
- totalByType: stats.byTypeAll,
- distinctCrons: stats.crons,
category,
- turns: stats.turns,
- byType: stats.byType,
limit,
- ...(summaries.length === limit
- ? { nextCursor: `${summaries[summaries.length - 1]!.lastActivity}~${summaries[summaries.length - 1]!.id}` }
- : {}),
+ ...(last ? { nextCursor: `${last.lastActivity}~${last.id}` } : {}),
+ ...(activity ? { counts: activityCounts(activity, scope, orgWide), countsAsOf: activity.asOf } : {}),
});
}
diff --git a/src/api/routes/admin/users.ts b/src/api/routes/admin/users.ts
index 71097a911..23a2f87a5 100644
--- a/src/api/routes/admin/users.ts
+++ b/src/api/routes/admin/users.ts
@@ -378,7 +378,7 @@ export async function getUserDetail(ctx: ApiCtx): Promise<void> {
const [grants, stats, onboarding] = await Promise.all([
deps.admin?.listGrants() ?? [],
- deps.sessions?.scopeSessionStats(personal, false, "conversation"),
+ deps.sessions?.scopeConversationCount(personal),
deps.memory ? deps.memory.read(personal).then(detectOnboardingStatus) : null,
]);
const config = deps.config
@@ -398,7 +398,7 @@ export async function getUserDetail(ctx: ApiCtx): Promise<void> {
scopeId: personal,
...(member?.displayName ? { displayName: member.displayName } : {}),
admin: adminStatusFromGrants(grants, principalId),
- stats: { sessions: stats?.total ?? 0 },
+ stats: { sessions: stats ?? 0 },
config,
onboarding,
});
diff --git a/src/delivery/delivery-store.ts b/src/delivery/delivery-store.ts
index 5eaf641f7..be5bfa565 100644
--- a/src/delivery/delivery-store.ts
+++ b/src/delivery/delivery-store.ts
@@ -1,7 +1,6 @@
import { randomUUID } from "node:crypto";
import type { Delivery, DeliveryProvenance, Destination, OutgoingAttachment, ScopeId } from "../types.ts";
import type { TurnOrigin } from "../core/turn-origin.ts";
-import { cronIdOf } from "../sessions/session-store.ts";
export const DELIVERY_MAX_AGE_MS = 6 * 3_600_000;
@@ -54,8 +53,6 @@ export interface DeliveryStore {
recordRecipientThread(id: string, recipientThreadRef: string, at: number): Promise<void>;
listByRecipientThread(recipientThreadRef: string, opts?: { limit?: number }): Promise<Delivery[]>;
listBySourceSession(sourceSessionId: string, sourceThreadRef: string, opts?: { limit?: number }): Promise<Delivery[]>;
- sentCountsBySourceSessions(sources: Array<{ sessionId: string; threadRef: string }>): Promise<Map<string, number>>;
- sentRunCountsByCron(cronIds: string[]): Promise<Map<string, number>>;
onEnqueue(listener: () => void): () => void;
}
@@ -211,35 +208,6 @@ export function createDeliveryStore(opts?: { maxAgeMs?: number }): DeliveryStore
.sort((a, b) => a.createdAt - b.createdAt)
.slice(-limit);
},
- async sentCountsBySourceSessions(sources) {
- const counts = new Map<string, number>();
- const byId = new Set(sources.map((s) => s.sessionId));
- const byThreadRef = new Map(sources.map((s) => [s.threadRef, s.sessionId]));
- for (const d of deliveries.values()) {
- if (d.shadow || d.expiredAt !== undefined || !d.provenance) continue;
- let id: string | undefined;
- if (d.provenance.sourceSessionId) {
- if (byId.has(d.provenance.sourceSessionId)) id = d.provenance.sourceSessionId;
- } else if (d.provenance.sourceThreadRef) {
- id = byThreadRef.get(d.provenance.sourceThreadRef);
- }
- if (id) counts.set(id, (counts.get(id) ?? 0) + 1);
- }
- return counts;
- },
- async sentRunCountsByCron(cronIds) {
- const wanted = new Set(cronIds);
- const runs = new Map<string, Set<string>>();
- for (const d of deliveries.values()) {
- if (d.shadow || d.expiredAt !== undefined || !d.provenance?.sourceThreadRef) continue;
- const cronId = cronIdOf(d.provenance.sourceThreadRef);
- if (!cronId || !wanted.has(cronId)) continue;
- let set = runs.get(cronId);
- if (!set) runs.set(cronId, (set = new Set()));
- set.add(d.provenance.sourceSessionId ?? d.provenance.sourceThreadRef);
- }
- return new Map([...runs].map(([k, v]) => [k, v.size]));
- },
onEnqueue(listener) {
enqueueListeners.add(listener);
return () => enqueueListeners.delete(listener);
diff --git a/src/delivery/postgres-delivery-store.ts b/src/delivery/postgres-delivery-store.ts
index 6fdfb980d..a208ef1c8 100644
--- a/src/delivery/postgres-delivery-store.ts
+++ b/src/delivery/postgres-delivery-store.ts
@@ -1,26 +1,10 @@
import { randomUUID } from "node:crypto";
-import { createPgPool, type PgPool } from "../persistence/pg-pool.ts";
+import { createPgPool } from "../persistence/pg-pool.ts";
import type { Delivery, DeliveryProvenance, Destination, OutgoingAttachment } from "../types.ts";
import { DELIVERY_MAX_AGE_MS, logDeliveryExpiry, type DeliveryStore } from "./delivery-store.ts";
-import { cronIdOf, threadRefCronIdExpr } from "../sessions/session-store.ts";
import { pgTextSafe } from "../util/text.ts";
const NOW_MS_SQL = "(EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::BIGINT";
-const SOURCE_CRON_ID_EXPR = threadRefCronIdExpr("provenance->>'sourceThreadRef'");
-
-export async function backfillDeliverySourceCronIdBatch(q: PgPool["q"], limit: number): Promise<number> {
- const updated = await q(
- `UPDATE deliveries
- SET source_cron_id = ${SOURCE_CRON_ID_EXPR}
- WHERE id IN (SELECT id FROM deliveries
- WHERE source_cron_id IS NULL AND ${SOURCE_CRON_ID_EXPR} IS NOT NULL
- LIMIT $1)
- RETURNING 1`,
- [limit],
- );
- return updated.length;
-}
-
function rowToDelivery(r: Record<string, unknown>): Delivery {
return {
id: r.id as string,
@@ -119,14 +103,13 @@ export function createPostgresDeliveryStore(connectionString: string, opts?: { m
return {
async enqueue(input) {
const inserted = await q(
- `INSERT INTO deliveries (id, idempotency_key, destination, text, attachments, provenance, source_cron_id, created_at, shadow)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
+ `INSERT INTO deliveries (id, idempotency_key, destination, text, attachments, provenance, created_at, shadow)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (idempotency_key) DO UPDATE
SET destination = EXCLUDED.destination,
text = EXCLUDED.text,
attachments = EXCLUDED.attachments,
provenance = EXCLUDED.provenance,
- source_cron_id = EXCLUDED.source_cron_id,
created_at = EXCLUDED.created_at,
shadow = EXCLUDED.shadow,
expired_at = NULL,
@@ -140,7 +123,6 @@ export function createPostgresDeliveryStore(connectionString: string, opts?: { m
pgTextSafe(input.text),
input.attachments?.length ? JSON.stringify(input.attachments) : null,
input.provenance ? JSON.stringify(input.provenance) : null,
- cronIdOf(input.provenance?.sourceThreadRef),
Date.now(),
input.shadow === true,
],
@@ -259,43 +241,6 @@ export function createPostgresDeliveryStore(connectionString: string, opts?: { m
);
return rows.map(rowToDelivery).reverse();
},
- async sentCountsBySourceSessions(sources) {
- const counts = new Map<string, number>();
- if (!sources.length) return counts;
- const byThreadRef = new Map(sources.map((s) => [s.threadRef, s.sessionId]));
- const rows = await q(
- `SELECT provenance->>'sourceSessionId' AS session_id,
- provenance->>'sourceThreadRef' AS thread_ref,
- COUNT(*)::int AS sent
- FROM deliveries
- WHERE NOT shadow AND expired_at IS NULL
- AND (provenance->>'sourceSessionId' = ANY($1)
- OR (provenance->>'sourceSessionId' IS NULL AND provenance->>'sourceThreadRef' = ANY($2)))
- GROUP BY 1, 2`,
- [sources.map((s) => s.sessionId), sources.map((s) => s.threadRef)],
- );
- for (const r of rows as Array<{ session_id: string | null; thread_ref: string | null; sent: number }>) {
- const id = r.session_id ?? (r.thread_ref ? byThreadRef.get(r.thread_ref) : undefined);
- if (id) counts.set(id, (counts.get(id) ?? 0) + Number(r.sent));
- }
- return counts;
- },
- async sentRunCountsByCron(cronIds) {
- const counts = new Map<string, number>();
- if (!cronIds.length) return counts;
- const rows = await q(
- `SELECT d.cron_id, COUNT(DISTINCT d.run)::int AS runs
- FROM (SELECT COALESCE(source_cron_id, ${SOURCE_CRON_ID_EXPR}) AS cron_id,
- COALESCE(provenance->>'sourceSessionId', provenance->>'sourceThreadRef') AS run
- FROM deliveries
- WHERE NOT shadow AND expired_at IS NULL AND provenance->>'sourceThreadRef' IS NOT NULL) d
- WHERE d.cron_id = ANY($1)
- GROUP BY d.cron_id`,
- [cronIds],
- );
- for (const r of rows as Array<{ cron_id: string; runs: number }>) counts.set(r.cron_id, Number(r.runs));
- return counts;
- },
onEnqueue(listener) {
enqueueListeners.add(listener);
return () => enqueueListeners.delete(listener);
diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.ts
index 14b84bfe4..b3c2aaefc 100644
--- a/src/sessions/memory-session-store.ts
+++ b/src/sessions/memory-session-store.ts
@@ -5,7 +5,7 @@ import type { GoalRecord } from "../harness/goal.ts";
import type {
AttributedTurn,
EntrySearchHit,
- CronGroupSummary,
+ ScopeActivity,
GetEntriesOptions,
LeaseAttempt,
LeaseHolder,
@@ -16,8 +16,6 @@ import type {
NewSearchEntry,
NewSessionPin,
ParticipantWindow,
- ScopeSessionRollup,
- ScopeSessionStats,
SessionPage,
SessionStore,
SessionSummary,
@@ -39,7 +37,6 @@ import {
isOverheardEntry,
DuplicateCallError,
promptEnvelopeBody,
- sessionBucket,
sessionCategory,
sessionOrigin,
userMessagePreview,
@@ -664,7 +661,7 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
messages: log.length,
lastActivity: log.length ? log[log.length - 1]!.createdAt : s.createdAt,
createdAt: s.createdAt,
- firstMessage: userEntries.length ? userMessagePreview(userEntries[0]!.payload) : "",
+ firstMessage: userEntries.length ? userMessagePreview(userEntries[0]!.payload) : (s.title ?? ""),
lastMessage: userEntries.length ? userMessagePreview(userEntries[userEntries.length - 1]!.payload, 100) : "",
});
}
@@ -680,114 +677,31 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
).slice(0, page.limit);
},
- async lastUserMessages(sessionIds): Promise<Map<string, string>> {
- const out = new Map<string, string>();
- for (const id of sessionIds) {
- const log = entries.get(id) ?? [];
- const userEntries = log.filter((e) => e.type === "user" && !isOverheardEntry(e));
- if (userEntries.length) out.set(id, userMessagePreview(userEntries[userEntries.length - 1]!.payload, 100));
- }
- return out;
+ async scopeConversationCount(scope): Promise<number> {
+ let n = 0;
+ for (const s of sessions.values()) if (s.scopeId === scope && sessionOrigin(s.threadRef) === "conversation") n++;
+ return n;
},
- async scopeCronGroups(scope, orgWide): Promise<CronGroupSummary[]> {
- const groups = new Map<string, CronGroupSummary>();
+ async scopeActivity(): Promise<ScopeActivity[]> {
+ const byScope = new Map<string, ScopeActivity>();
for (const s of sessions.values()) {
- if (!orgWide && s.scopeId !== scope) continue;
- const cronId = cronIdOf(s.threadRef);
- if (!cronId) continue;
- const log = entries.get(s.id) ?? [];
- const turns = log.filter((e) => e.type === "user" && !isOverheardEntry(e)).length;
- const lastActivity = log.length ? log[log.length - 1]!.createdAt : s.createdAt;
- const g = groups.get(cronId) ?? {
- cronId,
+ const r = byScope.get(s.scopeId) ?? {
scopeId: s.scopeId,
- sessions: 0,
- turns: 0,
- messages: 0,
+ conversations: 0,
+ crons: 0,
+ background: 0,
lastActivity: 0,
- createdAt: s.createdAt,
};
- g.sessions += 1;
- g.turns += turns;
- g.messages += log.length;
- g.lastActivity = Math.max(g.lastActivity, lastActivity);
- g.createdAt = Math.min(g.createdAt, s.createdAt);
- groups.set(cronId, g);
- }
- return [...groups.values()].sort((a, b) => {
- if (b.lastActivity !== a.lastActivity) return b.lastActivity - a.lastActivity;
- if (a.cronId < b.cronId) return 1;
- if (a.cronId > b.cronId) return -1;
- return 0;
- });
- },
-
- async scopeSessionRollups(scope, orgWide): Promise<ScopeSessionRollup[]> {
- const rollups = new Map<string, ScopeSessionRollup>();
- const winners = new Map<string, { at: number; id: string }>();
- for (const s of sessions.values()) {
- if (!orgWide && s.scopeId !== scope) continue;
+ byScope.set(s.scopeId, r);
const log = entries.get(s.id) ?? [];
- const turns = log.filter((e) => e.type === "user" && !isOverheardEntry(e)).length;
- const lastActivity = log.length ? log[log.length - 1]!.createdAt : s.createdAt;
- const r = rollups.get(s.scopeId) ?? {
- scopeId: s.scopeId,
- sessions: 0,
- backgroundSessions: 0,
- lastActivity: 0,
- lastConversationActivity: 0,
- previewSessionId: null,
- };
- rollups.set(s.scopeId, r);
- r.lastActivity = Math.max(r.lastActivity, lastActivity);
- if (sessionCategory(sessionOrigin(s.threadRef)) === "background") {
- r.backgroundSessions += 1;
- continue;
- }
- r.sessions += 1;
- r.lastConversationActivity = Math.max(r.lastConversationActivity, lastActivity);
- if (turns === 0) continue;
- const prev = winners.get(s.scopeId);
- if (!prev || lastActivity > prev.at || (lastActivity === prev.at && idDesc(s.id, prev.id) < 0)) {
- winners.set(s.scopeId, { at: lastActivity, id: s.id });
- r.previewSessionId = s.id;
- }
- }
- return [...rollups.values()];
- },
-
- async scopeSessionStats(scope, orgWide, category, originFilter, cronId): Promise<ScopeSessionStats> {
- const byType: Record<string, number> = {};
- const byTypeAll: Record<string, number> = {};
- const totalByCategory = { conversation: 0, background: 0, all: 0 };
- const cronIds = new Set<string>();
- let total = 0;
- let turns = 0;
- for (const s of sessions.values()) {
- if (!orgWide && s.scopeId !== scope) continue;
+ r.lastActivity = Math.max(r.lastActivity, log.length ? log[log.length - 1]!.createdAt : s.createdAt);
const origin = sessionOrigin(s.threadRef);
- const cat = sessionCategory(origin);
- totalByCategory[cat]++;
- totalByCategory.all++;
- const bucket = sessionBucket(origin, s.type);
- byTypeAll[bucket] = (byTypeAll[bucket] ?? 0) + 1;
- const sessionCronId = cronIdOf(s.threadRef);
- if (sessionCronId != null) cronIds.add(sessionCronId);
- if (category && cat !== category) continue;
- if (
- originFilter === "other_background"
- ? origin === "conversation" || origin === "cron"
- : originFilter && origin !== originFilter
- )
- continue;
- if (cronId && sessionCronId !== cronId) continue;
- total++;
- const log = entries.get(s.id) ?? [];
- turns += log.filter((e) => e.type === "user" && !isOverheardEntry(e)).length;
- byType[bucket] = (byType[bucket] ?? 0) + 1;
+ if (origin === "conversation") r.conversations++;
+ else if (origin === "cron") r.crons++;
+ else r.background++;
}
- return { total, turns, byType, byTypeAll, totalByCategory, crons: cronIds.size };
+ return [...byScope.values()];
},
async attributedTurns(): Promise<AttributedTurn[]> {
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index f0abd7845..9bd86510e 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -1,15 +1,9 @@
import { DIRECTORY_MIGRATIONS } from "../directory/postgres-directory-store.ts";
import { randomUUID } from "node:crypto";
import { jsonbStringify } from "../persistence/durable-map.ts";
-import {
- createPgPool,
- type PoolClient,
- type PgQueryOptions,
- type Rows,
- withPgTransaction,
-} from "../persistence/pg-pool.ts";
+import { createPgPool, type PoolClient, withPgTransaction } from "../persistence/pg-pool.ts";
import { jsonbSafeStringify, pgTextSafe } from "../util/text.ts";
-import { reportFailure, reportFailureAs } from "../util/errors.ts";
+import { reportFailure } from "../util/errors.ts";
import type { Session, SessionEntry, SessionType, ScopeId } from "../types.ts";
import type { GoalRecord } from "../harness/goal.ts";
import type {
@@ -17,8 +11,7 @@ import type {
SessionPin,
AttributedTurn,
EntrySearchHit,
- CronGroupSummary,
- ScopeSessionRollup,
+ ScopeActivity,
DistinctScope,
GetEntriesOptions,
Lease,
@@ -29,7 +22,6 @@ import type {
NewEntry,
NewLlmRequest,
ParticipantWindow,
- ScopeSessionStats,
ScreenSample,
SessionOrigin,
SessionOriginFilter,
@@ -910,6 +902,15 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
`DROP SCHEMA IF EXISTS items_copy_ops CASCADE`,
],
},
+ {
+ id: "sessions/store/0038-scoped-origin-pages",
+ statements: [
+ `CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_conversations_by_scope_activity
+ ON sessions(scope_id, (COALESCE(last_activity, created_at)) DESC, id DESC) WHERE origin = 'conversation'`,
+ `CREATE INDEX CONCURRENTLY IF NOT EXISTS sessions_other_background_by_scope_activity
+ ON sessions(scope_id, (COALESCE(last_activity, created_at)) DESC, id DESC) WHERE origin NOT IN ('conversation', 'cron')`,
+ ],
+ },
],
[
{
@@ -1030,39 +1031,6 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
return 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 jsonb_build_object('text',fe.text) FROM items fe
- WHERE fe.tenant_id = ${TENANT} AND fe.session_id = s.id AND ${userTurn("fe")} ORDER BY fe.seq ASC LIMIT 1) AS first_user,
- (SELECT jsonb_build_object('text',le.text) FROM items le
- WHERE le.tenant_id = ${TENANT} AND 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(r.first_user ?? null, FIRST_PREVIEW_LEN),
- last: (r.last_user_preview as string | null) ?? storedPreview(r.last_user ?? null, LAST_PREVIEW_LEN),
- });
- }
- return out;
- };
-
return {
leaseTtlMs,
async getOrCreateByThread(threadRef, type, scopeId, channelName, surface, clientTools): Promise<Session> {
@@ -1743,7 +1711,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
params.push(page.limit);
const rows = await q(
`SELECT s.id, s.type, s.scope_id, s.thread_ref, s.origin, s.created_at, s.messages, s.turns,
- ${lastActivityExpr("s")} AS last_activity, s.first_user_preview, s.last_user_preview
+ ${lastActivityExpr("s")} AS last_activity, s.first_user_preview, s.last_user_preview, s.title
FROM sessions s
WHERE ($1::boolean OR s.scope_id = $2)${categoryClause}${originClause}${cronClause}${keysetClause}
ORDER BY ${lastActivityExpr("s")} DESC, s.id DESC
@@ -1751,7 +1719,6 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
params,
options,
);
- const previews = await userPreviews(rows, options);
return rows.map((r) => ({
id: r.id as string,
type: r.type as Session["type"],
@@ -1762,132 +1729,39 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
messages: Number(r.messages ?? 0),
lastActivity: Number(r.last_activity),
createdAt: Number(r.created_at),
- firstMessage: previews.get(r.id as string)!.first,
- lastMessage: previews.get(r.id as string)!.last,
+ firstMessage: ((r.first_user_preview ?? r.title ?? "") as string).trim(),
+ lastMessage: (r.last_user_preview as string | null) ?? "",
}));
},
- async lastUserMessages(sessionIds): Promise<Map<string, string>> {
- const out = new Map<string, string>();
- if (sessionIds.length === 0) return out;
- const rows = await q(
- `SELECT DISTINCT ON (le.session_id) le.session_id, left(le.text, ${LAST_PREVIEW_LEN}) AS last_user
- FROM items le
- WHERE le.tenant_id = ${TENANT} AND le.session_id = ANY($1) AND ${userTurn("le")}
- ORDER BY le.session_id, le.seq DESC`,
- [sessionIds],
- );
- for (const r of rows) out.set(r.session_id as string, userMessagePreview(r.last_user ?? null, 100));
- return out;
- },
-
- async scopeCronGroups(scope, orgWide): Promise<CronGroupSummary[]> {
- const rows = await q(
- `SELECT t.cron_id,
- MIN(t.scope_id) AS scope_id,
- COUNT(*) AS sessions,
- COALESCE(SUM(t.messages), 0) AS messages,
- COALESCE(SUM(t.turns), 0) AS turns,
- MAX(t.activity) AS last_activity,
- MIN(t.created_at) AS created_at
- FROM (SELECT s.origin_id AS cron_id, s.scope_id, s.messages, s.turns, s.created_at,
- ${lastActivityExpr("s")} AS activity
- FROM sessions s
- WHERE ($1::boolean OR s.scope_id = $2)) t
- WHERE t.cron_id IS NOT NULL
- GROUP BY t.cron_id
- ORDER BY last_activity DESC, t.cron_id DESC`,
- [orgWide, scope],
- );
- return rows.map((r) => ({
- cronId: r.cron_id as string,
- scopeId: r.scope_id as ScopeId,
- sessions: Number(r.sessions),
- turns: Number(r.turns),
- messages: Number(r.messages),
- lastActivity: Number(r.last_activity),
- createdAt: Number(r.created_at),
- }));
+ async scopeConversationCount(scope): Promise<number> {
+ const rows = await q("SELECT COUNT(*) AS n FROM sessions WHERE scope_id = $1 AND origin = 'conversation'", [
+ scope,
+ ]);
+ return Number(rows[0]?.n ?? 0);
},
- async scopeSessionRollups(scope, orgWide): Promise<ScopeSessionRollup[]> {
+ async scopeActivity(): Promise<ScopeActivity[]> {
const rows = await q(
- `SELECT r.scope_id, r.sessions, r.background_sessions, r.last_activity, r.last_conversation_activity,
- (
- SELECT s.id FROM sessions s
- WHERE s.scope_id = r.scope_id AND ${lastActivityExpr("s")} <= r.preview_activity
- AND NOT (${isBackground("s")}) AND s.turns > 0
- ORDER BY ${lastActivityExpr("s")} DESC, s.id DESC LIMIT 1
- ) AS preview_session_id
- FROM (SELECT scope_id,
- COUNT(*) FILTER (WHERE NOT background) AS sessions,
- COUNT(*) FILTER (WHERE background) AS background_sessions,
- MAX(last_activity) AS last_activity,
- COALESCE(MAX(last_activity) FILTER (WHERE NOT background), 0) AS last_conversation_activity,
- MAX(last_activity) FILTER (WHERE NOT background AND turns > 0) AS preview_activity
- FROM (SELECT s.scope_id, s.turns, ${lastActivityExpr("s")} AS last_activity,
- ${isBackground("s")} AS background
- FROM sessions s
- WHERE ($1::boolean OR s.scope_id = $2)) t
- GROUP BY scope_id) r`,
- [orgWide, scope],
+ `SELECT scope_id,
+ COUNT(*) FILTER (WHERE origin = 'conversation') AS conversations,
+ COUNT(*) FILTER (WHERE origin = 'cron') AS crons,
+ COUNT(*) FILTER (WHERE origin NOT IN ('conversation', 'cron')) AS background,
+ MAX(${lastActivityExpr("sessions")}) AS last_activity
+ FROM sessions
+ GROUP BY scope_id`,
+ [],
+ { timeoutMs: ADMIN_READ_TIMEOUT_MS },
);
return rows.map((r) => ({
scopeId: r.scope_id as ScopeId,
- sessions: Number(r.sessions),
- backgroundSessions: Number(r.background_sessions),
+ conversations: Number(r.conversations),
+ crons: Number(r.crons),
+ background: Number(r.background),
lastActivity: Number(r.last_activity),
- lastConversationActivity: Number(r.last_conversation_activity),
- previewSessionId: (r.preview_session_id as string | null) ?? null,
}));
},
- async scopeSessionStats(scope, orgWide, category, originFilter, cronId): Promise<ScopeSessionStats> {
- const params: unknown[] = [orgWide, scope];
- const matchedClauses: string[] = [];
- if (category) {
- matchedClauses.push(category === "background" ? isBackground("s") : `NOT ${isBackground("s")}`);
- }
- if (originFilter) matchedClauses.push(originFilterClause("s", originFilter));
- if (cronId) {
- params.push(cronId);
- matchedClauses.push(`s.origin_id = $${params.length}`);
- }
- const rows = await q(
- `SELECT origin, CASE WHEN origin = 'conversation' THEN type ELSE origin END AS bucket,
- matched, COUNT(*) AS sessions, COALESCE(SUM(turns), 0) AS turns,
- COUNT(DISTINCT cron_id) AS crons
- FROM (SELECT s.type, s.origin AS origin,
- s.origin_id AS cron_id,
- (${matchedClauses.join(" AND ") || "TRUE"}) AS matched,
- COALESCE(s.turns, 0) AS turns
- FROM sessions s
- WHERE ($1::boolean OR s.scope_id = $2)) t
- GROUP BY origin, bucket, matched`,
- params,
- { timeoutMs: ADMIN_READ_TIMEOUT_MS },
- );
- const byType: Record<string, number> = {};
- const byTypeAll: Record<string, number> = {};
- const totalByCategory = { conversation: 0, background: 0, all: 0 };
- let total = 0;
- let turns = 0;
- let crons = 0;
- for (const r of rows) {
- const n = Number(r.sessions);
- const bucket = r.bucket as string;
- totalByCategory[r.origin === "conversation" ? "conversation" : "background"] += n;
- totalByCategory.all += n;
- byTypeAll[bucket] = (byTypeAll[bucket] ?? 0) + n;
- if (r.origin === "cron") crons += Number(r.crons);
- if (!r.matched) continue;
- total += n;
- turns += Number(r.turns);
- byType[bucket] = (byType[bucket] ?? 0) + n;
- }
- return { total, turns, byType, byTypeAll, totalByCategory, crons };
- },
-
async attributedTurns(): Promise<AttributedTurn[]> {
const rows = await q(
`SELECT p.principal_id, e.session_id, (${msOf("e.created_at")} / 86400000)::bigint AS day,
diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.ts
index 462bd991d..ed8d1ddb5 100644
--- a/src/sessions/session-store.ts
+++ b/src/sessions/session-store.ts
@@ -168,32 +168,12 @@ export interface SessionPage {
signal?: AbortSignal;
}
-export interface CronGroupSummary {
- cronId: string;
+export interface ScopeActivity {
scopeId: ScopeId;
- sessions: number;
- turns: number;
- messages: number;
- lastActivity: number;
- createdAt: number;
-}
-
-export interface ScopeSessionRollup {
- scopeId: ScopeId;
- sessions: number;
- backgroundSessions: number;
- lastActivity: number;
- lastConversationActivity: number;
- previewSessionId: string | null;
-}
-
-export interface ScopeSessionStats {
- total: number;
- turns: number;
- byType: Record<string, number>;
- byTypeAll: Record<string, number>;
- totalByCategory: Record<SessionCategory | "all", number>;
+ conversations: number;
crons: number;
+ background: number;
+ lastActivity: number;
}
export interface SpendRow {
@@ -370,10 +350,6 @@ export function cronIdOf(threadRef: string | null | undefined): string | null {
return m ? m[1]! : null;
}
-export function sessionBucket(origin: SessionOrigin, type: SessionType): string {
- return origin === "conversation" ? type : origin;
-}
-
export interface SessionRef {
id: string;
threadRef: string;
@@ -633,19 +609,9 @@ export interface SessionStore {
scopeSessionSummaries(scope: ScopeId, orgWide: boolean, page: SessionPage): Promise<SessionSummary[]>;
- lastUserMessages(sessionIds: string[]): Promise<Map<string, string>>;
-
- scopeCronGroups(scope: ScopeId, orgWide: boolean): Promise<CronGroupSummary[]>;
-
- scopeSessionRollups(scope: ScopeId, orgWide: boolean): Promise<ScopeSessionRollup[]>;
+ scopeActivity(): Promise<ScopeActivity[]>;
- scopeSessionStats(
- scope: ScopeId,
- orgWide: boolean,
- category?: SessionCategory,
- origin?: SessionOriginFilter,
- cronId?: string,
- ): Promise<ScopeSessionStats>;
+ scopeConversationCount(scope: ScopeId): Promise<number>;
attributedTurns(): Promise<AttributedTurn[]>;
diff --git a/test/admin-observability.test.ts b/test/admin-observability.test.ts
index c5d4fe884..3293aec9d 100644
--- a/test/admin-observability.test.ts
+++ b/test/admin-observability.test.ts
@@ -739,7 +739,16 @@ test("monitor delivery origin context is scope-gated in admin", async () => {
}
});
-test("conversation listing pages by keyset cursor; aggregates span the whole scope; offset is ignored", async () => {
+async function eventually(base: string, path: string, ready: (body: any) => boolean): Promise<any> {
+ let body = await getJson(base, path);
+ for (let i = 0; i < 50 && !ready(body); i++) {
+ await new Promise((r) => setTimeout(r, 10));
+ body = await getJson(base, path);
+ }
+ return body;
+}
+
+test("conversation listing pages by keyset cursor; counts come from the background snapshot; offset is ignored", async () => {
const s = start();
try {
for (let i = 0; i < 5; i++) {
@@ -755,8 +764,7 @@ test("conversation listing pages by keyset cursor; aggregates span the whole sco
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.total, undefined, "the listing computes no live totals");
assert.equal(p1.limit, 2);
assert.equal(p1.offset, undefined, "the listing no longer reports an offset");
@@ -766,6 +774,13 @@ test("conversation listing pages by keyset cursor; aggregates span the whole sco
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 exact = await getJson(s.base, "/v1/admin/sessions?scope=org:default-org&limit=5");
+ assert.equal(exact.sessions.length, 5);
+ assert.equal(exact.nextCursor, undefined, "a page that exactly fills the limit offers no empty next page");
+
+ const counted = await eventually(s.base, path, (body) => body.counts !== undefined);
+ assert.deepEqual(counted.counts, { conversation: 5, cron: 0, background: 0 }, "tab counts span the org");
+ assert.equal(typeof counted.countsAsOf, "number");
const ignored = await getJson(s.base, `${path}&offset=4`);
assert.deepEqual(
@@ -814,18 +829,22 @@ test("history separates background cron monologues from human conversations", as
const conversations = await getJson(s.base, `/v1/admin/sessions?scope=${encodeURIComponent(scope)}`);
assert.equal(conversations.category, "conversation");
- assert.equal(conversations.total, 1, "default history lists human conversations only");
assert.deepEqual(
conversations.sessions.map((x: { id: string }) => x.id),
[human.id],
);
- assert.deepEqual(conversations.totalByCategory, { conversation: 1, background: 1, all: 2 });
+ const counted = await eventually(
+ s.base,
+ `/v1/admin/sessions?scope=${encodeURIComponent(scope)}`,
+ (body) => body.counts !== undefined,
+ );
+ assert.deepEqual(counted.counts, { conversation: 1, cron: 1, background: 0 }, "tab counts are per scope");
const background = await getJson(
s.base,
`/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background`,
);
- assert.equal(background.total, 1);
+ assert.equal(background.sessions.length, 1);
assert.equal(background.sessions[0].id, monologue.id);
assert.equal(background.sessions[0].category, "background");
assert.equal(background.sessions[0].kind, "cron_monologue");
@@ -839,7 +858,7 @@ test("history separates background cron monologues from human conversations", as
assert.equal(transcript.origin.label, "Cron monologue");
assert.equal(transcript.origin.cronId, cron.id);
- const scopes = await getJson(s.base, "/v1/admin/scopes");
+ const scopes = await eventually(s.base, "/v1/admin/scopes", (body) => body.activityAsOf !== undefined);
const row = scopes.scopes.find((x: { scopeId: string }) => x.scopeId === scope);
assert.equal(row.sessions, 1, "scope index counts human conversations separately");
assert.equal(row.backgroundSessions, 1, "scope index exposes background monologues separately");
@@ -848,7 +867,7 @@ test("history separates background cron monologues from human conversations", as
}
});
-test("the Crons history lists one row per cron; ?cron= paginates that cron's fires", async () => {
+test("the Crons history lists cron fires as session rows; ?cron= paginates one cron's fires", async () => {
const s = start();
try {
const scope = "channel:C7";
@@ -876,41 +895,18 @@ test("the Crons history lists one row per cron; ?cron= paginates that cron's fir
s.base,
`/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron`,
);
- assert.equal(list.sessions, undefined, "grouped listing carries no per-fire rows");
- assert.equal(list.total, 2, "one row per cron, regardless of fire count");
- const g = list.crons.find((x: { cronId: string }) => x.cronId === cron.id);
- assert.equal(g.sessions, 3, "the group aggregates every fire");
- assert.equal(g.origin.cron.title, "Tweet watcher", "group row resolves the cron's name");
- assert.equal(g.deliveredRuns, 0, "no deliveries yet: delivered rollup is zero");
-
- await s.built.deliveries.enqueue({
- destination: { type: "principal", target: "U1", onBehalfOf: "U1" },
- text: "recap",
- idempotencyKey: `cron:${cron.id}:slot0:msg`,
- provenance: {
- trigger: "cron",
- surface: "cron",
- fireKey: `cron:${cron.id}:slot0`,
- sourceScopeId: scope,
- sourceThreadRef: `cron:${cron.id}:slot0`,
- sourceSessionId: "fire-0",
- },
- });
- const relisted = await getJson(
- s.base,
- `/v1/admin/sessions?scope=${encodeURIComponent(scope)}&category=background&origin=cron`,
- );
+ assert.equal(list.sessions.length, 5, "every fire is its own row");
+ assert.ok(list.sessions.every((x: { origin: { kind: string } }) => x.origin.kind === "cron"));
assert.equal(
- relisted.crons.find((x: { cronId: string }) => x.cronId === cron.id).deliveredRuns,
- 1,
- "the listing rolls up delivering runs per cron",
+ list.sessions.find((x: { threadRef: string }) => x.threadRef.includes(cron.id)).origin.cron.title,
+ "Tweet watcher",
+ "rows resolve the cron's name",
);
const p1 = await getJson(
s.base,
`/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,
diff --git a/test/admin-scopes-directory.test.ts b/test/admin-scopes-directory.test.ts
index ddf3ce44d..320f9b3c1 100644
--- a/test/admin-scopes-directory.test.ts
+++ b/test/admin-scopes-directory.test.ts
@@ -45,12 +45,10 @@ test("the scope directory starts independent resource reads together", async ()
app.listDeployments = hold("deployments", app.listDeployments.bind(app));
app.listSkills = hold("skills", app.listSkills.bind(app));
app.listEnvironments = hold("environments", app.listEnvironments.bind(app));
- const rollups = s.built.sessions.scopeSessionRollups.bind(s.built.sessions);
- s.built.sessions.scopeSessionRollups = hold("rollups", () => rollups("org:default-org", true));
const request = fetch(`${s.base}/v1/admin/scopes`, { headers: ALICE_ADMIN });
try {
await ready.promise;
- assert.deepEqual([...started].sort(), ["crons", "deployments", "environments", "rollups", "skills"]);
+ assert.deepEqual([...started].sort(), ["crons", "deployments", "environments", "skills"]);
} finally {
gate.resolve();
await request;
@@ -80,7 +78,15 @@ test("the admin scope directory lists every known scope with labels and counts (
const r = await fetch(`${s.base}/v1/admin/scopes`, { headers: ALICE_ADMIN });
assert.equal(r.status, 200);
- const d = await json(r);
+ const first = await json(r);
+ assert.equal(first.activityAsOf, undefined, "the first read does not wait for session counts");
+ assert.equal(first.scopes.find((row: any) => row.scopeId === "channel:C9").sessions, undefined);
+ let d = first;
+ for (let i = 0; i < 50 && d.activityAsOf === undefined; i++) {
+ await new Promise((r) => setTimeout(r, 10));
+ d = await json(await fetch(`${s.base}/v1/admin/scopes`, { headers: ALICE_ADMIN }));
+ }
+ assert.equal(typeof d.activityAsOf, "number", "a later read serves the counts the background refresh computed");
const byId = new Map(d.scopes.map((row: any) => [row.scopeId, row]));
assert.ok(byId.has("org:default-org"), "the org scope is always listed");
@@ -92,7 +98,7 @@ test("the admin scope directory lists every known scope with labels and counts (
const c9: any = byId.get("channel:C9");
assert.equal(c9.label, "#eng", "channel rows carry the human channel name");
assert.equal(c9.sessions, 1);
- assert.equal(c9.lastMessage, "ship the board deck", "rows carry the newest conversation's last-message preview");
+ assert.equal(c9.lastMessage, undefined, "the directory carries no message previews");
const gone: any = byId.get("channel:GONE");
assert.equal(gone.crons, 1, "rows carry artifact counts");
assert.equal(gone.sessions, 0);
diff --git a/test/admin-users.test.ts b/test/admin-users.test.ts
index 9bcdb284d..f3b9d66d3 100644
--- a/test/admin-users.test.ts
+++ b/test/admin-users.test.ts
@@ -329,10 +329,9 @@ test("/v1/admin/users: a freshly promoted user shows as admin in the roster", as
test("user detail resolves mixed-case email links to the canonical personal scope", async (t) => {
const s = start();
try {
- const stats = await s.built.sessions.scopeSessionStats("personal:[email protected]", false, "conversation");
- t.mock.method(s.built.sessions, "scopeSessionStats", async (scope: string) => {
+ t.mock.method(s.built.sessions, "scopeConversationCount", async (scope: string) => {
assert.equal(scope, "personal:[email protected]");
- return { ...stats, total: 3 };
+ return 3;
});
const response = await fetch(`${s.base}/v1/admin/users/Alice%40example.com`, {
headers: { "x-admin-actor": "admin-alice@default-org" },
diff --git a/test/delivery-store-contract.ts b/test/delivery-store-contract.ts
index 04bc1b2e3..d14eb74b5 100644
--- a/test/delivery-store-contract.ts
+++ b/test/delivery-store-contract.ts
@@ -176,28 +176,6 @@ export async function exerciseDeliveryStore(store: DeliveryStore): Promise<void>
sourceThreadRef: "agent:main:cron:legacy",
},
});
- const sentCounts = await store.sentCountsBySourceSessions([
- { sessionId: "source-session", threadRef: "agent:main:cron:c1" },
- { sessionId: "shadow-session", threadRef: "agent:main:cron:hb-1" },
- { sessionId: "legacy-session", threadRef: "agent:main:cron:legacy" },
- { sessionId: "silent-session", threadRef: "agent:main:cron:c9" },
- ]);
- assert.equal(
- sentCounts.get("source-session"),
- 1,
- "a row naming another session never counts for a page session sharing its thread",
- );
- assert.equal(sentCounts.get("shadow-session"), undefined, "shadow rows never count as sent");
- assert.equal(sentCounts.get("legacy-session"), 1, "rows without sourceSessionId fall back to threadRef");
- assert.equal(sentCounts.get("silent-session"), undefined, "sessions with no deliveries stay absent");
- assert.equal((await store.sentCountsBySourceSessions([])).size, 0);
-
- const runCounts = await store.sentRunCountsByCron(["c1", "hb-1", "legacy", "c9"]);
- assert.equal(runCounts.get("c1"), 2, "distinct delivering runs per cron (session id or legacy threadRef)");
- assert.equal(runCounts.get("legacy"), 1, "legacy provenance counts by threadRef");
- assert.equal(runCounts.get("hb-1"), undefined, "shadow rows never count as delivered runs");
- assert.equal(runCounts.get("c9"), undefined, "crons with no deliveries stay absent");
- assert.equal((await store.sentRunCountsByCron([])).size, 0);
const contested = await store.enqueue({
destination: { type: "group", target: "C-race" },
@@ -333,13 +311,6 @@ export async function exerciseDeliveryExpiryRevive(
await new Promise((r) => setTimeout(r, 400));
assert.deepEqual(await store.claimPending("slack", 60_000), [], "the overaged notice is swept, not handed out");
assert.ok(((await store.get(notice.id))?.expiredAt ?? 0) > 0);
- assert.equal(
- (await store.sentCountsBySourceSessions([{ sessionId: "notice-session", threadRef: "agent:main:cron:t1" }])).get(
- "notice-session",
- ),
- undefined,
- "dropped rows never count as sent",
- );
const revived = await store.enqueue({
destination: { type: "slack", target: "C-restored" },
diff --git a/test/postgres-delivery-store.test.ts b/test/postgres-delivery-store.test.ts
index 1f5a9328e..d60dedfa7 100644
--- a/test/postgres-delivery-store.test.ts
+++ b/test/postgres-delivery-store.test.ts
@@ -1,9 +1,6 @@
import { test, before } from "node:test";
import assert from "node:assert/strict";
-import {
- backfillDeliverySourceCronIdBatch,
- createPostgresDeliveryStore,
-} from "../src/delivery/postgres-delivery-store.ts";
+import { createPostgresDeliveryStore } from "../src/delivery/postgres-delivery-store.ts";
import {
exerciseDeliveryExpiry,
exerciseDeliveryExpiryRevive,
@@ -98,97 +95,3 @@ test("pg delivery store: undelivered rows expire after the TTL instead of cloggi
test("pg delivery store: once-ever keys revive after a TTL drop instead of deadlocking", { skip }, async () => {
await exerciseDeliveryExpiryRevive((opts) => createPostgresDeliveryStore(URL!, opts));
});
-
-test(
- "pg delivery store: source_cron_id is stamped at insert and the read path survives unstamped rows",
- { skip },
- async () => {
- const pg = (await import("pg")).default;
- const p = new pg.Pool({ connectionString: URL });
- try {
- const store = createPostgresDeliveryStore(URL!);
- const provenance = (cronId: string, sessionId: string) => ({
- trigger: "cron",
- surface: "cron",
- fireKey: `agent:main:cron:${cronId}`,
- sourceScopeId: "personal:U-carol",
- sourceThreadRef: `agent:main:cron:${cronId}`,
- sourceSessionId: sessionId,
- });
- const stamped = await store.enqueue({
- destination: { type: "principal", target: "U-alice" },
- text: "stamped at insert",
- idempotencyKey: "col:stamped",
- provenance: provenance("col-a", "run-1"),
- });
- const plain = await store.enqueue({
- destination: { type: "slack", target: "C-plain" },
- text: "no provenance at all",
- idempotencyKey: "col:plain",
- });
- const column = async (id: string) =>
- (await p.query("SELECT source_cron_id FROM deliveries WHERE id = $1", [id])).rows[0]!.source_cron_id;
- assert.equal(await column(stamped.id), "col-a");
- assert.equal(await column(plain.id), null);
-
- const unstamped = ["col:legacy-1", "col:legacy-2", "col:conversation"];
- await store.enqueue({
- destination: { type: "principal", target: "U-alice" },
- text: "pre-migration row",
- idempotencyKey: unstamped[0]!,
- provenance: provenance("col-a", "run-2"),
- });
- await store.enqueue({
- destination: { type: "principal", target: "U-alice" },
- text: "pre-migration row, legacy thread shape",
- idempotencyKey: unstamped[1]!,
- provenance: { ...provenance("col-b", "run-3"), sourceThreadRef: "cron:col-b:slot" },
- });
- await store.enqueue({
- destination: { type: "slack", target: "C-plain" },
- text: "pre-migration row from a conversation",
- idempotencyKey: unstamped[2]!,
- provenance: { ...provenance("col-a", "run-4"), sourceThreadRef: "ch:C1:thread" },
- });
- await p.query("UPDATE deliveries SET source_cron_id = NULL WHERE idempotency_key = ANY($1)", [unstamped]);
- const before = await store.sentRunCountsByCron(["col-a", "col-b"]);
- assert.equal(before.get("col-a"), 2, "an unstamped row still counts through the regex fallback");
- assert.equal(before.get("col-b"), 1);
-
- const q = async (text: string, params?: unknown[]) =>
- (await p.query(text, params)).rows as Record<string, unknown>[];
- const columns = async () =>
- (await q("SELECT idempotency_key, source_cron_id FROM deliveries WHERE idempotency_key = ANY($1)", [unstamped]))
- .map((r) => [r.idempotency_key, r.source_cron_id])
- .sort();
- let batches = 0;
- let updated = 0;
- for (
- let n = await backfillDeliverySourceCronIdBatch(q, 1);
- n > 0;
- n = await backfillDeliverySourceCronIdBatch(q, 1)
- ) {
- batches++;
- updated += n;
- }
- assert.equal(updated, 2, "the backfill touches exactly the unstamped cron rows");
- assert.equal(batches, 2, "the backfill works in bounded batches until nothing is left");
- assert.deepEqual(
- await columns(),
- [
- ["col:conversation", null],
- ["col:legacy-1", "col-a"],
- ["col:legacy-2", "col-b"],
- ],
- "the backfill derives the same values the insert path writes and leaves non-cron rows alone",
- );
- assert.deepEqual(
- await store.sentRunCountsByCron(["col-a", "col-b"]),
- before,
- "backfilled rows count identically",
- );
- } finally {
- await p.end();
- }
- },
-);
diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.ts
index 8a7074d12..e26a1ef79 100644
--- a/test/postgres-store.test.ts
+++ b/test/postgres-store.test.ts
@@ -12,7 +12,6 @@ import { scopeId, type Principal, type TurnRequest, type TurnResult } from "../s
import type { OrchestratorInput } from "../src/core/orchestrator.ts";
import { assertParticipantSessionParity } from "./support/participant-session-parity.ts";
import { assertSpendRollupParity } from "./support/spend-rollup-parity.ts";
-import { byScopeId, rollupsFromSummaries } from "./support/scope-rollup-oracle.ts";
const URL = process.env.DATABASE_URL;
const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the Postgres store tests";
@@ -1012,11 +1011,6 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",
"orgWide includes every scope",
);
- const previews = await s.lastUserMessages([a.id, b.id]);
- assert.equal(previews.get(a.id), "2", "lastUserMessages returns the newest user turn");
- 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, category: "conversation" });
assert.deepEqual(
conversations.map((r) => r.id).sort(),
@@ -1049,42 +1043,16 @@ test("pg scopeSessionSummaries: counts via aggregate, not per-transcript reads",
[webhook.id],
"other background page filters before pagination",
);
-
- const backgroundStats = await s.scopeSessionStats(team, false, "background", "cron");
- assert.equal(backgroundStats.total, 1);
- assert.equal(backgroundStats.byType.cron, 1);
- assert.deepEqual(backgroundStats.totalByCategory, { conversation: 2, background: 2, all: 4 });
});
-test("pg scopeCronGroups: one aggregated row per cron; cronId page filters one cron's fires", { skip }, async () => {
+test("pg scopeSessionSummaries: cronId page filters one cron's fires", { skip }, async () => {
const s = createPostgresSessionStore(URL!);
const team = scopeId("channel", "cron-groups");
const c1a = await s.getOrCreateByThread("cron:g1:slot1", "channel", team);
const c1b = await s.getOrCreateByThread("cron:g1:slot2", "channel", team);
- const c2 = await s.getOrCreateByThread("agent:main:cron:g2", "channel", team);
+ await s.getOrCreateByThread("agent:main:cron:g2", "channel", team);
await s.getOrCreateByThread("agent:main:webhook:gwh", "channel", team);
- const { lease } = await s.acquireLease(c1a.id);
- await s.append(lease!, { type: "user", payload: { text: "fire" }, scopeLabel: team });
- await s.append(lease!, { type: "assistant", payload: { text: "done" }, scopeLabel: team });
-
- const groups = await s.scopeCronGroups(team, false);
- assert.deepEqual(groups.map((g) => g.cronId).sort(), ["g1", "g2"], "one row per cron; webhook excluded");
- const g1 = groups.find((g) => g.cronId === "g1")!;
- assert.deepEqual([g1.sessions, g1.turns, g1.messages], [2, 1, 2]);
- const g2 = groups.find((g) => g.cronId === "g2")!;
- assert.deepEqual([g2.sessions, g2.turns, g2.messages], [1, 0, 0], "entry-less fire still counted");
- assert.equal(g2.lastActivity, c2.createdAt);
-
const fresh = await s.getOrCreateByThread("cron:g1:slot3", "channel", team);
- const regrouped = await s.scopeCronGroups(team, false);
- assert.equal(
- regrouped.find((g) => g.cronId === "g1")!.lastActivity,
- fresh.createdAt,
- "a fresh entry-less fire advances the group's last activity past older fires' entries",
- );
-
- const 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, cronId: "g1" });
assert.deepEqual(
@@ -1098,87 +1066,32 @@ test("pg scopeCronGroups: one aggregated row per cron; cronId page filters one c
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);
- assert.equal(stats.crons, 2, "distinct crons, not fires");
- assert.equal(stats.crons, regrouped.length, "tab count == listable cron rows (one classifier)");
- assert.equal(stats.byType.cron, 4, "byType still counts cron sessions (fires)");
- assert.equal(
- regrouped.reduce((n, g) => n + g.sessions, 0),
- stats.byType.cron,
- "every counted cron session is listed under some cron",
+test("pg scopeActivity: one row per scope with conversation, cron and other background counts", { skip }, async () => {
+ const nowRef = { v: 40_000_000_000 };
+ const t0 = nowRef.v;
+ const s = createPostgresSessionStore(URL!, { now: () => nowRef.v });
+ const a = scopeId("channel", "activity-a");
+ const b = scopeId("channel", "activity-b");
+ await s.getOrCreateByThread("activity:t1", "channel", a);
+ await s.getOrCreateByThread("agent:main:webhook:activity-w1", "channel", a);
+ nowRef.v = t0 + 50_000;
+ await s.getOrCreateByThread("agent:main:monitor:activity-m1", "channel", b);
+ nowRef.v = t0 + 300_000;
+ await s.getOrCreateByThread("cron:activity-c1:fire:abc", "channel", a);
+ await s.getOrCreateByThread("activity:t2", "channel", a);
+
+ const rows = (await s.scopeActivity()).filter((r) => [a, b].includes(r.scopeId));
+ assert.deepEqual(
+ rows.sort((x, y) => x.scopeId.localeCompare(y.scopeId)),
+ [
+ { scopeId: a, conversations: 2, crons: 1, background: 1, lastActivity: t0 + 300_000 },
+ { scopeId: b, conversations: 0, crons: 0, background: 1, lastActivity: t0 + 50_000 },
+ ],
);
- assert.equal((await s.scopeSessionStats(team, false, "conversation")).crons, 2, "crons ignores the active filter");
});
-test(
- "pg scopeSessionRollups: one aggregate row per scope matches the row-by-row summaries pass",
- { skip },
- async () => {
- const nowRef = { v: 40_000_000_000 };
- const t0 = nowRef.v;
- const s = createPostgresSessionStore(URL!, { now: () => nowRef.v });
- const a = scopeId("channel", "rollup-a");
- const b = scopeId("channel", "rollup-b");
- const c = scopeId("personal", "rollup-u9");
- const say = async (id: string, text: string) => {
- const { lease } = await s.acquireLease(id);
- await s.append(lease!, { type: "user", payload: { text }, scopeLabel: a });
- await s.releaseLease(lease!);
- };
- const conv1 = await s.getOrCreateByThread("rollup:t1", "channel", a);
- await s.getOrCreateByThread("agent:main:webhook:rollup-w1", "channel", a);
- nowRef.v = t0 + 50_000;
- await s.getOrCreateByThread("agent:main:monitor:rollup-m1", "channel", b);
- nowRef.v = t0 + 100_000;
- await say(conv1.id, "one");
- const conv3 = await s.getOrCreateByThread("rollup:t3", "channel", a);
- await say(conv3.id, "three");
- nowRef.v = t0 + 200_000;
- await s.getOrCreateByThread("rollup:t2", "channel", a);
- await s.getOrCreateByThread("rollup:dm9", "dm", c);
- nowRef.v = t0 + 300_000;
- const fire = await s.getOrCreateByThread("cron:rollup-c1:fire:abc", "channel", a);
- await say(fire.id, "fired");
-
- const orgWide = byScopeId(await s.scopeSessionRollups(a, true));
- assert.deepEqual(
- orgWide,
- 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));
- assert.deepEqual(mine, [
- {
- scopeId: a,
- sessions: 3,
- backgroundSessions: 2,
- lastActivity: t0 + 300_000,
- lastConversationActivity: t0 + 200_000,
- previewSessionId: [conv1.id, conv3.id].sort().at(-1)!,
- },
- {
- scopeId: b,
- sessions: 0,
- backgroundSessions: 1,
- lastActivity: t0 + 50_000,
- lastConversationActivity: 0,
- previewSessionId: null,
- },
- {
- scopeId: c,
- sessions: 1,
- backgroundSessions: 0,
- lastActivity: t0 + 200_000,
- lastConversationActivity: t0 + 200_000,
- previewSessionId: null,
- },
- ]);
- assert.deepEqual(await s.scopeSessionRollups(a, false), mine.slice(0, 1), "scope filter applies");
- assert.deepEqual(await s.scopeSessionRollups(scopeId("channel", "rollup-none"), false), []);
- },
-);
-
test(
"pg session origin columns: written at insert, and the store migration fills rows that predate them",
{ skip },
@@ -1223,14 +1136,10 @@ test(
.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"),
});
try {
assert.deepEqual(await columns(), shapes, "insert derives origin and origin_id from the thread ref");
const stored = await classify();
- assert.deepEqual(stored.groups, ["oc1", "oc2"]);
assert.deepEqual(stored.other, [
"agent:main:webhook:ocw",
"loop:ocl:fire:aaa",
@@ -1240,8 +1149,6 @@ test(
assert.deepEqual(stored.conversation, ["ch:C1:oc-thread"]);
assert.deepEqual(stored.cron, ["agent:main:cron:oc1", "cron:oc2:slot"]);
assert.deepEqual(stored.oc2, ["cron:oc2:slot"]);
- assert.equal(stored.stats.crons, 2);
- assert.equal(stored.cronStats.total, 1);
await q("UPDATE sessions SET origin = NULL, origin_id = NULL WHERE id = ANY($1)", [ids]);
await q("UPDATE sessions SET origin = 'conversation' WHERE id = ANY($1) AND thread_ref ~ '^(loop|swarm):'", [
@@ -1424,90 +1331,34 @@ test("pg appendMany maintains first/last user previews across batches", { skip }
});
test(
- "pg scopeSessionSummaries: legacy rows derive previews read-only, only for the requested page",
+ "pg scopeSessionSummaries: rows without stored previews fall back to the title and never read items",
{ 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 titled = await s.getOrCreateByThread("pvwLegacyTitled", "channel", scope);
+ const bare = await s.getOrCreateByThread("pvwLegacyBare", "channel", scope);
+ for (const session of [titled, bare]) {
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.append(lease!, { type: "user", payload: { text: "open" }, 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, body] of [
- [0, { text: "poisonedtail" }],
- [1, {}],
- ] as const) {
- await raw.query(
- "INSERT INTO items(tenant_id, session_id, seq, kind, text, body, created_at) VALUES (qm_tenant_id(),$1,$2,'UserMessage',$3,'{}',to_timestamp(0.001))",
- [junk.id, seq, "text" in body ? body.text : null],
- );
- }
- 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 });
+ await raw.query("UPDATE sessions SET first_user_preview = NULL, last_user_preview = NULL WHERE id = ANY($1)", [
+ [titled.id, bare.id],
+ ]);
+ await raw.query("UPDATE sessions SET title = 'Quarterly plan' WHERE id = $1", [titled.id]);
+ const rows = await s.scopeSessionSummaries(scope, false, { limit: 10 });
assert.deepEqual(
- page.map((r) => [r.firstMessage, r.lastMessage]),
+ rows.map((r) => [r.id, r.firstMessage, r.lastMessage]),
[
- ["open 3", "close 3"],
- ["open 2", "close 2"],
+ [bare.id, "", ""],
+ [titled.id, "Quarterly plan", ""],
],
- "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 body without text previews as empty",
- );
- 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",
+ "previews come only from stored session columns",
);
} finally {
await raw.end();
@@ -1582,17 +1433,26 @@ test("pg scopeSessionSummaries: the listing plan is an index walk with no per-ro
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" });
+ await s.scopeSessionSummaries(scope, false, {
+ limit: 50,
+ before,
+ category: "background",
+ origin: "other_background",
+ });
t.mock.restoreAll();
- assert.equal(captured.length, 2);
+ assert.equal(captured.length, 3);
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 indexes = [
+ "sessions_conversations_by_scope_activity",
+ "sessions_by_activity",
+ "sessions_other_background_by_scope_activity",
+ ];
+ for (const [query, index] of captured.map((c, i) => [c, indexes[i]!] 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|"Relation Name":"items"/, "no correlated subquery over items");
@@ -1882,9 +1742,6 @@ test(
assert.equal(row.firstMessage, "the real question", "overheard is not the first message");
assert.equal(row.lastMessage, "the real question", "overheard is not the last message");
- const stats = await s.scopeSessionStats(scope, false);
- assert.equal(stats.turns, 1, "stats turns excludes overheard");
-
const attributed = (await s.attributedTurns()).filter((t) => t.sessionId === a.id);
assert.equal(
attributed.reduce((n, t) => n + t.turns, 0),
@@ -2019,9 +1876,11 @@ test(
} finally {
await raw.end();
}
- const stats = await s.scopeSessionStats(scope, false);
- assert.equal(stats.total, 2, "stats count sessions in scope");
- assert.equal(stats.turns, 3 + 7, "stats sum the stored turns counters, never the entries");
+ assert.equal(
+ (await s.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === b.id)!.turns,
+ 7,
+ "listings read the stored turns counter, never the entries",
+ );
},
);
diff --git a/test/session-store.test.ts b/test/session-store.test.ts
index 4baf85271..0198da402 100644
--- a/test/session-store.test.ts
+++ b/test/session-store.test.ts
@@ -7,7 +7,6 @@ import { parseSessionWakeRef } from "../src/api/routes/admin/origins.ts";
import { scopeId } from "../src/types.ts";
import { assertParticipantSessionParity } from "./support/participant-session-parity.ts";
import { assertSpendRollupParity } from "./support/spend-rollup-parity.ts";
-import { byScopeId, rollupsFromSummaries } from "./support/scope-rollup-oracle.ts";
test("sessionOrigin classifies trigger threads by prefix", () => {
assert.equal(sessionOrigin("agent:main:cron:abc"), "cron");
@@ -227,30 +226,21 @@ for (const [name, make] of backends) {
assert.equal(byThreadOrigin.get(cron!.id), "cron");
});
- test(`${name}: scopeSessionStats rolls up totals/turns/byType across the whole scope`, async () => {
+ test(`${name}: scopeActivity counts conversations, cron fires and other background per scope`, async () => {
const store = make();
const scope = scopeId("personal", "U1");
const dm = await store.getOrCreateByThread("dm:D1", "dm", scope);
await store.getOrCreateByThread("agent:main:cron:c1", "dm", scope);
+ await store.getOrCreateByThread("cron:c1:slot2", "dm", scope);
+ await store.getOrCreateByThread("agent:main:webhook:wh1", "dm", scope);
+ await store.getOrCreateByThread("dm:D9", "dm", scopeId("personal", "U9"));
const { lease } = await store.acquireLease(dm.id);
- assert.ok(lease);
- await store.append(lease, { type: "user", payload: { text: "hi" }, scopeLabel: scope });
- await store.append(lease, { type: "user", payload: { text: "again" }, scopeLabel: scope });
- await store.releaseLease(lease);
+ await store.append(lease!, { type: "user", payload: { text: "hi" }, scopeLabel: scope });
+ await store.releaseLease(lease!);
- const stats = await store.scopeSessionStats(scope, false);
- assert.equal(stats.total, 2, "both sessions counted");
- assert.equal(stats.turns, 2, "two user entries across the scope");
- assert.equal(stats.byType.dm, 1, "the conversation buckets by channel type");
- assert.equal(stats.byType.cron, 1, "the trigger thread buckets by origin");
- assert.deepEqual(stats.totalByCategory, { conversation: 1, background: 1, all: 2 });
-
- const conversationStats = await store.scopeSessionStats(scope, false, "conversation");
- assert.equal(conversationStats.total, 1);
- assert.deepEqual(conversationStats.totalByCategory, { conversation: 1, background: 1, all: 2 });
- const backgroundStats = await store.scopeSessionStats(scope, false, "background");
- assert.equal(backgroundStats.total, 1);
- assert.equal(backgroundStats.byType.cron, 1);
+ const rows = new Map((await store.scopeActivity()).map((r) => [r.scopeId, r]));
+ assert.deepEqual([rows.get(scope)?.conversations, rows.get(scope)?.crons, rows.get(scope)?.background], [1, 2, 1]);
+ assert.equal(rows.get(scopeId("personal", "U9"))?.conversations, 1, "every scope gets its own row");
});
test(`${name}: overheard catch-up entries are NOT counted as turns or as the first/last message`, async () => {
@@ -274,9 +264,6 @@ for (const [name, make] of backends) {
});
await store.releaseLease(lease);
- 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, { limit: 500 });
const row = rows.find((r) => r.id === s.id)!;
assert.equal(row.turns, 1, "summary turns excludes overheard");
@@ -290,14 +277,6 @@ for (const [name, make] of backends) {
1,
"retention/usage counts only the real user turn",
);
-
- const previews = await store.lastUserMessages([s.id]);
- assert.equal(
- previews.get(s.id),
- "the real question",
- "lastUserMessages returns the latest real user turn, skipping overheard",
- );
- assert.equal((await store.lastUserMessages([])).size, 0, "empty id set → empty map");
});
test(`${name}: scopeSessionSummaries paginates newest-activity-first`, async () => {
@@ -377,54 +356,16 @@ for (const [name, make] of backends) {
otherRows.map((r) => r.id),
[webhook.id],
);
-
- const cronStats = await store.scopeSessionStats(scope, false, "background", "cron");
- assert.equal(cronStats.total, 1);
- assert.equal(cronStats.byType.cron, 1);
- assert.deepEqual(cronStats.totalByCategory, { conversation: 0, background: 2, all: 2 });
-
- const otherStats = await store.scopeSessionStats(scope, false, "background", "other_background");
- assert.equal(otherStats.total, 1);
- assert.equal(otherStats.byType.webhook, 1);
});
- test(`${name}: scopeCronGroups rolls up one row per cron; cronId page filters one cron's fires`, async () => {
+ test(`${name}: cronId page filters one cron's fires`, async () => {
const store = make();
const scope = scopeId("channel", "C1");
const c1a = await store.getOrCreateByThread("cron:c1:slot1", "channel", scope);
const c1b = await store.getOrCreateByThread("cron:c1:slot2", "channel", scope);
- const c2 = await store.getOrCreateByThread("agent:main:cron:c2", "channel", scope);
+ await store.getOrCreateByThread("agent:main:cron:c2", "channel", scope);
await store.getOrCreateByThread("agent:main:webhook:wh1", "channel", scope);
- await store.getOrCreateByThread("dm:D9", "dm", scopeId("personal", "U9"));
- const { lease } = await store.acquireLease(c1a.id);
- await store.append(lease!, { type: "user", payload: { text: "fire" }, scopeLabel: scope });
- await store.append(lease!, { type: "assistant", payload: { text: "done" }, scopeLabel: scope });
-
- const groups = await store.scopeCronGroups(scope, false);
- assert.deepEqual(
- groups.map((g) => g.cronId).sort(),
- ["c1", "c2"],
- "one row per cron, webhook and conversations excluded",
- );
- const g1 = groups.find((g) => g.cronId === "c1")!;
- assert.equal(g1.sessions, 2, "counts every fire session of the cron");
- assert.equal(g1.turns, 1);
- assert.equal(g1.messages, 2);
- assert.equal(g1.scopeId, scope);
- const g2 = groups.find((g) => g.cronId === "c2")!;
- assert.deepEqual([g2.sessions, g2.turns, g2.messages], [1, 0, 0], "entry-less fire still counted");
- assert.equal(g2.lastActivity, c2.createdAt);
-
const fresh = await store.getOrCreateByThread("cron:c1:slot3", "channel", scope);
- const regrouped = await store.scopeCronGroups(scope, false);
- assert.equal(
- regrouped.find((g) => g.cronId === "c1")!.lastActivity,
- fresh.createdAt,
- "a fresh entry-less fire advances the group's last activity past older fires' entries",
- );
-
- 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, cronId: "c1" });
assert.deepEqual(
@@ -438,73 +379,6 @@ for (const [name, make] of backends) {
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");
- });
-
- test(`${name}: scopeSessionRollups aggregates per scope what the scope index used to count row by row`, async () => {
- const nowRef = { v: 30_000_000_000 };
- const t0 = nowRef.v;
- const store = createMemorySessionStore({ now: () => nowRef.v });
- const a = scopeId("channel", "A");
- const b = scopeId("channel", "B");
- const c = scopeId("personal", "U9");
- const say = async (id: string, text: string) => {
- const { lease } = await store.acquireLease(id);
- await store.append(lease!, { type: "user", payload: { text }, scopeLabel: a });
- await store.releaseLease(lease!);
- };
- const conv1 = await store.getOrCreateByThread("ch:A:t1", "channel", a);
- await store.getOrCreateByThread("agent:main:webhook:w1", "channel", a);
- nowRef.v = t0 + 50_000;
- await store.getOrCreateByThread("agent:main:monitor:m1", "channel", b);
- nowRef.v = t0 + 100_000;
- await say(conv1.id, "one");
- const conv3 = await store.getOrCreateByThread("ch:A:t3", "channel", a);
- await say(conv3.id, "three");
- nowRef.v = t0 + 200_000;
- await store.getOrCreateByThread("ch:A:t2", "channel", a);
- await store.getOrCreateByThread("dm:D9", "dm", c);
- nowRef.v = t0 + 300_000;
- const fire = await store.getOrCreateByThread("cron:c1:fire:abc", "channel", a);
- await say(fire.id, "fired");
-
- const rollups = byScopeId(await store.scopeSessionRollups(a, true));
- assert.deepEqual(rollups, [
- {
- scopeId: a,
- sessions: 3,
- backgroundSessions: 2,
- lastActivity: t0 + 300_000,
- lastConversationActivity: t0 + 200_000,
- previewSessionId: [conv1.id, conv3.id].sort().at(-1)!,
- },
- {
- scopeId: b,
- sessions: 0,
- backgroundSessions: 1,
- lastActivity: t0 + 50_000,
- lastConversationActivity: 0,
- previewSessionId: null,
- },
- {
- scopeId: c,
- sessions: 1,
- backgroundSessions: 0,
- lastActivity: t0 + 200_000,
- lastConversationActivity: t0 + 200_000,
- previewSessionId: null,
- },
- ]);
- assert.deepEqual(
- rollups,
- rollupsFromSummaries(await store.scopeSessionSummaries(a, true, { limit: 500 })),
- "the aggregate matches a row-by-row pass over the summaries",
- );
- assert.deepEqual(
- await store.scopeSessionRollups(b, false),
- rollups.filter((r) => r.scopeId === b),
- "scope filter applies",
- );
});
test(`${name}: forceReleaseLease drops a held lease so a fresh acquire succeeds (reaper path)`, async () => {
@@ -960,33 +834,6 @@ for (const [name, make] of backends) {
assert.equal((await store.listByParticipant("U1"))[0]?.title, "My project chat");
});
- test(`${name}: stats' distinct-cron count always equals the rows scopeCronGroups lists`, async () => {
- const store = make();
- const scope = scopeId("channel", "C1");
- await store.getOrCreateByThread("dm:D1", "dm", scope);
- await store.getOrCreateByThread("agent:main:cron:stable1", "dm", scope);
- await store.getOrCreateByThread("cron:legacy1:slot1", "dm", scope);
- await store.getOrCreateByThread("cron:legacy1:slot2", "dm", scope);
- await store.getOrCreateByThread("cron:legacy2", "dm", scope);
- await store.getOrCreateByThread("agent:main:webhook:wh1", "dm", scope);
-
- const stats = await store.scopeSessionStats(scope, false);
- const groups = await store.scopeCronGroups(scope, false);
- assert.equal(stats.crons, 3, "distinct crons, not fires");
- assert.equal(stats.crons, groups.length, "tab count == listable rows (one classifier)");
- assert.equal(stats.byType.cron, 4, "byType still counts cron sessions (fires)");
- assert.equal(
- groups.reduce((n, g) => n + g.sessions, 0),
- stats.byType.cron,
- "every counted cron session is listed under some cron",
- );
- assert.equal(
- (await store.scopeSessionStats(scope, false, "conversation")).crons,
- 3,
- "crons ignores the active filter",
- );
- });
-
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 });
diff --git a/test/support/scope-rollup-oracle.ts b/test/support/scope-rollup-oracle.ts
deleted file mode 100644
index 9b27c15aa..000000000
--- a/test/support/scope-rollup-oracle.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { sessionCategory, type ScopeSessionRollup, type SessionSummary } from "../../src/sessions/session-store.ts";
-
-export function rollupsFromSummaries(summaries: SessionSummary[]): ScopeSessionRollup[] {
- const out = new Map<string, ScopeSessionRollup>();
- const previewAt = new Map<string, number>();
- for (const s of summaries) {
- const r = out.get(s.scopeId) ?? {
- scopeId: s.scopeId,
- sessions: 0,
- backgroundSessions: 0,
- lastActivity: 0,
- lastConversationActivity: 0,
- previewSessionId: null,
- };
- out.set(s.scopeId, r);
- r.lastActivity = Math.max(r.lastActivity, s.lastActivity);
- if (sessionCategory(s.origin) === "background") {
- r.backgroundSessions += 1;
- continue;
- }
- r.sessions += 1;
- r.lastConversationActivity = Math.max(r.lastConversationActivity, s.lastActivity);
- if (s.turns > 0 && (!previewAt.has(s.scopeId) || s.lastActivity > previewAt.get(s.scopeId)!)) {
- previewAt.set(s.scopeId, s.lastActivity);
- r.previewSessionId = s.id;
- }
- }
- return [...out.values()].sort((a, b) => a.scopeId.localeCompare(b.scopeId));
-}
-
-export const byScopeId = (rollups: ScopeSessionRollup[]): ScopeSessionRollup[] =>
- [...rollups].sort((a, b) => a.scopeId.localeCompare(b.scopeId));