Watch
0
0
Fork
You've already forked hyperhive
0

agents get a per-agent repo limit in their permissions record, orgs are locked

max_repos (default 0, 0..=100) lives in the agent's agent-permissions record and is edited on its permissions panel. The controller applies each agent's own value as max_repo_creation at CreateForgeUser and to existing accounts on change, restart and every five minutes; agent accounts may not create organizations.
This commit is contained in:
atlas 2026-11-15 20:47:21 +01:00 • committed by mara
commit 1edeb5c55d
3 changed files with 763 additions and 58 deletions

View file

@ -25,6 +25,7 @@ import { AgentLimits } from "./AgentLimits.js";
import {
AgentInternet,
AgentPermissions,
AgentRepos,
useGrant,
} from "./AgentPermissions.js";
import { AllowedDomains } from "./AllowedDomains.js";
@ -223,6 +224,10 @@ function GrantRows({ agent }: { agent: string }) {
<dd>
<AllowedDomains key={agent} agent={agent} />
</dd>
<dt>repositories</dt>
<dd>
{g.grant === null ? g.loading : <AgentRepos agent={agent} g={g} />}
</dd>
<dt>permissions</dt>
<dd>
{g.grant === null ? (

View file

@ -1,16 +1,17 @@
// <AgentPermissions> and <AgentInternet> — one agent's grant in an opened agent
// card: the permissions it holds, and how far its traffic may leave the host.
// Each is a badge that opens a control under it, which saves as soon as it
// <AgentPermissions>, <AgentInternet> and <AgentRepos> — one agent's grant in an
// opened agent card: the permissions it holds, how far its traffic may leave
// the host, and how many repositories its forge account may create. Each is a badge that opens a control under it, which saves as soon as it
// changes.
//
// Both read `GET /api/agents/{name}/permissions`, which answers the stored
// grant as `{ permissions?, unrestricted_internet?, internet_log? }` (`{}` when
// grant as `{ permissions?, unrestricted_internet?, internet_log?, max_repos? }` (`{}` when
// the swarm sets none, so the agent runs on the default set). The permissions
// control offers the catalogue from `GET /api/permissions` as checkboxes and
// PUTs the ticked ones, a PUT replacing the stored value whole. A selection
// equal to the default set DELETEs instead, as the create form sends nothing
// for it: the agent then follows the default if that changes. The internet
// control PUTs the mode with the stored list beside it. The route's refusal
// control PUTs the mode with the stored list beside it, and the repositories
// control the limit the same way. The route's refusal
// shows as it arrives, in the control.
//
// Their state belongs to one agent: callers key them by agent name, so
@ -23,6 +24,7 @@ import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
import { Button } from "../../ui/button/Button.js";
import { BadgeSkeleton } from "../../ui/skeleton/BadgeSkeleton.js";
import { Hint } from "../../ui/hint/Hint.js";
import { TextField } from "../../ui/text-field/TextField.js";
import { MultiselectFilterOptions } from "../../ui/multiselect-filter/MultiselectFilter.js";
import {
requestPermissions,
@ -34,6 +36,10 @@ import {
internetBody,
internetMode,
internetSummary,
MAX_REPOS,
maxReposBody,
parseMaxRepos,
reposSummary,
type InternetMode,
} from "./permissions.js";
import "./AgentLimits.css";
@ -43,6 +49,7 @@ interface Grant {
permissions?: string[];
unrestricted_internet?: boolean;
internet_log?: boolean;
max_repos?: number;
}
// Mirrors swarm-controller's `Catalogue` (`GET /api/permissions`).
@ -302,3 +309,72 @@ export function AgentInternet({ agent, g }: { agent: string; g: GrantState }) {
</div>
);
}
export function AgentRepos({ agent, g }: { agent: string; g: GrantState }) {
const { grant, busy, saveError, loading, send } = g;
// The box as typed; null shows the stored limit.
const [typed, setTyped] = useState<string | null>(null);
if (grant === null) return loading;
const stored = grant.max_repos ?? 0;
const text = typed ?? String(stored);
const parsed = parseMaxRepos(text);
async function save(e: Event) {
e.preventDefault();
if (parsed === null) return;
const ok = await send({
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify(maxReposBody(grant?.permissions, parsed)),
});
// The grant was read again, or the change refused: show what is stored.
if (ok) setTyped(null);
}
return (
<div class="ui-agent-limits">
<BadgePopover
label="repositories"
value={reposSummary(grant.max_repos)}
tone={stored === 0 ? "neutral" : "warning"}
title={`how many repositories ${agent}'s forge account may create`}
popoverLabel={`${agent} repository limit`}
>
<p class="ui-badge-popover-help">
How many repositories {agent}'s forge account may create, 0 to{" "}
{MAX_REPOS}. 0 means none. The swarm applies a change to the account
within seconds.
</p>
<p class="ui-badge-popover-help" role="note">
<strong>Warning:</strong> an agent that may create repositories can
add a CI workflow to them, and CI workflows run unsandboxed on the CI
runner. That is arbitrary code on the runner host. Only allow this for
agents you would trust with that.
</p>
<form onSubmit={(e) => void save(e)}>
<TextField
id={`max-repos-${agent}`}
label="repositories"
type="number"
value={text}
onInput={setTyped}
/>
<Button
variant="primary"
type="submit"
disabled={busy || parsed === null || parsed === stored}
>
{busy ? "saving…" : "save"}
</Button>
</form>
{saveError ? (
<ApiErrorPanel
context="the repository limit was not saved"
problem={saveError}
/>
) : null}
</BadgePopover>
</div>
);
}

View file

@ -69,3 +69,38 @@ export function internetBody(
...(mode === "log" ? { internet_log: true as const } : {}),
};
}
/** The most `max_repos` may be set to; the route refuses more. */
export const MAX_REPOS = 100;
/**
* The repository limit a typed value stands for, or null when it is not a
* whole number from 0 to {@link MAX_REPOS}.
*/
export function parseMaxRepos(text: string): number | null {
const t = text.trim();
if (!/^\d+$/.test(t)) return null;
const n = Number(t);
return n <= MAX_REPOS ? n : null;
}
/** What the repositories badge shows: an absent limit is none. */
export function reposSummary(stored: number | undefined): string {
return stored === undefined || stored === 0 ? "none" : `up to ${stored}`;
}
/**
* The `PUT` body that sets the repository limit. A `PUT` without
* `permissions` resets a stored list to the default, so the stored list goes
* along, as in {@link internetBody}; the internet fields are left out, which
* keeps them.
*/
export function maxReposBody(
stored: string[] | undefined,
n: number,
): { permissions?: string[]; max_repos: number } {
return {
...(stored === undefined ? {} : { permissions: stored }),
max_repos: n,
};
}