Public: anyone can read this; the crew can change it.
map: one canvas for every app #20
global-map → main 1 commit
8 files
changed +466
−17
docs/PLANNING.md +16 −1
| @@ -54,7 +54,22 @@ converging, the bars say whether anything is moving. | ||
| 54 | 54 | |
| 55 | 55 | The same three lists are data at `GET /api/v1/repos/:owner/:name/map`, |
| 56 | 56 | which is what an agent reads before it starts and what a repository in |
| 57 | autonomous mode reads to pick the next thing. | |
| 57 | autonomous mode reads to pick the next thing. Both carry where the crew | |
| 58 | is: an issue with an agent run in flight is marked _working_ (or _queued_) | |
| 59 | on the drawing, and listed under `working` in the data. | |
| 60 | ||
| 61 | ### The global map | |
| 62 | ||
| 63 | `/map` is every app on one canvas. Each repository the viewer can see that | |
| 64 | has an open issue or a run in flight is one lane, stacked, headed by its | |
| 65 | name (a link to its own map), how it runs itself — _off_, _assisted_ or | |
| 66 | _autonomous_, with today's spend against the ceiling when it is not off — | |
| 67 | and how many runs are in flight. The lane is the same drawing as the | |
| 68 | repository's map. Repositories with nothing to draw are left out; a forge | |
| 69 | with nothing to draw anywhere says so in one sentence. It is reached from | |
| 70 | the user menu and the command palette, and from the Apps card on either | |
| 71 | home. `GET /api/v1/map` is the same, one entry per repository, each the | |
| 72 | repository's map data plus `autonomy`, `spentTodayUsd` and `inFlight`. | |
| 58 | 73 | |
| 59 | 74 | ## Planning by the crew |
| 60 | 75 |
src/http/api.ts +21 −4
| @@ -168,13 +168,19 @@ import { assignIssue } from "../actions/issues.ts"; | ||
| 168 | 168 | import { steerRepoRun } from "../actions/runs.ts"; |
| 169 | 169 | import { linkIssue, unlinkIssue } from "../actions/dependencies.ts"; |
| 170 | 170 | import { dependenciesOf, repoGraph } from "../core/dependencies.ts"; |
| 171 | import { mapJson } from "../web/pages/map.ts"; | |
| 171 | import { crewMarks, globalMapJson, mapJson } from "../web/pages/map.ts"; | |
| 172 | 172 | import { appsRuntime, type AppsRuntime } from "../apps/runtime.ts"; |
| 173 | 173 | import { isEnvName } from "../apps/data.ts"; |
| 174 | 174 | import type { Agent } from "../core/agents.ts"; |
| 175 | 175 | import { listApps, type AppRecord } from "../core/apps.ts"; |
| 176 | 176 | import { listAssignedTo } from "../core/issues.ts"; |
| 177 | import { checksForHead, getRun, isTerminal, type Run } from "../core/runs.ts"; | |
| 177 | import { | |
| 178 | checksForHead, | |
| 179 | getRun, | |
| 180 | isTerminal, | |
| 181 | listActiveRuns, | |
| 182 | type Run, | |
| 183 | } from "../core/runs.ts"; | |
| 178 | 184 | import { resolveOid } from "../git/merge.ts"; |
| 179 | 185 | import { readRunLog, subscribeRunLog } from "../runs/log.ts"; |
| 180 | 186 | import { blobForRender, commitLog, listRefs, listTree } from "../git/read.ts"; |
| @@ -1542,12 +1548,23 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1542 | 1548 | return new Response(null, { status: 204 }); |
| 1543 | 1549 | }, |
| 1544 | 1550 | ); |
| 1545 | // The map as data: ready now, blocked on, the critical path, every edge. | |
| 1551 | // The map as data: ready now, blocked on, the critical path, every edge, | |
| 1552 | // and where the crew is. | |
| 1546 | 1553 | router.add("GET", "/api/v1/repos/:owner/:name/map", (ctx) => { |
| 1547 | 1554 | const repo = loadRepo(ctx); |
| 1548 | 1555 | if (!repo) return notFound(); |
| 1549 | return json(mapJson(repo, repoGraph(ctx.app.db, repo.id))); | |
| 1556 | return json( | |
| 1557 | mapJson( | |
| 1558 | repo, | |
| 1559 | repoGraph(ctx.app.db, repo.id), | |
| 1560 | crewMarks(listActiveRuns(ctx.app.db, [repo.id])), | |
| 1561 | ), | |
| 1562 | ); | |
| 1550 | 1563 | }); |
| 1564 | // Every repository the caller can see, one entry each: the global map. | |
| 1565 | router.add("GET", "/api/v1/map", (ctx) => | |
| 1566 | json(globalMapJson(ctx.app, ctx.auth.user)), | |
| 1567 | ); | |
| 1551 | 1568 | |
| 1552 | 1569 | router.add( |
| 1553 | 1570 | "PATCH", |
src/http/server.ts +3 −1
| @@ -75,7 +75,7 @@ import { | ||
| 75 | 75 | newIssuePage, |
| 76 | 76 | newIssueSubmit, |
| 77 | 77 | } from "../web/pages/issues.ts"; |
| 78 | import { mapPage } from "../web/pages/map.ts"; | |
| 78 | import { globalMapPage, mapPage } from "../web/pages/map.ts"; | |
| 79 | 79 | import { |
| 80 | 80 | asksSubmit, |
| 81 | 81 | modeSubmit, |
| @@ -364,6 +364,8 @@ export function buildRouter(): Router<Handler> { | ||
| 364 | 364 | router.add("GET", "/apps", appsList); |
| 365 | 365 | router.add("GET", "/apps/new", newAppPage); |
| 366 | 366 | router.add("POST", "/apps/new", newAppSubmit); |
| 367 | // Every app on one canvas; a literal route, so it must sit above /:owner. | |
| 368 | router.add("GET", "/map", globalMapPage); | |
| 367 | 369 | router.add("GET", "/runs/:id", runRedirect); |
| 368 | 370 | router.add("GET", "/settings", accountPage); |
| 369 | 371 | router.add("POST", "/settings", accountUpdate); |
src/web/layout.ts +2
| @@ -155,6 +155,7 @@ function userMenu(user: User, signOut: MenuItem): Html { | ||
| 155 | 155 | { label: "Your profile", href: `/${user.username}`, icon: "user" }, |
| 156 | 156 | { label: "Your repositories", href: "/", icon: "repo" }, |
| 157 | 157 | { label: "Apps", href: "/apps", icon: "rocket" }, |
| 158 | { label: "Map", href: "/map", icon: "map" }, | |
| 158 | 159 | { separator: true, label: "" }, |
| 159 | 160 | { label: "Settings", href: "/settings", icon: "settings" }, |
| 160 | 161 | { label: "Access tokens", href: "/settings/tokens", icon: "key" }, |
| @@ -218,6 +219,7 @@ function helpButton(): Html { | ||
| 218 | 219 | const STATIC_PALETTE: PaletteItem[] = [ |
| 219 | 220 | { t: "Home", h: "/", c: "Go" }, |
| 220 | 221 | { t: "Apps", h: "/apps", c: "Go" }, |
| 222 | { t: "Map", h: "/map", c: "Go" }, | |
| 221 | 223 | { t: "New app", h: "/apps/new", c: "Create" }, |
| 222 | 224 | { t: "New repository", h: "/new", c: "Create" }, |
| 223 | 225 | { t: "New organization", h: "/organizations/new", c: "Create" }, |
src/web/pages/apps.ts +1
| @@ -157,6 +157,7 @@ export function appsCard(ctx: Ctx): Html { | ||
| 157 | 157 | const head = html`${icon("rocket")} |
| 158 | 158 | <h2>Apps</h2> |
| 159 | 159 | <span class="spacer"></span> |
| 160 | ${btn("Map", { href: "/map", size: "sm", icon: "map" })} | |
| 160 | 161 | ${btn("New app", { href: "/apps/new", size: "sm", icon: "plus" })}`; |
| 161 | 162 | if (entries.length === 0) { |
| 162 | 163 | return card( |
src/web/pages/map.ts +185 −11
| @@ -8,8 +8,13 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * Drawn on the server as SVG, laid out by hand: a plan bigger than the |
| 10 | 10 | * few hundred issues this handles is not one a person reads. |
| 11 | * | |
| 12 | * The global map at `/map` is the same drawing once per repository the | |
| 13 | * viewer can see, stacked: one canvas for every app, and where the crew is | |
| 14 | * marked on it. | |
| 11 | 15 | */ |
| 12 | 16 | |
| 17 | import type { App } from "../../app.ts"; | |
| 13 | 18 | import { |
| 14 | 19 | blockedOn, |
| 15 | 20 | criticalPath, |
| @@ -21,20 +26,24 @@ import { | ||
| 21 | 26 | type Ranked, |
| 22 | 27 | } from "../../core/dependencies.ts"; |
| 23 | 28 | import type { Issue } from "../../core/issues.ts"; |
| 24 | import type { Repo } from "../../core/repos.ts"; | |
| 29 | import { listReposVisibleTo, type Repo } from "../../core/repos.ts"; | |
| 30 | import { listActiveRuns, type Run, type RunState } from "../../core/runs.ts"; | |
| 31 | import type { User } from "../../core/users.ts"; | |
| 25 | 32 | import type { Ctx } from "../../http/context.ts"; |
| 26 | 33 | import { |
| 27 | 34 | crumbsFor, |
| 28 | 35 | loadRepo, |
| 29 | 36 | notFoundPage, |
| 37 | pageHead, | |
| 30 | 38 | repoBase, |
| 31 | 39 | repoHeader, |
| 32 | 40 | repoPalette, |
| 41 | signInFirst, | |
| 33 | 42 | } from "../chrome.ts"; |
| 34 | 43 | import { html, raw, type Html } from "../html.ts"; |
| 35 | 44 | import { page } from "../layout.ts"; |
| 36 | 45 | import { icon } from "../ui.ts"; |
| 37 | import { autonomyStatus } from "../../crew/autonomy.ts"; | |
| 46 | import { autonomyStatus, type AutonomyStatus } from "../../crew/autonomy.ts"; | |
| 38 | 47 | |
| 39 | 48 | // ---- layout ---- |
| 40 | 49 | |
| @@ -163,11 +172,31 @@ function trim(text: string, max = 24): string { | ||
| 163 | 172 | return text.length <= max ? text : `${text.slice(0, max - 1)}…`; |
| 164 | 173 | } |
| 165 | 174 | |
| 175 | /** | |
| 176 | * Where the crew is: each issue with an agent run in flight, by issue id. | |
| 177 | * A running run outranks a queued one on the same issue. Issue ids are | |
| 178 | * forge-wide, so one map serves every repository's drawing at once. | |
| 179 | */ | |
| 180 | export function crewMarks(runs: readonly Run[]): Map<string, RunState> { | |
| 181 | const marks = new Map<string, RunState>(); | |
| 182 | for (const run of runs) | |
| 183 | if (run.issueId !== null && marks.get(run.issueId) !== "running") | |
| 184 | marks.set(run.issueId, run.state); | |
| 185 | return marks; | |
| 186 | } | |
| 187 | ||
| 188 | export interface Emphasis { | |
| 189 | readonly ready: Set<string>; | |
| 190 | readonly critical: Set<string>; | |
| 191 | /** Issues the crew is on right now; see `crewMarks`. */ | |
| 192 | readonly crew: Map<string, RunState>; | |
| 193 | } | |
| 194 | ||
| 166 | 195 | export function drawMap( |
| 167 | 196 | repo: Repo, |
| 168 | 197 | graph: Graph, |
| 169 | 198 | placed: Layout, |
| 170 | emphasis: { ready: Set<string>; critical: Set<string> }, | |
| 199 | emphasis: Emphasis, | |
| 171 | 200 | ): Html { |
| 172 | 201 | if (placed.nodes.length === 0) |
| 173 | 202 | return html`<div class="map-empty"> |
| @@ -206,13 +235,25 @@ export function drawMap( | ||
| 206 | 235 | : "waiting"; |
| 207 | 236 | const px = x(n.column); |
| 208 | 237 | const py = y(n.row); |
| 238 | // The crew's marker: the studio's word for the run, and a dot while it | |
| 239 | // actually runs. Quiet on purpose; the node's colour still says ready. | |
| 240 | const run = emphasis.crew.get(n.issue.id); | |
| 209 | 241 | return html`<a |
| 210 | 242 | class="map-node" |
| 211 | 243 | data-state="${state}" |
| 212 | 244 | data-crit="${emphasis.critical.has(n.issue.id) ? "1" : "0"}" |
| 245 | ${run ? html`data-run="${run}"` : null} | |
| 213 | 246 | href="${base}/issues/${n.issue.number}" |
| 214 | 247 | > |
| 215 | 248 | <rect x="${px}" y="${py}" width="${W}" height="${H}" rx="7" /> |
| 249 | ${run === "running" | |
| 250 | ? html`<circle | |
| 251 | class="map-crew-dot" | |
| 252 | cx="${px + W - 13}" | |
| 253 | cy="${py + 13}" | |
| 254 | r="3.5" | |
| 255 | />` | |
| 256 | : null} | |
| 216 | 257 | <text class="map-title" x="${px + 12}" y="${py + 19}"> |
| 217 | 258 | ${trim(n.issue.title)} |
| 218 | 259 | </text> |
| @@ -223,6 +264,12 @@ export function drawMap( | ||
| 223 | 264 | : state === "ready" |
| 224 | 265 | ? " · ready" |
| 225 | 266 | : ""}${n.issue.assigneeName ? ` · ${n.issue.assigneeName}` : ""} |
| 267 | ${run | |
| 268 | ? html` · | |
| 269 | <tspan class="map-crew" | |
| 270 | >${run === "running" ? "working" : "queued"}</tspan | |
| 271 | >` | |
| 272 | : null} | |
| 226 | 273 | </text> |
| 227 | 274 | </a>`; |
| 228 | 275 | }); |
| @@ -323,6 +370,8 @@ export const mapCss = /* css */ ` | ||
| 323 | 370 | .map-node text { font-family: var(--font); pointer-events: none; } |
| 324 | 371 | .map-title { font-size: 12.5px; font-weight: 560; fill: var(--fg); } |
| 325 | 372 | .map-sub { font-size: 11px; fill: var(--fg-muted, var(--fg)); opacity: .72; } |
| 373 | .map-crew { fill: var(--accent); } | |
| 374 | .map-crew-dot { fill: var(--accent); } | |
| 326 | 375 | .map-edge { fill: none; stroke: var(--border); stroke-width: 1.4; } |
| 327 | 376 | .map-edge-crit { stroke: var(--accent); stroke-width: 2; } |
| 328 | 377 | .map-list h2 { font-size: 13px; margin: 0 0 6px; } |
| @@ -338,8 +387,24 @@ export const mapCss = /* css */ ` | ||
| 338 | 387 | .map-legend i.ready { background: color-mix(in srgb, var(--accent) 14%, var(--bg)); border-color: var(--accent); } |
| 339 | 388 | .map-legend i.done { background: color-mix(in srgb, var(--merged) 12%, var(--bg)); } |
| 340 | 389 | .map-legend i.crit { border: 2px solid var(--accent); background: transparent; } |
| 390 | .map-legend i.crew { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); border-color: var(--accent); vertical-align: 0; } | |
| 391 | .map-lane + .map-lane { margin-top: 28px; } | |
| 392 | .map-lane-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin: 0 0 8px; } | |
| 393 | .map-lane-head h2 { font-size: 15px; margin: 0; } | |
| 394 | .map-lane-head h2 a { color: inherit; } | |
| 341 | 395 | `; |
| 342 | 396 | |
| 397 | /** The swatches under a drawing: what each colour and mark means. */ | |
| 398 | function legend(): Html { | |
| 399 | return html`<div class="map-legend"> | |
| 400 | <span><i class="ready"></i>ready now</span> | |
| 401 | <span><i></i>waiting</span> | |
| 402 | <span><i class="done"></i>done</span> | |
| 403 | <span><i class="crit"></i>critical path</span> | |
| 404 | <span><i class="crew"></i>the crew is on it</span> | |
| 405 | </div>`; | |
| 406 | } | |
| 407 | ||
| 343 | 408 | /** One line on how the repository runs itself, when it does. */ |
| 344 | 409 | function autonomyLine(ctx: Ctx, repo: Repo): Html | null { |
| 345 | 410 | if (repo.autonomy === "off") return null; |
| @@ -368,6 +433,7 @@ export function mapPage(ctx: Ctx): Response { | ||
| 368 | 433 | const blocked = blockedOn(graph); |
| 369 | 434 | const critical = criticalPath(graph); |
| 370 | 435 | const placed = layout(graph); |
| 436 | const crew = crewMarks(listActiveRuns(ctx.app.db, [repo.id])); | |
| 371 | 437 | const body = html`${repoHeader(ctx, repo, "map")} |
| 372 | 438 | <div class="page-head mt"> |
| 373 | 439 | <h1>Map</h1> |
| @@ -382,13 +448,9 @@ export function mapPage(ctx: Ctx): Response { | ||
| 382 | 448 | ${drawMap(repo, graph, placed, { |
| 383 | 449 | ready: new Set(ready.map((r) => r.issue.id)), |
| 384 | 450 | critical: new Set(critical.map((i) => i.id)), |
| 451 | crew, | |
| 385 | 452 | })} |
| 386 | <div class="map-legend"> | |
| 387 | <span><i class="ready"></i>ready now</span> | |
| 388 | <span><i></i>waiting</span> | |
| 389 | <span><i class="done"></i>done</span> | |
| 390 | <span><i class="crit"></i>critical path</span> | |
| 391 | </div> | |
| 453 | ${legend()} | |
| 392 | 454 | </div> |
| 393 | 455 | <aside> |
| 394 | 456 | ${graph.issues.length === 0 |
| @@ -438,8 +500,15 @@ export function mapPage(ctx: Ctx): Response { | ||
| 438 | 500 | }); |
| 439 | 501 | } |
| 440 | 502 | |
| 441 | /** The map as data, for the crew and the tick: the same three lists. */ | |
| 442 | export function mapJson(repo: Repo, graph: Graph): Record<string, unknown> { | |
| 503 | /** | |
| 504 | * The map as data, for the crew and the tick: the same three lists, and | |
| 505 | * `working`, the issues the crew is on right now. | |
| 506 | */ | |
| 507 | export function mapJson( | |
| 508 | repo: Repo, | |
| 509 | graph: Graph, | |
| 510 | crew: Map<string, RunState> = new Map(), | |
| 511 | ): Record<string, unknown> { | |
| 443 | 512 | const brief = (i: Issue) => ({ |
| 444 | 513 | number: i.number, |
| 445 | 514 | title: i.title, |
| @@ -458,6 +527,7 @@ export function mapJson(repo: Repo, graph: Graph): Record<string, unknown> { | ||
| 458 | 527 | waitsFor: r.waitsFor, |
| 459 | 528 | })), |
| 460 | 529 | critical: criticalPath(graph).map((i) => i.number), |
| 530 | working: graph.issues.filter((i) => crew.has(i.id)).map((i) => i.number), | |
| 461 | 531 | issues: graph.issues.map(brief), |
| 462 | 532 | edges: graph.edges.map((e) => { |
| 463 | 533 | const a = graph.issues.find((i) => i.id === e.issueId); |
| @@ -466,3 +536,107 @@ export function mapJson(repo: Repo, graph: Graph): Record<string, unknown> { | ||
| 466 | 536 | }), |
| 467 | 537 | }; |
| 468 | 538 | } |
| 539 | ||
| 540 | // ---- every app on one canvas ---- | |
| 541 | ||
| 542 | interface Lane { | |
| 543 | readonly repo: Repo; | |
| 544 | readonly graph: Graph; | |
| 545 | /** Agent runs queued or running here now. */ | |
| 546 | readonly inFlight: number; | |
| 547 | /** Set when the repository runs itself; `off` has no spend to show. */ | |
| 548 | readonly status: AutonomyStatus | undefined; | |
| 549 | } | |
| 550 | ||
| 551 | interface Lanes { | |
| 552 | readonly lanes: Lane[]; | |
| 553 | readonly crew: Map<string, RunState>; | |
| 554 | } | |
| 555 | ||
| 556 | /** | |
| 557 | * One lane per repository the viewer can see that has something to draw: | |
| 558 | * an open issue, or a run in flight. The rest are left out rather than | |
| 559 | * drawn empty, so the page is as long as the work and no longer. One read | |
| 560 | * for every run in flight; a graph only for the lanes that stay. | |
| 561 | */ | |
| 562 | export function globalLanes(app: App, viewer: User | undefined): Lanes { | |
| 563 | const db = app.db; | |
| 564 | const repos = listReposVisibleTo(db, viewer, { limit: 200 }); | |
| 565 | const runs = listActiveRuns( | |
| 566 | db, | |
| 567 | repos.map((r) => r.id), | |
| 568 | ); | |
| 569 | const inFlight = new Map<string, number>(); | |
| 570 | for (const run of runs) | |
| 571 | inFlight.set(run.repoId, (inFlight.get(run.repoId) ?? 0) + 1); | |
| 572 | const lanes: Lane[] = []; | |
| 573 | for (const repo of repos) { | |
| 574 | const flying = inFlight.get(repo.id) ?? 0; | |
| 575 | if ((repo.counts?.openIssues ?? 0) === 0 && flying === 0) continue; | |
| 576 | lanes.push({ | |
| 577 | repo, | |
| 578 | graph: repoGraph(db, repo.id), | |
| 579 | inFlight: flying, | |
| 580 | status: repo.autonomy === "off" ? undefined : autonomyStatus(app, repo), | |
| 581 | }); | |
| 582 | } | |
| 583 | return { lanes, crew: crewMarks(runs) }; | |
| 584 | } | |
| 585 | ||
| 586 | /** A lane: the repository's name, how it runs itself, and its map. */ | |
| 587 | function laneHtml(lane: Lane, crew: Map<string, RunState>): Html { | |
| 588 | const { repo, graph, status } = lane; | |
| 589 | const spend = | |
| 590 | status === undefined | |
| 591 | ? "" | |
| 592 | : ` · $${status.spentTodayUsd.toFixed(2)} of $${status.ceilingUsd.toFixed(0)} today`; | |
| 593 | const flying = | |
| 594 | lane.inFlight === 0 | |
| 595 | ? "" | |
| 596 | : ` · ${lane.inFlight} ${lane.inFlight === 1 ? "run" : "runs"} in flight`; | |
| 597 | return html`<section class="map-lane"> | |
| 598 | <header class="map-lane-head"> | |
| 599 | <h2> | |
| 600 | <a href="${repoBase(repo)}/map">${repo.ownerName}/${repo.name}</a> | |
| 601 | </h2> | |
| 602 | <span class="muted small">${repo.autonomy}${spend}${flying}</span> | |
| 603 | </header> | |
| 604 | ${drawMap(repo, graph, layout(graph), { | |
| 605 | ready: new Set(readyNow(graph).map((r) => r.issue.id)), | |
| 606 | critical: new Set(criticalPath(graph).map((i) => i.id)), | |
| 607 | crew, | |
| 608 | })} | |
| 609 | </section>`; | |
| 610 | } | |
| 611 | ||
| 612 | /** GET /map — every app the viewer can see, one lane each. */ | |
| 613 | export function globalMapPage(ctx: Ctx): Response { | |
| 614 | const viewer = ctx.auth.user; | |
| 615 | if (!viewer) return signInFirst(ctx); | |
| 616 | const { lanes, crew } = globalLanes(ctx.app, viewer); | |
| 617 | const body = html`${pageHead("Map", { | |
| 618 | sub: "Every app you can see, laid out by what waits for what. The left edge of each lane is ready now; a marked issue is where the crew is.", | |
| 619 | })} | |
| 620 | ${lanes.length === 0 | |
| 621 | ? html`<p class="muted"> | |
| 622 | Nothing to map: no open issues anywhere you can see, and nobody working. | |
| 623 | </p>` | |
| 624 | : html`${lanes.map((lane) => laneHtml(lane, crew))} ${legend()}`}`; | |
| 625 | return page({ ctx, title: "Map", body, styleCss: mapCss }); | |
| 626 | } | |
| 627 | ||
| 628 | /** GET /api/v1/map — the lanes as data, one entry per repository. */ | |
| 629 | export function globalMapJson( | |
| 630 | app: App, | |
| 631 | viewer: User | undefined, | |
| 632 | ): Record<string, unknown> { | |
| 633 | const { lanes, crew } = globalLanes(app, viewer); | |
| 634 | return { | |
| 635 | repos: lanes.map((lane) => ({ | |
| 636 | ...mapJson(lane.repo, lane.graph, crew), | |
| 637 | autonomy: lane.repo.autonomy, | |
| 638 | spentTodayUsd: lane.status?.spentTodayUsd ?? null, | |
| 639 | inFlight: lane.inFlight, | |
| 640 | })), | |
| 641 | }; | |
| 642 | } |
src/web/pages/studio.ts +1
| @@ -892,6 +892,7 @@ export async function studioHome(ctx: Ctx, user: User): Promise<Response> { | ||
| 892 | 892 | head: html`${icon("rocket")} |
| 893 | 893 | <h2>Your apps</h2> |
| 894 | 894 | <span class="spacer"></span> |
| 895 | ${btn("Map", { href: "/map", size: "sm", icon: "map" })} | |
| 895 | 896 | ${btn("New app", { href: "/apps/new", size: "sm", icon: "plus" })}`, |
| 896 | 897 | }); |
| 897 | 898 |
test/map.test.ts added +237
| @@ -0,0 +1,237 @@ | ||
| 1 | /** | |
| 2 | * The global map: every app a person can see on one canvas, one lane per | |
| 3 | * repository with something to draw, where the crew is marked on it. A | |
| 4 | * lane is exactly the repository's own map, so what the per-repo page | |
| 5 | * says and what `/map` says never disagree. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { assignIssue, openIssue } from "../src/actions/issues.ts"; | |
| 14 | import { createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { addDependency } from "../src/core/dependencies.ts"; | |
| 16 | import type { Issue } from "../src/core/issues.ts"; | |
| 17 | import type { Repo } from "../src/core/repos.ts"; | |
| 18 | import { claimRun, createRun } from "../src/core/runs.ts"; | |
| 19 | import { createToken } from "../src/core/tokens.ts"; | |
| 20 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | ||
| 23 | describe("the global map", () => { | |
| 24 | let dataDir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let base: string; | |
| 28 | let alice: User; | |
| 29 | let bob: User; | |
| 30 | let engine: Repo; | |
| 31 | const engineIssues: Issue[] = []; | |
| 32 | ||
| 33 | const signIn = async (username: string): Promise<string> => | |
| 34 | ( | |
| 35 | ( | |
| 36 | await fetch(`${base}/signin`, { | |
| 37 | method: "POST", | |
| 38 | body: new URLSearchParams({ | |
| 39 | username, | |
| 40 | password: "a-good-passphrase", | |
| 41 | }), | |
| 42 | redirect: "manual", | |
| 43 | }) | |
| 44 | ).headers.get("set-cookie") ?? "" | |
| 45 | ).split(";")[0]!; | |
| 46 | const person = async (username: string, admin = false): Promise<User> => { | |
| 47 | const made = await createUser(app.db, { | |
| 48 | username, | |
| 49 | email: `${username}@forge.local`, | |
| 50 | password: "a-good-passphrase", | |
| 51 | adminIfFirst: admin, | |
| 52 | }); | |
| 53 | if (!made.ok) throw new Error(made.error); | |
| 54 | return made.value; | |
| 55 | }; | |
| 56 | const repoOf = async ( | |
| 57 | owner: User, | |
| 58 | name: string, | |
| 59 | titles: string[], | |
| 60 | ): Promise<{ repo: Repo; issues: Issue[] }> => { | |
| 61 | const made = await createRepo(app, owner, { name, visibility: "private" }); | |
| 62 | if (!made.ok) throw new Error(String(made.error)); | |
| 63 | const issues = titles.map((title) => { | |
| 64 | const opened = openIssue(app, owner, made.value, { title }); | |
| 65 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 66 | return opened.value; | |
| 67 | }); | |
| 68 | return { repo: made.value, issues }; | |
| 69 | }; | |
| 70 | ||
| 71 | beforeAll(async () => { | |
| 72 | dataDir = await mkdtemp(join(tmpdir(), "forge-map-")); | |
| 73 | app = createApp({ dataDir, port: 0 }); | |
| 74 | servers = await serve(app); | |
| 75 | base = servers.url; | |
| 76 | alice = await person("alice", true); | |
| 77 | bob = await person("bob"); | |
| 78 | await person("carol"); | |
| 79 | ||
| 80 | // alice/engine: three issues, 2 waits for 1, and the crew on 1. | |
| 81 | const made = await repoOf(alice, "engine", [ | |
| 82 | "Extract renderer", | |
| 83 | "Scene graph", | |
| 84 | "Write the docs", | |
| 85 | ]); | |
| 86 | engine = made.repo; | |
| 87 | engineIssues.push(...made.issues); | |
| 88 | expect( | |
| 89 | addDependency(app.db, engineIssues[1]!, engineIssues[0]!, alice.id).ok, | |
| 90 | ).toBe(true); | |
| 91 | const scout = await createAgent(app, alice, { username: "scout" }); | |
| 92 | if (!scout.ok) throw new Error(String(scout.error)); | |
| 93 | expect(addToCrew(app, alice, engine, "scout").ok).toBe(true); | |
| 94 | const scoutUser = getUserByName(app.db, "scout")!; | |
| 95 | const assigned = assignIssue( | |
| 96 | app, | |
| 97 | alice, | |
| 98 | engine, | |
| 99 | engineIssues[0]!, | |
| 100 | scoutUser, | |
| 101 | ); | |
| 102 | if (!assigned.ok) throw new Error(String(assigned.error)); | |
| 103 | const run = createRun(app.db, { | |
| 104 | repoId: engine.id, | |
| 105 | kind: "agent", | |
| 106 | issueId: engineIssues[0]!.id, | |
| 107 | actorId: scoutUser.id, | |
| 108 | reason: "assigned", | |
| 109 | }); | |
| 110 | expect(claimRun(app.db, run.id)).toBeDefined(); | |
| 111 | ||
| 112 | // alice/bare: a repository with nothing open and nobody working. | |
| 113 | await repoOf(alice, "bare", []); | |
| 114 | // bob/secret: bob's own, which alice cannot see and bob can. | |
| 115 | await repoOf(bob, "secret", ["Rotate the keys"]); | |
| 116 | }); | |
| 117 | ||
| 118 | afterAll(async () => { | |
| 119 | servers.stop(); | |
| 120 | app.close(); | |
| 121 | await rm(dataDir, { recursive: true, force: true }); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a person sees a lane for each repository they can read, and no other", async () => { | |
| 125 | const cookie = await signIn("alice"); | |
| 126 | const page = await ( | |
| 127 | await fetch(`${base}/map`, { | |
| 128 | headers: { cookie: `${cookie}; forge_mode=studio` }, | |
| 129 | }) | |
| 130 | ).text(); | |
| 131 | expect(page).toContain("<h1>Map</h1>"); | |
| 132 | expect(page).toContain('href="/alice/engine/map"'); | |
| 133 | expect(page).toContain("Extract renderer"); | |
| 134 | expect(page).toContain("Scene graph"); | |
| 135 | // The lane says how the repository runs itself and what is in flight. | |
| 136 | expect(page).toContain("off · 1 run in flight"); | |
| 137 | // Where the crew is: the studio's word on the issue, and the dot. | |
| 138 | expect(page).toContain('data-run="running"'); | |
| 139 | expect(page).toContain(">working</tspan"); | |
| 140 | expect(page).toContain('class="map-crew-dot"'); | |
| 141 | // A repository with nothing to draw is not a lane. | |
| 142 | expect(page).not.toContain('href="/alice/bare/map"'); | |
| 143 | // The forge's admin reads every repository, so bob's is a lane here … | |
| 144 | expect(page).toContain('href="/bob/secret/map"'); | |
| 145 | ||
| 146 | // … and bob, who is nobody's admin, sees his own and not alice's. | |
| 147 | const bobs = await ( | |
| 148 | await fetch(`${base}/map`, { headers: { cookie: await signIn("bob") } }) | |
| 149 | ).text(); | |
| 150 | expect(bobs).toContain('href="/bob/secret/map"'); | |
| 151 | expect(bobs).toContain("Rotate the keys"); | |
| 152 | expect(bobs).not.toContain("alice/engine"); | |
| 153 | expect(bobs).not.toContain("Extract renderer"); | |
| 154 | ||
| 155 | // The per-repo map carries the same marker, so the two never disagree. | |
| 156 | const own = await ( | |
| 157 | await fetch(`${base}/alice/engine/map`, { headers: { cookie } }) | |
| 158 | ).text(); | |
| 159 | expect(own).toContain('data-run="running"'); | |
| 160 | expect(own).toContain(">working</tspan"); | |
| 161 | }); | |
| 162 | ||
| 163 | test("nothing to draw anywhere is one quiet sentence; a stranger is sent to sign in", async () => { | |
| 164 | const page = await ( | |
| 165 | await fetch(`${base}/map`, { headers: { cookie: await signIn("carol") } }) | |
| 166 | ).text(); | |
| 167 | expect(page).toContain("Nothing to map"); | |
| 168 | expect(page).not.toContain('class="map-lane"'); | |
| 169 | expect(page).not.toContain("No open issues to map"); | |
| 170 | ||
| 171 | const anonymous = await fetch(`${base}/map`, { redirect: "manual" }); | |
| 172 | expect(anonymous.status).toBe(303); | |
| 173 | expect(anonymous.headers.get("location")).toContain("/signin"); | |
| 174 | }); | |
| 175 | ||
| 176 | test("the map is reached from the user menu and the palette on both homes", async () => { | |
| 177 | const cookie = await signIn("alice"); | |
| 178 | for (const mode of ["studio", "dev"]) { | |
| 179 | const home = await ( | |
| 180 | await fetch(`${base}/`, { | |
| 181 | headers: { cookie: `${cookie}; forge_mode=${mode}` }, | |
| 182 | }) | |
| 183 | ).text(); | |
| 184 | expect(home).toContain('href="/map"'); | |
| 185 | expect(home).toContain('"t":"Map","h":"/map"'); | |
| 186 | } | |
| 187 | }); | |
| 188 | ||
| 189 | test("the data has one entry per visible repository, with its issues and where the crew is", async () => { | |
| 190 | const token = createToken(app.db, alice.id, "test", "rw").token; | |
| 191 | const res = await fetch(`${base}/api/v1/map`, { | |
| 192 | headers: { authorization: `Bearer ${token}` }, | |
| 193 | }); | |
| 194 | expect(res.status).toBe(200); | |
| 195 | const data = (await res.json()) as { | |
| 196 | repos: { | |
| 197 | repo: { owner: string; name: string }; | |
| 198 | autonomy: string; | |
| 199 | spentTodayUsd: number | null; | |
| 200 | inFlight: number; | |
| 201 | issues: { number: number; title: string }[]; | |
| 202 | ready: { number: number }[]; | |
| 203 | working: number[]; | |
| 204 | edges: { issue: number; blockedBy: number }[]; | |
| 205 | }[]; | |
| 206 | }; | |
| 207 | const names = data.repos.map((r) => `${r.repo.owner}/${r.repo.name}`); | |
| 208 | expect(names).toContain("alice/engine"); | |
| 209 | expect(names).toContain("bob/secret"); | |
| 210 | expect(names).not.toContain("alice/bare"); | |
| 211 | const lane = data.repos.find((r) => r.repo.name === "engine")!; | |
| 212 | expect(lane.issues.map((i) => i.number)).toEqual([1, 2, 3]); | |
| 213 | expect(lane.ready.map((r) => r.number)).toEqual([1, 3]); | |
| 214 | expect(lane.edges).toEqual([{ issue: 2, blockedBy: 1 }]); | |
| 215 | expect(lane.working).toEqual([1]); | |
| 216 | expect(lane.autonomy).toBe("off"); | |
| 217 | expect(lane.spentTodayUsd).toBeNull(); | |
| 218 | expect(lane.inFlight).toBe(1); | |
| 219 | ||
| 220 | // The per-repo data says the same about where the crew is. | |
| 221 | const own = (await ( | |
| 222 | await fetch(`${base}/api/v1/repos/alice/engine/map`, { | |
| 223 | headers: { authorization: `Bearer ${token}` }, | |
| 224 | }) | |
| 225 | ).json()) as { working: number[] }; | |
| 226 | expect(own.working).toEqual([1]); | |
| 227 | ||
| 228 | // A stranger's token sees only what a stranger can: nothing private. | |
| 229 | const bobToken = createToken(app.db, bob.id, "test", "ro").token; | |
| 230 | const bobs = (await ( | |
| 231 | await fetch(`${base}/api/v1/map`, { | |
| 232 | headers: { authorization: `Bearer ${bobToken}` }, | |
| 233 | }) | |
| 234 | ).json()) as { repos: { repo: { owner: string } }[] }; | |
| 235 | expect(bobs.repos.map((r) => r.repo.owner)).toEqual(["bob"]); | |
| 236 | }); | |
| 237 | }); |
new above
offline