PEP-177: every subscription shows whose it is — the person's name next to it; new pool subscriptions are named after their owner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Асель 2026-11-16 21:12:50 +05:00
parent d9f9f3c8ca
commit 186310a42b
6 changed files with 19 additions and 5 deletions

View File

@ -99,6 +99,7 @@ function Problem({ s, login, t }: { s: SubscriptionView; login: PendingLogin | n
<Card id={`s-${s.id}`} className="gap-2">
<div className="flex flex-wrap items-center gap-2 text-sm">
<span className="font-medium">{s.name}</span>
{s.ownerName && !s.name.includes(s.ownerName) && <span className="text-xs text-muted-foreground">· {s.ownerName}</span>}
<span className="text-xs text-muted-foreground">{PROVIDER_NAMES[s.provider]}</span>
{s.status === "auth_error" ? <Badge variant="danger">{t("ошибка входа")}</Badge> : <Badge variant="muted">{t("вход не закончен · {ago}", { ago: ago(s.createdAt, t) })}</Badge>}
</div>
@ -165,7 +166,7 @@ function SetCard({ o, subs, t }: { o: PoolOverview; subs: SubscriptionView[]; t:
<NativeSelect name="subscription" aria-label={t("Добавить подписку")} className="h-8 max-w-xs text-sm" required defaultValue="">
<option value="" disabled>{t("Добавить подписку…")}</option>
{addable.map((s) => (
<option key={s.id} value={s.id}>{s.name} ({PROVIDER_NAMES[s.provider]})</option>
<option key={s.id} value={s.id}>{s.name}{s.ownerName && !s.name.includes(s.ownerName) ? ` · ${s.ownerName}` : ""} ({PROVIDER_NAMES[s.provider]})</option>
))}
</NativeSelect>
<Button size="sm">{t("Добавить")}</Button>
@ -260,6 +261,7 @@ export default async function Subscriptions({ searchParams }: { searchParams: Pr
{loose.map((s) => (
<li key={s.id} className="flex flex-wrap items-center gap-2 py-2 text-sm first:pt-0 last:pb-0">
<span className="font-medium">{s.name}</span>
{s.ownerName && !s.name.includes(s.ownerName) && <span className="text-xs text-muted-foreground">· {s.ownerName}</span>}
<span className="text-xs text-muted-foreground">{PROVIDER_NAMES[s.provider]}</span>
<form action={deleteSubscriptionAction} className="ml-auto">
<Hidden fields={{ id: s.id }} />

View File

@ -28,6 +28,7 @@ export async function PoolSubscriptions({ o, action }: { o: PoolOverview; action
{o.subscriptions.map((s) => (
<li key={s.id} className="flex flex-wrap items-center gap-x-2 gap-y-1 py-2 text-sm first:pt-0 last:pb-0" data-pool-subscription={s.id}>
<span className="font-medium">{s.name}</span>
{s.ownerName && !s.name.includes(s.ownerName) && <span className="text-xs text-muted-foreground" data-subscription-owner>· {s.ownerName}</span>}
{!s.name.includes(KIND[s.provider]) && <span className="text-xs text-muted-foreground">{KIND[s.provider]}</span>}
<Badge variant={tone(s)}>{t.text(s.label)}</Badge>
{s.next && <Badge variant="info">{t("следующая")}</Badge>}

View File

@ -135,6 +135,8 @@ export interface PoolSubscriptionState {
loan: Loan | null;
/** PEP-170: the free subscription an agent of this pool moves to when its own runs out. */
next: boolean;
/** PEP-177: whose subscription (the person's name); null when the viewer must not see it. */
ownerName: string | null;
/** «в работе: 2 агента» / «свободна» / «лимит до 15:40» / «ждёт входа» / «ошибка входа». */
label: string;
}
@ -225,6 +227,8 @@ export async function poolOverviews(db: Db, poolIds?: number[], now = new Date()
const base = {
id: s.id,
name: viewer ? visibleSubscriptionName(s, viewer) : s.name,
// A neutral name for a non-admin hides whose it is too (PEP-121).
ownerName: viewer && visibleSubscriptionName(s, viewer) !== s.name ? null : s.ownerName,
provider: s.provider,
status: s.status,
createdBy: s.createdBy,

View File

@ -29,6 +29,8 @@ export const OPENCODE_KEY_ENV: Record<string, string> = {
export interface SubscriptionView extends Subscription {
createdByLogin: string | null;
/** PEP-177: whose subscription — the person's name (login when there is no name). */
ownerName: string | null;
activeSessions: number;
/** PEP-39: active sessions on it per project. */
projectSessions: Array<{ key: string; n: number }>;
@ -73,7 +75,7 @@ function countBySubscription(rows: ActiveRow[]): Map<number, number> {
export async function listSubscriptions(db: Db): Promise<SubscriptionView[]> {
const rows = await db
.select({ s: subscriptions, login: users.login })
.select({ s: subscriptions, login: users.login, personName: users.name })
.from(subscriptions)
.leftJoin(users, eq(subscriptions.createdBy, users.id))
.orderBy(asc(subscriptions.id));
@ -88,6 +90,7 @@ export async function listSubscriptions(db: Db): Promise<SubscriptionView[]> {
return rows.map((r) => ({
...r.s,
createdByLogin: r.login,
ownerName: r.personName?.trim() || r.login,
activeSessions: counts.get(r.s.id) ?? 0,
projectSessions: perProject(r.s.id),
poolIds: members.filter((m) => m.subscriptionId === r.s.id).map((m) => m.poolId),

View File

@ -5,7 +5,7 @@ import { spawn } from "node:child_process";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createSubscription, getSubscription, OPENCODE_KEY_ENV, PeppaError, subscriptionSecret, updateSubscription, type Subscription, getPool, listSubscriptions, poolSubscriptions, updatePool } from "@peppa/core";
import { createSubscription, getSubscription, OPENCODE_KEY_ENV, PeppaError, subscriptionSecret, updateSubscription, type Subscription, getPool, listSubscriptions, poolSubscriptions, updatePool, getUser } from "@peppa/core";
import type { Db } from "@peppa/db";
import { parseResetTime } from "./limits.ts";
import { codexHome, mask, removeSubscription, setClaudeSubscriptionToken, setCodexApiKey, startClaudeLogin, startCodexLogin } from "./subscription-login.ts";
@ -73,7 +73,9 @@ export async function addPoolSubscription(db: Db, o: { poolId: number; provider:
const taken = new Set((await listSubscriptions(db)).map((x) => x.name));
let n = members.filter((x) => x.provider === o.provider).length + 1;
while (taken.has(`${label} ${n} — ${pool.name}`)) n++;
const s = await addSubscription(db, { provider: o.provider, name: `${label} ${n} — ${pool.name}`, method: o.provider === "codex" ? "device" : "oauth", createdBy: o.createdBy });
// PEP-177: the name says whose it is — «Claude 2 — asel», not the pool's name.
const who = (await getUser(db, o.createdBy).catch(() => null))?.login ?? pool.name;
const s = await addSubscription(db, { provider: o.provider, name: `${label} ${n} — ${who}`, method: o.provider === "codex" ? "device" : "oauth", createdBy: o.createdBy });
await updatePool(db, pool.id, { subscriptionIds: [...members.map((m) => m.id), s.id] });
return s;
}

View File

@ -392,9 +392,11 @@ describe("PEP-164: ещё одна подписка прямо в пул", () =>
const pool = await core.createPool(db, { name: "Пул ротации PEP-164", subscriptionIds: [first.id] });
const added = await runner.addPoolSubscription(db, { poolId: pool.id, provider: "claude", createdBy: asel.id });
assert.equal(added.status, "pending", "waits for the sign-in");
assert.match(added.name, /^Claude 2 — Пул ротации PEP-164$/);
assert.match(added.name, /^Claude 2 — asel$/, "PEP-177: the name says whose it is");
const members = (await core.poolSubscriptions(db, pool.id)).map((s) => s.id);
assert.deepEqual(members, [first.id, added.id], "already a member of the pool, the first one stays");
const row = (await core.listSubscriptions(db)).find((x) => x.id === added.id)!;
assert.equal(row.ownerName, "asel", "PEP-177: the owner is shown next to the name");
const login = await runner.pendingLogin(db, added.id);
assert.ok(login?.url, "the sign-in link is ready");
await runner.cancelLogin(db, added.id);