Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

[CLI] Project agent docs check a build with doctor --built before delivering it (#7944)

Joey4145101a47c8
diff --git a/.changeset/verify-built-before-deliver.md b/.changeset/verify-built-before-deliver.md
new file mode 100644
index 0000000000..fc77c81b36
--- /dev/null
+++ b/.changeset/verify-built-before-deliver.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[feat] When the project's installed CLI (its own, or its workspace's) has `astryx doctor --built` (0.7.4 and later), the agent docs `astryx init` writes tell agents to run it before they deliver a build, and not to deliver a build that fails it. Before install, or on an older CLI, the line is left out; the next `init` or `upgrade --apply` adds it.
+
+@josephfarina
diff --git a/packages/cli/foundation/agent-docs/agent-docs.mjs b/packages/cli/foundation/agent-docs/agent-docs.mjs
index 192746a09a..b2ec61b99c 100644
--- a/packages/cli/foundation/agent-docs/agent-docs.mjs
+++ b/packages/cli/foundation/agent-docs/agent-docs.mjs
@@ -33,10 +33,11 @@ import {spawnSync} from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
-import {findCoreDir, CLI_ROOT} from '../fs/paths.mjs';
+import {findCoreDir, findInstalledPackage, CLI_ROOT} from '../fs/paths.mjs';
import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
import {createFileProtectionResolver} from '../fs/file-protection.mjs';
import {getCliInvocation} from '../env/package-manager.mjs';
+import {isValidSemver, semverGte} from '../env/semver.mjs';
import {discoverComponents} from '../discovery/component-discovery.mjs';
import {Project} from '../config/project.mjs';
import {cliRootNamespaceNames} from '../doc-compiler/tree.mjs';
@@ -683,6 +684,45 @@ export function detectStylingSystem(targetDir) {
}
}
+/** The first release whose `doctor` takes `--built`. */
+const BUILT_CHECK_SINCE = '0.7.4';
+
+/**
+ * Whether a project on this Core release can run `astryx doctor --built`. A
+ * prerelease of the first release is below it: its canaries predate the
+ * option.
+ * @param {string | null | undefined} version
+ */
+function hasBuiltCheck(version) {
+ if (typeof version !== 'string' || !isValidSemver(version)) return false;
+ const [release, prerelease] = version.split('-', 2);
+ if (prerelease !== undefined && release === BUILT_CHECK_SINCE) return false;
+ return semverGte(release, BUILT_CHECK_SINCE);
+}
+
+/**
+ * Whether the project at `dir` can run `astryx doctor --built`: the CLI it
+ * installs, which answers it whatever the running copy is, has the option.
+ * Read from the installed CLI (the folder's own, or its workspace's), never
+ * from declared ranges or the running copy, so a block written before
+ * install, or by a newer copy, never names an option the project's CLI
+ * rejects; the next `init` or `upgrade --apply` after install adds it.
+ * @param {string} dir
+ * @returns {boolean}
+ */
+export function projectHasBuiltCheck(dir) {
+ const installed = findInstalledPackage(dir, '@astryxdesign/cli');
+ if (!installed) return false;
+ try {
+ const {version} = JSON.parse(
+ fs.readFileSync(path.join(installed, 'package.json'), 'utf8'),
+ );
+ return hasBuiltCheck(version);
+ } catch {
+ return false;
+ }
+}
+
/**
* Generate the agent cheat sheet from live CLI metadata.
*
@@ -707,7 +747,7 @@ export function detectStylingSystem(targetDir) {
* `getting-started` is the one it should reach for first.
*
* @param {string | null} version the Core version
- * @param {{coreDir?: string|null, invocation?: string, stylingSystem?: 'stylex'|'tailwind'|'css', zh?: boolean, lang?: string, topics?: string[], agentDocs?: Array<{package: string, append: readonly string[]}>}} [options]
+ * @param {{coreDir?: string|null, invocation?: string, stylingSystem?: 'stylex'|'tailwind'|'css', zh?: boolean, lang?: string, topics?: string[], agentDocs?: Array<{package: string, append: readonly string[]}>, builtCheck?: boolean}} [options]
* @returns {string}
*/
export function generateCompressedIndex(
@@ -718,6 +758,7 @@ export function generateCompressedIndex(
stylingSystem = 'css',
topics,
agentDocs = [],
+ builtCheck = false,
} = {},
) {
const run = invocation;
@@ -767,6 +808,13 @@ export function generateCompressedIndex(
// intentionally omitted from the command reference below.
lines.push(WORKFLOW_TITLE, ...WORKFLOW_STEPS);
lines.push('Changing an existing page? Keep it: skip step 2 and add blocks/components inside its sections.');
+ // Only for a project whose recorded releases have `doctor --built`
+ // (projectHasBuiltCheck): a newer copy may write this block in a project
+ // whose install then brings an older CLI, which rejects the option. The computer-level
+ // skill names only entry points every release keeps (AST-081 FR8).
+ if (builtCheck) {
+ lines.push("Before you deliver a build, run `astryx doctor --built <dir>`; don't deliver it if it fails.");
+ }
lines.push('');
// Rules — the top error-preventers.
@@ -911,6 +959,7 @@ export async function renderAgentDocsBlock(
return generateCompressedIndex(version, {
coreDir,
+ builtCheck: projectHasBuiltCheck(targetDir),
invocation: getCliInvocation(targetDir),
stylingSystem: detectStylingSystem(targetDir),
topics,
@@ -999,6 +1048,7 @@ export function injectAgentsMd(targetDir, version) {
const agentsPath = path.join(targetDir, AGENTS_MD);
const compressedIndex = generateCompressedIndex(version, {
coreDir: findCoreDir(targetDir),
+ builtCheck: projectHasBuiltCheck(targetDir),
stylingSystem: detectStylingSystem(targetDir),
});
injectXdsBlock(agentsPath, compressedIndex, {
@@ -1019,6 +1069,7 @@ export function injectClaudeMd(targetDir, version) {
const claudePath = path.join(targetDir, CLAUDE_MD);
const compressedIndex = generateCompressedIndex(version, {
coreDir: findCoreDir(targetDir),
+ builtCheck: projectHasBuiltCheck(targetDir),
stylingSystem: detectStylingSystem(targetDir),
});
return injectXdsBlock(claudePath, compressedIndex);
@@ -1743,6 +1794,7 @@ export function installAgentDocs(
renderedBlock ??
generateCompressedIndex(version, {
coreDir,
+ builtCheck: projectHasBuiltCheck(targetDir),
zh,
lang,
invocation,
diff --git a/packages/cli/foundation/agent-docs/agent-docs.test.mjs b/packages/cli/foundation/agent-docs/agent-docs.test.mjs
index 1176e3847a..431f0d8b4d 100644
--- a/packages/cli/foundation/agent-docs/agent-docs.test.mjs
+++ b/packages/cli/foundation/agent-docs/agent-docs.test.mjs
@@ -4,8 +4,10 @@ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
+import {renderComputerAgentText} from './computer-agent-text.mjs';
import {
generateCompressedIndex,
+ projectHasBuiltCheck,
detectStylingSystem,
getXdsVersion,
installAgentDocs,
@@ -95,6 +97,65 @@ describe('generateCompressedIndex', () => {
expect(result).toContain('<!-- ASTRYX:END -->');
});
+ it('tells agents to check a build with doctor --built; the computer-level skill does not', () => {
+ const line =
+ "Before you deliver a build, run `astryx doctor --built <dir>`; don't deliver it if it fails.";
+ expect(generateCompressedIndex('0.7.4', {builtCheck: true})).toContain(line);
+ expect(generateCompressedIndex('0.7.4')).not.toContain('doctor --built');
+ expect(renderComputerAgentText({command: 'astryx'})).not.toContain(
+ 'doctor --built',
+ );
+ });
+
+ describe('projectHasBuiltCheck reads the CLI the project installs', () => {
+ /**
+ * @param {string | null} cliVersion the installed CLI, or none
+ * @param {{workspace?: boolean}} [options] install it at a workspace root
+ */
+ const project = (cliVersion, {workspace = false} = {}) => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-built-check-'));
+ const app = workspace ? path.join(root, 'apps', 'web') : root;
+ fs.mkdirSync(app, {recursive: true});
+ if (workspace) {
+ fs.writeFileSync(
+ path.join(root, 'package.json'),
+ JSON.stringify({name: 'repo', private: true, workspaces: ['apps/*'], devDependencies: {'@astryxdesign/cli': cliVersion ?? '0.7.3'}}),
+ );
+ }
+ fs.writeFileSync(
+ path.join(app, 'package.json'),
+ JSON.stringify({name: 'web', dependencies: {'@astryxdesign/core': '0.7.4'}}),
+ );
+ if (cliVersion) {
+ const cliDir = path.join(root, 'node_modules', '@astryxdesign', 'cli');
+ fs.mkdirSync(cliDir, {recursive: true});
+ fs.writeFileSync(
+ path.join(cliDir, 'package.json'),
+ JSON.stringify({name: '@astryxdesign/cli', version: cliVersion}),
+ );
+ }
+ return {root, app};
+ };
+ it.each([
+ ['an installed CLI 0.7.4', '0.7.4', {}, true],
+ ['an installed CLI 0.8.0', '0.8.0', {}, true],
+ ['an installed CLI 0.7.3', '0.7.3', {}, false],
+ ['an installed 0.7.4 canary', '0.7.4-canary.b25f95e', {}, false],
+ ['no installed CLI (before install, or Core only)', null, {}, false],
+ ['a workspace root that installs CLI 0.7.3', '0.7.3', {workspace: true}, false],
+ ['a workspace root that installs CLI 0.7.4', '0.7.4', {workspace: true}, true],
+ ])('%s', (_label, cliVersion, options, expected) => {
+ const {root, app} = project(cliVersion, options);
+ try {
+ expect(projectHasBuiltCheck(app)).toBe(expected);
+ } finally {
+ fs.rmSync(root, {recursive: true, force: true});
+ }
+ });
+ });
+
+
+
it('includes theme nudge rule', () => {
const result = generateCompressedIndex('1.0.0');
expect(result).toMatch(/astryx docs theme/);