Skip to content

feat(hub): score applied learnings within the evaluation budget - #396

Draft
nshki wants to merge 7 commits into
nshki/feat/learning/score-applied-learningsfrom
nshki/feat/hub/score-applied-learnings
Draft

nshki wants to merge 7 commits into
nshki/feat/learning/score-applied-learningsfrom
nshki/feat/hub/score-applied-learnings

Conversation

@nshki

@nshki nshki commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Why

The learning package can now score whether an applied learning helped, but nothing in Hub asks it to. Each score costs AI credits, so Hub needs to run scoring on its own after real work lands and stay within the shared monthly budget that a Hub administrator sets.

What changes

When an agent's report_usage call names a commit, Hub remembers that commit. The worker's recovery pass runs every minute. Once the commit has a successful Build, it queues a scoring job. The job asks the store which learnings were used in that commit. It then scores each one against the commit's diff from GitHub and any screenshots whose consent still holds.

Hub records every attempt and what it cost, including failed ones. The evaluation settings page now shows real spending for the month, and it already explains when evaluations pause because the budget is reached. When the budget runs out or Copilot is unavailable, requests wait and start again once there is room.

sequenceDiagram
    participant Agent
    participant Hub
    participant Worker
    participant CLI as replica-learning
    Agent->>Hub: report_usage with commit
    Hub->>Hub: remember commit
    Worker->>Hub: commit has a successful Build?
    Worker->>CLI: usage --commit
    loop each used learning, while budget lasts
        Worker->>Hub: record attempt and operation ID
        Worker->>CLI: score with diff and screenshots
        Worker->>Hub: record result and credits
    end
Loading

Turning evaluations on now also adds a consent grant that lets the store use the repository's review comments for evaluation. Without it the store refuses to score. Turning evaluations off withdraws that grant.

Needs design review

  • Scoring starts from the minute-by-minute recovery pass rather than from the usage report or the Build finishing. This works whichever arrives first, at the cost of up to a minute's delay.
  • Opting into evaluations grants the store use of review comments for evaluation. A repository that was turned on before this change is not scored until an administrator saves its settings again.
  • The budget is one Hub-wide limit per calendar month in UTC. A budget of 0 means nothing is scored.
  • While the budget is spent or Copilot is unavailable, the recovery pass re-queues waiting requests every minute. Each job stops after cheap checks. Requests without a successful Build are dropped after 30 days.
  • A failed score is recorded with the credits it reported and is never retried. A timed-out score reports no usage, so its cost is counted as 0.
  • A score interrupted by a worker stopping stays running and is retried with the same operation ID, so the store keeps one score per attempt.
  • Scoring takes the learning execution lock for each command. Agents may get a short busy reply while a score runs.

nshki and others added 4 commits November 26, 2026 01:00
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
@nshki
nshki added this pull request to stack #367 November 26, 2026 09:05
@nshki
nshki requested an adaptive review from Copilot November 26, 2026 09:05
nshki and others added 3 commits November 26, 2026 01:10
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 25291e01-0146-42cb-ac6d-f5e1dd5c35f5
@nshki
nshki force-pushed the nshki/feat/hub/score-applied-learnings branch from baf95e9 to a50a46d Compare November 26, 2026 09:10
used.id,
"--operation-id",
run.operationId,
...(diffFile ? ["--diff-file", diffFile] : []),
try {
const scored = scoredSchema.parse(
await dependencies.withLock((signal) =>
dependencies.runCommand(

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical flaws in budget accounting, recovery, and consent enforcement can cause untracked spending, lost scoring requests, and continued evidence processing after opt-out or repository removal.

20 open issues
What changed in this PR

Connects Hub’s agent usage reports to the learning package’s automatic scoring, with shared monthly spending and repository consent controls.

Changes:

  • Records reported commits and queues scoring after a successful Build.
  • Supplies commit diffs and consented screenshots, then records scoring outcomes and costs.
  • Exposes monthly spending and manages review-comment evaluation consent.
File Description
hub/​server/​workers/​github-recovery.test.ts Adds the commit-diff client mock.
hub/​server/​workers/​configuration.ts Registers the scoring processor.
hub/​server/​workers/​configuration.test.ts Updates processor registration expectations.
hub/​server/​utils/​start-of-utc-month/​index.ts Defines UTC monthly budget boundaries.
hub/​server/​utils/​format-ai-credits/​index.ts Shares precise credit formatting.
hub/​server/​routes/​api/​learnings/​types.ts Adds spending to evaluation settings.
hub/​server/​routes/​api/​learnings/​index.test.ts Updates evaluation settings fixtures.
hub/​server/​routes/​api/​learnings/​evaluations/​get/​index.ts Removes the placeholder spending value.
hub/​server/​routes/​api/​learnings/​create-learnings-route-dependencies/​index.ts Reads spending and manages evaluation consent.
hub/​server/​routes/​api/​learnings/​create-learnings-route-dependencies/​evaluation-settings.db.test.ts Covers evaluation consent transitions.
hub/​server/​routes/​agent-connections/​index.ts Persists scoring requests from agents.
hub/​server/​repositories/​learning-score-requests/​record-learning-score-request/​index.ts Creates or refreshes commit requests.
hub/​server/​repositories/​learning-score-requests/​list-ready-learning-score-requests/​index.ts Finds requests with successful Builds.
hub/​server/​repositories/​learning-score-requests/​list-ready-learning-score-requests/​index.db.test.ts Covers pagination and repository matching.
hub/​server/​repositories/​learning-score-requests/​delete-learning-score-request/​index.ts Deletes consumed requests using a timestamp guard.
hub/​server/​repositories/​learning-score-requests/​delete-expired-learning-score-requests/​index.ts Adds age-based request cleanup.
hub/​server/​repositories/​learning-evaluation-runs/​sum-learning-evaluation-credits/​index.ts Aggregates recorded evaluation costs.
hub/​server/​repositories/​learning-evaluation-runs/​start-learning-evaluation-run/​index.ts Creates resumable scoring attempts.
hub/​server/​repositories/​learning-evaluation-runs/​finish-learning-evaluation-run/​index.ts Saves outcomes and usage costs.
hub/​server/​repositories/​evaluation-settings/​save-repository-evaluation-setting/​index.ts Persists evaluation grant IDs.
hub/​server/​integrations/​github/​create-github-actions-client/​index.ts Fetches size-bounded commit diffs.
hub/​server/​integrations/​github/​create-github-actions-client/​index.test.ts Covers diff requests and size limits.
hub/​server/​domains/​learning/​score-applied-learnings/​index.ts Implements scoring, evidence preparation, and accounting.
hub/​server/​domains/​learning/​score-applied-learnings/​index.test.ts Covers failure, budget pause, and interruption.
hub/​server/​domains/​learning/​process-learning-run/​index.ts Reuses the shared credit formatter.
hub/​server/​domains/​learning/​jobs/​score-applied-learnings.ts Defines scoring payloads and retries.
hub/​server/​domains/​learning/​create-learning-job-processors/​index.ts Composes scoring and recovery dependencies.
hub/​server/​domains/​learning/​create-learning-job-processors/​index.test.ts Updates recovery mocks for requests.
hub/​server/​domains/​learning/​change-evaluation-consent/​index.ts Generalizes screenshot and review-comment consent.
hub/​server/​domains/​agent-connections/​create-learning-mcp-backend/​index.ts Requests scoring for reported commits.
hub/​server/​domains/​agent-connections/​create-learning-mcp-backend/​index.test.ts Covers commit scoring requests.
hub/​schema/​repository.prisma Adds scoring request and run relations.
hub/​schema/​repository-evaluation-setting.prisma Adds the evaluation consent grant field.
hub/​schema/​migrations/​20261225090000_learning_evaluation_runs/​migration.sql Creates scoring tables, indexes, and consent storage.
hub/​schema/​learning-score-request.prisma Models unique repository/commit requests.
hub/​schema/​learning-evaluation-run.prisma Models scoring attempts and spending.
hub/​docs/​learning-foundation.md Documents scoring, consent, budgets, and recovery.

Review effort: Max (adaptive)

Comment on lines +206 to +209
// Asked first so a report that lands is never missed. A request whose report failed
// finds nothing to score and is dropped.
if (typeof report.commitSha === "string" && /^[0-9a-f]{40}$/.test(report.commitSha))
await input.requestScore?.(report.commitSha)
Comment on lines +552 to +555
isEnabled: async () => {
const setting = await getRepositoryEvaluationSetting(prisma, request.repositoryId)
return Boolean(setting?.enabled && setting.evaluationGrantId)
},
for (const [index, used] of reports.flatMap((report) => report.used).entries()) {
const run = await dependencies.start(used.id)
if (run.state !== "RUNNING") continue
if (!(await dependencies.withinBudget())) return "paused"
Comment on lines +116 to +131
const scored = scoredSchema.parse(
await dependencies.withLock((signal) =>
dependencies.runCommand(
[
"score",
used.id,
"--operation-id",
run.operationId,
...(diffFile ? ["--diff-file", diffFile] : []),
...(hasScreenshots ? ["--screenshots", screenshots] : []),
...store,
],
{ ...credential, signal, timeoutMs: 10 * 60 * 1000 },
),
),
)
Comment on lines +135 to +136
aiCredits: formatAiCredits(scored.usage.nanoAiu),
copilotRequests: scored.usage.requests,
Comment on lines +142 to +144
if (!(error instanceof LearningCommandError) || error.code === "cancelled") throw error
const spent = spentSchema.safeParse(error.result)
await dependencies.finish(run.id, {
Comment on lines +22 to +24
orderBy: { id: "asc" },
take: limit,
...(cursor ? { cursor: { id: cursor }, skip: 1 } : {}),
return await operation(new AbortController().signal)
}

describe("scoreAppliedLearnings", () => {
Comment on lines +96 to +114
if (diffFile === undefined) {
const diff = await dependencies.readDiff()
diffFile = diff === null ? null : resolve(temporary, "commit.diff")
if (diffFile) await writeFile(diffFile, diff!, { mode: 0o600 })
}
// A hosted store keeps no screenshot bytes, so Hub hands over the ones whose
// consent still holds as files named by their digest.
const screenshots = resolve(temporary, `screenshots-${index}`)
let hasScreenshots = false
for (const shot of [used.before, used.after]) {
if (!shot) continue
const bytes = await dependencies.readScreenshot(shot.sha256)
if (!bytes) continue
await mkdir(screenshots, { recursive: true, mode: 0o700 })
await writeFile(resolve(screenshots, `${shot.sha256.slice(7)}.png`), bytes, {
mode: 0o600,
})
hasScreenshots = true
}
learning.run,
learning.redact,
learning.deleteScreenshots,
learning.score,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants