Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(notifications): keep workspace spend alerts admin-only
Sebastian B9af85471eb56
diff --git a/server/client-notifications.e2e.test.ts b/server/client-notifications.e2e.test.tsindex c27cfc7e3a..11dab2fb58 100644--- a/server/client-notifications.e2e.test.ts+++ b/server/client-notifications.e2e.test.ts@@ -26,6 +26,7 @@ it("lets a Team Cloud member list and read only their own notification feed", asfor (const [id, kind, threadId, person] of [["own-a", "done", bot.mainThreadId, null], ["own-b", "done", bot.mainThreadId, null],["private", "done", opened.body.task.threadId, null],+ ["spend", "spend", bot.mainThreadId, null],["owners-ask", "question", bot.mainThreadId, null], ["targeted", "done", bot.mainThreadId, ada],] as const) insert.run(id, Date.now(), kind, bot.id, threadId, person);@@ -33,12 +34,12 @@ it("lets a Team Cloud member list and read only their own notification feed", asexpect(listed.status).toBe(200);expect(listed.body.notifications.map((entry: { id: string }) => entry.id).sort()).toEqual(["own-a", "own-b"]);expect((await team.as(member.token!, "POST", "/api/notifications/own-a/read")).status).toBe(200);- for (const id of ["private", "owners-ask", "targeted"]) {+ for (const id of ["private", "owners-ask", "targeted", "spend"]) {expect((await team.as(member.token!, "POST", `/api/notifications/${id}/read`)).status, id).toBe(404);}expect((await team.as(member.token!, "POST", "/api/notifications/read-all")).status).toBe(200);expect(db.prepare("SELECT id FROM notification_log WHERE read = 1 ORDER BY id").all().map((row) => row.id)).toEqual(["own-a", "own-b"]);- expect(db.prepare("SELECT id FROM notification_log WHERE read = 0 ORDER BY id").all().map((row) => row.id)).toEqual(["owners-ask", "private", "targeted"]);+ expect(db.prepare("SELECT id FROM notification_log WHERE read = 0 ORDER BY id").all().map((row) => row.id)).toEqual(["owners-ask", "private", "spend", "targeted"]);} finally {db.close();}diff --git a/server/routes/notifications-admin.test.ts b/server/routes/notifications-admin.test.tsnew file mode 100644index 0000000000..e79ac22d03--- /dev/null+++ b/server/routes/notifications-admin.test.ts@@ -0,0 +1,82 @@+import { mkdirSync, rmSync } from "node:fs";+import { createServer, type Server } from "node:http";+import type { AddressInfo } from "node:net";+import { afterEach, beforeEach, describe, expect, it } from "vitest";++import type { NotificationLogEntry } from "../../shared/notification.ts";+import { DATA_DIR } from "../config.ts";+import { json, readBody } from "../harness/http.ts";+import { closeMessageDb, insertNotificationLog, recentNotificationLog } from "../message-db.ts";+import type { RequestAuth } from "../request-auth.ts";+import type { SessionRecord } from "../sessions.ts";+import { createNotificationRoutes } from "./notifications.ts";+import { dispatchRoutes } from "./table.ts";++const session: SessionRecord = { id: "fixture", tokenHash: "0".repeat(64), label: "Fixture", scopes: ["client"],+ createdAt: 1, lastSeenAt: 1, expiresAt: Number.MAX_SAFE_INTEGER };+const client: RequestAuth = { kind: "session", session, via: "bearer", scopes: ["client"] };+const callers: Array<{ label: string; auth: RequestAuth; admin: boolean }> = [+ { label: "Team Cloud member", auth: { ...client, session: { ...session, email: "[email protected]",+ membershipAuthority: "team", teamRole: "member" } }, admin: false },+ { label: "guest", auth: { ...client, via: "cookie" }, admin: false },+ { label: "paired client", auth: client, admin: false },+ { label: "service loopback", auth: { kind: "loopback", trust: "service", scopes: ["client"] }, admin: false },+ { label: "owner loopback", auth: { kind: "loopback", scopes: ["admin", "client"] }, admin: true },+ { label: "admin session", auth: { ...client, session: { ...session, scopes: ["admin", "client"] },+ scopes: ["admin", "client"] }, admin: true },+];+const sample: NotificationLogEntry = { id: "done", at: 1, kind: "done", botId: "scout", botName: "Scout",+ threadId: "shared", title: "Done", body: "", read: false };+let server: Server | undefined;++beforeEach(() => {+ closeMessageDb();+ rmSync(DATA_DIR, { recursive: true, force: true });+ mkdirSync(DATA_DIR, { recursive: true });+ for (const entry of [sample, { ...sample, id: "spend", kind: "spend" as const, body: "$12 of $20 spent this month" },+ { ...sample, id: "workspace-spend", kind: "spend" as const, threadId: "" }]) insertNotificationLog(entry);+});+afterEach(async () => {+ if (server) await new Promise((done) => server!.close(done));+ server = undefined;+});++async function serve(auth: RequestAuth) {+ const routes = [createNotificationRoutes()];+ server = createServer(async (req, res) => {+ const url = new URL(req.url ?? "/", "http://localhost");+ if (!await dispatchRoutes(routes, { req, res, url, path: url.pathname, method: req.method ?? "GET", auth, json, readBody })) {+ json(res, 404, { error: "no such route" });+ }+ });+ await new Promise<void>((ready) => server!.listen(0, "127.0.0.1", ready));+ return `http://127.0.0.1:${(server.address() as AddressInfo).port}/api/notifications`;+}++describe.each(callers)("$label notification feed", ({ auth, admin }) => {+ it("lists spend alerts only for admins, including alerts in visible shared conversations", async () => {+ const url = await serve(auth);+ const res = await fetch(url);+ expect(res.status).toBe(200);+ const { notifications } = await res.json() as { notifications: NotificationLogEntry[] };+ expect(notifications.map((entry) => entry.id).sort()).toEqual(admin ? ["done", "spend", "workspace-spend"] : ["done"]);+ });++ it("allows only admins to mark a spend alert read", async () => {+ const url = await serve(auth);+ for (const id of ["spend", "workspace-spend"]) {+ expect((await fetch(`${url}/${id}/read`, { method: "POST" })).status, id).toBe(admin ? 200 : 404);+ }+ expect(recentNotificationLog().filter((entry) => entry.read).map((entry) => entry.id).sort())+ .toEqual(admin ? ["spend", "workspace-spend"] : []);+ });++ it("preserves admin-only unread alerts when a non-admin reads all", async () => {+ const url = await serve(auth);+ expect((await fetch(`${url}/read-all`, { method: "POST" })).status).toBe(200);+ expect(recentNotificationLog().filter((entry) => entry.read).map((entry) => entry.id).sort())+ .toEqual(admin ? ["done", "spend", "workspace-spend"] : ["done"]);+ expect(recentNotificationLog().filter((entry) => !entry.read).map((entry) => entry.id).sort())+ .toEqual(admin ? [] : ["spend", "workspace-spend"]);+ });+});diff --git a/server/routes/notifications.ts b/server/routes/notifications.tsindex 69f62cd0e1..8bfee874f0 100644--- a/server/routes/notifications.ts+++ b/server/routes/notifications.ts@@ -1,6 +1,6 @@import { markNotificationLogRead, markThreadNotificationLogRead, recentNotificationLog } from "../message-db.ts";import type { NotificationLogEntry } from "../../shared/notification.ts";-import type { RequestAuth } from "../request-auth.ts";+import { ownerOrAdmin, type RequestAuth } from "../request-auth.ts";import { PASS, type RouteHandler } from "./table.ts";/** Opening a conversation reads its notifications, so every open app's badge drops them too. */@@ -17,7 +17,8 @@ export function createNotificationRoutes(deps = {reaches: (auth: RequestAuth, entry: NotificationLogEntry) => boolean = () => true): RouteHandler {const visible = (auth: RequestAuth) => {const seen = shown(auth);- return deps.recent().filter((entry) => (!entry.threadId || seen(entry.threadId)) && reaches(auth, entry));+ const admin = ownerOrAdmin(auth);+ return deps.recent().filter((entry) => (entry.kind !== "spend" || admin) && (!entry.threadId || seen(entry.threadId)) && reaches(auth, entry));};return async ({ res, path, method, auth, json }) => {if (method === "GET" && path === "/api/notifications") {