Skip to content

Commit 44d6cab

Browse files
Brooooooklynclaude
andcommitted
Publish AI logging settings from the API, with plan defaults and readback
The API is now the writer of the ROUTING KV key the proxy reads. It no longer needs PLATFORM_AUTH_DATABASE_UNCACHED for AI logging. - publishProjectAiLogging locks the project row, then reads every input through a write statement (so cached Hyperdrive reads never feed a publish), and writes KV before the lock is released. A later publisher always writes last. - The operator POST orders the write by direction: a tighten writes in the same transaction and rolls back on a KV failure; a loosen, or any change with inherit, commits first and publishes in a second locked step. Failures are ai_logging_publish_failed / ai_logging_publish_pending. - Plan defaults live in platform_settings 'plans:ai-logging', edited through the plan routes and dashboard, and published in pages of 100. A user plan change publishes the account's projects one page per call; transfer, project create and delete keep the key in step. - Readback (project show, GET .../ai-logging, dashboard) returns the saved mode plus plan, effective mode, source, published value and a state: applied, pending, pending_plan_publish, drift or unknown. - An hourly sweep repairs drifted keys within a 400 KV operation budget. - The internal route 1.8.x proxies call is kept and answers from KV. Refs #793. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 25db0c8 commit 44d6cab

40 files changed

Lines changed: 4362 additions & 153 deletions

‎platform/packages/api/package.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"./routing": "./src/routing.ts",
1111
"./isr-store": "./src/isr-store.ts",
1212
"./usage-auth": "./src/usage-auth.ts",
13-
"./proxy-token": "./src/proxy-token.ts",
14-
"./ai-logging-policy": "./src/ai-logging-policy.ts"
13+
"./proxy-token": "./src/proxy-token.ts"
1514
},
1615
"scripts": {
1716
"db:generate": "drizzle-kit generate",

‎platform/packages/api/src/admin/fate/page-data.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import type { Db } from '../../db.ts';
2727
import type { ApiEnv } from '../../env.ts';
2828
import { OperatorError } from '../../operator/common.ts';
2929
import { cliVersionStats } from '../../operator/system-operations.ts';
30-
import { readPlanCatalog, readPlanCatalogWithAccounts, planFromCatalog } from '../../plans.ts';
30+
import {
31+
initialPlanDefaultFor,
32+
readPlanCatalog,
33+
readPlanCatalogWithAccounts,
34+
planFromCatalog,
35+
} from '../../plans.ts';
3136
import { getPlatformApiUrl, platformConnectCommand } from '../../platform-identity.ts';
3237
import { projectUrl, projectWithUrl } from '../../project-url.ts';
3338
import { invitations, platformLoginTests } from '../../schema.ts';
@@ -147,11 +152,17 @@ export async function loadUserDetailPage(context: AdminContext, path: string) {
147152
const { id } = detailLocation(path, '/admin/users/');
148153
const { getOperatorUser } = await import('../../operator/tenant-service.ts');
149154
const detail = await getOperatorUser(context, id);
150-
const catalog = await readPlanCatalog(context.get('db'));
155+
const { readPlanAiLoggingSettings } = await import('../../ai-logging-plans.ts');
156+
const { planAiLogging } = await import('../../ai-logging-policy.ts');
157+
const [catalog, aiLoggingSettings] = await Promise.all([
158+
readPlanCatalog(context.get('db'), initialPlanDefaultFor(context.env)),
159+
readPlanAiLoggingSettings(context.get('db')),
160+
]);
151161
return {
152162
id: path,
153163
...detail,
154164
limits: planFromCatalog(catalog, detail.user.plan).limits,
165+
aiLogging: planAiLogging(aiLoggingSettings, catalog, detail.user.plan),
155166
plans: catalog.plans
156167
.filter((plan) => !plan.archived || plan.id === detail.user.plan)
157168
.map((plan) => ({ id: plan.id, name: plan.name, archived: plan.archived })),
@@ -198,7 +209,7 @@ export async function loadProjectDetailPage(context: AdminContext, path: string)
198209
const { getOperatorProject } = await import('../../operator/tenant-service.ts');
199210
const db = context.get('db');
200211
const [detail, members, invitations, zeroTrust] = await Promise.all([
201-
getOperatorProject(db, id),
212+
getOperatorProject(db, context.env, id),
202213
getProjectMembers(db, id),
203214
getPendingProjectInvitations(db, id),
204215
loadProjectZeroTrust(db, id),
@@ -497,13 +508,15 @@ export async function loadPlansPage(context: AdminContext, path: string) {
497508
if (url.pathname !== home && url.pathname !== home + '/new' && !match) {
498509
throw new OperatorError(400, 'Invalid plans page request.');
499510
}
500-
const data = await readPlanCatalogWithAccounts(
501-
context.get('db'),
502-
context.env.PLATFORM_INSTALLATION_ID ? 'custom' : 'free',
503-
);
511+
const initialPlanDefault = initialPlanDefaultFor(context.env);
512+
const { readPlanAiLoggingProgress } = await import('../../ai-logging-plans.ts');
513+
const [data, aiLogging] = await Promise.all([
514+
readPlanCatalogWithAccounts(context.get('db'), initialPlanDefault),
515+
readPlanAiLoggingProgress(context.get('db'), initialPlanDefault),
516+
]);
504517
const selectedPlanId = match?.[1] ?? null;
505518
if (selectedPlanId && !data.catalog.plans.some((plan) => plan.id === selectedPlanId)) {
506519
throw new OperatorError(404, 'Plan not found.');
507520
}
508-
return { id: path, ...data, selectedPlanId };
521+
return { id: path, ...data, aiLogging, selectedPlanId };
509522
}

‎platform/packages/api/src/admin/fate/page-server.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const plansDataView = dataView<PlansRecord>('AdminPlans')({
3434
id: true,
3535
catalog: true,
3636
accounts: true,
37+
aiLogging: true,
3738
selectedPlanId: true,
3839
});
3940
export type AdminPlans = Entity<typeof plansDataView, 'AdminPlans'>;
@@ -84,6 +85,7 @@ export const deploymentsPageDataView = dataView<DeploymentsPageRecord>('AdminDep
8485
});
8586
export const userDetailDataView = dataView<UserDetailRecord>('AdminUserDetail')({
8687
limits: true,
88+
aiLogging: true,
8789
plans: true,
8890
id: true,
8991
user: true,
@@ -103,6 +105,7 @@ export const projectDetailDataView = dataView<ProjectDetailRecord>('AdminProject
103105
domains: true,
104106
builds: true,
105107
zeroTrust: true,
108+
aiLogging: true,
106109
cfAccountId: true,
107110
deployUrl: true,
108111
tz: true,

‎platform/packages/api/src/admin/fate/views.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export const deploymentsPageRequest = (path: string) =>
121121

122122
export const UserDetailView = view<AdminUserDetail>()({
123123
limits: true,
124+
aiLogging: true,
124125
plans: true,
125126
id: true,
126127
user: true,
@@ -140,6 +141,7 @@ export const ProjectDetailView = view<AdminProjectDetail>()({
140141
domains: true,
141142
builds: true,
142143
zeroTrust: true,
144+
aiLogging: true,
143145
cfAccountId: true,
144146
deployUrl: true,
145147
tz: true,
@@ -320,6 +322,7 @@ export const PlansView = view<AdminPlans>()({
320322
id: true,
321323
catalog: true,
322324
accounts: true,
325+
aiLogging: true,
323326
selectedPlanId: true,
324327
});
325328
export const plansRequest = (path: string) =>

‎platform/packages/api/src/admin/plans.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PlatformPlanCatalog } from '@void/platform/plans';
2+
import type { PlanAiLoggingSettings } from '../ai-logging-policy.ts';
23
import type { PlanCatalogAction } from '../operator/plans.ts';
34

45
export type AdminPlanPreview = {
@@ -8,16 +9,26 @@ export type AdminPlanPreview = {
89
expectedRevision: number;
910
before: PlatformPlanCatalog;
1011
after: PlatformPlanCatalog;
12+
/** Plan AI logging defaults before and after the change. */
13+
aiLoggingBefore: PlanAiLoggingSettings['modes'];
14+
aiLoggingAfter: PlanAiLoggingSettings['modes'];
1115
affectedAccounts: number;
1216
warnings: Array<string>;
1317
overLimitAccounts: Array<{ id: string; login: string; reasons: Array<string> }>;
1418
usageSampleSize: number;
1519
};
1620

21+
/** Progress of account updates and the AI logging publish, combined. */
1722
export type AdminPlanProgress = {
1823
revision: number;
1924
complete: boolean;
25+
/** Accounts plus projects written by this step; zero means it made no progress. */
2026
processed: number;
27+
/** Accounts whose limits were updated by this step. */
28+
accounts: number;
29+
/** Projects whose AI logging setting was published by this step. */
30+
projects: number;
31+
/** Changes whenever either step moves, so the page can tell it is not stuck. */
2132
cursor: string | null;
2233
error: string | null;
2334
};

‎platform/packages/api/src/admin/react/views/PlansPage.tsx‎

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { Link } from '@void/react';
2020
import { useEffect, useState } from 'react';
2121
import { useRequest, useView } from 'react-fate';
22+
import type { AiLoggingMode, PlanAiLoggingSettings } from '../../../ai-logging-policy.ts';
2223
import { PlansView, plansRequest } from '../../fate/views.ts';
2324
import type { AdminPlanPreview, AdminPlanProgress } from '../../plans.ts';
2425
import type { AdminPageProps } from '../page-utils.ts';
@@ -65,6 +66,15 @@ function limit(value: number, key?: EditablePlanLimit) {
6566
: 'Unlimited'
6667
: value.toLocaleString('en-US');
6768
}
69+
const aiLoggingLabels: Record<AiLoggingMode, string> = {
70+
inherit: 'Inherit (AI Gateway setting)',
71+
off: 'Off',
72+
metadata: 'Metadata only',
73+
full: 'Full (prompts and responses)',
74+
};
75+
function aiLoggingMode(modes: PlanAiLoggingSettings['modes'], planId: string): AiLoggingMode {
76+
return Object.hasOwn(modes, planId) ? modes[planId]! : 'inherit';
77+
}
6878
function usePlans(path: string) {
6979
const { plans: ref } = useRequest(plansRequest(path), { mode: 'stale-while-revalidate' });
7080
const data = useView(PlansView, ref);
@@ -122,8 +132,9 @@ function PlanAction({
122132

123133
export function PlansPage({ path, platformName, username }: AdminPageProps) {
124134
usePlanConfirmation(path);
125-
const { catalog, accounts } = usePlans(path);
135+
const { catalog, accounts, aiLogging } = usePlans(path);
126136
const pending = catalog.reconciliation && !catalog.reconciliation.complete;
137+
const publishPending = aiLogging.publish && !aiLogging.publish.complete;
127138
return (
128139
<Layout platformName={platformName} username={username} title="Plans">
129140
<SectionStack>
@@ -153,6 +164,20 @@ export function PlansPage({ path, platformName, username }: AdminPageProps) {
153164
</form>
154165
</SectionStack>
155166
)}
167+
{!pending && publishPending && (
168+
<SectionStack aria-label="Pending AI logging publish">
169+
<h2>AI logging publish pending</h2>
170+
<p>
171+
{aiLogging.remaining} projects still use their previous AI Gateway logging setting.
172+
Updates continue every hour, or continue them now.
173+
</p>
174+
{aiLogging.publish?.error && <p role="alert">{aiLogging.publish.error}</p>}
175+
<form method="post" action={`${HOME}/resume`}>
176+
<input type="hidden" name="revision" defaultValue={catalog.revision} />
177+
<Button type="submit" label="Continue AI logging publish" />
178+
</form>
179+
</SectionStack>
180+
)}
156181
<Table>
157182
<TableHeader>
158183
<TableRow>
@@ -161,6 +186,7 @@ export function PlansPage({ path, platformName, username }: AdminPageProps) {
161186
<TableHeaderCell>Accounts</TableHeaderCell>
162187
<TableHeaderCell>Monthly requests</TableHeaderCell>
163188
<TableHeaderCell>Build size</TableHeaderCell>
189+
<TableHeaderCell>AI Gateway logging</TableHeaderCell>
164190
<TableHeaderCell>Actions</TableHeaderCell>
165191
</TableRow>
166192
</TableHeader>
@@ -187,6 +213,7 @@ export function PlansPage({ path, platformName, username }: AdminPageProps) {
187213
<TableCell>
188214
{plan.buildInstanceType === 'standard-3' ? 'Standard' : 'Large'}
189215
</TableCell>
216+
<TableCell>{aiLoggingLabels[aiLoggingMode(aiLogging.modes, plan.id)]}</TableCell>
190217
<TableCell>
191218
<Button
192219
href={`${HOME}/${plan.id}/edit`}
@@ -205,7 +232,7 @@ export function PlansPage({ path, platformName, username }: AdminPageProps) {
205232

206233
export function PlanEditPage({ path, platformName, username }: AdminPageProps) {
207234
usePlanConfirmation(path);
208-
const { catalog, accounts, selectedPlanId } = usePlans(path);
235+
const { catalog, accounts, aiLogging, selectedPlanId } = usePlans(path);
209236
const plan = catalog.plans.find((p) => p.id === selectedPlanId);
210237
const pending = catalog.reconciliation && !catalog.reconciliation.complete;
211238
const count = accounts.find((a) => a.planId === plan?.id)?.count ?? 0;
@@ -300,6 +327,25 @@ export function PlanEditPage({ path, platformName, username }: AdminPageProps) {
300327
submitValue="true"
301328
initialValue={plan.allowShortSlugs}
302329
/>
330+
{/* The page value can come from a cached read; only a changed selector is sent. */}
331+
<input
332+
type="hidden"
333+
name="aiLoggingInitial"
334+
defaultValue={aiLoggingMode(aiLogging.modes, plan.id)}
335+
/>
336+
<FormSelector
337+
label="AI Gateway logging default"
338+
name="aiLogging"
339+
initialValue={aiLoggingMode(aiLogging.modes, plan.id)}
340+
options={Object.entries(aiLoggingLabels).map(([value, label]) => ({
341+
value,
342+
label,
343+
}))}
344+
/>
345+
<p>
346+
Applies to projects on this plan that have no project setting. Accounts on a
347+
plan missing from the catalog follow the signup default plan.
348+
</p>
303349
<p>
304350
Existing URLs remain available. Storage figures are informational; static
305351
deployment retention is unchanged.
@@ -390,6 +436,7 @@ export function PlanEditPage({ path, platformName, username }: AdminPageProps) {
390436
function differences(
391437
before: PlatformPlanDefinition | undefined,
392438
after: PlatformPlanDefinition | undefined,
439+
aiLogging: Readonly<{ before: AiLoggingMode | undefined; after: AiLoggingMode }>,
393440
) {
394441
if (!after) {
395442
return [{ label: 'Plan', before: before!.name, after: 'Removed' }];
@@ -411,6 +458,11 @@ function differences(
411458
before: before ? (before.allowShortSlugs ? 'Allowed' : 'Not allowed') : '—',
412459
after: after.allowShortSlugs ? 'Allowed' : 'Not allowed',
413460
},
461+
{
462+
label: 'AI Gateway logging default',
463+
before: aiLogging.before ? aiLoggingLabels[aiLogging.before] : '—',
464+
after: aiLoggingLabels[aiLogging.after],
465+
},
414466
...PLAN_EDITABLE_LIMIT_KEYS.map((key) => ({
415467
label: labels[key],
416468
before: before ? limit(before.limits[key], key) : '—',
@@ -429,9 +481,14 @@ export function PlanPreviewPage({
429481
username?: string;
430482
}) {
431483
const { before, after, target } = preview;
484+
const beforePlan = before.plans.find((p) => p.id === target);
432485
const changes = differences(
433-
before.plans.find((p) => p.id === target),
486+
beforePlan,
434487
after.plans.find((p) => p.id === target),
488+
{
489+
before: beforePlan ? aiLoggingMode(preview.aiLoggingBefore, target) : undefined,
490+
after: aiLoggingMode(preview.aiLoggingAfter, target),
491+
},
435492
);
436493
if (before.defaultPlanId !== after.defaultPlanId) {
437494
changes.push({
@@ -449,10 +506,10 @@ export function PlanPreviewPage({
449506
const replacement = after.plans.find((p) => p.id === preview.body.replacementPlanId);
450507
if (replacement) {
451508
changes.push(
452-
...differences(
453-
before.plans.find((p) => p.id === target),
454-
replacement,
455-
).filter((row) => row.label !== 'Display name'),
509+
...differences(beforePlan, replacement, {
510+
before: aiLoggingMode(preview.aiLoggingBefore, target),
511+
after: aiLoggingMode(preview.aiLoggingAfter, replacement.id),
512+
}).filter((row) => row.label !== 'Display name'),
456513
);
457514
}
458515
}
@@ -542,11 +599,11 @@ export function PlanProgressPage({
542599
username?: string;
543600
}) {
544601
const [state, setState] = useState(initial);
545-
const [processed, setProcessed] = useState(initial.processed);
602+
const [counts, setCounts] = useState({ accounts: initial.accounts, projects: initial.projects });
546603
const [running, setRunning] = useState(false);
547604
useEffect(() => {
548605
setState(initial);
549-
setProcessed(initial.processed);
606+
setCounts({ accounts: initial.accounts, projects: initial.projects });
550607
setRunning(!initial.complete && !initial.error);
551608
const controller = new AbortController();
552609
let timer: ReturnType<typeof setTimeout>;
@@ -581,7 +638,10 @@ export function PlanProgressPage({
581638
return;
582639
}
583640
setState(next);
584-
setProcessed((count) => count + next.processed);
641+
setCounts((count) => ({
642+
accounts: count.accounts + (next.accounts ?? 0),
643+
projects: count.projects + (next.projects ?? 0),
644+
}));
585645
previous = next;
586646
if (next.complete || next.error) {
587647
setRunning(false);
@@ -622,7 +682,9 @@ export function PlanProgressPage({
622682
: state.error
623683
? 'Account updates paused.'
624684
: 'Updating accounts…'}{' '}
625-
{processed} accounts updated in this session.
685+
{counts.accounts} accounts updated
686+
{counts.projects ? `, AI logging published to ${counts.projects} projects` : ''} in this
687+
session.
626688
</p>
627689
{state.error && <p role="alert">{state.error}</p>}
628690
{!state.complete && (

‎platform/packages/api/src/admin/react/views/ProjectDetailPage.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ProjectDetailView, projectDetailRequest } from '../../fate/views.ts';
1515
import { type AdminPageProps } from '../page-utils.ts';
1616
import { BuildTable } from './BuildTable.tsx';
1717
import { DeploymentTable } from './DeploymentTable.tsx';
18-
import { formatDateTime } from './format.ts';
18+
import { formatAiLoggingStatus, formatDateTime } from './format.ts';
1919
import { Layout } from './Layout.tsx';
2020
import { ProjectOwnerTransferSection } from './ProjectOwnerTransferSection.tsx';
2121
import { ProjectTeamSection } from './ProjectTeamSection.tsx';
@@ -40,6 +40,7 @@ export function ProjectDetailPage({ path, platformName, username }: AdminPagePro
4040
domains,
4141
builds,
4242
zeroTrust,
43+
aiLogging,
4344
cfAccountId,
4445
deployUrl,
4546
tz,
@@ -103,6 +104,10 @@ export function ProjectDetailPage({ path, platformName, username }: AdminPagePro
103104
</Link>
104105
</TableCell>
105106
</TableRow>
107+
<TableRow>
108+
<TableHeaderCell>AI Gateway logging</TableHeaderCell>
109+
<TableCell>{formatAiLoggingStatus(aiLogging)}</TableCell>
110+
</TableRow>
106111
<TableRow>
107112
<TableHeaderCell>Created</TableHeaderCell>
108113
<TableCell>{formatDateTime(project.created_at, tz)}</TableCell>

0 commit comments

Comments
 (0)