Public: anyone can read this; the crew can change it.
The look is taken when the preview starts serving, not when its tests finish #45
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/40-the-look-is-taken-when-the-preview-start → main 1 commit fork
5 files
changed +179
−5
src/apps/runtime.ts +19
| @@ -674,6 +674,25 @@ export class AppsRuntime { | ||
| 674 | 674 | ? `serving ${url} — ${elapsedSeconds(deployStartedAt, servingAt)} after the deploy started` |
| 675 | 675 | : `serving ${url}`, |
| 676 | 676 | ); |
| 677 | // The same statement the log line above just made, on the bus: a | |
| 678 | // subscriber (the looks service) must never be able to take the | |
| 679 | // deploy path down with it. | |
| 680 | try { | |
| 681 | this.app.bus.append({ | |
| 682 | kind: "app.serving", | |
| 683 | repoId: repo.id, | |
| 684 | payload: { | |
| 685 | app: state.app.slug, | |
| 686 | env: state.env, | |
| 687 | commitOid: live.commitOid, | |
| 688 | servingAt, | |
| 689 | ...(deployStartedAt === null ? {} : { deployStartedAt }), | |
| 690 | testsRunning: live.manifest.test !== null, | |
| 691 | }, | |
| 692 | }); | |
| 693 | } catch (error) { | |
| 694 | log.error("could not publish app.serving", { error: String(error) }); | |
| 695 | } | |
| 677 | 696 | |
| 678 | 697 | const tested = await outcome.tested; |
| 679 | 698 | state.deploying = undefined; |
src/design/activity.ts +2
| @@ -52,6 +52,8 @@ export function eventVerb(event: ForgeEvent): string { | ||
| 52 | 52 | ? `put ${who} on the crew` |
| 53 | 53 | : `took ${who} off the crew`; |
| 54 | 54 | } |
| 55 | if (kind === "app.serving") | |
| 56 | return `started serving ${environment(payload["env"])}`; | |
| 55 | 57 | if (kind === "app.stopped") return `stopped ${environment(payload["env"])}`; |
| 56 | 58 | if (kind === "app.env_set" || kind === "app.env_deleted") { |
| 57 | 59 | const name = payload["name"]; |
src/looks/service.ts +32 −5
| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | /** |
| 2 | * The look (docs/APPS.md): when an environment's deploy comes up green, | |
| 3 | * the forge takes a picture of it at phone and desktop width, as the | |
| 2 | * The look (docs/APPS.md): when an environment's deploy answers and swaps | |
| 3 | * in, the forge takes a picture of it at phone and desktop width, as the | |
| 4 | 4 | * repository's owner would see it, and keeps the pictures beside the |
| 5 | * commit they are of. A person judges a change from what it is. | |
| 5 | * commit they are of. A person judges a change from what it is — the | |
| 6 | * moment it starts serving, not the moment its tests, still running | |
| 7 | * beside it, happen to finish (and a preview whose tests then fail keeps | |
| 8 | * the picture it already has). | |
| 6 | 9 | * |
| 7 | 10 | * Best-effort by design: a look never blocks a check, never fails a run, |
| 8 | 11 | * and a picture that could not be taken is a row that says why, so a page |
| @@ -129,6 +132,23 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 129 | 132 | if (stopped || event.repoId === null) return; |
| 130 | 133 | const payload = event.payload; |
| 131 | 134 | switch (event.kind) { |
| 135 | // The instant a deploy answers and swaps in — the look is of what | |
| 136 | // is serving, not of whether its tests, still running beside it, | |
| 137 | // turn out green. | |
| 138 | case "app.serving": { | |
| 139 | const env = payload["env"]; | |
| 140 | const commitOid = payload["commitOid"]; | |
| 141 | if (typeof env !== "string" || !isEnvName(env)) return; | |
| 142 | if (typeof commitOid !== "string" || commitOid === "") return; | |
| 143 | const record = getAppByRepo(app.db, event.repoId); | |
| 144 | if (!record) return; | |
| 145 | enqueue({ appId: record.id, env, commitOid, at: Date.now() }); | |
| 146 | return; | |
| 147 | } | |
| 148 | // Kept for an environment brought up by a forge from before this | |
| 149 | // event existed: its deploy never published `app.serving`, so this | |
| 150 | // is the only signal it has. For one deployed since, `app.serving` | |
| 151 | // already queued the same commit — `enqueue` below dedupes it. | |
| 132 | 152 | case "run.finished": { |
| 133 | 153 | if (payload["kind"] !== "deploy" || payload["state"] !== "ok") return; |
| 134 | 154 | const env = payload["env"]; |
| @@ -153,12 +173,19 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 153 | 173 | } |
| 154 | 174 | } |
| 155 | 175 | |
| 156 | /** One waiting look per environment: a newer head takes the older's place. */ | |
| 176 | /** | |
| 177 | * One waiting look per environment: a newer head takes the older's | |
| 178 | * place. The same head queued twice — `app.serving` and then | |
| 179 | * `run.finished` for the one deploy — is one entry, not two. | |
| 180 | */ | |
| 157 | 181 | function enqueue(item: Pending): void { |
| 158 | 182 | const at = queue.findIndex( |
| 159 | 183 | (q) => q.appId === item.appId && q.env === item.env, |
| 160 | 184 | ); |
| 161 | if (at !== -1) queue.splice(at, 1); | |
| 185 | if (at !== -1) { | |
| 186 | if (queue[at]!.commitOid === item.commitOid) return; | |
| 187 | queue.splice(at, 1); | |
| 188 | } | |
| 162 | 189 | queue.push(item); |
| 163 | 190 | if (pumping) return; |
| 164 | 191 | pumping = true; |
test/apps.test.ts +22
| @@ -35,6 +35,7 @@ import { | ||
| 35 | 35 | updateRepoSettings, |
| 36 | 36 | } from "../src/actions/repos.ts"; |
| 37 | 37 | import type { AppRecord } from "../src/core/apps.ts"; |
| 38 | import { listEvents } from "../src/core/events.ts"; | |
| 38 | 39 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 39 | 40 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 40 | 41 | import { resolveOid } from "../src/git/merge.ts"; |
| @@ -557,6 +558,18 @@ describe("apps: production, previews, data, gates, crew", () => { | ||
| 557 | 558 | // finished, so `servingAt` is already recorded on the still-open run. |
| 558 | 559 | expect(run?.servingAt).not.toBeNull(); |
| 559 | 560 | |
| 561 | // `app.serving` published the same instant, with the tests it started | |
| 562 | // beside still going — this is what a preview's look is taken from. | |
| 563 | const serving = listEvents(app.db, { repoId: repo.id, limit: 200 }).find( | |
| 564 | (e) => | |
| 565 | e.kind === "app.serving" && | |
| 566 | e.payload["env"] === `pull-${opened.value.number}` && | |
| 567 | e.payload["commitOid"] === committed.value.commitOid, | |
| 568 | ); | |
| 569 | expect(serving).toBeDefined(); | |
| 570 | expect(serving?.payload["servingAt"]).toBe(run?.servingAt); | |
| 571 | expect(serving?.payload["testsRunning"]).toBe(true); | |
| 572 | ||
| 560 | 573 | const done = await settled( |
| 561 | 574 | (r) => |
| 562 | 575 | r.env === `pull-${opened.value.number}` && |
| @@ -692,6 +705,15 @@ describe("apps: production, previews, data, gates, crew", () => { | ||
| 692 | 705 | const failed = await settled(latest(`pull-${opened.value.number}`)); |
| 693 | 706 | expect(failed.state).toBe("failed"); |
| 694 | 707 | expect(failed.servingAt).toBeNull(); |
| 708 | // It never swapped in, so it never published the event a look is | |
| 709 | // taken from — the same reason it is pictureless. | |
| 710 | expect( | |
| 711 | listEvents(app.db, { repoId: repo.id, limit: 200 }).some( | |
| 712 | (e) => | |
| 713 | e.kind === "app.serving" && | |
| 714 | e.payload["env"] === `pull-${opened.value.number}`, | |
| 715 | ), | |
| 716 | ).toBe(false); | |
| 695 | 717 | |
| 696 | 718 | // Close it — a pull that never comes up is not this test's to leave |
| 697 | 719 | // open for the next one, which counts what git still has open. |
test/looks.test.ts +104
| @@ -23,6 +23,7 @@ import { envDataDir, previewEnv } from "../src/apps/data.ts"; | ||
| 23 | 23 | import { readGrant } from "../src/apps/proxy.ts"; |
| 24 | 24 | import { AppsRuntime } from "../src/apps/runtime.ts"; |
| 25 | 25 | import type { AppRecord } from "../src/core/apps.ts"; |
| 26 | import { listEvents } from "../src/core/events.ts"; | |
| 26 | 27 | import { looksForEnv, type Look } from "../src/core/looks.ts"; |
| 27 | 28 | import type { Pull } from "../src/core/pulls.ts"; |
| 28 | 29 | import type { Repo } from "../src/core/repos.ts"; |
| @@ -390,6 +391,109 @@ describe("the look", () => { | ||
| 390 | 391 | expect(page).not.toContain("<h2>Look</h2>"); |
| 391 | 392 | }); |
| 392 | 393 | |
| 394 | test("a look is taken from `app.serving`, while the deploy's tests are still running, and a red check afterward does not remove it", async () => { | |
| 395 | const branch = await commitToBranch(app, alice, repo, { | |
| 396 | branch: "slow-red-test", | |
| 397 | base: repo.defaultBranch, | |
| 398 | message: "Slow, then red, test", | |
| 399 | files: [ | |
| 400 | { | |
| 401 | path: "forge.json", | |
| 402 | content: `${JSON.stringify( | |
| 403 | { | |
| 404 | app: { | |
| 405 | run: "bun run src/server.ts", | |
| 406 | test: "sleep 2 && exit 1", | |
| 407 | tty: "bun run src/tty.ts", | |
| 408 | }, | |
| 409 | }, | |
| 410 | null, | |
| 411 | 2, | |
| 412 | )}\n`, | |
| 413 | }, | |
| 414 | ], | |
| 415 | }); | |
| 416 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 417 | const opened = await openPull(app, alice, repo, { | |
| 418 | title: "Slow, then red, test", | |
| 419 | baseBranch: repo.defaultBranch, | |
| 420 | headBranch: "slow-red-test", | |
| 421 | }); | |
| 422 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 423 | const env = `pull-${opened.value.number}`; | |
| 424 | const commitOid = branch.value.commitOid; | |
| 425 | ||
| 426 | // Pictured well before the two-second test step could have finished — | |
| 427 | // the run is still open, checking. | |
| 428 | const rows = await pictured(env, commitOid); | |
| 429 | expect(rows.every((l) => l.error === null)).toBe(true); | |
| 430 | const run = listRuns(app.db, repo.id, { env }).find( | |
| 431 | (r) => r.commitOid === commitOid, | |
| 432 | ); | |
| 433 | expect(run?.state).toBe("running"); | |
| 434 | const serving = listEvents(app.db, { repoId: repo.id, limit: 200 }).find( | |
| 435 | (e) => e.kind === "app.serving" && e.payload["commitOid"] === commitOid, | |
| 436 | ); | |
| 437 | expect(serving?.payload["env"]).toBe(env); | |
| 438 | expect(serving?.payload["testsRunning"]).toBe(true); | |
| 439 | ||
| 440 | // The test then finishes red; the run fails, but the picture already | |
| 441 | // taken of what is serving is untouched. | |
| 442 | const failed = await settled( | |
| 443 | (r) => r.env === env && r.commitOid === commitOid, | |
| 444 | ); | |
| 445 | expect(failed.state).toBe("failed"); | |
| 446 | expect( | |
| 447 | looksForEnv(app.db, record.id, env).filter( | |
| 448 | (l) => l.commitOid === commitOid && l.error === null, | |
| 449 | ), | |
| 450 | ).toHaveLength(2); | |
| 451 | ||
| 452 | expect( | |
| 453 | (await setIssueStateThrough(app, alice, repo, opened.value, "closed")) | |
| 454 | .ok, | |
| 455 | ).toBe(true); | |
| 456 | }, 30_000); | |
| 457 | ||
| 458 | test("two events for one environment, back to back, leave one queued look — at the newer commit", async () => { | |
| 459 | const before = calls.length; | |
| 460 | const live = runtime.envStatus(record, "production"); | |
| 461 | if (live === undefined) throw new Error("production is not live"); | |
| 462 | const superseded = `${"0".repeat(39)}1`; | |
| 463 | // Same tick, before the queue's pump has run at all: the second | |
| 464 | // replaces the first in `enqueue` rather than both being taken. | |
| 465 | app.bus.append({ | |
| 466 | kind: "app.serving", | |
| 467 | repoId: repo.id, | |
| 468 | payload: { | |
| 469 | env: "production", | |
| 470 | commitOid: superseded, | |
| 471 | servingAt: new Date().toISOString(), | |
| 472 | testsRunning: true, | |
| 473 | }, | |
| 474 | }); | |
| 475 | app.bus.append({ | |
| 476 | kind: "app.serving", | |
| 477 | repoId: repo.id, | |
| 478 | payload: { | |
| 479 | env: "production", | |
| 480 | commitOid: live.commitOid, | |
| 481 | servingAt: new Date().toISOString(), | |
| 482 | testsRunning: false, | |
| 483 | }, | |
| 484 | }); | |
| 485 | await looks.idle(); | |
| 486 | // The superseded commit was never even looked at: production's own | |
| 487 | // head was already pictured (from the first test), so nothing new | |
| 488 | // was captured for it either. | |
| 489 | expect(calls.length).toBe(before); | |
| 490 | expect( | |
| 491 | looksForEnv(app.db, record.id, "production").some( | |
| 492 | (l) => l.commitOid === superseded, | |
| 493 | ), | |
| 494 | ).toBe(false); | |
| 495 | }); | |
| 496 | ||
| 393 | 497 | test("the studio's look ask leads with the phone picture, and the app's head shows production", async () => { |
| 394 | 498 | const scout = await createAgent(app, alice, { username: "scout" }); |
| 395 | 499 | if (!scout.ok) throw new Error(String(scout.error)); |
new above
offline