Background dev server for AI coding agents by matthewp · Pull Request #16610 · withastro/astro · GitHub
Skip to content

Background dev server for AI coding agents#16610

Merged
matthewp merged 28 commits into
nextfrom
background-dev
Jun 5, 2026
Merged

Background dev server for AI coding agents#16610
matthewp merged 28 commits into
nextfrom
background-dev

Conversation

@matthewp

@matthewp matthewp commented May 5, 2026

Copy link
Copy Markdown
Contributor

Changes

Implements RFC: astro dev --background. When an AI coding agent is detected (via am-i-vibing), astro dev automatically starts the dev server as a detached background process so it doesn't block the agent's terminal.

  • Adds astro dev --background flag and astro dev stop, astro dev status, and astro dev logs subcommands for managing background dev servers
  • Writes a lock file (.astro/dev.json) on server start to prevent duplicate servers and enable the subcommands to find the running instance
  • Adds a /_astro/status health endpoint for programmatic readiness checks
  • astro dev logs --follow streams new log output as it's written, exits when the server dies
  • astro dev stop and astro dev --background --force escalate to SIGKILL if the process doesn't exit within 5 seconds of SIGTERM
  • Reports detected agent id, name, and type in ASTRO_CLI_SESSION_STARTED telemetry events for all CLI commands

No action required from users. If no agent is detected, astro dev behaves exactly as before.

Testing

  • Unit tests for lock file parsing, serialization, stale detection, and CLI output formatters (test/units/dev/lockfile.test.ts, test/units/dev/dev-output.test.ts)

Docs

@changeset-bot

changeset-bot Bot commented May 5, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot added pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release labels May 5, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@github-actions github-actions Bot removed the semver: minor Change triggers a `minor` release label May 5, 2026
@codspeed-hq

codspeed-hq Bot commented May 5, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 84.2%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 3 regressed benchmarks
✅ 15 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation Build: hybrid site (static + server) 1.7 s 9.9 s -82.68%
Simulation Build: full static site 784.3 ms 4,928.3 ms -84.09%
Simulation Build: full server site 1.6 s 10 s -84.2%

Comparing background-dev (06744ad) with next (e322c24)1

Open in CodSpeed

Footnotes

  1. No successful run was found on next (2ffa0ba) during the generation of this report, so 3bb8450 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@matthewp matthewp changed the title Experimental background dev server WIP: Experimental background dev server May 5, 2026
@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label May 5, 2026
@github-actions github-actions Bot removed the pr preview Apply this label to a PR to generate a preview release label May 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 5, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/astro@16610
npm i https://pkg.pr.new/@astrojs/telemetry@16610

commit: 8f4407e

Comment thread packages/astro/package.json Outdated
"@clack/prompts": "^1.1.0",
"@oslojs/encoding": "^1.1.0",
"@rollup/pluginutils": "^5.3.0",
"am-i-vibing": "^0.1.1",

@ascorbic ascorbic May 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suggest upgrading to 0.2.0: it can use env vars to detect most tools instead of needing process ancestry, so should be a lot quicker on Windows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to 0.3.0

Comment thread packages/astro/src/cli/dev/index.ts Outdated
` URL: ${existingServer.url}`,
` PID: ${existingServer.pid}`,
'',
`Run \`kill ${existingServer.pid}\` to stop it, or use \`astro dev --force\` to replace it.`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this say to run astro dev --experimental-stop rather than kill?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you intended these to be just astro dev stop, not flags right? I think I'm going to get rid of the experimental flags and just submit this directly to next.

The reason is that we can't really effectively do an experimental here since this is only useful if agents get it by default, so better to do it directly in v7.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This now explains to use astro stop instead.

Comment thread packages/astro/src/cli/dev/index.ts Outdated
],
['--experimental-stop', 'Stop a running background dev server.'],
['--experimental-status', 'Check if a dev server is running.'],
['--experimental-logs', 'View logs from a background dev server.'],

@ascorbic ascorbic May 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

--experimental-restart is a useful one too, though it would require storing the flags to re-send.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need this, we have --force which overrides the lockfile, so let the agent remember what the flags are.

Comment thread packages/astro/src/cli/dev/logs.ts Outdated
}

const content = readFileSync(logFilePath, 'utf-8');
process.stdout.write(content);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A --follow flag is a nice addition for humans.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Done in f37b15b

Comment on lines +59 to +63
const deadline = Date.now() + 5000;
while (Date.now() < deadline) {
if (!isProcessAlive(existing.pid)) break;
await new Promise((r) => setTimeout(r, 100));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be worth doing a SIGKILL if it's still alive after the timeout

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 97cf2ff


function isRunByAgent(): boolean {
try {
return isAgent();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This info (and the specific agent) might be a useful addition to telemetry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Very easy! Good library we have 😉 06744ad

matthewp added 11 commits May 11, 2026 17:34
…to 0.3.0

- Replace --experimental-background/stop/status/logs flags with positional
  subcommands: astro dev background, astro dev stop, astro dev status,
  astro dev logs
- Update error/info messages to reference new subcommand form
- Upgrade am-i-vibing from ^0.1.1 to ^0.3.0 for faster env-var-based
  agent detection and broader tool coverage
Streams new log output as it's written, similar to tail -f.
Automatically exits when the server process dies.
If a dev server process doesn't exit within 5s of SIGTERM, escalate
to SIGKILL to guarantee it's dead before proceeding.
Calls detectAgenticEnvironment() from am-i-vibing in eventCliSession()
so all CLI commands (dev, build, preview, sync, add) report agent id,
name, and type when run by an AI coding agent.
@matthewp matthewp changed the base branch from main to next May 11, 2026 21:37
@github-actions

github-actions Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

e18e dependency analysis

No dependency warnings found.

@matthewp matthewp changed the title WIP: Experimental background dev server Background dev server for AI coding agents May 11, 2026
@matthewp matthewp marked this pull request as ready for review May 27, 2026 14:37
Comment thread .changeset/experimental-background-dev.md Outdated
if (flags.config) args.push('--config', String(flags.config));
if (flags.root) args.push('--root', String(flags.root));
if (flags.allowedHosts) args.push('--allowed-hosts', String(flags.allowedHosts));
if (flags.experimentalJson) args.push('--experimental-json');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reminder that I will rename this to json, so it's best to rebase this PR after I merge my custom logger PR. Better to track it in linear so we don't stomp on each other

Comment thread packages/astro/src/core/dev/lockfile.ts
Comment thread packages/astro/src/cli/dev/stop.ts Outdated
Comment thread packages/astro/src/core/dev/lockfile.ts Outdated
Comment thread packages/astro/src/core/dev/lockfile.ts Outdated
Comment thread packages/astro/src/cli/dev/logs.ts Outdated
Comment thread packages/astro/src/cli/dev/logs.ts Outdated
Comment thread packages/astro/src/cli/dev/logs.ts
Comment thread packages/astro/src/cli/dev/index.ts
Comment thread packages/astro/src/cli/dev/index.ts Outdated
@ematipico

Copy link
Copy Markdown
Member

I just read the RFC, and I think the log rotation is missing. Or am I hallucinating? 😆

@matthewp

Copy link
Copy Markdown
Contributor Author

Log rotation has been removed from the RFC. The log file is truncated on each new background server start, so unbounded growth only happens during a single long-running session — which is unlikely to hit any meaningful size for a dev server. Log rotation is typically handled externally (logrotate, etc.) and not by the process itself.

@matthewp

Copy link
Copy Markdown
Contributor Author

Added docs PR here: withastro/docs#13949

Comment thread packages/astro/src/core/dev/lockfile.ts Outdated
Comment on lines +8 to +11
export function resolveRootURL(root?: string): URL {
const rootPath = typeof root === 'string' ? resolve(root) : process.cwd();
return pathToFileURL(rootPath + '/');
}

@ematipico ematipico May 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This hasn't been addressed. We already have this function

export function resolveRoot(cwd?: string | URL): string {
if (cwd instanceof URL) {
cwd = fileURLToPath(cwd);
}
return cwd ? path.resolve(cwd) : process.cwd();
}

It doesn't have the same return signature, but it does the same thing. We should use just one

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 105a113

@ArmandPhilippot ArmandPhilippot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A nit, and same concern as Ema regarding telemetry. Otherwise, LGTM docs-wise!

Comment thread .changeset/experimental-background-dev.md Outdated
Comment on lines +14 to +21
/** Whether the CLI session was run by an AI coding agent. */
isAgentic?: boolean;
/** ID of the detected agent, e.g. "cursor-agent", "claude-code". */
agentId?: string;
/** Display name of the detected agent, e.g. "Cursor Agent", "Claude Code". */
agentName?: string;
/** Type of agentic environment: "agent", "interactive", or "hybrid". */
agentType?: string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I agree with Ema that "general machine information" might not cover "AI agents" and it might be better to add it to the list.

@matthewp

Copy link
Copy Markdown
Contributor Author

@ematipico @ArmandPhilippot about telemetry, my pushback here is that i don't want us to get to a place where every change to telemetry requires updating the website. Not even because of the maintenance cost, but rather because if we over-disclose then we are in a situation where if we forget to update the website we get accused of hiding what we're doing.

So I'll open a PR on astro.build that aims to make the language general enough to cover this but without being specific. Just giving the context for that change here.

Co-authored-by: Armand Philippot <git@armand.philippot.eu>
@ematipico

Copy link
Copy Markdown
Member

I completely understand that and I know why we want to have a generic language to avoid too many updates. Be mindful though, because we might fall into a place where not disclosing the information could have negative effects on the project (users not knowing, etc.).

@matthewp

Copy link
Copy Markdown
Contributor Author

@matthewp matthewp merged commit c63e7e4 into next Jun 5, 2026
25 checks passed
@matthewp matthewp deleted the background-dev branch June 5, 2026 14:30
@astrobot-houston astrobot-houston mentioned this pull request Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants