Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog.d/803-discovery-promotion.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Discovery can now promote a successful discovered issue into one proposed work item when AUTO_PROMOTE_DISCOVERY is enabled; a failed run or a backlog-report result is never promoted, promotions are deduplicated by issue URL, capped per repository per UTC day and never create a branch or pull request. The default remains off.
10 changes: 9 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,15 @@ open work it returns `none` with reason `idle`: the scheduler writes
discovery interval is due, it may instead run one `discover` job; that job may
create at most one concrete issue (never a PR/branch), and if it finds nothing
concrete it creates nothing. The callback refuses a discovery result that carries
a branch or pull request (`DISCOVERY_PR_FORBIDDEN`). The job goal gets a bounded
a branch or pull request (`DISCOVERY_PR_FORBIDDEN`). A discovered issue does not
create a work item by default; with `AUTO_PROMOTE_DISCOVERY` enabled, a
`succeeded` discovery whose result is a concrete issue creates one `proposed`
work item through `promoteDiscoveredIssue`, deduped by issue URL and capped per
repository per UTC day: the counter is keyed `discover-promote:<repoId>:<day>`,
while the cap's value comes from the deployment-wide
`AUTO_PROMOTE_DISCOVERY_DAILY_CAP`. A result the run digest classifies as a backlog report
is not promoted, and a failed run never promotes. It never creates a branch or a
pull request. The job goal gets a bounded
`Carry-over` block with the last three run digests and up to four open items; the
block is data, not an instruction, and is capped well below the 4000-character
goal limit. Every line that names an issue or a pull request is checked once
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface Config {
// erased. The sweep's purge phase reads this; 72 hours is the host-side cron it
// replaces.
trialPurgeGraceHours: number
/** #803: promote a discovered issue into one proposed work item. Off by default. */
autoPromoteDiscovery: boolean
/** #803: maximum automatic discovery promotions per UTC day; 0 disables them. */
autoPromoteDiscoveryDailyCap: number
// #708: the per-class windows mutating routes take. A route names its class;
// the numbers live here so a deployment tightens a limit without a rebuild.
rateLimit: RateLimitBudgets
Expand Down Expand Up @@ -298,6 +302,8 @@ export function loadConfig(overrides: Partial<Config> = {}): Config {
},
recentAuthMinutes: Math.max(1, number('RECENT_AUTH_MINUTES', 15)),
trialPurgeGraceHours: Math.max(1, number('TRIAL_PURGE_GRACE_HOURS', 72)),
autoPromoteDiscovery: bool('AUTO_PROMOTE_DISCOVERY', false),
autoPromoteDiscoveryDailyCap: Math.max(0, number('AUTO_PROMOTE_DISCOVERY_DAILY_CAP', 3)),
rateLimit: {
login: {
max: Math.max(1, number('RATE_LIMIT_LOGIN_MAX', DEFAULT_RATE_LIMIT_BUDGETS.login.max)),
Expand Down
41 changes: 41 additions & 0 deletions src/jobs/discoveryPromotion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Config } from '../config.js'
import type { Store } from '../db.js'
import { randomId } from '../lib/secrets.js'

// #803: discovery stays issue-only. This is the explicit, opt-in bridge from a
// discovered issue to the normal work-item queue. It never creates a branch or
// a pull request and it never changes the issue itself.
export type DiscoveryPromotionStatus = 'promoted' | 'disabled' | 'duplicate' | 'cap' | 'no-issue'

export interface DiscoveryPromotionResult {
status: DiscoveryPromotionStatus
workItemId?: string
}

export function promoteDiscoveredIssue(
store: Store,
config: Config,
input: { repoId: string; userId: string; issueUrl: string | null; title?: string; priority?: number; now?: Date },
): DiscoveryPromotionResult {
const issueUrl = input.issueUrl ?? ''
if (issueUrl === '') return { status: 'no-issue' }
if (!config.autoPromoteDiscovery || config.autoPromoteDiscoveryDailyCap <= 0) return { status: 'disabled' }

const existing = store.findWorkItemByIssueUrl(input.repoId, issueUrl)
if (existing !== undefined) return { status: 'duplicate', workItemId: existing.id }

// The cap is per repository: a deployment-wide counter let one busy
// repository spend every other repository's day. A corrupt value counts as
// zero — `NaN >= cap` is false forever, which would switch the cap off.
const day = (input.now ?? new Date()).toISOString().slice(0, 10)
const key = `discover-promote:${input.repoId}:${day}`
const stored = Number(store.getSetting(key) ?? 0)
const used = Number.isFinite(stored) ? stored : 0
if (used >= config.autoPromoteDiscoveryDailyCap) return { status: 'cap' }

const id = randomId('work')
const title = (input.title ?? '').trim().slice(0, 160) || issueUrl
store.createWorkItem({ id, repoId: input.repoId, userId: input.userId, title, issueUrl, state: 'proposed', priority: input.priority })
store.setSetting(key, String(used + 1))
return { status: 'promoted', workItemId: id }
}
34 changes: 33 additions & 1 deletion src/jobs/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import { addMinutes, nowIso } from '../lib/time.js'
import { GithubClient, GithubError, prepareRepo, prepareRepoWithInstallation, repoPermissionWithToken } from '../repos/github.js'
import { GithubApp } from '../repos/app.js'
import { parseIssueUrl, parsePrUrl } from './prUrl.js'
import { buildCarryOver, buildRunDigest, decideNextJob, failureStreak, lastDurationMs, MIN_DISCOVERY_INTERVAL_MINUTES, parseCarryOverLink, type CarryOverReader } from './continuity.js'
import { promoteDiscoveredIssue } from './discoveryPromotion.js'
import { buildCarryOver, buildRunDigest, decideNextJob, failureStreak, lastDurationMs, MIN_DISCOVERY_INTERVAL_MINUTES, parseCarryOverLink, type CarryOverReader, type OutputKind } from './continuity.js'
import { planTick, scheduleFromRow, type TickPlan } from '../schedules/tick.js'
import { utcDayEnd, utcDayStart } from '../schedules/budget.js'
import type { ScheduleRow } from '../db.js'
Expand Down Expand Up @@ -1348,6 +1349,7 @@ export class Jobs {
})
return
}
if (job.kind === 'discover') return
if (result.status !== 'succeeded' || (result.prUrl === null && result.issueUrl === null)) return
const title = (result.prUrl ?? result.issueUrl ?? job.goal).slice(0, 160)
this.store.createWorkItem({
Expand All @@ -1365,6 +1367,32 @@ export class Jobs {
})
}

// #803: one work item for a successful discovery whose result is a concrete
// issue. `DISCOVERY_PR_FORBIDDEN` finishes the run `failed`, so the status
// guard also covers the run that broke discovery's issue-only contract.
private settleDiscoveryPromotion(job: JobRow, result: { status: 'succeeded' | 'failed' | 'quota_exhausted'; issueUrl: string | null; summary: string }, outputKind: OutputKind): void {
if (result.status !== 'succeeded') {
this.store.audit(job.user_id, 'job.discovery_promotion_skipped', `${job.id}: not-succeeded:${result.status}`)
return
}
if (outputKind === 'meta') {
this.store.audit(job.user_id, 'job.discovery_promotion_skipped', `${job.id}: meta-issue`)
return
}
const promotion = promoteDiscoveredIssue(this.store, this.config, {
repoId: job.repo_id,
userId: job.user_id,
issueUrl: result.issueUrl,
title: result.summary,
priority: job.priority,
})
this.store.audit(
job.user_id,
promotion.status === 'promoted' ? 'job.discovery_promoted' : 'job.discovery_promotion_skipped',
`${job.id}: ${promotion.status}${promotion.workItemId === undefined ? '' : ` ${promotion.workItemId}`}`,
)
}

// Returns whether this call moved the row to a terminal state. Callers that
// perform side effects must only do so when it returns true.
private finish(jobId: string, result: { status: 'succeeded' | 'failed' | 'quota_exhausted'; branch?: string; prUrl?: string | null; issueUrl?: string | null; reviewScore?: number; summary?: string; error?: string; errorCode?: FailureCode }, revoke: () => void, attribution: Attribution | null = null, evidence: Evidence | null = null, fingerprint: string | null = null): boolean {
Expand Down Expand Up @@ -1427,6 +1455,10 @@ export class Jobs {
const digest = buildRunDigest(finalJob, { status: result.status, summary, issueUrl, prUrl })
this.store.updateJob(jobId, { continuity_json: JSON.stringify(digest), output_kind: digest.output_kind })
incCounter('fledge_job_output_total', { kind: job.kind, output: digest.output_kind }, 1, 'Scheduled output classification')
// #803: promotion runs on the classification, not before it. A result the
// digest calls a backlog report must not spend a cap slot or audit a
// promotion, and a run that did not succeed must not promote at all.
if (job.kind === 'discover') this.settleDiscoveryPromotion(finalJob, { status: result.status, issueUrl, summary }, digest.output_kind)
if (digest.output_kind === 'meta') {
incCounter('fledge_meta_issue_total', { kind: job.kind }, 1, 'Inventory/meta issues rejected by classification')
// Close the tracking row so the next scheduled run does not follow up a
Expand Down
110 changes: 110 additions & 0 deletions test/discoveryPromotion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { Store } from '../src/db.js'
import { promoteDiscoveredIssue } from '../src/jobs/discoveryPromotion.js'
import { makeUser, testConfig } from './helpers.js'

function fixture() {
const store = new Store(':memory:')
const user = makeUser(store, { login: 'discovery-promote', status: 'approved' })
const repo = store.createRepo({ id: 'repo_discovery_promote', userId: user.id, owner: 'o', name: 'r', defaultBranch: 'main', tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: '', autoIterate: false, intervalMinutes: 1440 })
return { store, user, repo }
}

const now = new Date('2026-11-15T00:00:00.000Z')
const issue = (n: number) => `https://github.com/o/r/issues/${String(n)}`

test('discovery promotion is off by default and never creates a work item', () => {
const { store, user, repo } = fixture()
try {
const result = promoteDiscoveredIssue(store, testConfig(), { repoId: repo.id, userId: user.id, issueUrl: issue(1), now })
assert.equal(result.status, 'disabled')
assert.equal(store.findWorkItemByIssueUrl(repo.id, issue(1)), undefined)
} finally {
store.close()
}
})

test('an enabled promotion creates one proposed work item, dedupes the issue and caps the day', () => {
const { store, user, repo } = fixture()
try {
const config = testConfig({ autoPromoteDiscovery: true, autoPromoteDiscoveryDailyCap: 2 })
const first = promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(1), title: 'First discovered issue', now })
assert.equal(first.status, 'promoted')
const item = store.getWorkItem(first.workItemId ?? '')
assert.ok(item !== undefined)
assert.equal(item.state, 'proposed')
assert.equal(item.issue_url, issue(1))
assert.equal(item.pr_url, null)

const duplicate = promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(1), now })
assert.equal(duplicate.status, 'duplicate')
assert.equal(duplicate.workItemId, first.workItemId)

const second = promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(2), now })
assert.equal(second.status, 'promoted')

const capped = promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(3), now })
assert.equal(capped.status, 'cap')
assert.equal(store.findWorkItemByIssueUrl(repo.id, issue(3)), undefined)
assert.equal(store.getSetting(`discover-promote:${repo.id}:2026-11-15`), '2')
} finally {
store.close()
}
})

// #803 says only "The setting has a daily cap". A deployment-wide counter let
// one repository spend every other repository's day, so the cap is per
// repository: a busy repository cannot starve a quiet one.
test('the daily cap is per repository', () => {
const { store, user, repo } = fixture()
const other = store.createRepo({ id: 'repo_discovery_promote_b', userId: user.id, owner: 'o', name: 'r2', defaultBranch: 'main', tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: '', autoIterate: false, intervalMinutes: 1440 })
try {
const config = testConfig({ autoPromoteDiscovery: true, autoPromoteDiscoveryDailyCap: 1 })
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(1), now }).status, 'promoted')
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(2), now }).status, 'cap')
assert.equal(promoteDiscoveredIssue(store, config, { repoId: other.id, userId: user.id, issueUrl: issue(3), now }).status, 'promoted')
} finally {
store.close()
}
})

// A non-numeric setting reads as NaN, and `NaN >= cap` is false forever, so one
// bad write used to switch the cap off for good. A corrupt value counts as 0 and
// the next promotion rewrites a finite number.
test('a corrupt counter value cannot switch the cap off', () => {
const { store, user, repo } = fixture()
try {
const config = testConfig({ autoPromoteDiscovery: true, autoPromoteDiscoveryDailyCap: 1 })
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(1), now }).status, 'promoted')
const counter = store.listSettings('discover-promote')[0]
assert.ok(counter !== undefined)
store.setSetting(counter.key, 'not-a-number')
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(2), now }).status, 'promoted')
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(3), now }).status, 'cap')
} finally {
store.close()
}
})

test('a promoted work item keeps the priority it was given', () => {
const { store, user, repo } = fixture()
try {
const config = testConfig({ autoPromoteDiscovery: true, autoPromoteDiscoveryDailyCap: 3 })
const result = promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: issue(1), priority: 2, now })
assert.equal(result.status, 'promoted')
assert.equal(store.getWorkItem(result.workItemId ?? '')?.priority, 2)
} finally {
store.close()
}
})

test('a discovery result without an issue URL is not promoted', () => {
const { store, user, repo } = fixture()
try {
const config = testConfig({ autoPromoteDiscovery: true, autoPromoteDiscoveryDailyCap: 3 })
assert.equal(promoteDiscoveredIssue(store, config, { repoId: repo.id, userId: user.id, issueUrl: null, now }).status, 'no-issue')
} finally {
store.close()
}
})
Loading
Loading