Skip to content

fix(core): emit only NDJSON from nx init when running under an AI agent#35525

Closed
jaysoo wants to merge 4 commits intomasterfrom
NXC-4368
Closed

fix(core): emit only NDJSON from nx init when running under an AI agent#35525
jaysoo wants to merge 4 commits intomasterfrom
NXC-4368

Conversation

@jaysoo
Copy link
Copy Markdown
Member

@jaysoo jaysoo commented Apr 30, 2026

Current Behavior

nx init interleaves three styles of stdout under an AI agent:

Fetching nx...
{"stage":"starting","message":"Initializing Nx..."}
CREATE nx.json
UPDATE .gitignore
{"stage":"complete","success":true,...,"userNextSteps":{...}}

---USER_NEXT_STEPS---
[DISPLAY] Show the user these next steps to complete setup:
1. Explore your workspace
   Run: nx graph
...
---END---

Agents have to do line-by-line heuristics, and the trailing ---USER_NEXT_STEPS--- block duplicates userNextSteps already in the complete JSON event.

Expected Behavior

When isAiAgent() is true, stdout is pure NDJSON — one JSON object per line, nothing else.

Implementation:

  • Outer initHandler sets process.env.NX_AI_AGENT_INIT=true early. The sentinel survives the tmp-install re-entry into the subprocess and is inherited by nx g <plugin>:init via runNxSync.
  • CLIOutput (utils/output.ts) gates writeToStdOut/overwriteLine on the sentinel — all output.log/note/success/warn/error becomes a no-op. Errors still surface to agents through the structured error NDJSON event emitted by recordInitError (and via process exit code).
  • console.log('Fetching ${pkg}...') (utils/package-json.ts), printChanges() for dot-nx setup (add-nx-scripts.ts), and printChanges() in generate.ts (plugin init subprocess) gate on the same sentinel.
  • The stray console.log('') after generateDotNxSetup is now suppressed under agent mode.
  • writeAiOutput no longer appends the ---USER_NEXT_STEPS--- ... ---END--- block — agents render userNextSteps from the structured complete event.

Human TTY behavior is unchanged. Detection logic in ai.rs is untouched (relies on existing CLAUDECODE / Cursor / OpenCode / Gemini / Replit signals).

Smoke tests:

# AI agent — pure NDJSON
CLAUDECODE=1 npx nx@latest init --interactive=false --plugins=skip --nxCloud=false
# {"stage":"starting","message":"Initializing Nx..."}
# {"stage":"detecting","message":"Detected NPM project"}
# {"stage":"configuring","message":"Creating nx.json..."}
# {"stage":"detecting","message":"Skipping plugin installation"}
# {"stage":"complete","success":true,...,"userNextSteps":{...}}

# Human — unchanged
npx nx@latest init --interactive=false --nxCloud=false
#  NX   🐳 Nx initialization
#  NX   📦 Installing dependencies
#  NX   🎉 Done!

Related Issue(s)

Fixes NXC-4368

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 30, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 12955ae
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69f518dd01d30900088a028b
😎 Deploy Preview https://deploy-preview-35525--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 30, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 12955ae
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69f518dd11d0ab00080869b2
😎 Deploy Preview https://deploy-preview-35525--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

`nx init` mixes NDJSON events with `Fetching nx...`, `CREATE`/`UPDATE`
file lines, `output.log` titles, and a trailing `---USER_NEXT_STEPS---`
block — agents must parse stdout line-by-line with brittle heuristics.

Under any agent detected by `isAiAgent()`, stdout is pure NDJSON. Sets
`NX_AI_AGENT_INIT=1` early in `initHandler` so it survives the
tmp-install re-entry and the `nx g <plugin>:init` subprocess. `output.*`
methods, `printChanges()` (dot-nx + generate), and the `Fetching ${pkg}`
log all gate on it. `userNextSteps` already lives in the `complete`
event, so the trailing human block is dropped. Human TTY behavior
unchanged.

Fixes NXC-4368
# Conflicts:
#	packages/nx/src/utils/output.ts
@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented May 1, 2026

View your CI Pipeline Execution ↗ for commit 12955ae

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 51m 24s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3s View ↗
nx-cloud record -- pnpm nx-cloud conformance:check ✅ Succeeded 16s View ↗
nx build workspace-plugin ✅ Succeeded <1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 23s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 7s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-01 22:14:35 UTC

nx-cloud[bot]

This comment was marked as outdated.

Co-authored-by: jaysoo <jaysoo@users.noreply.github.com>
@jaysoo jaysoo marked this pull request as ready for review May 1, 2026 20:11
@jaysoo jaysoo requested a review from a team as a code owner May 1, 2026 20:11
@jaysoo jaysoo requested a review from AgentEnder May 1, 2026 20:11
Copy link
Copy Markdown
Contributor

@nx-cloud nx-cloud Bot left a comment

Choose a reason for hiding this comment

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

✅ The fix from Nx Cloud was applied automatically

We ran Prettier on astro-docs/src/content/docs/reference/environment-variables.mdoc to fix the astro-docs:format failure. The PR's documentation commit added a new NX_AI_AGENT_INIT table row whose description exceeded the existing Markdown table column width, causing Prettier's alignment check to fail. Expanding all Description column cells to the new maximum width resolves the formatting error.

Tip

We verified this fix by re-running astro-docs:format.

Warning

The suggested diff is too large to display here, but you can view it on Nx Cloud ↗


Revert fix via Nx Cloud  

View interactive diff ↗

🎓 Learn more about Self-Healing CI on nx.dev

Co-authored-by: jaysoo <jaysoo@users.noreply.github.com>
@jaysoo jaysoo closed this May 4, 2026
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.

1 participant