Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: track agent credential usage
Richie McIlroyc8061de287bd
diff --git a/apps/web/__tests__/unit/agent-auth.test.ts b/apps/web/__tests__/unit/agent-auth.test.tsindex 9421241c5..402a2364b 100644--- a/apps/web/__tests__/unit/agent-auth.test.ts+++ b/apps/web/__tests__/unit/agent-auth.test.ts@@ -1,5 +1,8 @@import { createHash } from "node:crypto";-import { isLegacyAgentKeySource } from "@cap/web-backend";+import {+ isLegacyAgentKeySource,+ shouldRefreshAgentLastUsedAt,+} from "@cap/web-backend";import { describe, expect, it } from "vitest";import {buildAgentCallbackUrl,@@ -155,3 +158,16 @@ describe("legacy agent credentials", () => {expect(isLegacyAgentKeySource("extension")).toBe(false);});});++describe("agent credential usage tracking", () => {+ it("refreshes missing and stale usage timestamps", () => {+ const now = new Date("2026-07-19T00:00:00.000Z");+ expect(shouldRefreshAgentLastUsedAt(null, now)).toBe(true);+ expect(+ shouldRefreshAgentLastUsedAt(new Date("2026-07-18T23:55:00.000Z"), now),+ ).toBe(true);+ expect(+ shouldRefreshAgentLastUsedAt(new Date("2026-07-18T23:55:00.001Z"), now),+ ).toBe(false);+ });+});diff --git a/packages/web-backend/src/AgentAuth.ts b/packages/web-backend/src/AgentAuth.tsindex bb152e0b7..4a3c79a29 100644--- a/packages/web-backend/src/AgentAuth.ts+++ b/packages/web-backend/src/AgentAuth.ts@@ -2,7 +2,7 @@ import { createHash } from "node:crypto";import * as Db from "@cap/database/schema";import { Agent } from "@cap/web-domain";import { HttpServerRequest } from "@effect/platform";-import { eq } from "drizzle-orm";+import { and, eq, isNull, lte, or } from "drizzle-orm";import { Effect, Layer, Schema } from "effect";import { Database } from "./Database.ts";@@ -45,6 +45,15 @@ const parseBearerToken = (authorization: string | undefined) => {const hashToken = (token: string) =>createHash("sha256").update(token).digest("hex");+const agentLastUsedRefreshMs = 5 * 60 * 1000;++export const shouldRefreshAgentLastUsedAt = (+ lastUsedAt: Date | null,+ now: Date,+) =>+ lastUsedAt === null ||+ now.getTime() - lastUsedAt.getTime() >= agentLastUsedRefreshMs;+const agentScopes = new Set<Agent.AgentScope>(["caps:read","caps:comment",@@ -118,6 +127,7 @@ export const AgentHttpAuthMiddlewareLive = Layer.effect(scopes: Db.agentApiKeys.scopes,expiresAt: Db.agentApiKeys.expiresAt,revokedAt: Db.agentApiKeys.revokedAt,+ lastUsedAt: Db.agentApiKeys.lastUsedAt,id: Db.users.id,email: Db.users.email,activeOrganizationId: Db.users.activeOrganizationId,@@ -136,6 +146,28 @@ export const AgentHttpAuthMiddlewareLive = Layer.effect(if (!isAgentReadAccessEnabled(row.email)) {return yield* temporarilyUnavailable();}+ const now = new Date();+ if (shouldRefreshAgentLastUsedAt(row.lastUsedAt, now)) {+ yield* database+ .use((db) =>+ db+ .update(Db.agentApiKeys)+ .set({ lastUsedAt: now })+ .where(+ and(+ eq(Db.agentApiKeys.id, row.tokenId),+ or(+ isNull(Db.agentApiKeys.lastUsedAt),+ lte(+ Db.agentApiKeys.lastUsedAt,+ new Date(now.getTime() - agentLastUsedRefreshMs),+ ),+ ),+ ),+ ),+ )+ .pipe(Effect.catchAll(() => Effect.void));+ }return Agent.AgentPrincipal.of({id: row.id,email: row.email,