Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 59 additions & 16 deletions hub/client/application/hub/learnings-contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ export const appliedLearningsResponseSchema = z
})
.strict() satisfies z.ZodType<AppliedLearningsResponse>

const buildSchema = z
.object({
prototypeId: recordIdentifierSchema,
prototypeName: z.string().min(1),
number: identifier,
})
.strict()

const learningOutcomeResponseSchema = z
.object({
id: z.string().min(1),
source: z.enum(["automated", "reviewer_confirmed"]),
efficiency: z.number().int().min(1).max(4),
followed: z.enum(["followed", "not_followed", "unclear"]),
reasoning: z.string(),
issue: z.string().nullable(),
scoredAt: timestamp,
build: buildSchema,
before: appliedScreenshotSchema.optional(),
after: appliedScreenshotSchema.optional(),
})
.strict()

const proposalId = z.string().regex(/^[a-z][a-z0-9]{1,15}_[A-Za-z0-9_-]{8,128}$/)

export const learningHistoryResponseSchema = z
.object({
decisions: z.array(
Expand All @@ -143,28 +168,30 @@ export const learningHistoryResponseSchema = z
otherEvidenceCount: count,
contradictions: z.array(learningEvidenceResponseSchema),
otherContradictionsCount: count,
outcomes: z.array(
outcomes: z.array(learningOutcomeResponseSchema),
earlierGuidance: z.array(
z
.object({
id: z.string().min(1),
source: z.enum(["automated", "reviewer_confirmed"]),
efficiency: z.number().int().min(1).max(4),
followed: z.enum(["followed", "not_followed", "unclear"]),
version: identifier,
title: z.string().nullable(),
content: z.string(),
outcomes: z.array(learningOutcomeResponseSchema),
})
.strict(),
),
updates: z.array(
z
.object({
id: proposalId,
issue: z.string(),
title: z.string().nullable(),
content: z.string(),
reasoning: z.string(),
issue: z.string().nullable(),
scoredAt: timestamp,
build: z
.object({
prototypeId: recordIdentifierSchema,
prototypeName: z.string().min(1),
number: identifier,
})
.strict(),
before: appliedScreenshotSchema.optional(),
after: appliedScreenshotSchema.optional(),
support: z.array(buildSchema),
})
.strict(),
),
updateNeedsMoreEvidence: z.boolean(),
})
.strict() satisfies z.ZodType<LearningHistoryResponse>

Expand Down Expand Up @@ -309,6 +336,22 @@ export const learningDecisionInputSchema = z.discriminatedUnion("decision", [
reason: z.string().trim().min(1).max(500),
})
.strict(),
z
.object({
decision: z.literal("apply_update"),
proposalId,
title: titleInput.optional(),
content: z.string().trim().min(1).max(2000).optional(),
reason: z.string().trim().min(1).max(500),
})
.strict(),
z
.object({
decision: z.literal("dismiss_update"),
proposalId,
reason: z.string().trim().min(1).max(500),
})
.strict(),
]) satisfies z.ZodType<LearningDecisionInput>

export const learningShareInputSchema = z
Expand Down
30 changes: 30 additions & 0 deletions hub/client/application/hub/learnings-contracts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ export interface LearningHistoryResponse {
contradictions: LearningEvidenceResponse[]
otherContradictionsCount: number
outcomes: LearningOutcomeResponse[]
/** Wording agents were given before the current version, newest first. */
earlierGuidance: Array<{
version: number
title: string | null
content: string
outcomes: LearningOutcomeResponse[]
}>
/** Suggested new wording waiting for an administrator. */
updates: LearningUpdateResponse[]
/** Whether the last check found too few scored uses to suggest new wording. */
updateNeedsMoreEvidence: boolean
}

/** Suggested new wording, drafted because scored uses kept showing the same issue. */
export interface LearningUpdateResponse {
id: string
issue: string
title: string | null
content: string
reasoning: string
/** The Builds whose scores showed the issue. */
support: LearningOutcomeResponse["build"][]
}

/** How one use of a learning was scored, with the Build it judged. */
Expand Down Expand Up @@ -174,6 +196,14 @@ export type LearningDecisionInput =
| { decision: "retire"; reason: string }
| { decision: "edit"; digest: string; title?: string; content?: string; reason: string }
| { decision: "reinstate"; digest: string; reason: string }
| {
decision: "apply_update"
proposalId: string
title?: string
content?: string
reason: string
}
| { decision: "dismiss_update"; proposalId: string; reason: string }

export interface LearningShareInput {
shared: boolean
Expand Down
12 changes: 11 additions & 1 deletion hub/client/application/hub/routes/settings-learnings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,17 @@ function submitDecision(
}
: decision === "reinstate"
? { decision, digest: form.get("digest"), reason: form.get("reason") }
: { decision }
: decision === "apply_update"
? {
decision,
proposalId: form.get("proposalId"),
...(typeof title === "string" && title.trim() ? { title } : {}),
Comment thread
nshki marked this conversation as resolved.
Resolved
content: form.get("content"),
reason: form.get("reason"),
}
: decision === "dismiss_update"
? { decision, proposalId: form.get("proposalId"), reason: form.get("reason") }
: { decision }
const parsed = learningDecisionInputSchema.safeParse(input)
if (!parsed.success) {
throw new Response(parsed.error.issues.map(({ message }) => message).join(" "), { status: 422 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ describe("learningsLibrary()", () => {
contradictions: [],
otherContradictionsCount: 0,
outcomes: [],
earlierGuidance: [],
updates: [],
updateNeedsMoreEvidence: false,
},
)

Expand Down Expand Up @@ -223,6 +226,9 @@ describe("learningsLibrary()", () => {
],
otherContradictionsCount: 1,
outcomes: [],
earlierGuidance: [],
updates: [],
updateNeedsMoreEvidence: false,
},
)

Expand Down Expand Up @@ -265,6 +271,9 @@ describe("learningsLibrary()", () => {
otherEvidenceCount: 0,
contradictions: [],
otherContradictionsCount: 0,
earlierGuidance: [],
updates: [],
updateNeedsMoreEvidence: false,
outcomes: [
{
...score,
Expand Down Expand Up @@ -306,4 +315,64 @@ describe("learningsLibrary()", () => {
afterScreenshotUrl: `/api/repositories/2001/learnings/screenshots/${"b".repeat(64)}`,
})
})

it("links each Build that showed a suggested update's issue once", () => {
const state = emptyLearnings()
state.learnings = [
{
id: "mem_active",
title: "Keep labels visible",
content: "Show a label above every field.",
digest,
acceptedBy: "hubot",
acceptedAt: "2026-10-09T15:00:00Z",
edited: false,
lastDecision: "accepted",
status: "recalled",
shared: false,
},
]

const result = learningsLibrary(
state,
{ pathname, search: "?status=active&learning=mem_active" },
{
decisions: [],
evidence: [],
otherEvidenceCount: 0,
contradictions: [],
otherContradictionsCount: 0,
earlierGuidance: [],
updateNeedsMoreEvidence: false,
// A reviewer's later score replaced these in the current list, but the
// suggestion still links the Builds it was drafted from.
outcomes: [],
updates: [
{
id: "upd_1",
issue: "The rule did not say where the label goes.",
title: null,
content: "Show a label above every field, outside the input.",
reasoning: "Builds kept placing labels inside inputs.",
// Both scores of one Build supported it.
support: [
{ prototypeId: "17", prototypeName: "Checkout", number: 3 },
{ prototypeId: "17", prototypeName: "Checkout", number: 3 },
],
},
],
},
)

expect(result.selected?.updates).toEqual([
{
id: "upd_1",
issue: "The rule did not say where the label goes.",
title: null,
content: "Show a label above every field, outside the input.",
reasoning: "Builds kept placing labels inside inputs.",
support: [{ label: "Checkout, Build 3", path: "/repositories/2001/prototypes/17?build=3" }],
},
])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function evidence(items: LearningEvidenceResponse[], repositoryId: string): Lear
* Reads a score as a plain result. A score only says something about the guidance when
* the agent followed it, and 3 or 4 means the work went well.
*/
function buildLink(build: LearningOutcomeResponse["build"], repositoryId: string) {
return {
label: `${build.prototypeName}, Build ${build.number}`,
path: `/repositories/${repositoryId}/prototypes/${build.prototypeId}?build=${build.number}`,
}
}

function outcomes(items: LearningOutcomeResponse[], repositoryId: string): LearningOutcome[] {
function screenshot(value?: { sha256: string }) {
return value ? `/api/repositories/${repositoryId}/learnings/screenshots/${value.sha256}` : null
Expand All @@ -90,8 +97,8 @@ function outcomes(items: LearningOutcomeResponse[], repositoryId: string): Learn
item.followed !== "followed" ? "inconclusive" : item.efficiency >= 3 ? "supports" : "concern",
reviewerConfirmed: item.source === "reviewer_confirmed",
summary: item.issue ?? item.reasoning,
buildLabel: `${item.build.prototypeName}, Build ${item.build.number}`,
buildPath: `/repositories/${repositoryId}/prototypes/${item.build.prototypeId}?build=${item.build.number}`,
buildLabel: buildLink(item.build, repositoryId).label,
buildPath: buildLink(item.build, repositoryId).path,
evaluatedAt: item.scoredAt,
beforeScreenshotUrl: screenshot(item.before),
afterScreenshotUrl: screenshot(item.after),
Expand Down Expand Up @@ -227,6 +234,7 @@ function detail(
const repositoryId = state.repository.id
const path = `/repositories/${repositoryId}/learnings/${encodeURIComponent(record.id)}/decision`
const canDecide = state.permissions.decide
const current = outcomes(history?.outcomes ?? [], repositoryId)
const common = {
id: record.id,
status: record.status,
Expand All @@ -252,7 +260,15 @@ function detail(
reason: entry.reason,
})),
// Only guidance agents get now can have later Builds to judge.
outcomes: record.status === "active" ? outcomes(history?.outcomes ?? [], repositoryId) : null,
outcomes: record.status === "active" ? current : null,
updates: [],
updateNeedsMoreEvidence: false,
earlierGuidance: (history?.earlierGuidance ?? []).map((guidance) => ({
version: guidance.version,
title: guidance.title,
guidance: guidance.content,
outcomes: outcomes(guidance.outcomes, repositoryId),
})),
source: record.source,
share: null,
}
Expand Down Expand Up @@ -317,6 +333,23 @@ function detail(
recallFact(accepted.status),
...(accepted.shared ? ["Shared across Hub"] : []),
],
updates: (history?.updates ?? []).map((update) => ({
id: update.id,
issue: update.issue,
title: update.title,
content: update.content,
reasoning: update.reasoning,
// Both scores of one Build can support a suggestion, so each Build is linked once.
support: [
...new Map(
update.support.map((build) => {
const link = buildLink(build, repositoryId)
return [link.path, link]
}),
).values(),
],
})),
updateNeedsMoreEvidence: history?.updateNeedsMoreEvidence ?? false,
decision:
canDecide && accepted.digest && accepted.content !== null
? {
Expand Down
Loading
Loading