Public: anyone can read this; the crew can change it.

On a repository's crew page, adding a member to a slot redraws in place without losing the scroll, and a member is one line with its description on demand #18627

opened this

The crew is building it, 3m so far

main moved: src/web/pages/settings.ts

torque/forge:torque/18541-on-a-repository-s-crew-page-adding-a-mem → main 11 commits fork

  • Ready to merge — no conflicts with main. 11 commits will land on main.

    Check runningdeploy 934996f

    You need write access to merge.

Review

Sign in to review this change.
11 files changed +75 −36
src/http/lazy_lists.ts +1
@@ -100,6 +100,7 @@ export const SETTINGS_ROUTES = [
100100 ["POST", "/settings/subscriptions"],
101101 ["POST", "/settings/crew/key"],
102102 ["POST", "/settings/crew/budgets"],
103 ["GET", "/settings/crew/fallback/card"],
103104 ["POST", "/settings/crew/fallback"],
104105 ["POST", "/settings/crew/fallback/cap"],
105106 ["POST", "/settings/subscriptions/:id/delete"],
src/http/marks/settings.json +1
@@ -74,6 +74,7 @@
7474 "/settings/crew/budgets": "admin",
7575 "/settings/crew/fallback": "admin",
7676 "/settings/crew/fallback/cap": "admin",
77 "/settings/crew/fallback/card": "admin",
7778 "/settings/subscriptions/:id/delete": "admin",
7879 "/settings/subscriptions/allow": "admin",
7980 "/settings/subscriptions/:id/wake": "admin",
src/http/settings_routes.ts +2 −1
@@ -10,7 +10,7 @@
1010 import { redirect } from "../web/layout.ts";
1111 import { crewKeyAdd } from "../web/studio/crew_key.ts";
1212 import { crewBudgetsSave } from "../web/studio/crew_budgets.ts";
13 import { fallbackCapSave, fallbackSave } from "../web/studio/crew_fallback.ts";
13 import { fallbackCapSave, fallbackFragment, fallbackSave } from "../web/studio/crew_fallback.ts";
1414 import type { Handler } from "./context.ts";
1515 import type { Router } from "./router.ts";
1616 import { parentLinkStart } from "./rp.ts";
@@ -258,6 +258,7 @@ export function settingsRoutes(router: Router<Handler>, knownPatterns: () => rea
258258 router.add("POST", "/settings/subscriptions", subscriptionAdd);
259259 router.add("POST", "/settings/crew/key", crewKeyAdd);
260260 router.add("POST", "/settings/crew/budgets", crewBudgetsSave);
261 router.add("GET", "/settings/crew/fallback/card", fallbackFragment);
261262 router.add("POST", "/settings/crew/fallback", fallbackSave);
262263 router.add("POST", "/settings/crew/fallback/cap", fallbackCapSave);
263264 router.add("POST", "/settings/subscriptions/:id/delete", subscriptionDelete);
src/web/pages/settings.ts +10 −7
@@ -549,7 +549,7 @@ export async function settingsUpdate(ctx: Ctx): Promise<Response> {
549549 ? `Polish share is a whole number of percent, 0 to ${POLISH_SHARE_MAX}.`
550550 : "Those settings could not be saved.",
551551 };
552 return flashTo(ctx, card === "template" ? `${base}/settings/app` : `${base}/settings`, flash);
552 return flashOrJson(ctx, card === "template" ? `${base}/settings/app` : `${base}/settings`, flash);
553553 }
554554
555555 /** A return address that stays on this origin, as a path; anything else is undefined. */
@@ -1720,7 +1720,7 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> {
17201720 const slots = slotsField === "" ? repo.autonomySlots : Number(slotsField);
17211721 const checked = checkAutonomy(field(form, "autonomy"), slots);
17221722 if (!checked.ok)
1723 return flashTo(ctx, `${base}/settings#autonomy`, {
1723 return flashOrJson(ctx, `${base}/settings#autonomy`, {
17241724 tone: "danger",
17251725 message: checked.message,
17261726 });
@@ -1728,7 +1728,7 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> {
17281728 const rule = field(form, "decisions");
17291729 const decisionsBy = rule === "" ? repo.decisionsBy : rule;
17301730 if (!isDecisionsBy(decisionsBy))
1731 return flashTo(ctx, `${base}/settings#autonomy`, {
1731 return flashOrJson(ctx, `${base}/settings#autonomy`, {
17321732 tone: "danger",
17331733 message: "Decisions here go to you, the decider, or the decider telling you.",
17341734 });
@@ -1747,8 +1747,11 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> {
17471747 // through the function the `decisions_by` MCP tool calls.
17481748 const ruled = updated.ok && decisionsBy !== repo.decisionsBy ? await setDecisionsBy(ctx.app, ctx.auth.user, repo, decisionsBy, false) : undefined;
17491749 if (ruled !== undefined && !ruled.ok)
1750 return flashTo(ctx, `${base}/settings#autonomy`, { tone: "danger", message: "Those settings could not be saved." });
1751 return flashTo(ctx, `${base}/settings#autonomy`, {
1752 ...(updated.ok ? { message: AUTONOMY_SAID[mode] } : { tone: "danger" as const, message: "Those settings could not be saved." }),
1753 });
1750 return flashOrJson(ctx, `${base}/settings#autonomy`, { tone: "danger", message: "Those settings could not be saved." });
1751 return flashOrJson(
1752 ctx,
1753 `${base}/settings#autonomy`,
1754 updated.ok ? { message: AUTONOMY_SAID[mode] } : { tone: "danger", message: "Those settings could not be saved." },
1755 { redraw: [`${base}/settings/crew/line`] },
1756 );
17541757 }
src/web/studio/crew.ts +2 −2
@@ -74,7 +74,7 @@ function aboutCard(
7474 const noun = isApp ? "app" : "repository";
7575 return html`<section class="settings-block">
7676 <h2>About</h2>
77 <form method="post" action="${base}/settings">
77 <form method="post" action="${base}/settings" data-in-place>
7878 ${field({
7979 label: "Description",
8080 name: "description",
@@ -426,7 +426,7 @@ function autonomyCard(ctx: Ctx, repo: Repo, base: string, admin: boolean): Html
426426 </dl>`;
427427 const form = !admin
428428 ? null
429 : html`<form method="post" action="${base}/settings/autonomy">
429 : html`<form method="post" action="${base}/settings/autonomy" data-in-place>
430430 ${selectField({
431431 label: "Mode",
432432 name: "autonomy",
src/web/studio/crew_budgets.ts +4 −4
@@ -12,7 +12,7 @@ import type { Ctx } from "../../http/context.ts";
1212 import { notFoundPage, signInFirst } from "../chrome.ts";
1313 import { field, readForm } from "../form.ts";
1414 import { html, type Html } from "../html.ts";
15 import { flashTo } from "../layout.ts";
15 import { flashOrJson } from "../layout.ts";
1616 import { btn } from "../ui.ts";
1717 import { adminPage } from "../../http/page_may.ts";
1818
@@ -30,7 +30,7 @@ export function budgetsCard(ctx: Ctx): Html | null {
3030 return html`<section class="settings-block" id="budgets">
3131 <h2>Budgets</h2>
3232 <p class="small muted">What the crew may spend on your model key; a run a limit stops says which one.</p>
33 <form class="stack" method="post" action="${BUDGETS_ACTION}">
33 <form class="stack" method="post" action="${BUDGETS_ACTION}" data-in-place>
3434 ${lines.map(
3535 (l) => html`<div class="field">
3636 <label for="budget-${l.key}">Spend ${BUDGET_LABEL[l.key]} (dollars)</label>
@@ -61,9 +61,9 @@ export async function crewBudgetsSave(ctx: Ctx): Promise<Response> {
6161 if (!saved.ok)
6262 return saved.error === "not_found"
6363 ? notFoundPage(ctx)
64 : flashTo(ctx, "/settings/crew#budgets", {
64 : flashOrJson(ctx, "/settings/crew#budgets", {
6565 tone: "danger",
6666 message: `Each limit is dollars and cents, from $0.01 to $${BUDGET_MAX_USD.toLocaleString("en-US")}.`,
6767 });
68 return flashTo(ctx, "/settings/crew#budgets", { message: `Budgets saved. ${wokeWords(saved.woken)}`.trim() });
68 return flashOrJson(ctx, "/settings/crew#budgets", { message: `Budgets saved. ${wokeWords(saved.woken)}`.trim() });
6969 }
src/web/studio/crew_fallback.ts +21 −10
@@ -11,11 +11,12 @@ import type { Ctx } from "../../http/context.ts";
1111 import { notFoundPage, signInFirst } from "../chrome.ts";
1212 import { field, readForm } from "../form.ts";
1313 import { html, type Html } from "../html.ts";
14 import { flashTo } from "../layout.ts";
14 import { flashOrJson, fragment } from "../layout.ts";
1515 import { btn, meter, row, rowList } from "../ui.ts";
1616 import { adminPage } from "../../http/page_may.ts";
1717
1818 export const FALLBACK_ACTION = "/settings/crew/fallback";
19 export const FALLBACK_CARD = "/settings/crew/fallback/card";
1920 export const FALLBACK_CAP_ACTION = "/settings/crew/fallback/cap";
2021 const usd = (n: number): string => `$${n.toFixed(2)}`;
2122 const AMOUNT = /^\d{1,6}(\.\d{1,2})?$/;
@@ -26,10 +27,10 @@ export function fallbackCard(ctx: Ctx): Html | null {
2627 const v = fallbackView(ctx.app);
2728 const was = Number(ctx.url.searchParams.get("was") ?? "");
2829 const k = v.key;
29 return html`<section class="settings-block" id="fallback">
30 return html`<section class="settings-block" id="fallback" data-fragment="${FALLBACK_CARD}">
3031 <h2>Metered fallback</h2>
3132 <p data-fallback-line>${v.line}</p>
32 <form class="bar" method="post" action="${FALLBACK_ACTION}">
33 <form class="bar" method="post" action="${FALLBACK_ACTION}" data-in-place>
3334 <input type="hidden" name="on" value="${v.on ? "off" : "on"}" />
3435 <span class="small">The fallback is <b>${v.on ? "on" : "off"}</b>.</span>
3536 ${btn(v.on ? "Turn it off" : "Turn it on", { size: "sm" })}
@@ -39,14 +40,14 @@ export function fallbackCard(ctx: Ctx): Html | null {
3940 : html`<div class="mt">
4041 ${meter(Math.min(100, (k.spentUsd / k.capUsd) * 100), `${k.name}: ${usd(k.spentUsd)} of its ${usd(k.capUsd)} cap`, k.spentUsd >= k.capUsd * 0.8 ? "attn" : undefined)}
4142 <p class="small muted">${k.name}: ${usd(k.todayUsd)} today · ${usd(k.weekUsd)} this week · ${usd(k.spentUsd)} of its ${usd(k.capUsd)} cap, at list price.</p>
42 <form class="bar mt" method="post" action="${FALLBACK_CAP_ACTION}">
43 <form class="bar mt" method="post" action="${FALLBACK_CAP_ACTION}" data-in-place>
4344 <label for="fallback-cap" class="small">Cap (dollars)</label>
4445 <input class="input" id="fallback-cap" name="cap" type="number" min="1" step="0.01" required value="${k.capUsd.toFixed(2)}" inputmode="decimal" data-size="sm" />
4546 ${btn("Set the cap", { size: "sm" })}
4647 </form>
4748 <p class="small muted" data-fallback-forecast>${capForecast(k)}</p>
4849 ${Number.isFinite(was) && was > 0 && was !== k.capUsd
49 ? html`<form class="bar" method="post" action="${FALLBACK_CAP_ACTION}">
50 ? html`<form class="bar" method="post" action="${FALLBACK_CAP_ACTION}" data-in-place>
5051 <input type="hidden" name="cap" value="${was.toFixed(2)}" />
5152 <span class="small">The cap was ${usd(was)}.</span>
5253 ${btn("Undo", { size: "sm" })}
@@ -77,9 +78,12 @@ export async function fallbackSave(ctx: Ctx): Promise<Response> {
7778 const on = field(form, "on") === "on";
7879 const done = setFallback(ctx.app, ctx.auth.user, on);
7980 if (!done.ok) return notFoundPage(ctx);
80 return flashTo(ctx, "/settings/crew#fallback", {
81 message: on ? "The fallback is on: the capped key takes crew runs once every plan's window is full." : "The fallback is off: crew runs wait for the first window to reset.",
82 });
81 return flashOrJson(
82 ctx,
83 "/settings/crew#fallback",
84 { message: on ? "The fallback is on: the capped key takes crew runs once every plan's window is full." : "The fallback is off: crew runs wait for the first window to reset." },
85 { redraw: [FALLBACK_CARD] },
86 );
8387 }
8488
8589 // POST /settings/crew/fallback/cap
@@ -93,6 +97,13 @@ export async function fallbackCapSave(ctx: Ctx): Promise<Response> {
9397 if (!done.ok)
9498 return done.error === "not_found"
9599 ? notFoundPage(ctx)
96 : flashTo(ctx, "/settings/crew#fallback", { tone: "danger", message: done.error === "locked" ? "This key's cap was set by whoever made this forge: it can only come down." : "The cap is dollars and cents, more than $0, within the forge's budget limit." });
97 return flashTo(ctx, `/settings/crew?was=${done.value.from.toFixed(2)}#fallback`, { message: `The cap is ${usd(done.value.to)} (it was ${usd(done.value.from)}); Undo is beside it.` });
100 : flashOrJson(ctx, "/settings/crew#fallback", { tone: "danger", message: done.error === "locked" ? "This key's cap was set by whoever made this forge: it can only come down." : "The cap is dollars and cents, more than $0, within the forge's budget limit." });
101 const was = done.value.from.toFixed(2);
102 return flashOrJson(ctx, `/settings/crew?was=${was}#fallback`, { message: `The cap is ${usd(done.value.to)} (it was ${usd(done.value.from)}); Undo is beside it.` }, { redraw: [`${FALLBACK_CARD}?was=${was}`] });
103 }
104
105 // GET /settings/crew/fallback/card: the card alone, as the page draws it.
106 export function fallbackFragment(ctx: Ctx): Response {
107 if (!adminPage(ctx)) return fragment(html``, { status: 404 });
108 return fragment(fallbackCard(ctx) ?? html``, { request: ctx.request });
98109 }
src/web/studio/crew_key.ts +4 −4
@@ -23,7 +23,7 @@ import { notFoundPage, signInFirst } from "../chrome.ts";
2323 import { field, readForm } from "../form.ts";
2424 import { html, type Html } from "../html.ts";
2525 import { icon } from "../icons.ts";
26 import { flashTo } from "../layout.ts";
26 import { flashOrJson } from "../layout.ts";
2727 import { btn } from "../ui.ts";
2828 import { adminPage } from "../../http/page_may.ts";
2929
@@ -68,7 +68,7 @@ export function crewFirstLine(app: App, admin: boolean): Html {
6868 return html`The crew cannot build until you ${admin ? html`<a href="/settings/subscriptions">add a model key</a>` : "add a model key"} or <a href="/pricing">pick a plan</a>.`;
6969 }
7070
71 const keyField = (): Html => html`<form class="stack" method="post" action="${KEY_ACTION}" id="crew-key-form">
71 const keyField = (): Html => html`<form class="stack" method="post" action="${KEY_ACTION}" id="crew-key-form" data-in-place>
7272 <div class="field">
7373 <label for="crew-key">Your AI's API key or login</label>
7474 <input class="input" id="crew-key" name="token" type="password" required maxlength="512" autocomplete="off" spellcheck="false" placeholder="paste it here" />
@@ -141,6 +141,6 @@ export async function crewKeyAdd(ctx: Ctx): Promise<Response> {
141141 if (!saved.ok)
142142 return saved.error === "not_found"
143143 ? notFoundPage(ctx)
144 : flashTo(ctx, "/", { tone: "danger", message: "That key could not be saved. Check that it is the whole key and try again." });
145 return flashTo(ctx, "/", { message: `Key saved. The crew can work now. ${wokeWords(saved.woken)}`.trim() });
144 : flashOrJson(ctx, "/", { tone: "danger", message: "That key could not be saved. Check that it is the whole key and try again." });
145 return flashOrJson(ctx, "/", { message: `Key saved. The crew can work now. ${wokeWords(saved.woken)}`.trim() });
146146 }
test/crew-in-place.test.ts +29
@@ -138,6 +138,35 @@ describe("crew card in place", () => {
138138 expect(nope.status).toBe(404);
139139 });
140140
141 test("About and On its own save in place: JSON for a JSON ask, the redirect without script", async () => {
142 const about = await post("", { description: "A yard", visibility: "public", card: "about" });
143 expect(about.status).toBe(200);
144 expect(((await about.json()) as { ok: boolean; flash: string }).flash).toBe("Settings saved.");
145 const own = await post("/autonomy", { autonomy: "assisted", direction: "Keep it tidy", decisions: "me" });
146 expect(own.status).toBe(200);
147 expect(((await own.json()) as { redraw: string[] }).redraw).toEqual(["/alice/yard/settings/crew/line"]);
148 const bad = await post("/autonomy", { autonomy: "reckless" });
149 expect(bad.status).toBe(422);
150 expect((await post("/autonomy", { autonomy: "assisted" }, { json: false })).status).toBe(303);
151 expect((await post("/autonomy", { autonomy: "assisted" }, { origin: "https://evil.example" })).status).not.toBe(200);
152 const page = await (await fetch(base(), { headers: { cookie: admin } })).text();
153 expect(page).toMatch(/<form[^>]*action="\/alice\/yard\/settings\/autonomy"[^>]*data-in-place/);
154 expect(page).toMatch(/<form[^>]*action="\/alice\/yard\/settings"[^>]*data-in-place/);
155 });
156
157 test("the forge-wide crew forms (key, budgets, fallback) answer JSON, and the fallback card is an admin's fragment", async () => {
158 const root = (path: string, body: Record<string, string>, cookie = admin) =>
159 fetch(`${servers.url}${path}`, { method: "POST", headers: { origin: servers.url, cookie, accept: "application/json" }, body: new URLSearchParams(body), redirect: "manual" });
160 for (const [path, body] of [["/settings/crew/budgets", { day: "abc" }], ["/settings/crew/fallback/cap", { cap: "nope" }]] as const) {
161 const r = await root(path, body);
162 expect(r.status).not.toBe(303);
163 if (r.status !== 404) expect(r.headers.get("content-type")).toContain("json");
164 }
165 const key = await root("/settings/crew/key", { token: "x" });
166 expect(key.status).not.toBe(303);
167 expect((await fetch(`${servers.url}/settings/crew/fallback/card`, { headers: { cookie: bob } })).status).toBe(403);
168 });
169
141170 test("the page marks its forms and seats, and the script stays inside its budget", async () => {
142171 const page = await (await fetch(`${base()}`, { headers: { cookie: admin } })).text();
143172 expect(page).toContain("data-in-place");
test/design-audit.test.ts +1 −1
@@ -18,7 +18,7 @@ const doc = readFileSync("docs/design/audit.md", "utf8");
1818
1919 /** Routes that answer a file, a fragment, a redirect hop or a callback. */
2020 const NOT_PAGES =
21 /^\/(design\/theme\.css|design\/theme\/|assets\/|\.well-known\/|icons\/|og\/|live\/|__forge\/|go\/|new\/issue$|asked$|events$|boards\/stream$|system\/stream$|talk\/stream$|peek$|settings\/teams\/stream$|(tabforge\/)?sw\.js$|offline$|robots\.txt$|favicon\.ico$|apple-touch-icon(-precomposed)?\.png$|sitemap\.xml$|edition$|oembed$|manifest\.webmanifest$|opensearch\.xml$|llms(-full)?\.txt$|openapi\.json$|releases\.json$|templates\.json$|maintenance\.json$|changelog\.atom$|explore\.(atom|json)$|feed\.json$|docs(\/[^/]*)?$|audit\.csv$|settings\/traffic\/scrapes\/export$|settings\/payments\.csv$|fleet\/graph\/live$|fleet\/graph\.csv$|forges\/export\.csv$|settings\/origins\/callback$|settings\/origins\/reach$|fleet\/result$|broker\/origins\/start$|home\/archived\/|settings\/backups\/live$|api\/v1(\/|$))/;
21 /^\/(design\/theme\.css|design\/theme\/|assets\/|\.well-known\/|icons\/|og\/|live\/|__forge\/|go\/|new\/issue$|asked$|events$|boards\/stream$|system\/stream$|talk\/stream$|peek$|settings\/teams\/stream$|(tabforge\/)?sw\.js$|offline$|robots\.txt$|favicon\.ico$|apple-touch-icon(-precomposed)?\.png$|sitemap\.xml$|edition$|oembed$|manifest\.webmanifest$|opensearch\.xml$|llms(-full)?\.txt$|openapi\.json$|releases\.json$|templates\.json$|maintenance\.json$|changelog\.atom$|explore\.(atom|json)$|feed\.json$|docs(\/[^/]*)?$|audit\.csv$|settings\/traffic\/scrapes\/export$|settings\/payments\.csv$|fleet\/graph\/live$|fleet\/graph\.csv$|forges\/export\.csv$|settings\/origins\/callback$|settings\/origins\/reach$|fleet\/result$|broker\/origins\/start$|home\/archived\/|settings\/backups\/live$|settings\/crew\/fallback\/card$|api\/v1(\/|$))/;
2222
2323 const getRoutes = [
2424 ...server.matchAll(/(?:router|own)\.add\(\s*"GET",\s*"([^"]+)"/g),
test/in-place-offenders.json −7
@@ -485,13 +485,6 @@
485485 "src/web/studio/change.ts POST :/review-state",
486486 "src/web/studio/change.ts POST :/ship",
487487 "src/web/studio/crash_card.ts POST :/settings/report-key",
488 "src/web/studio/crew.ts POST :/settings",
489 "src/web/studio/crew.ts POST :/settings/autonomy",
490 "src/web/studio/crew_budgets.ts POST (form 1)",
491 "src/web/studio/crew_fallback.ts POST (form 1)",
492 "src/web/studio/crew_fallback.ts POST (form 2)",
493 "src/web/studio/crew_fallback.ts POST (form 3)",
494 "src/web/studio/crew_key.ts POST (form 1)",
495488 "src/web/studio/cut_over.ts POST :/cut-over",
496489 "src/web/studio/data_move.ts POST :/data-move",
497490 "src/web/studio/decisions.ts POST (form 13)",

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs