Public: anyone can read this; the crew can change it.
Spend 3: the same cost reads and planned stop/delete as MCP tools and a terminal screen #18206
Merged 14h 19m ago, live in production
torque/forge:torque/18103-spend-3-the-same-cost-reads-and-planned → main 0 commits fork 3 threads · 3 unresolved
15 files
changed +475
−127
- docs/features/cloud-end.md
- src/actions/cloud_cost.ts
- src/crew/conversation_ask.ts
- src/crew/conversation_confirm.ts
- src/generated/screens.ts
- src/http/mcp/tools/bring.ts
- src/http/mcp/tools/infra.ts
- src/http/mcp/tools/new_app.ts
- src/http/mcp/tools/propose_upstream.ts
- src/tui/screens/cloudcost.ts
- src/web/pages/infra.ts
- test/conversation-ask.test.ts
- test/delete-tier.test.ts
- test/mcp-cloud-cost.test.ts
- test/palette-roster.test.ts
docs/features/cloud-end.md +2
| @@ -10,3 +10,5 @@ On a machine's or a volume's page under /infra, an account the forge may write t | ||
| 10 | 10 | - **Web:** `/infra/end/<stop|delete>/<cloud>/<account>/<project>/<id>` (`src/web/pages/cloud_end.ts`, `src/http/infra_routes.ts`). |
| 11 | 11 | - **Terminal:** Cloud resources (`src/tui/screens/cloud_end.ts`): `x` plans a stop, `X` a delete, on the selected resource. |
| 12 | 12 | - **Rules:** `src/actions/cloud_end.ts`. A stopped machine is still billed by DigitalOcean, Hetzner and OVHcloud, and the plan says so. |
| 13 | - **MCP (#18103):** the `infra` tool's `cost` answers each cloud account against its ceiling and budget and the ten resources that cost most, each with the `id` the rest take (`src/actions/cloud_cost.ts`). `end` takes that `id` and `want: {act: stop|delete}` and returns the plan and its `digest`; the same call with `want.digest` (and a delete's `want.typed`, the resource's name) does it. From the forge's own agent that second call is held for the person's yes (`crew/conversation_confirm.ts`) and nothing reaches the cloud before it; a read-only account is refused by the same rules as the page. | |
| 14 | - **Terminal:** What the clouds cost (`src/tui/screens/cloudcost.ts`, palette, admins): the same account lines and the costliest resources. |
src/actions/cloud_cost.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | /** | |
| 2 | * The cost table in words, for every door that is not the page (#18103): what each cloud account | |
| 3 | * costs against its ceiling and budget, and the resources that cost most, each with the id the | |
| 4 | * `infra` tool's `end` plans a stop or delete from. The MCP tool and the terminal screen both read | |
| 5 | * it, so they say the same. Reads the last read of the clouds; no cloud is called. | |
| 6 | */ | |
| 7 | ||
| 8 | import { standingOf, standingWords } from "../cloud/budget.ts"; | |
| 9 | import { dollars } from "../cloud/prices.ts"; | |
| 10 | import { cloudName, costliestOf, moneyWords } from "../cloud/tree.ts"; | |
| 11 | import type { App } from "../app.ts"; | |
| 12 | import { listCloudAccounts } from "../core/cloud_account.ts"; | |
| 13 | import type { User } from "../core/users.ts"; | |
| 14 | import { ok, type Result } from "../kernel/result.ts"; | |
| 15 | import { cloudResources, infraOf, type Meter } from "./infra.ts"; | |
| 16 | ||
| 17 | export interface CostRow { | |
| 18 | readonly account: string; | |
| 19 | readonly says: string; | |
| 20 | } | |
| 21 | ||
| 22 | export interface CostliestRow { | |
| 23 | /** The id `infra` `cloud` and `end` take. */ | |
| 24 | readonly id: string; | |
| 25 | readonly name: string; | |
| 26 | readonly kind: string; | |
| 27 | readonly cost: string; | |
| 28 | /** Where it is: cloud ▸ account. */ | |
| 29 | readonly where: string; | |
| 30 | } | |
| 31 | ||
| 32 | const ceiling = (cents: number): string => dollars(cents).replace(/\.00$/, ""); | |
| 33 | ||
| 34 | /** | |
| 35 | * One account's month in two parts, so the page and the words are built from the same figures: | |
| 36 | * the amount (empty when the forge makes nothing on it, "unknown" when unread) and what follows | |
| 37 | * it ("of $50 a month on DigitalOcean"). The page puts the amount in a span, as it does a figure. | |
| 38 | */ | |
| 39 | export function meterFigure(m: Meter): { readonly amount: string; readonly tail: string } { | |
| 40 | if (!m.makes) return { amount: "", tail: m.label }; | |
| 41 | const amount = m.budget.spend.kind === "unknown" ? "unknown" : dollars(m.monthlyCents); | |
| 42 | return { amount, tail: ` of ${ceiling(m.ceilingCents)} a month on ${m.label}` }; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * One account's month against its ceiling and budget: "$13.00 of $50 a month on DigitalOcean | |
| 47 | * (main); budget $40 a month, 32% of it; …". | |
| 48 | */ | |
| 49 | export function meterWords(m: Meter): string { | |
| 50 | const name = m.account.split("/")[1] ?? m.account; | |
| 51 | const f = meterFigure(m); | |
| 52 | return `${f.amount}${f.tail} (${name}); ${standingWords(m.budget)}`; | |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * The cost table: each account with its budget, then the ten resources that cost most. An admin's. | |
| 57 | */ | |
| 58 | export function costTable(app: App, actor: User | undefined): Result<{ accounts: readonly CostRow[]; costliest: readonly CostliestRow[] }, string> { | |
| 59 | const infra = infraOf(app, actor); | |
| 60 | if (!infra.ok) return infra; | |
| 61 | const view = cloudResources(app, actor, ""); | |
| 62 | if (!view.ok) return view; | |
| 63 | const costliest = costliestOf(view.value.whole) | |
| 64 | .slice(0, 10) | |
| 65 | .map((n): CostliestRow => ({ id: n.path.join("/"), name: n.name, kind: n.resource?.kind ?? "", cost: moneyWords(n.money), where: n.path.slice(0, 2).join(" ▸ ") })); | |
| 66 | // Every account the forge holds, so a read-only one with no budget is never missing: the meter's | |
| 67 | // words where it has one, else its standing alone. | |
| 68 | const now = new Date(); | |
| 69 | const accounts = listCloudAccounts(app.db) | |
| 70 | .filter((a) => a.provider !== "github") | |
| 71 | .map((a): CostRow => { | |
| 72 | const account = `${a.provider}/${a.name}`; | |
| 73 | const m = infra.value.meters.find((x) => x.account === account); | |
| 74 | return { account, says: m !== undefined ? meterWords(m) : `${cloudName(a.provider)} (${a.name}); ${standingWords(standingOf(app.db, a, now))}` }; | |
| 75 | }); | |
| 76 | return ok({ accounts, costliest }); | |
| 77 | } |
src/crew/conversation_ask.ts +2
| @@ -136,6 +136,8 @@ function infra(a: Args): string { | ||
| 136 | 136 | const names = Array.isArray(a["names"]) ? a["names"].filter((n): n is string => typeof n === "string").map(plain).filter((n) => n !== "") : []; |
| 137 | 137 | if (names.length > 0) |
| 138 | 138 | return `Clear ${names.length === 1 ? "the dead row" : "the dead rows"} ${names.join(", ")}: nothing exists at the provider for ${names.length === 1 ? "it" : "them"}, so only the forge's ${names.length === 1 ? "row goes" : "rows go"} and nothing is deleted there`; |
| 139 | // Only a stop is ever held: a delete is the delete tier's, refused before it is asked. | |
| 140 | if (a["action"] === "end") return `Stop the cloud resource ${val(a, "id")} on your own cloud, as the plan you were shown says`; | |
| 139 | 141 | return `Build the infrastructure plan ${val(a, "id")} on your own cloud: it spends money each month`; |
| 140 | 142 | } |
| 141 | 143 |
src/crew/conversation_confirm.ts +1 −1
| @@ -149,7 +149,7 @@ export const ASKING: Readonly<Record<string, Asking>> = { | ||
| 149 | 149 | pinned_forge: { asks: not("verb", "list") }, |
| 150 | 150 | games: { asks: not("action", "list") }, |
| 151 | 151 | // A plan built on the owner's own cloud spends their money: the person says so first (#16696). |
| 152 | infra: { asks: (a) => a["action"] === "apply" }, | |
| 152 | infra: { asks: (a) => a["action"] === "apply" || (a["action"] === "end" && typeof (a["want"] as Args | undefined)?.["digest"] === "string" && (a["want"] as Args | undefined)?.["act"] !== "delete") }, | |
| 153 | 153 | outsiders: {}, |
| 154 | 154 | seal: {}, |
| 155 | 155 | migrate: {}, |
src/generated/screens.ts +115 −113
| @@ -28,115 +28,116 @@ import * as s23 from "../tui/screens/claim_link.ts"; | ||
| 28 | 28 | import * as s24 from "../tui/screens/claim_start.ts"; |
| 29 | 29 | import * as s25 from "../tui/screens/client_clouds.ts"; |
| 30 | 30 | import * as s26 from "../tui/screens/cloud.ts"; |
| 31 | import * as s27 from "../tui/screens/cloudtree.ts"; | |
| 32 | import * as s28 from "../tui/screens/codes.ts"; | |
| 33 | import * as s29 from "../tui/screens/connections.ts"; | |
| 34 | import * as s30 from "../tui/screens/controls.ts"; | |
| 35 | import * as s31 from "../tui/screens/cores.ts"; | |
| 36 | import * as s32 from "../tui/screens/crewbudgets.ts"; | |
| 37 | import * as s33 from "../tui/screens/crewfallback.ts"; | |
| 38 | import * as s34 from "../tui/screens/crewhandover.ts"; | |
| 39 | import * as s35 from "../tui/screens/crewkey.ts"; | |
| 40 | import * as s36 from "../tui/screens/crewsplits.ts"; | |
| 41 | import * as s37 from "../tui/screens/crewtop.ts"; | |
| 42 | import * as s38 from "../tui/screens/dataset.ts"; | |
| 43 | import * as s39 from "../tui/screens/design.ts"; | |
| 44 | import * as s40 from "../tui/screens/design_gallery.ts"; | |
| 45 | import * as s41 from "../tui/screens/devices.ts"; | |
| 46 | import * as s42 from "../tui/screens/disks.ts"; | |
| 47 | import * as s43 from "../tui/screens/displays.ts"; | |
| 48 | import * as s44 from "../tui/screens/domains.ts"; | |
| 49 | import * as s45 from "../tui/screens/dotfiles.ts"; | |
| 50 | import * as s46 from "../tui/screens/drift.ts"; | |
| 51 | import * as s47 from "../tui/screens/dutybudgets.ts"; | |
| 52 | import * as s48 from "../tui/screens/dutyswitch.ts"; | |
| 53 | import * as s49 from "../tui/screens/economics.ts"; | |
| 54 | import * as s50 from "../tui/screens/edgeonly.ts"; | |
| 55 | import * as s51 from "../tui/screens/experiments.ts"; | |
| 56 | import * as s52 from "../tui/screens/featurefeed.ts"; | |
| 57 | import * as s53 from "../tui/screens/features.ts"; | |
| 58 | import * as s54 from "../tui/screens/featureswitch.ts"; | |
| 59 | import * as s55 from "../tui/screens/feed.ts"; | |
| 60 | import * as s56 from "../tui/screens/flowsall.ts"; | |
| 61 | import * as s57 from "../tui/screens/footing.ts"; | |
| 62 | import * as s58 from "../tui/screens/forgeready.ts"; | |
| 63 | import * as s59 from "../tui/screens/forges.ts"; | |
| 64 | import * as s60 from "../tui/screens/forgeview.ts"; | |
| 65 | import * as s61 from "../tui/screens/front.ts"; | |
| 66 | import * as s62 from "../tui/screens/frontms.ts"; | |
| 67 | import * as s63 from "../tui/screens/funnel.ts"; | |
| 68 | import * as s64 from "../tui/screens/games.ts"; | |
| 69 | import * as s65 from "../tui/screens/gifts.ts"; | |
| 70 | import * as s66 from "../tui/screens/health_voice.ts"; | |
| 71 | import * as s67 from "../tui/screens/home_pins.ts"; | |
| 72 | import * as s68 from "../tui/screens/hostreach.ts"; | |
| 73 | import * as s69 from "../tui/screens/infra.ts"; | |
| 74 | import * as s70 from "../tui/screens/inventory.ts"; | |
| 75 | import * as s71 from "../tui/screens/invites.ts"; | |
| 76 | import * as s72 from "../tui/screens/knobs.ts"; | |
| 77 | import * as s73 from "../tui/screens/lapsed.ts"; | |
| 78 | import * as s74 from "../tui/screens/limits.ts"; | |
| 79 | import * as s75 from "../tui/screens/loopstalls.ts"; | |
| 80 | import * as s76 from "../tui/screens/madefrom.ts"; | |
| 81 | import * as s77 from "../tui/screens/maintainruns.ts"; | |
| 82 | import * as s78 from "../tui/screens/maintenance.ts"; | |
| 83 | import * as s79 from "../tui/screens/maintenanceweek.ts"; | |
| 84 | import * as s80 from "../tui/screens/mergedbranches.ts"; | |
| 85 | import * as s81 from "../tui/screens/mirrorpacks.ts"; | |
| 86 | import * as s82 from "../tui/screens/model.ts"; | |
| 87 | import * as s83 from "../tui/screens/money.ts"; | |
| 88 | import * as s84 from "../tui/screens/newissue.ts"; | |
| 89 | import * as s85 from "../tui/screens/next.ts"; | |
| 90 | import * as s86 from "../tui/screens/nowlist.ts"; | |
| 91 | import * as s87 from "../tui/screens/offers.ts"; | |
| 92 | import * as s88 from "../tui/screens/offsite.ts"; | |
| 93 | import * as s89 from "../tui/screens/orggrants.ts"; | |
| 94 | import * as s90 from "../tui/screens/orgteams.ts"; | |
| 95 | import * as s91 from "../tui/screens/originahead.ts"; | |
| 96 | import * as s92 from "../tui/screens/owner.ts"; | |
| 97 | import * as s93 from "../tui/screens/packrepos.ts"; | |
| 98 | import * as s94 from "../tui/screens/payments.ts"; | |
| 99 | import * as s95 from "../tui/screens/people.ts"; | |
| 100 | import * as s96 from "../tui/screens/pinned.ts"; | |
| 101 | import * as s97 from "../tui/screens/planner.ts"; | |
| 102 | import * as s98 from "../tui/screens/planrow.ts"; | |
| 103 | import * as s99 from "../tui/screens/plans.ts"; | |
| 104 | import * as s100 from "../tui/screens/previewdirs.ts"; | |
| 105 | import * as s101 from "../tui/screens/profile.ts"; | |
| 106 | import * as s102 from "../tui/screens/quotas.ts"; | |
| 107 | import * as s103 from "../tui/screens/recoverykit.ts"; | |
| 108 | import * as s104 from "../tui/screens/refused.ts"; | |
| 109 | import * as s105 from "../tui/screens/registry_items.ts"; | |
| 110 | import * as s106 from "../tui/screens/repolist.ts"; | |
| 111 | import * as s107 from "../tui/screens/requests.ts"; | |
| 112 | import * as s108 from "../tui/screens/resilience.ts"; | |
| 113 | import * as s109 from "../tui/screens/returning.ts"; | |
| 114 | import * as s110 from "../tui/screens/returns.ts"; | |
| 115 | import * as s111 from "../tui/screens/scorecard.ts"; | |
| 116 | import * as s112 from "../tui/screens/scrapes.ts"; | |
| 117 | import * as s113 from "../tui/screens/secrets.ts"; | |
| 118 | import * as s114 from "../tui/screens/sentinel.ts"; | |
| 119 | import * as s115 from "../tui/screens/series.ts"; | |
| 120 | import * as s116 from "../tui/screens/ship_time.ts"; | |
| 121 | import * as s117 from "../tui/screens/slowpages.ts"; | |
| 122 | import * as s118 from "../tui/screens/sold.ts"; | |
| 123 | import * as s119 from "../tui/screens/spend.ts"; | |
| 124 | import * as s120 from "../tui/screens/stalepulls.ts"; | |
| 125 | import * as s121 from "../tui/screens/subscriptions.ts"; | |
| 126 | import * as s122 from "../tui/screens/system.ts"; | |
| 127 | import * as s123 from "../tui/screens/tab.ts"; | |
| 128 | import * as s124 from "../tui/screens/teams.ts"; | |
| 129 | import * as s125 from "../tui/screens/telemetry.ts"; | |
| 130 | import * as s126 from "../tui/screens/tell_undo.ts"; | |
| 131 | import * as s127 from "../tui/screens/traces.ts"; | |
| 132 | import * as s128 from "../tui/screens/traffic.ts"; | |
| 133 | import * as s129 from "../tui/screens/trafficview.ts"; | |
| 134 | import * as s130 from "../tui/screens/valves.ts"; | |
| 135 | import * as s131 from "../tui/screens/waiting.ts"; | |
| 136 | import * as s132 from "../tui/screens/waiting_for_me.ts"; | |
| 137 | import * as s133 from "../tui/screens/wayback.ts"; | |
| 138 | import * as s134 from "../tui/screens/weekcard.ts"; | |
| 139 | import * as s135 from "../tui/screens/whocan.ts"; | |
| 31 | import * as s27 from "../tui/screens/cloudcost.ts"; | |
| 32 | import * as s28 from "../tui/screens/cloudtree.ts"; | |
| 33 | import * as s29 from "../tui/screens/codes.ts"; | |
| 34 | import * as s30 from "../tui/screens/connections.ts"; | |
| 35 | import * as s31 from "../tui/screens/controls.ts"; | |
| 36 | import * as s32 from "../tui/screens/cores.ts"; | |
| 37 | import * as s33 from "../tui/screens/crewbudgets.ts"; | |
| 38 | import * as s34 from "../tui/screens/crewfallback.ts"; | |
| 39 | import * as s35 from "../tui/screens/crewhandover.ts"; | |
| 40 | import * as s36 from "../tui/screens/crewkey.ts"; | |
| 41 | import * as s37 from "../tui/screens/crewsplits.ts"; | |
| 42 | import * as s38 from "../tui/screens/crewtop.ts"; | |
| 43 | import * as s39 from "../tui/screens/dataset.ts"; | |
| 44 | import * as s40 from "../tui/screens/design.ts"; | |
| 45 | import * as s41 from "../tui/screens/design_gallery.ts"; | |
| 46 | import * as s42 from "../tui/screens/devices.ts"; | |
| 47 | import * as s43 from "../tui/screens/disks.ts"; | |
| 48 | import * as s44 from "../tui/screens/displays.ts"; | |
| 49 | import * as s45 from "../tui/screens/domains.ts"; | |
| 50 | import * as s46 from "../tui/screens/dotfiles.ts"; | |
| 51 | import * as s47 from "../tui/screens/drift.ts"; | |
| 52 | import * as s48 from "../tui/screens/dutybudgets.ts"; | |
| 53 | import * as s49 from "../tui/screens/dutyswitch.ts"; | |
| 54 | import * as s50 from "../tui/screens/economics.ts"; | |
| 55 | import * as s51 from "../tui/screens/edgeonly.ts"; | |
| 56 | import * as s52 from "../tui/screens/experiments.ts"; | |
| 57 | import * as s53 from "../tui/screens/featurefeed.ts"; | |
| 58 | import * as s54 from "../tui/screens/features.ts"; | |
| 59 | import * as s55 from "../tui/screens/featureswitch.ts"; | |
| 60 | import * as s56 from "../tui/screens/feed.ts"; | |
| 61 | import * as s57 from "../tui/screens/flowsall.ts"; | |
| 62 | import * as s58 from "../tui/screens/footing.ts"; | |
| 63 | import * as s59 from "../tui/screens/forgeready.ts"; | |
| 64 | import * as s60 from "../tui/screens/forges.ts"; | |
| 65 | import * as s61 from "../tui/screens/forgeview.ts"; | |
| 66 | import * as s62 from "../tui/screens/front.ts"; | |
| 67 | import * as s63 from "../tui/screens/frontms.ts"; | |
| 68 | import * as s64 from "../tui/screens/funnel.ts"; | |
| 69 | import * as s65 from "../tui/screens/games.ts"; | |
| 70 | import * as s66 from "../tui/screens/gifts.ts"; | |
| 71 | import * as s67 from "../tui/screens/health_voice.ts"; | |
| 72 | import * as s68 from "../tui/screens/home_pins.ts"; | |
| 73 | import * as s69 from "../tui/screens/hostreach.ts"; | |
| 74 | import * as s70 from "../tui/screens/infra.ts"; | |
| 75 | import * as s71 from "../tui/screens/inventory.ts"; | |
| 76 | import * as s72 from "../tui/screens/invites.ts"; | |
| 77 | import * as s73 from "../tui/screens/knobs.ts"; | |
| 78 | import * as s74 from "../tui/screens/lapsed.ts"; | |
| 79 | import * as s75 from "../tui/screens/limits.ts"; | |
| 80 | import * as s76 from "../tui/screens/loopstalls.ts"; | |
| 81 | import * as s77 from "../tui/screens/madefrom.ts"; | |
| 82 | import * as s78 from "../tui/screens/maintainruns.ts"; | |
| 83 | import * as s79 from "../tui/screens/maintenance.ts"; | |
| 84 | import * as s80 from "../tui/screens/maintenanceweek.ts"; | |
| 85 | import * as s81 from "../tui/screens/mergedbranches.ts"; | |
| 86 | import * as s82 from "../tui/screens/mirrorpacks.ts"; | |
| 87 | import * as s83 from "../tui/screens/model.ts"; | |
| 88 | import * as s84 from "../tui/screens/money.ts"; | |
| 89 | import * as s85 from "../tui/screens/newissue.ts"; | |
| 90 | import * as s86 from "../tui/screens/next.ts"; | |
| 91 | import * as s87 from "../tui/screens/nowlist.ts"; | |
| 92 | import * as s88 from "../tui/screens/offers.ts"; | |
| 93 | import * as s89 from "../tui/screens/offsite.ts"; | |
| 94 | import * as s90 from "../tui/screens/orggrants.ts"; | |
| 95 | import * as s91 from "../tui/screens/orgteams.ts"; | |
| 96 | import * as s92 from "../tui/screens/originahead.ts"; | |
| 97 | import * as s93 from "../tui/screens/owner.ts"; | |
| 98 | import * as s94 from "../tui/screens/packrepos.ts"; | |
| 99 | import * as s95 from "../tui/screens/payments.ts"; | |
| 100 | import * as s96 from "../tui/screens/people.ts"; | |
| 101 | import * as s97 from "../tui/screens/pinned.ts"; | |
| 102 | import * as s98 from "../tui/screens/planner.ts"; | |
| 103 | import * as s99 from "../tui/screens/planrow.ts"; | |
| 104 | import * as s100 from "../tui/screens/plans.ts"; | |
| 105 | import * as s101 from "../tui/screens/previewdirs.ts"; | |
| 106 | import * as s102 from "../tui/screens/profile.ts"; | |
| 107 | import * as s103 from "../tui/screens/quotas.ts"; | |
| 108 | import * as s104 from "../tui/screens/recoverykit.ts"; | |
| 109 | import * as s105 from "../tui/screens/refused.ts"; | |
| 110 | import * as s106 from "../tui/screens/registry_items.ts"; | |
| 111 | import * as s107 from "../tui/screens/repolist.ts"; | |
| 112 | import * as s108 from "../tui/screens/requests.ts"; | |
| 113 | import * as s109 from "../tui/screens/resilience.ts"; | |
| 114 | import * as s110 from "../tui/screens/returning.ts"; | |
| 115 | import * as s111 from "../tui/screens/returns.ts"; | |
| 116 | import * as s112 from "../tui/screens/scorecard.ts"; | |
| 117 | import * as s113 from "../tui/screens/scrapes.ts"; | |
| 118 | import * as s114 from "../tui/screens/secrets.ts"; | |
| 119 | import * as s115 from "../tui/screens/sentinel.ts"; | |
| 120 | import * as s116 from "../tui/screens/series.ts"; | |
| 121 | import * as s117 from "../tui/screens/ship_time.ts"; | |
| 122 | import * as s118 from "../tui/screens/slowpages.ts"; | |
| 123 | import * as s119 from "../tui/screens/sold.ts"; | |
| 124 | import * as s120 from "../tui/screens/spend.ts"; | |
| 125 | import * as s121 from "../tui/screens/stalepulls.ts"; | |
| 126 | import * as s122 from "../tui/screens/subscriptions.ts"; | |
| 127 | import * as s123 from "../tui/screens/system.ts"; | |
| 128 | import * as s124 from "../tui/screens/tab.ts"; | |
| 129 | import * as s125 from "../tui/screens/teams.ts"; | |
| 130 | import * as s126 from "../tui/screens/telemetry.ts"; | |
| 131 | import * as s127 from "../tui/screens/tell_undo.ts"; | |
| 132 | import * as s128 from "../tui/screens/traces.ts"; | |
| 133 | import * as s129 from "../tui/screens/traffic.ts"; | |
| 134 | import * as s130 from "../tui/screens/trafficview.ts"; | |
| 135 | import * as s131 from "../tui/screens/valves.ts"; | |
| 136 | import * as s132 from "../tui/screens/waiting.ts"; | |
| 137 | import * as s133 from "../tui/screens/waiting_for_me.ts"; | |
| 138 | import * as s134 from "../tui/screens/wayback.ts"; | |
| 139 | import * as s135 from "../tui/screens/weekcard.ts"; | |
| 140 | import * as s136 from "../tui/screens/whocan.ts"; | |
| 140 | 141 | |
| 141 | 142 | /** Each screen file that declares palette entries: its module and the export names. */ |
| 142 | 143 | export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readonly string[]])[] = [ |
| @@ -222,11 +223,11 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 222 | 223 | [s79, ["entry"]], |
| 223 | 224 | [s80, ["entry"]], |
| 224 | 225 | [s81, ["entry"]], |
| 225 | [s82, ["modelGrantEntry","modelCeilingEntry"]], | |
| 226 | [s83, ["entry"]], | |
| 226 | [s82, ["entry"]], | |
| 227 | [s83, ["modelGrantEntry","modelCeilingEntry"]], | |
| 227 | 228 | [s84, ["entry"]], |
| 228 | [s85, ["nextAllEntry"]], | |
| 229 | [s86, ["entry"]], | |
| 229 | [s85, ["entry"]], | |
| 230 | [s86, ["nextAllEntry"]], | |
| 230 | 231 | [s87, ["entry"]], |
| 231 | 232 | [s88, ["entry"]], |
| 232 | 233 | [s89, ["entry"]], |
| @@ -276,4 +277,5 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 276 | 277 | [s133, ["entry"]], |
| 277 | 278 | [s134, ["entry"]], |
| 278 | 279 | [s135, ["entry"]], |
| 280 | [s136, ["entry"]], | |
| 279 | 281 | ]; |
src/http/mcp/tools/bring.ts +1 −1
| @@ -3,7 +3,7 @@ import { good, bad, str, type Tool } from "../shared.ts"; | ||
| 3 | 3 | |
| 4 | 4 | export const tool: Tool = { |
| 5 | 5 | name: "bring", |
| 6 | description: "Bring `repo` (link, or owner/name on `origin`); `share`: counted (default), named, none.", | |
| 6 | description: "Bring `repo` (link, or owner/name on `origin`); `share`: counted (default)|named|none.", | |
| 7 | 7 | inputSchema: { |
| 8 | 8 | type: "object", |
| 9 | 9 | properties: { |
src/http/mcp/tools/infra.ts +40 −4
| @@ -6,6 +6,8 @@ import { accountReads, projectsOf, projectsSays } from "../../../cloud/projects. | ||
| 6 | 6 | import { logsOfContainer } from "../../../cloud/containers.ts"; |
| 7 | 7 | import { CLOUD_PROPS, CONTAINER_COST, costLine, costliestOf, cutSays, factsOf, findingOf, listedOf, moneyWords, nodeAt, orderSays, pathIn, readingOf, tabsOf, tiesOf, type CloudNode } from "../../../cloud/tree.ts"; |
| 8 | 8 | import { buildOf, cloudResources, firewallWant, gameWant, planDatabase, goneWords, infraOf, infraSays, machineOf, nameFor, planById, planCompose, planWants, startBuild } from "../../../actions/infra.ts"; |
| 9 | import { costTable } from "../../../actions/cloud_cost.ts"; | |
| 10 | import { endActOf, performEnd, planEnd, planText } from "../../../actions/cloud_end.ts"; | |
| 9 | 11 | import { machineView } from "../../../actions/machine_view.ts"; |
| 10 | 12 | import { bindBucket, readBucket } from "../../../actions/bucket.ts"; |
| 11 | 13 | import { endsWords, keepPinned } from "../../../actions/pinned.ts"; |
| @@ -47,6 +49,15 @@ function named(whole: CloudNode, nodes: readonly CloudNode[], args: Args, says: | ||
| 47 | 49 | }); |
| 48 | 50 | } |
| 49 | 51 | |
| 52 | /** | |
| 53 | * The `want` of an `end`: the act, the digest of the plan being confirmed, and a delete's typed | |
| 54 | * name. | |
| 55 | */ | |
| 56 | const endWant = (args: Args): { act: string; digest: string; typed: string } => { | |
| 57 | const w = (typeof args["want"] === "object" && args["want"] !== null ? args["want"] : {}) as Record<string, unknown>; | |
| 58 | return { act: s(w["act"], 10), digest: s(w["digest"], 32), typed: s(w["typed"], 200) }; | |
| 59 | }; | |
| 60 | ||
| 50 | 61 | /** |
| 51 | 62 | * Infrastructure (#16696): what runs on the owner's own cloud, what it is for, what it costs and |
| 52 | 63 | * when it ends, and building one more machine through a plan. The experiments it holds took the |
| @@ -56,12 +67,12 @@ function named(whole: CloudNode, nodes: readonly CloudNode[], args: Args, says: | ||
| 56 | 67 | export const tool: Tool = { |
| 57 | 68 | name: "infra", |
| 58 | 69 | listedTo: "admin", |
| 59 | doc: "`{ action, id?, want?, question?, names?, result?, bucket?, cursor? }`: `list` (its `says` names every machine; with a filter line as `id`, like `kind:volume cost:>10 sort:-created`, the cloud resources it leaves, each with `partOf`, its app, and `attachedTo`; 50 a page, `cursor` for the next), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, repo, name?, host?}` or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against the forge's record), `keep`, `begin`, `attach`, `record_result`; `cloud` a machine's name as `id` (its provider's view: power, size, IPs, actions) or a resource's `id` from `list` (as last read; a game box's machine names its `containers`, each by the `id` this takes; `file` is what declares a machine or a container, env names only); `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`.", | |
| 70 | doc: "`{ action, id?, want?, question?, names?, result?, bucket?, cursor? }`: `list` (its `says` names every machine; with a filter line as `id`, like `kind:volume cost:>10 sort:-created`, the cloud resources it leaves, each with `partOf`, its app, and `attachedTo`; 50 a page, `cursor` for the next), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, repo, name?, host?}` or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against the forge's record), `keep`, `begin`, `attach`, `record_result`; `cloud` a machine's name as `id` (its provider's view: power, size, IPs, actions) or a resource's `id` from `list` (as last read; a game box's machine names its `containers`, each by the `id` this takes; `file` is what declares a machine or a container, env names only); `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`; `cost` (each account against its ceiling and budget, and what costs most); `end` a resource's `id` from `list` with `want` `{act: stop|delete}` for its plan and `digest`, then again with `want` `{act, digest, typed?}` to do it (held for the person's yes; `typed` is a delete's resource name).", | |
| 60 | 71 | description: "Machines, spend.", |
| 61 | 72 | inputSchema: { |
| 62 | 73 | type: "object", |
| 63 | 74 | properties: { |
| 64 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "check", "code", "begin", "attach", "record_result", "price", "bind", "cloud"] }, | |
| 75 | action: { type: "string", enum: ["list", "show", "plan", "apply", "keep", "check", "code", "begin", "attach", "record_result", "price", "bind", "cloud", "cost", "end"] }, | |
| 65 | 76 | id: { type: "string" }, |
| 66 | 77 | want: { type: "object" }, |
| 67 | 78 | question: { type: "string" }, |
| @@ -75,7 +86,12 @@ export const tool: Tool = { | ||
| 75 | 86 | // Marked a write so a `ro` token never reads the owner's spend. Only `apply` makes a machine, so |
| 76 | 87 | // only it is ever held for the person's word. |
| 77 | 88 | writes: true, |
| 78 | heldNotWhen: (args) => args["action"] !== "apply", | |
| 89 | // The one thing that destroys, a delete's confirm, is the delete tier's: refused to the forge's | |
HIGH: a delete is only held for a yes here. The owner's rule says a delete needs the red button and the person typing the name, and the forge's own agent must be refused outright ( |
||
| 90 | // own agent, done by a person on the page. A stop is held for the person's yes like an apply. | |
| 91 | tier: "delete", | |
| 92 | deletes: (args) => args["action"] === "end" && endWant(args).act === "delete" && endWant(args).digest !== "", | |
| 93 | where: "on the Cloud resources page (/infra/end/delete/<resource>), by typing its name", | |
| 94 | heldNotWhen: (args) => args["action"] !== "apply" && !(args["action"] === "end" && endWant(args).digest !== ""), | |
| 79 | 95 | async run(ctx, user, args) { |
| 80 | 96 | if (!user.isAdmin) return bad("not_found", "no such tool here"); |
| 81 | 97 | const db = ctx.app.db; |
| @@ -139,6 +155,26 @@ export const tool: Tool = { | ||
| 139 | 155 | const cloud = await cloudLinesFresh(ctx.app, v.value.groups.flatMap((g) => g.machines.map((m) => m.name))); |
| 140 | 156 | return good({ says: infraSays(v.value), ...experiments, ...rest, projects, cloud }); |
| 141 | 157 | } |
| 158 | if (action === "cost") { | |
| 159 | const t = costTable(ctx.app, user); | |
| 160 | return t.ok ? good({ says: t.value.accounts.length === 0 ? "No cloud account is attached." : t.value.accounts.map((a) => a.says).join(". "), accounts: t.value.accounts, costliest: t.value.costliest }) : bad("not_found", "no such tool here"); | |
| 161 | } | |
| 162 | if (action === "end") { | |
| 163 | // The cost table's planned stop or delete (#18103): the plan without a digest, the act with | |
| 164 | // the plan's. A read-only account is refused by the rules, not here. | |
| 165 | const want = endWant(args); | |
| 166 | const act = endActOf(want.act); | |
| 167 | if (id === "" || act === undefined) return bad("incomplete", "end takes a resource's id from list or cost, and want {act: stop or delete}"); | |
| 168 | const path = pathIn(id); | |
| 169 | if (want.digest === "") { | |
| 170 | const planned = planEnd(ctx.app, user, act, path); | |
| 171 | if (!planned.ok) return bad("not_found", planned.error === "not_found" ? "no such tool here" : planned.error); | |
| 172 | const p = planned.value; | |
| 173 | return good({ says: p.refusal === "" ? `${planText(p)} Confirm with want.digest ${p.digest}${act === "delete" ? ` and want.typed ${p.name}` : ""}.` : p.refusal, what: p.what, lines: p.lines, refusal: p.refusal, digest: p.digest }); | |
| 174 | } | |
| 175 | const done = await performEnd(ctx.app, user, act, path, { digest: want.digest, typed: want.typed }); | |
| 176 | return done.ok ? good({ says: done.value }) : bad("refused", done.error); | |
| 177 | } | |
| 142 | 178 | if (action === "check") { |
| 143 | 179 | // Compare the DNS records the forge made with Cloudflare now, as the hourly watch does. |
| 144 | 180 | const looked = await lookAtRecords(ctx.app, user); |
| @@ -317,7 +353,7 @@ export const tool: Tool = { | ||
| 317 | 353 | } |
| 318 | 354 | return good({ says: `Attached ${names.join(", ")}.`, experiment: findExperiment(db, id) }); |
| 319 | 355 | } |
| 320 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach, record_result, price, bind or cloud"); | |
| 356 | if (action !== "record_result") return bad("bad_action", "action is list, show, plan, apply, keep, begin, attach, record_result, price, bind, cloud, cost or end"); | |
| 321 | 357 | const done = recordResult(ctx.app, user.id, id, s(args["result"], 500)); |
| 322 | 358 | if (!done.ok) return bad(done.error, EXPERIMENT_WORDS[done.error]); |
| 323 | 359 | return good({ says: "Result recorded: the experiment is done.", experiment: findExperiment(db, id) }); |
src/http/mcp/tools/new_app.ts +1 −1
| @@ -34,7 +34,7 @@ export const tool: Tool = { | ||
| 34 | 34 | visibility: { type: "string", enum: ["public", "private"] }, |
| 35 | 35 | agent: { type: "string", maxLength: 39 }, |
| 36 | 36 | pick: { type: "boolean" }, |
| 37 | template: { type: "string", maxLength: 201, description: "owner/name to copy, readable by you" }, | |
| 37 | template: { type: "string", maxLength: 201, description: "owner/name to copy, yours to read" }, | |
| 38 | 38 | }, |
| 39 | 39 | }, |
| 40 | 40 | writes: true, |
src/http/mcp/tools/propose_upstream.ts +1 −1
| @@ -4,7 +4,7 @@ import { good, bad, str, type Tool } from "../shared.ts"; | ||
| 4 | 4 | |
| 5 | 5 | export const tool: Tool = { |
| 6 | 6 | name: "propose_upstream", |
| 7 | description: "Your copy `repo`'s `branch` as a pull request (`title`) at its origin; without `plan`, plans.", | |
| 7 | description: "Your copy `repo`'s `branch` as a pull (`title`) at its origin; no `plan`: plans.", | |
| 8 | 8 | inputSchema: { |
| 9 | 9 | type: "object", |
| 10 | 10 | properties: { |
src/tui/screens/cloudcost.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** | |
| 2 | * What the cloud accounts cost, in the terminal (#18103): each account against its ceiling and | |
| 3 | * budget, then the resources that cost most, in the words the `infra` MCP tool's `cost` answers | |
| 4 | * (`actions/cloud_cost.ts`). A stop or delete is planned from Cloud resources (`x`, `X`), or by the | |
| 5 | * tool's `end`. A forge admin's, and a person's: read-only. | |
| 6 | */ | |
| 7 | ||
| 8 | import { costTable } from "../../actions/cloud_cost.ts"; | |
| 9 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 10 | import { binding } from "../app/keys.ts"; | |
| 11 | import type { Session } from "../session.ts"; | |
| 12 | import { style } from "../term/style.ts"; | |
| 13 | import { Scroll, drawLines, hitScroll } from "./common.ts"; | |
| 14 | import type { PaletteEntry } from "./entries.ts"; | |
| 15 | ||
| 16 | const mayRead = (session: Session): boolean => session.user?.isAdmin === true && session.user.kind !== "agent"; | |
| 17 | ||
| 18 | /** The screen's lines; empty for anyone the gate refuses. */ | |
| 19 | export function costLines(session: Session): string[] { | |
| 20 | if (!mayRead(session)) return []; | |
| 21 | const t = costTable(session.app, session.user); | |
| 22 | if (!t.ok) return []; | |
| 23 | if (t.value.accounts.length === 0 && t.value.costliest.length === 0) return ["No cloud account is attached."]; | |
| 24 | return [ | |
| 25 | ...t.value.accounts.map((a) => a.says), | |
| 26 | ...(t.value.costliest.length === 0 ? [] : ["", "Costs most", ...t.value.costliest.map((r) => `${r.cost.padEnd(24)} ${r.kind} ${r.name} · ${r.where}`)]), | |
| 27 | ]; | |
| 28 | } | |
| 29 | ||
| 30 | export function cloudCostScreen(session: Session, ctx: Ctx): Screen { | |
| 31 | const scroll = new Scroll(); | |
| 32 | return { | |
| 33 | title: "What the clouds cost", | |
| 34 | keys: [binding(["j", "k"], "scroll"), binding(["esc", "q"], "back")], | |
| 35 | render(area, s) { | |
| 36 | const c = ctx.theme.c; | |
| 37 | const lines = costLines(session); | |
| 38 | drawLines(s, area, c, scroll, lines.map((text) => [{ text, style: style(text === "Costs most" ? c.fg : c.mutedFg, c.bg) }])); | |
| 39 | hitScroll(ctx, area, scroll, lines.length); | |
| 40 | }, | |
| 41 | key(e) { | |
| 42 | if (scroll.key(e, costLines(session).length)) return true; | |
| 43 | if (e.key === "esc" || e.key === "q") { | |
| 44 | ctx.pop(); | |
| 45 | return true; | |
| 46 | } | |
| 47 | return false; | |
| 48 | }, | |
| 49 | }; | |
| 50 | } | |
| 51 | ||
| 52 | export const entry: PaletteEntry = { | |
| 53 | rank: 142, | |
| 54 | id: "cloudcost", | |
| 55 | title: "What the clouds cost", | |
| 56 | hint: "each account against its budget, and what costs most", | |
| 57 | after: ["footing", "system"], | |
| 58 | offered: mayRead, | |
| 59 | run: (session, ctx) => ctx.push(cloudCostScreen(session, ctx)), | |
| 60 | }; |
src/web/pages/infra.ts +3 −6
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | import { standingWords } from "../../cloud/budget.ts"; |
| 15 | import { meterFigure } from "../../actions/cloud_cost.ts"; | |
| 15 | 16 | import { LIFETIME_DAYS } from "../../actions/pinned.ts"; |
| 16 | 17 | import type { CodeFile } from "../../cloud/infra_read.ts"; |
| 17 | 18 | import { restoredName, type SnapshotView } from "../../actions/infra_snapshot.ts"; |
| @@ -110,7 +111,6 @@ export interface ComposeState { | ||
| 110 | 111 | } |
| 111 | 112 | |
| 112 | 113 | const money = (cents: number): Html => html`<span class="num">${dollars(cents)}</span>`; |
| 113 | const ceiling = (cents: number): string => dollars(cents).replace(/\.00$/, ""); | |
| 114 | 114 | |
| 115 | 115 | /** One account's month against its ceiling, a sentence a line: "$13.00 of $50 a month on DigitalOcean". */ |
| 116 | 116 | function meters(v: InfraView): Html { |
| @@ -119,11 +119,8 @@ function meters(v: InfraView): Html { | ||
| 119 | 119 | return html`<div data-meters>${v.meters.map((m) => { |
| 120 | 120 | const who = many ? html` <span class="mono muted">${m.account.split("/")[1]}</span>` : null; |
| 121 | 121 | // The figure is the forge's own reckoning of what it made; unread, it says so and is never $0. |
| 122 | const figure = !m.makes | |
| 123 | ? html`${m.label}${who}` | |
| 124 | : m.budget.spend.kind === "unknown" | |
| 125 | ? html`<span class="num">unknown</span> of ${ceiling(m.ceilingCents)} a month on ${m.label}${who}` | |
| 126 | : html`${money(m.monthlyCents)} of ${ceiling(m.ceilingCents)} a month on ${m.label}${who}`; | |
| 122 | const f = meterFigure(m); | |
| 123 | const figure = f.amount === "" ? html`${f.tail}${who}` : html`<span class="num">${f.amount}</span>${f.tail}${who}`; | |
| 127 | 124 | return html`<p${m.makes && m.monthlyCents > m.ceilingCents ? raw(' data-over="1"') : null}>${figure}<br /><span class="muted small" data-budget>${standingWords(m.budget)}</span></p>`; |
| 128 | 125 | })}</div>`; |
| 129 | 126 | } |
test/conversation-ask.test.ts +1
| @@ -718,6 +718,7 @@ describe("the held action, in the person's words", () => { | ||
| 718 | 718 | test("the owner's held request reads as what it does", () => { |
| 719 | 719 | const text = |
| 720 | 720 | "Record forge metrics over time so the forge can draw graphs\nThe health read gives only a snapshot: memory free, disk free"; |
| 721 | expect(confirmSentence("infra", { action: "end", id: "hetzner/main/vol1", want: { act: "stop", digest: "d" } })).toStartWith("Stop the cloud resource"); | |
| 721 | 722 | expect(confirmSentence("tell", { text })).toBe( |
| 722 | 723 | 'Keep a note for you: "Record forge metrics over time so the forge can draw graphs".', |
| 723 | 724 | ); |
test/delete-tier.test.ts +4
| @@ -73,6 +73,10 @@ const SAMPLES: Readonly< | ||
| 73 | 73 | destroys: { repo: "alice/shop", dump: "old.sql", apply: true }, |
| 74 | 74 | spares: { repo: "alice/shop", dump: "old.sql" }, |
| 75 | 75 | }, |
| 76 | infra: { | |
| 77 | destroys: { action: "end", id: "hetzner/main/vol1", want: { act: "delete", digest: "abc", typed: "vol1" } }, | |
| 78 | spares: { action: "end", id: "hetzner/main/vol1", want: { act: "stop", digest: "abc" } }, | |
| 79 | }, | |
| 76 | 80 | mirror_set: { destroys: { action: "forget" } }, |
| 77 | 81 | org_teams: { |
| 78 | 82 | destroys: { org: "acme", action: "remove", team: "core" }, |
test/mcp-cloud-cost.test.ts added +166
| @@ -0,0 +1,166 @@ | ||
| 1 | /** | |
| 2 | * The cost table as MCP tools and a terminal screen (#18103), against a stand-in vendor: `infra` | |
| 3 | * `cost` reads each account with its budget, `end` without a digest returns the plan, and `end` | |
| 4 | * with one is held for the person's yes when the forge's own agent asks, refused on a read-only | |
| 5 | * account, and done (and audited) only for the person's own token. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync } from "node:fs"; | |
| 9 | import { rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { addAccount, allowWrite, net as cloudNet } from "../src/actions/cloud.ts"; | |
| 14 | import { costTable } from "../src/actions/cloud_cost.ts"; | |
| 15 | import { net } from "../src/actions/pinned.ts"; | |
| 16 | import { READ_ONLY } from "../src/core/cloud_account.ts"; | |
| 17 | import { createConversation, appendTurn } from "../src/core/conversations.ts"; | |
| 18 | import { createToken } from "../src/core/tokens.ts"; | |
| 19 | import { createUser, type User } from "../src/core/users.ts"; | |
| 20 | import { mintViaToken } from "../src/core/via_token.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | import { Session } from "../src/tui/forge.ts"; | |
| 23 | import { ENTRIES } from "../src/tui/screens/entries.ts"; | |
| 24 | import { costLines } from "../src/tui/screens/cloudcost.ts"; | |
| 25 | ||
| 26 | let dir = ""; | |
| 27 | let app: App; | |
| 28 | let servers: Servers; | |
| 29 | let admin: User; | |
| 30 | const AT = new Date().toISOString(); | |
| 31 | const asked: string[] = []; | |
| 32 | const saved = { fetch: net.fetch, cloud: cloudNet.fetch }; | |
| 33 | let rpc = 1; | |
| 34 | ||
| 35 | const resource = (account: string, id: string, title: string, cents: number, state: string): void => { | |
| 36 | app.db.run( | |
| 37 | "INSERT INTO cloud_resource (cloud_provider, cloud_account_id, cloud_resource_id, cloud_project_id, kind, name, cloud_region, host_type, label, made_by, cents, created_at, read_at, state, attached_to, made_from) VALUES ('digitalocean',?,?,'default',?,?,'nyc3','','','found',?,'2026-01-02T00:00:00Z',?,?,'','')", | |
| 38 | [account, id, id.split(":")[0]!, title, cents, AT, state], | |
| 39 | ); | |
| 40 | }; | |
| 41 | const call = async (token: string, args: Record<string, unknown>): Promise<{ text: string; error: boolean }> => { | |
| 42 | const res = await fetch(`${servers.url}/mcp`, { | |
| 43 | method: "POST", | |
| 44 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 45 | body: JSON.stringify({ jsonrpc: "2.0", id: rpc++, method: "tools/call", params: { name: "infra", arguments: args } }), | |
| 46 | }); | |
| 47 | const body = (await res.json()) as { result: { content: { text: string }[]; isError?: boolean } }; | |
| 48 | return { text: body.result.content[0]!.text, error: body.result.isError === true }; | |
| 49 | }; | |
| 50 | const idOf = async (token: string, name: string): Promise<string> => { | |
| 51 | const t = JSON.parse((await call(token, { action: "cost" })).text) as { costliest: { id: string; name: string }[] }; | |
| 52 | return t.costliest.find((r) => r.name === name)!.id; | |
| 53 | }; | |
| 54 | ||
| 55 | beforeAll(async () => { | |
| 56 | dir = mkdtempSync(join(tmpdir(), "forge-cloud-cost-")); | |
| 57 | app = createApp({ dataDir: dir, port: 0, url: "https://forge.example.net" }); | |
| 58 | servers = await serve(app); | |
| 59 | const u = await createUser(app.db, { username: "ada", email: "[email protected]", password: "a-good-passphrase", origin: "seed", isAdmin: true, trusted: true }); | |
| 60 | if (!u.ok) throw new Error(u.error); | |
| 61 | admin = u.value; | |
| 62 | cloudNet.fetch = async () => new Response(JSON.stringify({ account: { email: "[email protected]", status: "active" } }), { status: 200 }); | |
| 63 | for (const name of ["rw", "ro"]) { | |
| 64 | const a = await addAccount(app, admin, "digitalocean", name, "dop_v1_9Zq4TxLm7Kp2Vb8nWcR5"); | |
| 65 | if (!a.ok) throw new Error(String(a.error)); | |
| 66 | app.db.run("INSERT INTO cloud_project (cloud_provider, cloud_account_id, cloud_project_id, name, is_default, read_at) VALUES ('digitalocean',?,'default','default',1,?)", [name, AT]); | |
| 67 | } | |
| 68 | allowWrite(app, admin, "digitalocean", "rw", true); | |
| 69 | app.db.run("UPDATE cloud_account SET budget_dollars = 40 WHERE name = 'rw'"); | |
| 70 | resource("rw", "machine:111", "web", 1200, "active"); | |
| 71 | resource("rw", "volume:vol-9", "web-data", 100, "unattached"); | |
| 72 | resource("ro", "machine:222", "locked", 600, "active"); | |
| 73 | net.fetch = async (url, init) => { | |
| 74 | asked.push(`${init?.method ?? "GET"} ${new URL(url).pathname}`); | |
| 75 | return new Response(null, { status: 204 }); | |
| 76 | }; | |
| 77 | }, 30_000); | |
| 78 | afterAll(async () => { | |
| 79 | Object.assign(net, { fetch: saved.fetch }); | |
| 80 | cloudNet.fetch = saved.cloud; | |
| 81 | servers.stop(); | |
| 82 | app.close(); | |
| 83 | await rm(dir, { recursive: true, force: true }); | |
| 84 | }); | |
| 85 | ||
| 86 | describe("cost", () => { | |
| 87 | test("names each account with its budget, and the costliest resources", async () => { | |
| 88 | const token = createToken(app.db, admin.id, "t", "admin").token; | |
| 89 | const out = await call(token, { action: "cost" }); | |
| 90 | expect(out.error).toBe(false); | |
| 91 | const body = JSON.parse(out.text) as { says: string; accounts: { account: string; says: string }[]; costliest: { name: string }[] }; | |
| 92 | expect(body.accounts.map((a) => a.account).sort()).toEqual(["digitalocean/ro", "digitalocean/rw"]); | |
| 93 | expect(body.accounts.find((a) => a.account === "digitalocean/rw")!.says).toContain("budget $40 a month"); | |
| 94 | expect(body.accounts.find((a) => a.account === "digitalocean/ro")!.says).toContain("no budget set"); | |
| 95 | expect(body.costliest[0]!.name).toBe("web"); | |
| 96 | }); | |
| 97 | ||
| 98 | test("the terminal screen says what the tool says", async () => { | |
| 99 | const t = costTable(app, admin); | |
| 100 | if (!t.ok) throw new Error(t.error); | |
| 101 | expect(ENTRIES.some((e) => e.id === "cloudcost")).toBe(true); | |
| 102 | const as = (user: User) => new Session(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }); | |
| 103 | const lines = costLines(as(admin)); | |
| 104 | for (const a of t.value.accounts) expect(lines).toContain(a.says); | |
| 105 | expect(lines.join("\n")).toContain("Costs most"); | |
| 106 | const bob = await createUser(app.db, { username: "bob", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true }); | |
| 107 | if (!bob.ok) throw new Error(bob.error); | |
| 108 | expect(costLines(as(bob.value))).toEqual([]); | |
| 109 | }); | |
| 110 | }); | |
| 111 | ||
| 112 | describe("end", () => { | |
| 113 | test("without a digest it is a plan and calls no cloud", async () => { | |
| 114 | const token = createToken(app.db, admin.id, "t", "admin").token; | |
| 115 | const id = await idOf(token, "web"); | |
| 116 | const out = await call(token, { action: "end", id, want: { act: "stop" } }); | |
| 117 | expect(out.error).toBe(false); | |
| 118 | const plan = JSON.parse(out.text) as { digest: string; refusal: string; says: string }; | |
| 119 | expect(plan.refusal).toBe(""); | |
| 120 | expect(plan.says).toContain("$12.00 a month"); | |
| 121 | expect(plan.digest).toMatch(/^[0-9a-f]{16}$/); | |
| 122 | expect(asked).toEqual([]); | |
| 123 | }); | |
| 124 | ||
| 125 | test("the forge's own agent's confirm is held until the person says yes: nothing is called", async () => { | |
| 126 | const token = createToken(app.db, admin.id, "t", "admin").token; | |
| 127 | const id = await idOf(token, "web"); | |
| 128 | const plan = JSON.parse((await call(token, { action: "end", id, want: { act: "stop" } })).text) as { digest: string }; | |
| 129 | const c = createConversation(app.db, admin.id); | |
| 130 | appendTurn(app.db, admin.id, c.value.id, { role: "person", text: "stop web" }); | |
| 131 | const minted = mintViaToken(app.db, admin.id, `conversation:${c.value.id}`, "stop web", new Date(Date.now() + 600_000).toISOString()); | |
| 132 | if (!minted.ok) throw new Error(minted.error); | |
| 133 | const held = await call(minted.value.token, { action: "end", id, want: { act: "stop", digest: plan.digest } }); | |
| 134 | expect(held.error).toBe(true); | |
| 135 | expect(held.text).toContain("needs_confirm"); | |
| 136 | expect(asked).toEqual([]); | |
| 137 | }); | |
| 138 | ||
| 139 | test("a person's own token does it with the digest, and a stale digest is refused", async () => { | |
| 140 | const token = createToken(app.db, admin.id, "t", "admin").token; | |
| 141 | const id = await idOf(token, "web"); | |
| 142 | const stale = await call(token, { action: "end", id, want: { act: "stop", digest: "0".repeat(16) } }); | |
| 143 | expect(stale.error).toBe(true); | |
| 144 | expect(asked).toEqual([]); | |
| 145 | const plan = JSON.parse((await call(token, { action: "end", id, want: { act: "stop" } })).text) as { digest: string }; | |
| 146 | const done = await call(token, { action: "end", id, want: { act: "stop", digest: plan.digest } }); | |
| 147 | expect(done.error).toBe(false); | |
| 148 | expect(asked.filter((c) => !c.startsWith("GET"))).toEqual(["POST /v2/droplets/111/actions"]); | |
| 149 | }); | |
| 150 | ||
| 151 | test("a read-only account is refused with a plan that says so, and a delete wants the name typed", async () => { | |
| 152 | asked.length = 0; | |
| 153 | const token = createToken(app.db, admin.id, "t", "admin").token; | |
| 154 | const ro = await idOf(token, "locked"); | |
| 155 | const plan = JSON.parse((await call(token, { action: "end", id: ro, want: { act: "delete" } })).text) as { refusal: string }; | |
| 156 | expect(plan.refusal).toBe(READ_ONLY); | |
| 157 | const refused = await call(token, { action: "end", id: ro, want: { act: "delete", digest: "x", typed: "locked" } }); | |
| 158 | expect(refused.error).toBe(true); | |
| 159 | expect(refused.text).toContain(READ_ONLY); | |
| 160 | const vol = await idOf(token, "web-data"); | |
| 161 | const p = JSON.parse((await call(token, { action: "end", id: vol, want: { act: "delete" } })).text) as { digest: string }; | |
| 162 | const wrong = await call(token, { action: "end", id: vol, want: { act: "delete", digest: p.digest, typed: "no" } }); | |
| 163 | expect(wrong.error).toBe(true); | |
| 164 | expect(asked.filter((c) => !c.startsWith("GET"))).toEqual([]); | |
| 165 | }); | |
| 166 | }); |
test/palette-roster.test.ts +1
| @@ -155,6 +155,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 155 | 155 | ["stalls", "Main-thread stalls", "the last hour's stall ms by route"], |
| 156 | 156 | ["series", "Explore series", "query /metrics over time, explain a region"], |
| 157 | 157 | ["footing", "What the forge stands on", "each piece on another machine, in the order it would break the forge"], |
| 158 | ["cloudcost", "What the clouds cost", "each account against its budget, and what costs most"], | |
| 158 | 159 | ["design", "Design: the forge's theme", "colours and scales, the AA verdict; publish or revert"], |
| 159 | 160 | ["maintenanceweek", "Maintenance, last 7 days", "what each maintenance worker found and did this week"], |
| 160 | 161 | ["offsite", "Off-site backup", "where the sealed lane writes, when it last succeeded, and the last restore drill"], |
new above
offline
Only a stop should be askable here. A delete (
want.act === "delete") belongs to the delete tier and must be refused for the forge's own agent, not held.