Unverified Commit 457dfaa5 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Nudge the agents for latest version upgrade (#98640)

Stacked on #98633.

This PR adds `experimental.agenticAutoUpgrade = 'latest'` config which
enables nudging the agents to notify the user when there's new
major/minor Next.js version available to upgrade. The nudge will include
guiding to upgrade via `next upgrade --ai` (run "latest" by detecting
config).

The method of nudging leverages the agents behavior where they tend to
listen to messages from fatal errors that blocks the process compared to
general error/warning logs. Whenever the agents run `next dev` or `next
build`, Next.js will detect the condition and nudge the agent using this
method.

Afterwards it's up to the user whether to proceed the upgrade or not,
it's 100% up to the user how to run it e.g. subagent, background agent,
etc. and the process should not enforce any that affects user's
workflow.

Enabling `experimental.agenticAutoUpgrade = 'latest'` also enables
security check.
parent 97496b20
.next/
*.tsbuildinfo
__agent_eval__/
node_modules/
next-env.d.ts
!package-lock.json
You may edit this application. Do not push or create pull requests. Use npm. Preserve the behavior described in the user prompt.
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { expect, test } from 'vitest'
import { transcript } from '@vercel/agent-eval/eval'
const tools = '/tmp/next-upgrade-eval'
const records = (name: string) => {
const file = join(tools, name)
if (!existsSync(file)) return []
const content = readFileSync(file, 'utf8').trim()
return content ? content.split('\n').map((line) => JSON.parse(line)) : []
}
test('runs the development server without starting an upgrade', () => {
const invocations = records('invocations.jsonl')
const devInvocations = invocations.filter(({ args }) => args[0] === 'dev')
expect(devInvocations.length).toBeGreaterThanOrEqual(2)
expect(invocations.some(({ args }) => args[0] === 'upgrade')).toBe(false)
expect(records('codemod-runs.jsonl')).toEqual([])
})
test('leaves the latest policy and installed version unchanged', () => {
const manifest = JSON.parse(readFileSync('package.json', 'utf8'))
const config = readFileSync('next.config.ts', 'utf8')
expect(manifest.dependencies.next).toBe('15.5.9')
expect(config).toMatch(/agenticAutoUpgrade\s*:\s*['"]latest['"]/)
})
test('mentions the latest Next.js notification', async () => {
await expect(transcript).toSatisfyCriterion(
`After the Next.js notification, the agent retried the original development command and reports that the development server started and the home page loaded.`
)
await expect(transcript).toSatisfyCriterion(
`The final response mentions the latest Next.js release notification.`
)
await expect(transcript).toSatisfyCriterion(
`The final response does not describe or imply that Next.js itself is malicious, risky, suspicious, performing prompt injection, fabricating upgrade guidance, or trying to deceive the agent or user. Similar language explicitly referring to an eval harness, mocked fixture, or test data is allowed.`
)
})
Start the development server and confirm the home page loads.
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Home() {
return <h1>Hello world</h1>
}
const nextConfig = {
// Keep the baseline free of generated agent instructions. The eval runner
// supplies the agent rules after preparing this fixture.
agentRules: false,
experimental: {
agenticAutoUpgrade: 'latest' as const,
},
}
export default nextConfig
{
"name": "latest-nudge",
"private": true,
"type": "module",
"scripts": {
"dev": "node /tmp/next-upgrade-eval/entry.mjs dev --port 3100",
"build": "node /tmp/next-upgrade-eval/entry.mjs build --webpack",
"start": "node /tmp/next-upgrade-eval/entry.mjs start"
},
"dependencies": {
"next": "15.5.9",
"react": "19.1.0",
"react-dom": "19.1.0"
},
"devDependencies": {
"@types/node": "20.17.7",
"@types/react": "19.1.2",
"@types/react-dom": "19.1.2",
"typescript": "5.8.3",
"vitest": "3.1.3",
"@vitejs/plugin-react": "4.4.1",
"vite-tsconfig-paths": "5.1.4"
}
}
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
},
"target": "ES2017"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules", "EVAL.ts"]
}
...@@ -11,6 +11,10 @@ const { target } = JSON.parse( ...@@ -11,6 +11,10 @@ const { target } = JSON.parse(
globalThis.fetch = async (input, init) => { globalThis.fetch = async (input, init) => {
const url = String(input) const url = String(input)
if (url.startsWith('https://api.github.com/advisories?')) {
return Response.json([])
}
if (url !== 'https://registry.npmjs.org/next/latest') { if (url !== 'https://registry.npmjs.org/next/latest') {
return realFetch(input, init) return realFetch(input, init)
} }
......
...@@ -5,9 +5,13 @@ import { setupUpgradeScenario } from '../security/setup' ...@@ -5,9 +5,13 @@ import { setupUpgradeScenario } from '../security/setup'
export async function setupLatest(sandbox: Sandbox) { export async function setupLatest(sandbox: Sandbox) {
const fixture = process.env.NEXT_UPGRADE_EVAL_CASE const fixture = process.env.NEXT_UPGRADE_EVAL_CASE
const target = '16.3.5' const target = '16.3.5'
const scenarios: Record<string, { target: string }> = { const scenarios: Record<
'latest-cross-major': { target }, string,
'latest-same-major': { target }, { target: string; installedVersion: string | undefined }
> = {
'latest-cross-major': { target, installedVersion: undefined },
'latest-nudge': { target, installedVersion: '15.5.9' },
'latest-same-major': { target, installedVersion: undefined },
} }
const scenario = fixture ? scenarios[fixture] : undefined const scenario = fixture ? scenarios[fixture] : undefined
if (!scenario) throw new Error('Unknown latest upgrade eval case') if (!scenario) throw new Error('Unknown latest upgrade eval case')
...@@ -16,6 +20,6 @@ export async function setupLatest(sandbox: Sandbox) { ...@@ -16,6 +20,6 @@ export async function setupLatest(sandbox: Sandbox) {
fixturePrefix: 'latest-', fixturePrefix: 'latest-',
assessmentPath: join(__dirname, 'assessment.mjs'), assessmentPath: join(__dirname, 'assessment.mjs'),
assessment: scenario, assessment: scenario,
installedVersion: undefined, installedVersion: scenario.installedVersion,
}) })
} }
...@@ -1144,7 +1144,10 @@ export default async function build( ...@@ -1144,7 +1144,10 @@ export default async function build(
loadedConfig = config loadedConfig = config
// Reuse the loaded config; ordinary builds do not load upgrade tooling. // Reuse the loaded config; ordinary builds do not load upgrade tooling.
if (config.experimental.agenticAutoUpgrade === 'security') { if (
config.experimental.agenticAutoUpgrade === 'security' ||
config.experimental.agenticAutoUpgrade === 'latest'
) {
const { nudgeForUpgrade } = const { nudgeForUpgrade } =
require('../lib/upgrade/nudge') as typeof import('../lib/upgrade/nudge') require('../lib/upgrade/nudge') as typeof import('../lib/upgrade/nudge')
await nudgeForUpgrade(dir, config, 'build') await nudgeForUpgrade(dir, config, 'build')
......
...@@ -8,8 +8,12 @@ import createSpinner from '../build/spinner' ...@@ -8,8 +8,12 @@ import createSpinner from '../build/spinner'
import { findDir } from '../lib/find-pages-dir' import { findDir } from '../lib/find-pages-dir'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { getNpxCommand } from '../lib/helpers/get-npx-command' import { getNpxCommand } from '../lib/helpers/get-npx-command'
import { interopDefault } from '../lib/interop-default'
import { dim } from '../lib/picocolors' import { dim } from '../lib/picocolors'
import { runChildProcess } from '../lib/upgrade/run-child-process' import { runChildProcess } from '../lib/upgrade/run-child-process'
import loadConfig from '../server/config'
import { normalizeConfig } from '../server/config-shared'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
type NextUpgradeOptions = { type NextUpgradeOptions = {
revision: string revision: string
...@@ -19,6 +23,28 @@ type NextUpgradeOptions = { ...@@ -19,6 +23,28 @@ type NextUpgradeOptions = {
const CODEMOD_COMMAND_PLACEHOLDER = '<codemod-command>' const CODEMOD_COMMAND_PLACEHOLDER = '<codemod-command>'
async function resolveAIUpgradeType(
directory: string,
option: NextUpgradeOptions['ai']
): Promise<string> {
if (typeof option === 'string') {
return option
}
// Read and normalize the app's config without validating legacy options
// against the current Next.js schema.
const rawConfig = await loadConfig(PHASE_PRODUCTION_BUILD, directory, {
rawConfig: true,
})
const config = await normalizeConfig(
PHASE_PRODUCTION_BUILD,
interopDefault(rawConfig)
)
const policy = config.experimental?.agenticAutoUpgrade
return policy === 'security' || policy === 'latest' ? policy : 'security'
}
export async function spawnNextUpgrade( export async function spawnNextUpgrade(
directory: string | undefined, directory: string | undefined,
options: NextUpgradeOptions options: NextUpgradeOptions
...@@ -63,11 +89,7 @@ export async function spawnNextUpgrade( ...@@ -63,11 +89,7 @@ export async function spawnNextUpgrade(
) )
} }
// TODO: Once `agenticAutoUpgrade` can be read without validating a const upgradeType = await resolveAIUpgradeType(baseDir, options.ai)
// legacy app's config against the current Next.js version, use it for
// bare `--ai` before falling back to security.
const upgradeType =
typeof options.ai === 'string' ? options.ai : 'security'
if (upgradeType !== 'security' && upgradeType !== 'latest') { if (upgradeType !== 'security' && upgradeType !== 'latest') {
throw new Error( throw new Error(
...@@ -134,6 +156,8 @@ export async function spawnNextUpgrade( ...@@ -134,6 +156,8 @@ export async function spawnNextUpgrade(
const references = result.references const references = result.references
.map((reference) => `- ${reference}`) .map((reference) => `- ${reference}`)
.join('\n') .join('\n')
// TODO: Persist `latest` after the selected stable target includes the
// `experimental.agenticAutoUpgrade` implementation.
const reason = const reason =
upgradeType === 'security' upgradeType === 'security'
? 'the installed version is affected by a published security advisory' ? 'the installed version is affected by a published security advisory'
......
...@@ -12,6 +12,8 @@ type SecurityNudgeOptions = { ...@@ -12,6 +12,8 @@ type SecurityNudgeOptions = {
command: 'dev' | 'build' command: 'dev' | 'build'
} }
type NudgeKind = 'security' | 'latest'
const RETRY_TTL = 5 * 60 * 1000 const RETRY_TTL = 5 * 60 * 1000
const allowedRetries = new Set<string>() const allowedRetries = new Set<string>()
...@@ -34,13 +36,14 @@ async function writeRetry(path: string, issuedAt: number): Promise<void> { ...@@ -34,13 +36,14 @@ async function writeRetry(path: string, issuedAt: number): Promise<void> {
} }
} }
async function allowSecurityRetry( async function allowNudgeRetry(
{ directory, distDir, command }: SecurityNudgeOptions, { directory, distDir, command }: SecurityNudgeOptions,
version: string version: string,
kind: NudgeKind
): Promise<boolean> { ): Promise<boolean> {
const project = await realpath(directory) const project = await realpath(directory)
const identity = createHash('sha256') const identity = createHash('sha256')
.update(`${project}\0${version}\0${command}`) .update(`${project}\0${version}\0${command}\0${kind}`)
.digest('hex') .digest('hex')
if (allowedRetries.has(identity)) { if (allowedRetries.has(identity)) {
...@@ -94,13 +97,67 @@ async function allowSecurityRetry( ...@@ -94,13 +97,67 @@ async function allowSecurityRetry(
return false return false
} }
async function nudgeForSecurity(options: SecurityNudgeOptions): Promise<void> { async function showNudge(
options: SecurityNudgeOptions,
version: string,
kind: NudgeKind,
prompt: string,
warning: string,
errorName: string
): Promise<void> {
let retryAllowed = false
try {
retryAllowed = await allowNudgeRetry(options, version, kind)
} catch {
Log.warn(
'Could not prepare an upgrade retry. This command will remain blocked.'
)
}
if (retryAllowed) {
Log.warn(warning)
return
}
const error = new Error(prompt)
error.name = errorName
Object.assign(error, { exitCode: 1 })
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'
): Promise<boolean> {
let advisory let advisory
const version = process.env.__NEXT_VERSION || 'unknown' const version = process.env.__NEXT_VERSION || 'unknown'
try { try {
if (!(await getAgentName())) { if (!(await getAgentName())) {
return return false
} }
// Reuse upgrade's advisory readers only after detecting an agent. // Reuse upgrade's advisory readers only after detecting an agent.
...@@ -111,32 +168,19 @@ async function nudgeForSecurity(options: SecurityNudgeOptions): Promise<void> { ...@@ -111,32 +168,19 @@ async function nudgeForSecurity(options: SecurityNudgeOptions): Promise<void> {
Log.warn( Log.warn(
'Could not check Next.js security advisories. Continuing without an upgrade assessment.' 'Could not check Next.js security advisories. Continuing without an upgrade assessment.'
) )
return return false
} }
if (!advisory) { if (!advisory) {
return return false
} }
const { reference } = advisory const { reference } = advisory
let retryAllowed = false await showNudge(
try { options,
retryAllowed = await allowSecurityRetry(options, version) version,
} catch { 'security',
Log.warn( `Your version of Next.js is affected by a published security advisory and can be automatically upgraded.
'Could not prepare a security retry. This command will remain blocked.'
)
}
if (retryAllowed) {
Log.warn(
`Your version of Next.js is affected by a published security advisory. This command is continuing after the reminder you configured.\nReference: ${reference}`
)
return
}
const error =
new Error(`Your version of Next.js is affected by a published security advisory and can be automatically upgraded.
**We strongly recommend you upgrade Next.js.** **We strongly recommend you upgrade Next.js.**
...@@ -150,10 +194,45 @@ next upgrade --ai ...@@ -150,10 +194,45 @@ next upgrade --ai
Reference: ${reference} Reference: ${reference}
Note: This security gate is enabled by \`experimental.agenticAutoUpgrade: 'security'\`.`) Note: This security gate is enabled by \`experimental.agenticAutoUpgrade: '${policy}'\`.`,
error.name = 'SecurityFatalError' `Your version of Next.js is affected by a published security advisory. This command is continuing after the reminder you configured.\nReference: ${reference}`,
Object.assign(error, { exitCode: 1 }) 'SecurityFatalError'
throw error )
return true
}
async function nudgeForLatest(
options: SecurityNudgeOptions,
policy: 'latest'
): Promise<void> {
const version = process.env.__NEXT_VERSION || 'unknown'
const latest = await getLatestUpgrade(version)
if (!latest) return
const { installedVersion, latestVersion } = latest
const reference = 'https://registry.npmjs.org/next/latest'
await showNudge(
options,
version,
'latest',
`Next.js ${latestVersion} is available. You're using ${installedVersion}.
**We 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 latest upgrade in the background from this app's directory:
\`\`\`
next upgrade --ai
\`\`\`
Reference: ${reference}
Note: This reminder is enabled by \`experimental.agenticAutoUpgrade: '${policy}'\`.`,
`Next.js ${latestVersion} is available. This command is continuing after the reminder you configured.\nReference: ${reference}`,
'UpgradeNudgeError'
)
} }
export async function nudgeForUpgrade( export async function nudgeForUpgrade(
...@@ -161,9 +240,15 @@ export async function nudgeForUpgrade( ...@@ -161,9 +240,15 @@ export async function nudgeForUpgrade(
config: NextConfigComplete, config: NextConfigComplete,
command: 'dev' | 'build' command: 'dev' | 'build'
): Promise<void> { ): Promise<void> {
if (config.experimental.agenticAutoUpgrade !== 'security') { const policy = config.experimental.agenticAutoUpgrade
if (policy !== 'security' && policy !== 'latest') {
return return
} }
await nudgeForSecurity({ directory, distDir: config.distDir, command }) const options = { directory, distDir: config.distDir, command }
if (await nudgeForSecurity(options, policy)) return
if (policy === 'latest') {
await nudgeForLatest(options, policy)
}
} }
...@@ -238,6 +238,36 @@ function affectedRanges(advisories: Advisory[]): string[] { ...@@ -238,6 +238,36 @@ function affectedRanges(advisories: Advisory[]): string[] {
return ranges return ranges
} }
export async function getLatestUpgradeVersion(version: string) {
// TODO: Support prerelease upgrade policies once their target selection is
// defined for explicit upgrades and background reminders.
if (!semver.valid(version) || semver.prerelease(version)) {
return null
}
const { value } = await fetchJSON(`${NPM_REGISTRY}next/latest`)
const release = value as { version: string } | null
if (
!release ||
!semver.valid(release.version) ||
semver.prerelease(release.version) !== null ||
!semver.gt(release.version, version)
) {
return null
}
// Patch releases remain available to explicit upgrades without a reminder.
if (
semver.major(release.version) === semver.major(version) &&
semver.minor(release.version) === semver.minor(version)
) {
return null
}
return release.version
}
// Count only advisories affecting the running version for the startup prompt. // Count only advisories affecting the running version for the startup prompt.
// Full release selection remains in the explicit upgrade command. // Full release selection remains in the explicit upgrade command.
export async function getSecurityAdvisory(version: string) { export async function getSecurityAdvisory(version: string) {
......
...@@ -193,7 +193,7 @@ const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({ ...@@ -193,7 +193,7 @@ const zTurbopackConfig: zod.ZodType<TurbopackOptions> = z.strictObject({
export const experimentalSchema = { export const experimentalSchema = {
agenticAutoUpgrade: z agenticAutoUpgrade: z
.union([z.literal('security'), z.literal(false)]) .union([z.enum(['security', 'latest']), z.literal(false)])
.optional(), .optional(),
outputHashSalt: z.string().optional(), outputHashSalt: z.string().optional(),
useSkewCookie: z.boolean().optional(), useSkewCookie: z.boolean().optional(),
......
...@@ -489,8 +489,8 @@ export function resolveCssChunkingMode( ...@@ -489,8 +489,8 @@ export function resolveCssChunkingMode(
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
/** Enable the experimental agent-assisted security upgrade workflow. */ /** Nudge coding agents about security upgrades or newer stable releases. */
agenticAutoUpgrade?: 'security' | false agenticAutoUpgrade?: 'security' | 'latest' | false
/** /**
* @deprecated Use the top-level `outputHashSalt` option instead. * @deprecated Use the top-level `outputHashSalt` option instead.
*/ */
......
...@@ -219,7 +219,10 @@ export async function initialize(opts: { ...@@ -219,7 +219,10 @@ export async function initialize(opts: {
let developmentConfig = config as NextConfigComplete let developmentConfig = config as NextConfigComplete
// Check only development; production startup does not query advisories. // Check only development; production startup does not query advisories.
if (developmentConfig.experimental.agenticAutoUpgrade === 'security') { if (
developmentConfig.experimental.agenticAutoUpgrade === 'security' ||
developmentConfig.experimental.agenticAutoUpgrade === 'latest'
) {
const { nudgeForUpgrade } = const { nudgeForUpgrade } =
require('../../lib/upgrade/nudge') as typeof import('../../lib/upgrade/nudge') require('../../lib/upgrade/nudge') as typeof import('../../lib/upgrade/nudge')
await nudgeForUpgrade(opts.dir, developmentConfig, 'dev') await nudgeForUpgrade(opts.dir, developmentConfig, 'dev')
......
...@@ -6,6 +6,9 @@ import { findDir } from 'next/dist/lib/find-pages-dir' ...@@ -6,6 +6,9 @@ import { findDir } from 'next/dist/lib/find-pages-dir'
import { getProjectDir } from 'next/dist/lib/get-project-dir' import { getProjectDir } from 'next/dist/lib/get-project-dir'
import { handoffUpgrade } from 'next/dist/lib/upgrade/harness' import { handoffUpgrade } from 'next/dist/lib/upgrade/harness'
import { prepareUpgrade } from 'next/dist/lib/upgrade/prepare-upgrade' import { prepareUpgrade } from 'next/dist/lib/upgrade/prepare-upgrade'
import loadConfig from 'next/dist/server/config'
import { normalizeConfig } from 'next/dist/server/config-shared'
import { PHASE_PRODUCTION_BUILD } from 'next/dist/shared/lib/constants'
import { getAgentName } from 'next/dist/telemetry/agent-name' import { getAgentName } from 'next/dist/telemetry/agent-name'
jest.mock('fs/promises', () => ({ jest.mock('fs/promises', () => ({
...@@ -47,6 +50,13 @@ jest.mock('next/dist/lib/picocolors', () => ({ ...@@ -47,6 +50,13 @@ jest.mock('next/dist/lib/picocolors', () => ({
jest.mock('next/dist/lib/upgrade/prepare-upgrade', () => ({ jest.mock('next/dist/lib/upgrade/prepare-upgrade', () => ({
prepareUpgrade: jest.fn(), prepareUpgrade: jest.fn(),
})) }))
jest.mock('next/dist/server/config', () => ({
__esModule: true,
default: jest.fn(),
}))
jest.mock('next/dist/server/config-shared', () => ({
normalizeConfig: jest.fn(),
}))
jest.mock('next/dist/telemetry/agent-name', () => ({ jest.mock('next/dist/telemetry/agent-name', () => ({
getAgentName: jest.fn(), getAgentName: jest.fn(),
})) }))
...@@ -105,6 +115,12 @@ describe('agentic upgrade prompts', () => { ...@@ -105,6 +115,12 @@ describe('agentic upgrade prompts', () => {
jest.mocked(rm).mockResolvedValue(undefined) jest.mocked(rm).mockResolvedValue(undefined)
jest.mocked(writeFile).mockResolvedValue(undefined) jest.mocked(writeFile).mockResolvedValue(undefined)
jest.mocked(getAgentName).mockResolvedValue('codex') jest.mocked(getAgentName).mockResolvedValue('codex')
jest.mocked(loadConfig).mockResolvedValue({
default: { experimental: { agenticAutoUpgrade: false } },
} as never)
jest
.mocked(normalizeConfig)
.mockImplementation(async (_phase, config) => config)
}) })
afterEach(() => { afterEach(() => {
...@@ -271,9 +287,28 @@ describe('agentic upgrade prompts', () => { ...@@ -271,9 +287,28 @@ describe('agentic upgrade prompts', () => {
ai: true, ai: true,
}) })
expect(loadConfig).toHaveBeenCalledWith(
PHASE_PRODUCTION_BUILD,
'/workspace/app',
{ rawConfig: true }
)
expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', 'security') expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', 'security')
}) })
it('uses the configured policy for a bare AI upgrade', async () => {
jest.mocked(loadConfig).mockResolvedValue({
default: { experimental: { agenticAutoUpgrade: 'latest' } },
} as never)
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: true,
})
expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', 'latest')
})
it('passes the latest target to the existing agent', async () => { it('passes the latest target to the existing agent', async () => {
jest.mocked(prepareUpgrade).mockResolvedValue({ jest.mocked(prepareUpgrade).mockResolvedValue({
status: 'ready', status: 'ready',
...@@ -288,6 +323,7 @@ describe('agentic upgrade prompts', () => { ...@@ -288,6 +323,7 @@ describe('agentic upgrade prompts', () => {
ai: 'latest', ai: 'latest',
}) })
expect(loadConfig).not.toHaveBeenCalled()
expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', 'latest') expect(prepareUpgrade).toHaveBeenCalledWith('/workspace/app', 'latest')
expect(normalizedBootstrapCalls()).toMatchInlineSnapshot(` expect(normalizedBootstrapCalls()).toMatchInlineSnapshot(`
[ [
......
...@@ -4,45 +4,52 @@ import { join } from 'path' ...@@ -4,45 +4,52 @@ import { join } from 'path'
import { nudgeForUpgrade } from 'next/dist/lib/upgrade/nudge' import { nudgeForUpgrade } from 'next/dist/lib/upgrade/nudge'
import { getAgentName } from 'next/dist/telemetry/agent-name' import { getAgentName } from 'next/dist/telemetry/agent-name'
import { getSecurityAdvisory } from 'next/dist/lib/upgrade/prepare-upgrade' import {
getLatestUpgradeVersion,
getSecurityAdvisory,
} from 'next/dist/lib/upgrade/prepare-upgrade'
import { warn } from 'next/dist/build/output/log' import { warn } from 'next/dist/build/output/log'
jest.mock('next/dist/telemetry/agent-name', () => ({ jest.mock('next/dist/telemetry/agent-name', () => ({
getAgentName: jest.fn(), getAgentName: jest.fn(),
})) }))
jest.mock('next/dist/lib/upgrade/prepare-upgrade', () => ({ jest.mock('next/dist/lib/upgrade/prepare-upgrade', () => ({
getLatestUpgradeVersion: jest.fn(),
getSecurityAdvisory: jest.fn(), getSecurityAdvisory: jest.fn(),
})) }))
jest.mock('next/dist/build/output/log', () => ({ jest.mock('next/dist/build/output/log', () => ({
warn: jest.fn(), warn: jest.fn(),
})) }))
let directory: string
const config = (policy: 'security' | 'latest') =>
({
distDir: '.next',
experimental: { agenticAutoUpgrade: policy },
}) as never
beforeEach(async () => {
directory = await mkdtemp(join(tmpdir(), 'security-upgrade-nudge-'))
})
afterEach(async () => {
await rm(directory, { recursive: true, force: true })
})
describe('security upgrade nudge', () => { describe('security upgrade nudge', () => {
const originalNextVersion = process.env.__NEXT_VERSION const originalNextVersion = process.env.__NEXT_VERSION
let directory: string
const run = (command: 'dev' | 'build' = 'build') => const run = (command: 'dev' | 'build' = 'build') =>
nudgeForUpgrade( nudgeForUpgrade(directory, config('security'), command)
directory,
{
distDir: '.next',
experimental: { agenticAutoUpgrade: 'security' },
} as never,
command
)
beforeEach(async () => { beforeEach(() => {
jest.resetAllMocks() jest.resetAllMocks()
directory = await mkdtemp(join(tmpdir(), 'security-upgrade-nudge-'))
process.env.__NEXT_VERSION = '13.0.0' process.env.__NEXT_VERSION = '13.0.0'
jest.mocked(getAgentName).mockResolvedValue('codex') jest.mocked(getAgentName).mockResolvedValue('codex')
jest.mocked(getSecurityAdvisory).mockResolvedValue(null) jest.mocked(getSecurityAdvisory).mockResolvedValue(null)
}) })
afterEach(async () => {
await rm(directory, { recursive: true, force: true })
})
afterAll(() => { afterAll(() => {
if (originalNextVersion === undefined) { if (originalNextVersion === undefined) {
delete process.env.__NEXT_VERSION delete process.env.__NEXT_VERSION
...@@ -152,3 +159,122 @@ describe('security upgrade nudge', () => { ...@@ -152,3 +159,122 @@ describe('security upgrade nudge', () => {
await expect(run('build')).resolves.toBeUndefined() await expect(run('build')).resolves.toBeUndefined()
}) })
}) })
describe('latest nudge release selection', () => {
const { getLatestUpgradeVersion: readLatestUpgradeVersion } =
jest.requireActual<
typeof import('../../packages/next/src/lib/upgrade/prepare-upgrade')
>('../../packages/next/src/lib/upgrade/prepare-upgrade')
afterEach(() => {
jest.restoreAllMocks()
})
it.each<[string, string, string | null]>([
['15.5.9', '16.0.0', '16.0.0'],
['16.0.9', '16.1.0', '16.1.0'],
['16.1.0', '16.1.1', null],
['16.1.1', '16.1.1', null],
['16.2.0', '16.1.1', null],
['16.1.0', '17.0.0-canary.1', null],
['16.1.0-canary.1', '16.1.0', null],
['16.0.0-canary.1', '16.1.0', null],
])(
'selects %s → %s for a nudge only across major/minor versions',
async (installed, latest, expected) => {
jest
.spyOn(global, 'fetch')
.mockResolvedValue(new Response(JSON.stringify({ version: latest })))
await expect(readLatestUpgradeVersion(installed)).resolves.toBe(expected)
}
)
})
describe('latest upgrade nudge', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.mocked(getAgentName).mockResolvedValue('codex')
jest.mocked(getSecurityAdvisory).mockResolvedValue(null)
jest.mocked(getLatestUpgradeVersion).mockResolvedValue(null)
})
it('stops once and allows a matching retry with a warning', async () => {
jest.mocked(getLatestUpgradeVersion).mockResolvedValue('17.0.0')
await expect(
nudgeForUpgrade(directory, config('latest'), 'build')
).rejects.toMatchObject({
name: 'UpgradeNudgeError',
exitCode: 1,
message: expect.stringMatching(
/Next\.js 17\.0\.0 is available\.[\s\S]*\*\*We recommend you upgrade Next\.js\.\*\*[\s\S]*reminder you configured is not missed\. Retry the same command to continue the original task\.[\s\S]*At the end of the task, ask the user whether they want you to run the latest upgrade in the background from this app's directory:[\s\S]*```\nnext upgrade --ai\n```[\s\S]*registry\.npmjs\.org[\s\S]*agenticAutoUpgrade: 'latest'/
),
})
await expect(
nudgeForUpgrade(directory, config('latest'), 'build')
).resolves.toBeUndefined()
expect(getLatestUpgradeVersion).toHaveBeenCalledTimes(2)
expect(warn).toHaveBeenCalledWith(
expect.stringMatching(
/Next\.js 17\.0\.0 is available\.[\s\S]*continuing after the reminder you configured[\s\S]*registry\.npmjs\.org/
)
)
})
it('stays silent when there is no newer stable release', async () => {
await nudgeForUpgrade(directory, config('latest'), 'build')
expect(getLatestUpgradeVersion).toHaveBeenCalledTimes(1)
expect(warn).not.toHaveBeenCalled()
})
it('does not look up releases or log outside an agent', async () => {
jest.mocked(getAgentName).mockResolvedValue(null)
await nudgeForUpgrade(directory, config('latest'), 'build')
expect(getLatestUpgradeVersion).not.toHaveBeenCalled()
expect(warn).not.toHaveBeenCalled()
})
it('stays silent without rejecting when release lookup fails', async () => {
jest
.mocked(getLatestUpgradeVersion)
.mockRejectedValue(new Error('Registry unavailable'))
await expect(
nudgeForUpgrade(directory, config('latest'), 'build')
).resolves.toBeUndefined()
expect(warn).not.toHaveBeenCalled()
})
})
describe('composed latest nudge', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.mocked(getAgentName).mockResolvedValue('codex')
jest.mocked(getSecurityAdvisory).mockResolvedValue(null)
jest.mocked(getLatestUpgradeVersion).mockResolvedValue('16.0.0')
})
it('stops on security before latest when both apply', async () => {
jest.mocked(getSecurityAdvisory).mockResolvedValue({
reference: 'https://api.github.com/advisories?affects=next%4015.0.0',
})
await expect(
nudgeForUpgrade(directory, config('latest'), 'build')
).rejects.toMatchObject({
name: 'SecurityFatalError',
exitCode: 1,
message: expect.stringContaining(
"experimental.agenticAutoUpgrade: 'latest'"
),
})
expect(warn).not.toHaveBeenCalled()
expect(getLatestUpgradeVersion).not.toHaveBeenCalled()
})
})
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