Skip to content
Draft
21 changes: 21 additions & 0 deletions hub/client/application/hub/learnings-contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { recordIdentifierSchema, repositorySchema } from "@application/hub/contr

import type {
AcceptedLearningResponse,
AppliedLearningsResponse,
EvaluationSettingsResponse,
LearningActivityDayResponse,
LearningDecisionInput,
Expand Down Expand Up @@ -104,6 +105,26 @@ export const rejectedLearningResponseSchema = z
})
.strict() satisfies z.ZodType<RejectedLearningResponse>

const appliedScreenshotSchema = z
.object({ sha256: z.string().regex(/^[0-9a-f]{64}$/), route: z.string() })
.strict()

export const appliedLearningsResponseSchema = z
.object({
learnings: z.array(
z
.object({
id: z.string().min(1),
title: z.string().nullable(),
version: identifier.nullable(),
before: appliedScreenshotSchema.optional(),
after: appliedScreenshotSchema.optional(),
})
.strict(),
),
})
.strict() satisfies z.ZodType<AppliedLearningsResponse>

export const learningHistoryResponseSchema = z
.object({
decisions: z.array(
Expand Down
12 changes: 12 additions & 0 deletions hub/client/application/hub/learnings-contracts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ export interface RejectedLearningResponse {
shared: boolean
}

export interface AppliedLearningResponse {
id: string
title: string | null
version: number | null
before?: { sha256: string; route: string }
after?: { sha256: string; route: string }
}

export interface AppliedLearningsResponse {
learnings: AppliedLearningResponse[]
}

export interface LearningHistoryResponse {
decisions: Array<{
version: number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { appliedLearningsResponseSchema } from "@application/hub/learnings-contracts"

import { requestJson } from "../request-json"

/** Reads what agents said they applied in the commit a Build was made from. */
export function getAppliedLearnings(
request: Request,
repositoryId: string,
prototypeId: string,
commitSha: string,
) {
const url = new URL(`/api/repositories/${repositoryId}/learnings/applied`, request.url)
url.searchParams.set("prototype", prototypeId)
url.searchParams.set("commit", commitSha)
return requestJson(url, appliedLearningsResponseSchema, { signal: request.signal })
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function previewHandlers(data: ReturnType<typeof previewRecords>) {
canBranch,
})
}),
http.get(`/api/repositories/${repository.id}/learnings/applied`, () =>
Response.json({ learnings: [] }),
),
http.get(`/api${path}/builds`, () =>
Response.json({
repository,
Expand Down
25 changes: 25 additions & 0 deletions hub/client/application/hub/routes/prototype/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it, vi } from "vitest"

import { http, stubHubFetch } from "@/test/stub-hub-fetch"

import { previewRecords } from "../fixtures/preview-records"
import { clientAction, clientLoader } from "."

describe("prototype route", () => {
Expand All @@ -17,6 +19,29 @@ describe("prototype route", () => {
expect(send).not.toHaveBeenCalled()
})

it("loads the prototype without waiting for applied learnings", async () => {
const { historical, path, repository, prototype } = previewRecords()
const readApplied = vi.fn()
stubHubFetch(
http.get(`${window.location.origin}/api${path}`, () =>
Response.json({ repository, prototype, selectedBuild: historical }),
),
http.get(
`${window.location.origin}/api/repositories/${repository.id}/learnings/applied`,
readApplied,
),
)
const request = new Request(new URL(`${path}?build=1`, window.location.origin))

const result = await clientLoader({
request,
params: { repositoryId: repository.id, prototypeId: prototype.id },
})

expect(result.selectedBuild?.number).toBe(1)
expect(readApplied).not.toHaveBeenCalled()
})

it("does not confirm a save when the API returns an invalid success response", async () => {
const url = new URL("/api/repositories/2001/prototypes/1", window.location.origin)
const respond = vi.fn(() => Response.json({ unexpected: "Not a prototype response." }))
Expand Down
33 changes: 33 additions & 0 deletions hub/client/application/hub/routes/prototype/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from "react"
import { data, useFetcher, useOutletContext } from "react-router"

import { prototypeEditSchema } from "@application/hub/contracts"
Expand All @@ -9,7 +10,9 @@ import type {
import { readRecordIdentifier } from "@application/hub/read-record-identifier"
import { ApiContractError } from "@application/hub/requests/api-contract-error"
import { ApiRequestError } from "@application/hub/requests/api-request-error"
import { getAppliedLearnings } from "@application/hub/requests/get-applied-learnings"
import { getPrototype } from "@application/hub/requests/get-prototype"
import type { AppliedLearningResponse } from "@application/hub/learnings-contracts/types"
import { updatePrototype } from "@application/hub/requests/update-prototype"
import { PrototypeScreen } from "@application/hub/screens/prototype-screen"

Expand All @@ -34,6 +37,34 @@ export async function clientLoader({
)
}

/**
* Reads what agents applied in the selected Build after the page shows. Learnings are
* optional, so a slow or failed read never holds up or breaks the prototype page.
*/
function useAppliedLearnings(prototype: PrototypeResponse) {
const repositoryId = prototype.repository.id
const prototypeId = prototype.prototype.id
const commitSha = prototype.selectedBuild?.commitSha?.toLowerCase()
const key = `${repositoryId}/${prototypeId}/${commitSha}`
const [loaded, setLoaded] = useState<{ key: string; learnings: AppliedLearningResponse[] }>()
useEffect(() => {
if (!commitSha) return
const controller = new AbortController()
getAppliedLearnings(
new Request(window.location.href, { signal: controller.signal }),
repositoryId,
prototypeId,
commitSha,
).then(
(response) => setLoaded({ key, learnings: response.learnings }),
() => undefined,
)
return () => controller.abort()
}, [commitSha, key, prototypeId, repositoryId])
// Learnings from a previously selected Build must not show beside this one.
return loaded?.key === key ? loaded.learnings : []
}

export async function clientAction({
request,
params,
Expand Down Expand Up @@ -94,11 +125,13 @@ export default function Prototype({ loaderData }: Pick<Route.ComponentProps, "lo
key: `prototype-details:${loaderData.repository.id}/${loaderData.prototype.id}`,
})
const saving = fetcher.state !== "idle"
const appliedLearnings = useAppliedLearnings(loaderData)
return (
<PrototypeScreen
key={`${loaderData.repository.id}/${loaderData.prototype.id}`}
workspace={workspace}
data={loaderData}
appliedLearnings={appliedLearnings}
onSaveDetails={(details) => {
void fetcher.submit(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MentionProvider } from "@components/mention-provider"
import { PrototypesShowPage } from "@pages/prototypes-show-page"
import type { PrototypeDetailsInput } from "@pages/prototypes-show-page/types"
import type { PrototypeResponse, WorkspaceResponse } from "@application/hub/contracts/types"
import type { AppliedLearningResponse } from "@application/hub/learnings-contracts/types"
import { prototypeShowData } from "@application/hub/view-models/prototype-show-data"
import { createReviewTransport } from "@application/hub/create-review-transport"
import { readReviewQuery } from "@application/hub/read-review-query"
Expand All @@ -23,13 +24,15 @@ import { readPreviewReturnPath } from "@utils/read-preview-return-path"
export function PrototypeScreen({
workspace,
data,
appliedLearnings,
onSaveDetails,
savingDetails,
detailsError,
detailsSaved,
}: {
workspace: WorkspaceResponse
data: PrototypeResponse
appliedLearnings?: AppliedLearningResponse[]
onSaveDetails: (details: PrototypeDetailsInput) => void
savingDetails: boolean
detailsError: string | null
Expand All @@ -43,7 +46,12 @@ export function PrototypeScreen({
const repositoryCapabilities = workspace.repositories.find(
({ id }) => id === data.repository.id,
)?.capabilities
const pageData = prototypeShowData(workspace, data, new URLSearchParams(location.search))
const pageData = prototypeShowData(
workspace,
data,
new URLSearchParams(location.search),
appliedLearnings,
)
pageData.reviewReadOnly = !repositoryCapabilities?.reviewPrototypes
if (pageData.reviewReadOnly) {
pageData.reviewComments = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,61 @@ it("uses the per-prototype sharing decision rather than repository or edit acces
prototypeShowData(workspace, data("2", false), new URLSearchParams()).shareLinksPath,
).toBeUndefined()
})

it("lists the learnings agents applied in the selected Build", () => {
const repository = { id: "2001", nameWithOwner: "example/repository", defaultBranch: "main" }
const workspace: WorkspaceResponse = {
viewer: { id: "123", login: "octocat", name: null, provider: "github", avatarUrl: null },
repositories: [repository],
}
const build = {
number: 3,
repositoryId: repository.id,
prototypeId: "1",
state: "successful",
commitSha: "a".repeat(40),
commitAuthorLogin: null,
createdAt: "2026-12-20T12:00:00Z",
startedAt: null,
finishedAt: null,
previewUrl: "https://preview.example.com/",
desktopScreenshotUrl: null,
narrowScreenshotUrl: null,
failureSummary: null,
}
const response = prototypeResponseSchema.parse({
repository,
prototype: {
id: "1",
repositoryId: repository.id,
name: "Prototype",
branchName: "prototype/welcome",
authorLogin: null,
canEdit: false,
description: null,
builds: [build],
},
selectedBuild: build,
})
const sha256 = "b".repeat(64)

const result = prototypeShowData(workspace, response, new URLSearchParams("build=3"), [
{ id: "mem_1", title: "Label every field", version: 2, after: { sha256, route: "/" } },
])

expect(result.agentReportedUseLabel).toBe("Applied learnings")
expect(result.agentReportedUse).toMatchObject({
buildNumber: 3,
learnings: [
{
id: "mem_1",
label: "Label every field · v2",
afterScreenshot: {
url: `/api/repositories/2001/learnings/screenshots/${sha256}`,
label: "View after screenshot for Label every field",
},
},
],
})
expect(result.agentReportedUse?.learnings[0]?.beforeScreenshot).toBeUndefined()
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PrototypesShowPageData } from "@pages/prototypes-show-page/types"
import { reviewCommentsCopy } from "@utils/review-comment/en"
import type { PrototypeResponse, WorkspaceResponse } from "@application/hub/contracts/types"
import type { AppliedLearningResponse } from "@application/hub/learnings-contracts/types"

import { buildStateLabel } from "../build-state-label"
import { productContinuation } from "../product-continuation"
Expand All @@ -10,6 +11,7 @@ export function prototypeShowData(
workspace: WorkspaceResponse,
data: PrototypeResponse,
search: URLSearchParams,
appliedLearnings: AppliedLearningResponse[] = [],
): PrototypesShowPageData {
const selectedBuild = data.selectedBuild
const previewBuild =
Expand Down Expand Up @@ -106,6 +108,16 @@ export function prototypeShowData(
selectedBuild && !previewBuild
? `Build ${selectedBuild.number} · ${buildStateLabel(selectedBuild.state)}`
: "Choose build",
...(previewBuild && appliedLearnings.length > 0
? {
agentReportedUseLabel: "Applied learnings",
agentReportedUse: agentReportedUse(
data.repository.id,
previewBuild.number,
appliedLearnings,
),
}
: {}),
selectedBuild: previewBuild?.previewUrl
? {
number: previewBuild.number,
Expand All @@ -115,3 +127,45 @@ export function prototypeShowData(
: undefined,
}
}

/**
* Shows what agents said they applied in a Build. Screenshots are served by Hub, which
* stops serving them once the repository withdraws screenshot consent.
*/
function agentReportedUse(
repositoryId: string,
buildNumber: number,
learnings: AppliedLearningResponse[],
): NonNullable<PrototypesShowPageData["agentReportedUse"]> {
function screenshot(name: string, kind: string, value?: { sha256: string }) {
return value
? {
url: `/api/repositories/${repositoryId}/learnings/screenshots/${value.sha256}`,
label: `View ${kind} screenshot for ${name}`,
}
: undefined
}
return {
buildNumber,
copy: {
heading: `Applied learnings for Build ${buildNumber}`,
caption: "Reported by the agent; not independently verified.",
learningLabel: "Learning",
beforeLabel: "Before",
afterLabel: "After",
viewLabel: "View",
notAttachedLabel: "Not attached",
},
learnings: learnings.map((learning) => {
const name = learning.title ?? "Untitled learning"
const beforeScreenshot = screenshot(name, "before", learning.before)
const afterScreenshot = screenshot(name, "after", learning.after)
return {
id: learning.id,
label: learning.version === null ? name : `${name} · v${learning.version}`,
...(beforeScreenshot ? { beforeScreenshot } : {}),
...(afterScreenshot ? { afterScreenshot } : {}),
}
}),
}
}
11 changes: 11 additions & 0 deletions hub/docs/learning-foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ The OAuth endpoints are `/.well-known/oauth-authorization-server`, `/.well-known

The MCP endpoint accepts `POST` only and needs `HUB_APPLICATION_ORIGIN` and the learning package. Local repositories are not supported, because learnings belong to GitHub repositories, so their Learnings page offers no agent setup.

## Applied learnings

When an agent reports the learnings it applied, the prototype page shows them on the Build made from that commit. For example, an agent recalls "Label every form field", builds a checkout prototype, and reports it with commit `4f2c…`. Anyone who can read the library opens that Build with `?build=N` and chooses **Applied learnings** from **More actions**. The dialog lists each learning with the version the agent received and any before and after screenshots. It says the list is the agent's report and not verified.

Hub matches a report to a Build only by the exact commit SHA, never by branch or time. A report that names a prototype shows only on that prototype. A report without one shows on every prototype built from that commit. Hub reads the package's `usage --commit` command after the page shows, so a slow read never holds up the Build. If Learnings is off or the read fails, the page stays without the menu item.

Screenshots open from Hub, not the agent. Hub serves one only while the screenshot consent it was saved under is still active, so withdrawing consent hides it before the deletion job runs. The browser contracts are:

- `GET /api/repositories/:repositoryId/learnings/applied?prototype=<id>&commit=<sha>`
- `GET /api/repositories/:repositoryId/learnings/screenshots/:sha256`, which returns the PNG

## Automated evaluations

Hub can check whether a repository's learnings help and suggest updates. Someone who administers Hub or the repository opens it from **Automated evaluations** on the Learnings page. For example, a Hub administrator sets a shared budget of 100 credits per month, and an administrator of `github/checkout` turns on **Evaluate github/checkout**.
Expand Down
Loading
Loading