Download nextjs-agent-rules-refresh/solution/gold.patch from dari-ai/nextjs-selfbench: direct link, hf CLI and curl.
- Browser
- Download file 7.95 kB
-
https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-agent-rules-refresh/solution/gold.patch
- Command line
-
hf download hf://datasets/dari-ai/nextjs-selfbench/nextjs-agent-rules-refresh/solution/gold.patch
-
curl -L -o gold.patch https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-agent-rules-refresh/solution/gold.patch
7.95 kB
| diff --git a/packages/next-codemod/bin/upgrade.ts b/packages/next-codemod/bin/upgrade.ts | |
| index d33372bb53..a1b83f2bd9 100644 | |
| --- a/packages/next-codemod/bin/upgrade.ts | |
| +++ b/packages/next-codemod/bin/upgrade.ts | |
| import { | |
| } from '../lib/handle-package' | |
| import { runTransform } from './transform' | |
| import { onCancel, TRANSFORMER_INQUIRER_CHOICES } from '../lib/utils' | |
| +import { refreshAgentRulesBlock } from '../lib/agents-md' | |
| import { BadInput } from './shared' | |
| type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun' | |
| export async function runUpgrade( | |
| console.log(`${pc.green('✔')} Codemods have been applied successfully.`) | |
| } | |
| + try { | |
| + if (refreshAgentRulesBlock(cwd) === 'refreshed') { | |
| + console.log( | |
| + `${pc.green('✔')} Refreshed the managed agent-rules block in AGENTS.md / CLAUDE.md to match the upgraded Next.js.` | |
| + ) | |
| + } | |
| + } catch { | |
| + // The block refresh is best-effort — never fail the upgrade over it. | |
| + } | |
| + | |
| warnDependenciesOutOfRange(appPackageJson, versionMapping) | |
| endMessage(targetNextVersion) | |
| diff --git a/packages/next-codemod/lib/agents-md.ts b/packages/next-codemod/lib/agents-md.ts | |
| index 1e816a0284..18eacfb406 100644 | |
| --- a/packages/next-codemod/lib/agents-md.ts | |
| +++ b/packages/next-codemod/lib/agents-md.ts | |
| interface NextjsVersionResult { | |
| error?: string | |
| } | |
| +const AGENT_RULES_START_MARKER = '<!-- BEGIN:nextjs-agent-rules -->' | |
| + | |
| +/** | |
| + * After an upgrade, refresh the managed agent-rules block in | |
| + * AGENTS.md / CLAUDE.md so its content matches the Next.js version | |
| + * that is now installed. | |
| + * | |
| + * Delegates to the installed package's own generator | |
| + * (`next/dist/server/lib/generate-agent-files`), so the block text is | |
| + * always the one shipped with that version — this codemod never | |
| + * carries its own copy. Returns `'refreshed'` when a file was | |
| + * rewritten, `'current'` when the block was already up to date, and | |
| + * `'skipped'` when there is nothing to do: the project never adopted | |
| + * the managed block, or the installed Next.js predates the generator | |
| + * (< 16.3). | |
| + */ | |
| +export function refreshAgentRulesBlock( | |
| + cwd: string | |
| +): 'refreshed' | 'current' | 'skipped' { | |
| + const hostsBlock = ['AGENTS.md', 'CLAUDE.md'].some((file) => { | |
| + try { | |
| + return fs | |
| + .readFileSync(path.join(cwd, file), 'utf-8') | |
| + .includes(AGENT_RULES_START_MARKER) | |
| + } catch { | |
| + return false | |
| + } | |
| + }) | |
| + if (!hostsBlock) return 'skipped' | |
| + | |
| + let writeAgentFiles: (dir: string) => { agentsMd: string; claudeMd: string } | |
| + try { | |
| + const generatorPath = require.resolve( | |
| + 'next/dist/server/lib/generate-agent-files', | |
| + { paths: [cwd] } | |
| + ) | |
| + writeAgentFiles = require(generatorPath).writeAgentFiles | |
| + if (typeof writeAgentFiles !== 'function') return 'skipped' | |
| + } catch { | |
| + return 'skipped' | |
| + } | |
| + | |
| + const result = writeAgentFiles(cwd) | |
| + return result.agentsMd === 'updated' || result.claudeMd === 'updated' | |
| + ? 'refreshed' | |
| + : 'current' | |
| +} | |
| + | |
| export function getNextjsVersion(cwd: string): NextjsVersionResult { | |
| try { | |
| const nextPkgPath = require.resolve('next/package.json', { paths: [cwd] }) | |
| diff --git a/packages/next/src/server/lib/app-info-log.ts b/packages/next/src/server/lib/app-info-log.ts | |
| index 70eab53c8c..175c88eb73 100644 | |
| --- a/packages/next/src/server/lib/app-info-log.ts | |
| +++ b/packages/next/src/server/lib/app-info-log.ts | |
| import { experimentalSchema } from '../config-schema' | |
| import { detectAgent } from '../../telemetry/detect-agent' | |
| import { bundlerName, getBundlerFromEnv } from '../../lib/bundler' | |
| import { | |
| - hasAgentRulesInstalled, | |
| + hasCurrentAgentRules, | |
| writeAgentFiles, | |
| type AgentFilesResult, | |
| } from './generate-agent-files' | |
| export function logExperimentalInfo({ | |
| /** | |
| * When `next dev` detects an AI coding agent but the managed | |
| - * agent-rules block is missing from AGENTS.md / CLAUDE.md, | |
| - * auto-generate the files so the agent has access to version-matched | |
| - * docs. Returns the write result when files were generated, or `null` | |
| - * when no action was needed. | |
| + * agent-rules block is missing from AGENTS.md / CLAUDE.md — or an | |
| + * outdated version of it is installed — auto-generate or refresh the | |
| + * files so the agent has access to version-matched docs. Returns the | |
| + * write result when files were touched, or `null` when no action was | |
| + * needed. | |
| * | |
| * Callers gate this on `config.agentRules !== false` — opt-out is | |
| * declarative in next.config, not inside this function. | |
| */ | |
| export function ensureAgentRulesForDev(dir: string): AgentFilesResult | null { | |
| if (detectAgent() === null) return null | |
| - if (hasAgentRulesInstalled(dir)) return null | |
| + if (hasCurrentAgentRules(dir)) return null | |
| return writeAgentFiles(dir) | |
| } | |
| diff --git a/packages/next/src/server/lib/generate-agent-files.ts b/packages/next/src/server/lib/generate-agent-files.ts | |
| index c3f1f70886..12618c6f53 100644 | |
| --- a/packages/next/src/server/lib/generate-agent-files.ts | |
| +++ b/packages/next/src/server/lib/generate-agent-files.ts | |
| export interface AgentFilesResult { | |
| } | |
| /** | |
| - * Returns true when `AGENTS.md` or `CLAUDE.md` at `dir` contains the | |
| - * managed agent-rules marker. | |
| + * Returns the managed block (markers included) found in `content`, or | |
| + * `null` when the markers are absent or malformed. | |
| */ | |
| -export function hasAgentRulesInstalled(dir: string): boolean { | |
| - const agentsContent = tryReadFile(path.join(dir, 'AGENTS.md')) | |
| - if (agentsContent?.includes(AGENT_RULES_START_MARKER)) return true | |
| - | |
| - const claudeContent = tryReadFile(path.join(dir, 'CLAUDE.md')) | |
| - if (claudeContent?.includes(AGENT_RULES_START_MARKER)) return true | |
| +function extractAgentRulesBlock(content: string): string | null { | |
| + const start = content.indexOf(AGENT_RULES_START_MARKER) | |
| + if (start === -1) return null | |
| + const end = content.indexOf(AGENT_RULES_END_MARKER, start) | |
| + if (end === -1) return null | |
| + return content.slice(start, end + AGENT_RULES_END_MARKER.length) | |
| +} | |
| +/** | |
| + * Returns true when `AGENTS.md` or `CLAUDE.md` at `dir` already | |
| + * contains the current agent-rules block. A block from an earlier | |
| + * Next.js version (older wording, legacy markers) returns false so | |
| + * callers know to upsert the current one over it. | |
| + */ | |
| +export function hasCurrentAgentRules(dir: string): boolean { | |
| + const block = buildAgentRulesBlock() | |
| + for (const file of ['AGENTS.md', 'CLAUDE.md']) { | |
| + const content = tryReadFile(path.join(dir, file)) | |
| + if (!content) continue | |
| + const installed = extractAgentRulesBlock(content) | |
| + if (installed !== null && normalizeEol(installed, '\n') === block) { | |
| + return true | |
| + } | |
| + } | |
| return false | |
| } | |
| export function hasAgentRulesInstalled(dir: string): boolean { | |
| * Write the agent-rules block into `projectDir`, respecting whichever | |
| * file the user already uses: | |
| * | |
| + * - A file already hosting the managed block → upsert into it, so | |
| + * upgrades rewrite the block in place instead of adding a copy. | |
| * - `AGENTS.md` exists → upsert into it, leave `CLAUDE.md` alone. | |
| * - `CLAUDE.md` exists (but not `AGENTS.md`) → upsert into it. | |
| * - Neither exists → create both (`AGENTS.md` + `CLAUDE.md` with | |
| export function writeAgentFiles(projectDir: string): AgentFilesResult { | |
| const agentsMdExists = fs.existsSync(agentsMdPath) | |
| const claudeMdExists = fs.existsSync(claudeMdPath) | |
| - if (agentsMdExists) { | |
| + const claudeMdHostsBlock = | |
| + claudeMdExists && | |
| + (tryReadFile(claudeMdPath)?.includes(AGENT_RULES_START_MARKER) ?? false) | |
| + const agentsMdHostsBlock = | |
| + agentsMdExists && | |
| + (tryReadFile(agentsMdPath)?.includes(AGENT_RULES_START_MARKER) ?? false) | |
| + | |
| + if (agentsMdExists && (agentsMdHostsBlock || !claudeMdHostsBlock)) { | |
| return { | |
| agentsMd: upsertFile(agentsMdPath, block), | |
| claudeMd: 'skipped', | |