Unverified Commit 7353da4c authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Enable AI upgrade for canary (#99001)

> [!TIP]
> Recommended to review commit by commit.

### Why?

Support `canary` releases for agentic upgrade so the workflow is
available for the dogfooders. Startup reminders also need to check
target eligibility before offering an upgrade that the configured policy
cannot perform.

### How?

Enable latest and Future upgrades on the canary channel while keeping
stable installations on stable releases. Security upgrades and advisory
checks remain stable-only; canary version ordering is not used to infer
security fixes. Share policy-aware advisory and target assessment
between startup reminders and explicit upgrades, retaining security
warnings when no eligible target is available and offering bare `--ai`
only when the configured policy can proceed.

Explicit stable `latest` upgrades also check target advisories, after
treating strictly older targets as a no-op; equal affected targets
remain blocked. Canary latest and Future upgrades skip advisory
assessment. Version reminders keep their major/minor threshold, and
Future Defaults retain their full-version availability checks.
Assessment caching is left as a TODO.

The agentic upgrade guide remains a bundled, agent-consumed workflow
marked `version: draft`; public-facing documentation will be handled
separately.
parent becee66e
......@@ -13,7 +13,11 @@ as instructed so the original task can continue with a warning. Tell the user
about the finding and ask whether they want to run the upgrade. Continue
independent work while waiting. If approved, complete the upgrade.
For `latest`, the CLI pins npm's latest stable Next.js release as the target.
For `latest`, the CLI pins the release from the app's channel: npm's `latest`
dist-tag for stable installations and `canary` for canary installations.
Security upgrades and advisory checks are supported only for stable installations.
Canary installations support `latest` and `future` without advisory assessment.
## 1. Check for duplicates
......
......@@ -2,7 +2,7 @@ import { spawn } from 'child_process'
import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { dirname, join } from 'path'
import { valid } from 'next/dist/compiled/semver'
import { prerelease, valid } from 'next/dist/compiled/semver'
import * as Log from '../build/output/log'
import createSpinner from '../build/spinner'
import { findDir } from '../lib/find-pages-dir'
......@@ -351,12 +351,14 @@ export async function spawnNextUpgrade(
const references = result.references
.map((reference) => `- ${reference}`)
.join('\n')
const releaseKind =
prerelease(result.targetVersion)?.[0] === 'canary' ? 'canary' : 'stable'
const reason =
upgradeType === 'security'
? 'the installed version is affected by a published security advisory'
: upgradeType === 'latest'
? 'a newer stable Next.js release is available'
: 'the Future policy applies the latest stable release and adopts its Future Defaults'
? `a newer ${releaseKind} Next.js release is available`
: `the Future policy applies the latest ${releaseKind} release and adopts its Future Defaults`
const futureDefaultsPrompt = preparedFutureDefaults.length
? `
${needsVersionMigration ? 'After completing and verifying the version migration, adopt' : 'Adopt'} these Future Defaults in order:
......
......@@ -8,6 +8,7 @@ import * as Log from '../../build/output/log'
import type { NextConfigComplete } from '../../server/config-shared'
import { getAgentName } from '../../telemetry/agent-name'
import { futureDefaults } from './future-defaults'
import type { UpgradeAssessment } from './prepare-upgrade'
type SecurityNudgeOptions = {
directory: string
......@@ -128,77 +129,40 @@ async function showNudge(
throw error
}
async function getLatestUpgrade(
installedVersion: string = process.env.__NEXT_VERSION || 'unknown'
): Promise<{ installedVersion: string; latestVersion: string } | null> {
try {
if (!(await getAgentName())) {
return null
}
const { getLatestUpgradeVersion } =
require('./prepare-upgrade') as typeof import('./prepare-upgrade')
const latestVersion = await getLatestUpgradeVersion(installedVersion)
if (!latestVersion) {
return null
}
return { installedVersion, latestVersion }
} catch {
// A release reminder is best-effort; lookup failures should stay quiet.
return null
}
}
async function nudgeForSecurity(
options: SecurityNudgeOptions,
policy: 'security' | 'latest' | 'future'
policy: 'security' | 'latest' | 'future',
assessment: UpgradeAssessment
): Promise<boolean> {
let advisory
const version = process.env.__NEXT_VERSION || 'unknown'
try {
if (!(await getAgentName())) {
return false
}
// Reuse upgrade's advisory readers only after detecting an agent.
const { getSecurityAdvisory } =
require('./prepare-upgrade') as typeof import('./prepare-upgrade')
advisory = await getSecurityAdvisory(version)
} catch {
Log.warn(
'Could not check Next.js security advisories. Continuing without an upgrade assessment.'
)
return false
}
if (!advisory) {
if (!assessment.affected) {
return false
}
const version = process.env.__NEXT_VERSION || 'unknown'
const { reference, upgrade } = assessment
const action =
upgrade.status === 'ready'
? `At the end of the task, ask the user whether they want you to run the upgrade using the configured '${policy}' policy in the background from this app's directory:
const { reference } = advisory
\`\`\`
next upgrade --ai
\`\`\``
: `${upgrade.status === 'unknown' ? 'Upgrade availability could not be checked.' : 'No safe newer target is available for the configured upgrade policy.'} ${upgrade.reason}`
await showNudge(
options,
version,
'security',
`Your version of Next.js is affected by a published security advisory and can be automatically upgraded.
`Your version of Next.js is affected by a published security advisory.
**We strongly recommend you upgrade Next.js.**
This command stopped so the reminder you configured is not missed. Retry the same command to continue the original task.
At the end of the task, ask the user whether they want you to run the security upgrade in the background from this app's directory:
\`\`\`
next upgrade --ai
\`\`\`
${action}
Reference: ${reference}
Note: This security gate is enabled by \`experimental.agenticAutoUpgrade: '${policy}'\`.`,
`Your version of Next.js is affected by a published security advisory. This command is continuing after the reminder you configured.\nReference: ${reference}`,
`Your version of Next.js is affected by a published security advisory. This command is continuing after the reminder you configured.${upgrade.status === 'ready' ? '' : `\n${action}`}\nReference: ${reference}`,
'SecurityFatalError'
)
return true
......@@ -206,14 +170,23 @@ Note: This security gate is enabled by \`experimental.agenticAutoUpgrade: '${pol
async function nudgeForLatest(
options: SecurityNudgeOptions,
policy: 'latest' | 'future'
policy: 'latest' | 'future',
assessment: UpgradeAssessment
): Promise<boolean> {
if (assessment.upgrade.status !== 'ready') {
return false
}
const version = process.env.__NEXT_VERSION || 'unknown'
const latest = await getLatestUpgrade(version)
if (!latest) return false
const { installedVersion, latestVersion } = latest
const reference = 'https://registry.npmjs.org/next/latest'
const { getLatestUpgradeVersion } =
require('./prepare-upgrade') as typeof import('./prepare-upgrade')
const { installedVersion, targetVersion } = assessment.upgrade
const latestVersion = getLatestUpgradeVersion(installedVersion, targetVersion)
if (!latestVersion) {
return false
}
const distTag =
semver.prerelease(latestVersion)?.[0] === 'canary' ? 'canary' : 'latest'
const reference = `https://registry.npmjs.org/next/${distTag}`
await showNudge(
options,
version,
......@@ -247,7 +220,8 @@ export async function getFutureUpgrade(
if (
!(await getAgentName()) ||
!semver.valid(installedVersion) ||
semver.prerelease(installedVersion)
(semver.prerelease(installedVersion) &&
semver.prerelease(installedVersion)?.[0] !== 'canary')
) {
return null
}
......@@ -315,13 +289,39 @@ export async function nudgeForUpgrade(
return
}
const options = { directory, distDir: config.distDir, command }
if (await nudgeForSecurity(options, policy)) return
const version = process.env.__NEXT_VERSION || 'unknown'
let assessment: UpgradeAssessment
try {
if (
!(await getAgentName()) ||
!semver.valid(version) ||
(semver.prerelease(version) &&
semver.prerelease(version)?.[0] !== 'canary')
) {
return
}
const { getUpgradeAssessment } =
require('./prepare-upgrade') as typeof import('./prepare-upgrade')
assessment = await getUpgradeAssessment(version, policy)
} catch {
Log.warn(
'Could not check Next.js security advisories. Continuing without an upgrade assessment.'
)
return
}
const options = { directory, distDir: config.distDir, command }
if (await nudgeForSecurity(options, policy, assessment)) {
return
}
if (assessment.upgrade.status !== 'ready') {
return
}
if (policy === 'latest' || policy === 'future') {
if (await nudgeForLatest(options, policy)) return
if (await nudgeForLatest(options, policy, assessment)) {
return
}
}
if (policy === 'future') {
await nudgeForFuture(options, config)
}
......
......@@ -547,6 +547,40 @@ describe('agentic upgrade prompts', () => {
`)
})
it.each(['latest', 'future'] as const)(
'hands off the exact canary target for %s upgrades',
async (policy) => {
jest.mocked(prepareUpgrade).mockResolvedValue({
status: 'ready',
installedVersion: '17.2.0-canary.4',
targetVersion: '17.2.0-canary.9',
references: ['https://registry.npmjs.org/next/canary'],
futureDefaults: [],
})
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: policy,
})
expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', policy)
const prompt = normalizedBootstrapCalls().flat().join('\n')
expect(prompt).toContain(
'from Next.js 17.2.0-canary.4 to 17.2.0-canary.9'
)
expect(prompt).toContain(
policy === 'latest'
? 'newer canary Next.js release'
: 'latest canary release'
)
expect(prompt).toContain('https://registry.npmjs.org/next/canary')
expect(String(jest.mocked(writeFile).mock.calls[0][1])).toContain(
'upgrade 17.2.0-canary.9 --yes --skip-adoption'
)
}
)
it('adds temporary Future Default instructions to the migration prompt', async () => {
jest.mocked(prepareUpgrade).mockResolvedValue({
status: 'ready',
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment