fix(bundled-defaults): auto-generate import list, emit inline strings (#1263)

* fix(bundled-defaults): auto-generate import list, emit inline strings

Root-cause fix for bundle drift (15 commands + 7 workflows previously
missing from binary distributions) and a prerequisite for packaging
@archon/workflows as a Node-loadable SDK.

The hand-maintained `bundled-defaults.ts` import list is replaced by
`scripts/generate-bundled-defaults.ts`, which walks
`.archon/{commands,workflows}/defaults/` and emits a generated source
file with inline string literals. `bundled-defaults.ts` becomes a thin
facade that re-exports the generated records and keeps the
`isBinaryBuild()` helper.

Inline strings (via JSON.stringify) replace Bun's
`import X from '...' with { type: 'text' }` attributes. The binary build
still embeds the data at compile time, but the module now loads under
Node too — removing SDK blocker #2.

- Generator: `scripts/generate-bundled-defaults.ts` (+ `--check` mode for CI)
- `package.json`: `generate:bundled`, `check:bundled`; wired into `validate`
- `build-binaries.sh`: regenerates defaults before compile
- Test: `bundle completeness` now derives expected set from on-disk files
- All 56 defaults (36 commands + 20 workflows) now in the bundle

* fix(bundled-defaults): address PR review feedback

Review: https://github.com/coleam00/Archon/pull/1263#issuecomment-4262719090

Generator:
- Guard against .yaml/.yml name collisions (previously silent overwrite)
- Add early access() check with actionable error when run from wrong cwd
- Type top-level catch as unknown; print only message for Error instances
- Drop redundant /* eslint-disable */ emission (global ignore covers it)
- Fix misleading CI-mechanism claim in header comment
- Collapse dead `if (!ext) continue` guard into a single typed pass

Scripts get real type-checking + linting:
- New scripts/tsconfig.json extending root config
- type-check now includes scripts/ via `tsc --noEmit -p scripts/tsconfig.json`
- Drop `scripts/**` from eslint ignores; add to projectService file scope

Tests:
- Inline listNames helper (Rule of Three)
- Drop redundant toBeDefined/typeof assertions; the Record<string, string>
  type plus length > 50 already cover them
- Add content-fidelity round-trip assertion (defense against generator
  content bugs, not just key-set drift)

Facade comment: drop dead reference to .claude/rules/dx-quirks.md.

CI: wire `bun run check:bundled` into .github/workflows/test.yml so the
header's CI-verification claim is truthful.

Docs: CLAUDE.md step count four→five; add contributor bullet about
`bun run generate:bundled` in the Defaults section and CONTRIBUTING.md.

* chore(e2e): bump Codex model to gpt-5.2

gpt-5.1-codex-mini is deprecated and unavailable on ChatGPT-account Codex
auth. Plain gpt-5.2 works. Verified end-to-end:

- e2e-codex-smoke: structured output returns {category:'math'}
- e2e-mixed-providers: claude+codex both return expected tokens
This commit is contained in:
Rasmus Widing 2026-04-16 21:27:51 +02:00 committed by GitHub
parent d535c832e3
commit 86e4c8d605
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 368 additions and 193 deletions

View file

@ -3,7 +3,7 @@
name: e2e-codex-smoke
description: "E2E smoke test for Codex provider. Runs a simple prompt + structured output node."
provider: codex
model: gpt-5.1-codex-mini
model: gpt-5.2
nodes:
- id: simple

View file

@ -18,7 +18,7 @@ nodes:
- id: codex-node
prompt: "Say 'codex-ok' and nothing else."
provider: codex
model: gpt-5.1-codex-mini
model: gpt-5.2
idle_timeout: 30000
# 3. Assert both providers returned output

View file

@ -27,6 +27,9 @@ jobs:
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Check bundled defaults
run: bun run check:bundled
- name: Type check
run: bun run type-check

View file

@ -22,6 +22,9 @@ workspace/
# Lock files (auto-generated)
package-lock.json
# Auto-generated source (regenerated by scripts/generate-bundled-defaults.ts)
**/*.generated.ts
# Agent commands and documentation (user-managed)
.agents/
.claude/

View file

@ -150,7 +150,7 @@ bun run format:check
bun run validate
```
This runs type-check, lint, format check, and tests. All four must pass for CI to succeed.
This runs `check:bundled`, type-check, lint, format check, and tests. All five must pass for CI to succeed.
### ESLint Guidelines
@ -710,10 +710,11 @@ async function createSession(conversationId: string, codebaseId: string) {
**Defaults:**
- Bundled in `.archon/commands/defaults/` and `.archon/workflows/defaults/`
- Binary builds: Embedded at compile time (no filesystem access needed)
- Binary builds: Embedded at compile time (no filesystem access needed) via `packages/workflows/src/defaults/bundled-defaults.generated.ts`
- Source builds: Loaded from filesystem at runtime
- Merged with repo-specific commands/workflows (repo overrides defaults by name)
- Opt-out: Set `defaults.loadDefaultCommands: false` or `defaults.loadDefaultWorkflows: false` in `.archon/config.yaml`
- **After adding, removing, or editing a default file, run `bun run generate:bundled`** to refresh the embedded bundle. `bun run validate` (and CI) run `check:bundled` and will fail loudly if the generated file is stale.
**Global workflows** (user-level, applies to every project):
- Path: `~/.archon/.archon/workflows/` (or `$ARCHON_HOME/.archon/workflows/`)

View file

@ -17,15 +17,20 @@ Thank you for your interest in contributing to Archon!
Before submitting a PR, ensure:
```bash
bun run type-check # TypeScript types
bun run lint # ESLint
bun run format # Prettier
bun run test # All tests (per-package isolation)
bun run check:bundled # Bundled defaults are up to date (see note below)
bun run type-check # TypeScript types
bun run lint # ESLint
bun run format # Prettier
bun run test # All tests (per-package isolation)
# Or run the full validation suite:
bun run validate
```
**Bundled defaults**: If you added, removed, or edited a file under
`.archon/commands/defaults/` or `.archon/workflows/defaults/`, run
`bun run generate:bundled` to refresh the embedded bundle before committing.
**Important:** Use `bun run test` (not `bun test` from the repo root) to avoid mock pollution across packages.
### Commit Messages

View file

@ -17,6 +17,7 @@ export default tseslint.config(
'worktrees/**',
'.claude/worktrees/**',
'.claude/skills/**',
'**/*.generated.ts', // Auto-generated source files (content inlined via JSON.stringify)
'**/*.js',
'*.mjs',
'**/*.test.ts',
@ -41,7 +42,7 @@ export default tseslint.config(
// Project-specific settings
{
files: ['packages/*/src/**/*.{ts,tsx}'],
files: ['packages/*/src/**/*.{ts,tsx}', 'scripts/**/*.ts'],
languageOptions: {
parserOptions: {
projectService: true,

View file

@ -14,9 +14,11 @@
"build": "bun --filter '*' build",
"build:binaries": "bash scripts/build-binaries.sh",
"build:checksums": "bash scripts/checksums.sh",
"generate:bundled": "bun run scripts/generate-bundled-defaults.ts",
"check:bundled": "bun run scripts/generate-bundled-defaults.ts --check",
"test": "bun --filter '*' --parallel test",
"test:watch": "bun --filter @archon/server test:watch",
"type-check": "bun --filter '*' type-check",
"type-check": "bun --filter '*' type-check && bun x tsc --noEmit -p scripts/tsconfig.json",
"lint": "bun x eslint . --cache",
"lint:fix": "bun x eslint . --cache --fix",
"format": "bun x prettier --write .",
@ -25,7 +27,7 @@
"build:web": "bun --filter @archon/web build",
"dev:docs": "bun --filter @archon/docs-web dev",
"build:docs": "bun --filter @archon/docs-web build",
"validate": "bun run type-check && bun run lint --max-warnings 0 && bun run format:check && bun run test",
"validate": "bun run check:bundled && bun run type-check && bun run lint --max-warnings 0 && bun run format:check && bun run test",
"prepare": "husky",
"setup-auth": "bun --filter @archon/server setup-auth"
},

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,15 @@
import { describe, it, expect } from 'bun:test';
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';
import { isBinaryBuild, BUNDLED_COMMANDS, BUNDLED_WORKFLOWS } from './bundled-defaults';
// Resolve the on-disk defaults directories relative to this test file so the
// tests work regardless of cwd. From packages/workflows/src/defaults go up
// four levels to the repo root, then into .archon/.
const REPO_ROOT = join(import.meta.dir, '..', '..', '..', '..');
const COMMANDS_DIR = join(REPO_ROOT, '.archon/commands/defaults');
const WORKFLOWS_DIR = join(REPO_ROOT, '.archon/workflows/defaults');
describe('bundled-defaults', () => {
describe('isBinaryBuild', () => {
it('should return false in dev/test mode', () => {
@ -12,57 +21,54 @@ describe('bundled-defaults', () => {
});
});
describe('bundle completeness', () => {
// These assertions are the canary for bundle drift: if someone adds a
// default file without regenerating bundled-defaults.generated.ts, the
// bundle would be missing in compiled binaries (see #979 context). The
// generator is `scripts/generate-bundled-defaults.ts`, and
// `bun run check:bundled` verifies the generated file is up to date.
it('BUNDLED_COMMANDS contains every .md file in .archon/commands/defaults/', () => {
const onDisk = readdirSync(COMMANDS_DIR)
.filter(f => f.endsWith('.md'))
.map(f => f.slice(0, -'.md'.length))
.sort();
expect(Object.keys(BUNDLED_COMMANDS).sort()).toEqual(onDisk);
});
it('BUNDLED_WORKFLOWS contains every .yaml/.yml file in .archon/workflows/defaults/', () => {
const onDisk = readdirSync(WORKFLOWS_DIR)
.filter(f => f.endsWith('.yaml') || f.endsWith('.yml'))
.map(f => f.replace(/\.ya?ml$/, ''))
.sort();
expect(Object.keys(BUNDLED_WORKFLOWS).sort()).toEqual(onDisk);
});
it('bundled content matches on-disk file content (defense against generator corruption)', () => {
for (const [name, content] of Object.entries(BUNDLED_COMMANDS)) {
const diskContent = readFileSync(join(COMMANDS_DIR, `${name}.md`), 'utf-8');
expect(content).toBe(diskContent);
}
for (const [name, content] of Object.entries(BUNDLED_WORKFLOWS)) {
// Workflows may be .yaml or .yml — prefer .yaml, fall back.
let diskContent: string;
try {
diskContent = readFileSync(join(WORKFLOWS_DIR, `${name}.yaml`), 'utf-8');
} catch {
diskContent = readFileSync(join(WORKFLOWS_DIR, `${name}.yml`), 'utf-8');
}
expect(content).toBe(diskContent);
}
});
});
describe('BUNDLED_COMMANDS', () => {
it('should have all expected default commands', () => {
const expectedCommands = [
'archon-assist',
'archon-code-review-agent',
'archon-comment-quality-agent',
'archon-create-pr',
'archon-docs-impact-agent',
'archon-error-handling-agent',
'archon-implement-issue',
'archon-implement-review-fixes',
'archon-implement',
'archon-investigate-issue',
'archon-pr-review-scope',
'archon-ralph-prd',
'archon-resolve-merge-conflicts',
'archon-sync-pr-with-main',
'archon-synthesize-review',
'archon-test-coverage-agent',
'archon-validate-pr-code-review-feature',
'archon-validate-pr-code-review-main',
'archon-validate-pr-e2e-feature',
'archon-validate-pr-e2e-main',
'archon-validate-pr-report',
];
for (const cmd of expectedCommands) {
expect(BUNDLED_COMMANDS).toHaveProperty(cmd);
}
expect(Object.keys(BUNDLED_COMMANDS)).toHaveLength(21);
});
it('should have non-empty content for all commands', () => {
for (const [name, content] of Object.entries(BUNDLED_COMMANDS)) {
expect(content).toBeDefined();
expect(typeof content).toBe('string');
expect(content.length).toBeGreaterThan(0);
// Commands should have meaningful content (at least some markdown)
it('every command has meaningful content (>50 chars)', () => {
for (const content of Object.values(BUNDLED_COMMANDS)) {
expect(content.length).toBeGreaterThan(50);
}
});
it('should have markdown content format', () => {
// Commands are markdown files, should have typical markdown patterns
for (const [name, content] of Object.entries(BUNDLED_COMMANDS)) {
// Should contain some text (not just whitespace)
expect(content.trim().length).toBeGreaterThan(0);
}
});
it('archon-pr-review-scope should read .pr-number before other discovery', () => {
const content = BUNDLED_COMMANDS['archon-pr-review-scope'];
expect(content).toContain('$ARTIFACTS_DIR/.pr-number');
@ -76,36 +82,8 @@ describe('bundled-defaults', () => {
});
describe('BUNDLED_WORKFLOWS', () => {
it('should have all expected default workflows', () => {
const expectedWorkflows = [
'archon-assist',
'archon-comprehensive-pr-review',
'archon-create-issue',
'archon-feature-development',
'archon-fix-github-issue',
'archon-resolve-conflicts',
'archon-smart-pr-review',
'archon-validate-pr',
'archon-remotion-generate',
'archon-interactive-prd',
'archon-piv-loop',
'archon-adversarial-dev',
'archon-workflow-builder',
];
for (const wf of expectedWorkflows) {
expect(BUNDLED_WORKFLOWS).toHaveProperty(wf);
}
expect(Object.keys(BUNDLED_WORKFLOWS)).toHaveLength(13);
});
it('should have non-empty content for all workflows', () => {
for (const [name, content] of Object.entries(BUNDLED_WORKFLOWS)) {
expect(content).toBeDefined();
expect(typeof content).toBe('string');
expect(content.length).toBeGreaterThan(0);
// Workflows should have meaningful YAML content
it('every workflow has meaningful content (>50 chars)', () => {
for (const content of Object.values(BUNDLED_WORKFLOWS)) {
expect(content.length).toBeGreaterThan(50);
}
});
@ -120,15 +98,10 @@ describe('bundled-defaults', () => {
});
it('should have valid YAML structure', () => {
// Workflows are YAML files, should parse without error
for (const [name, content] of Object.entries(BUNDLED_WORKFLOWS)) {
// Should contain 'name:' as all workflows require a name field
for (const content of Object.values(BUNDLED_WORKFLOWS)) {
expect(content).toContain('name:');
// Should contain 'description:' as all workflows require description
expect(content).toContain('description:');
// Should contain nodes: (with optional loop: inside nodes)
const hasNodes = content.includes('nodes:');
expect(hasNodes).toBe(true);
expect(content.includes('nodes:')).toBe(true);
}
});
});

View file

@ -1,108 +1,28 @@
/**
* Bundled default commands and workflows for binary distribution
* Bundled default commands and workflows for binary distribution.
*
* These static imports are resolved at compile time and embedded into the binary.
* When running as a standalone binary (without Bun), these provide the default
* commands and workflows without needing filesystem access to the source repo.
* Content lives in `bundled-defaults.generated.ts`, which is regenerated from
* `.archon/{commands,workflows}/defaults/` by `scripts/generate-bundled-defaults.ts`.
* This file is the hand-written facade: it re-exports the records and defines
* the binary-detection helper.
*
* Import syntax uses `with { type: 'text' }` to import file contents as strings.
* Why two files:
* - Generated file is pure data never hand-edited, diff on PRs shows
* exactly which defaults changed.
* - Facade keeps the documented `isBinaryBuild()` wrapper in a file that
* humans own.
*
* Why inline strings (and not `import X from '...file.md' with { type: 'text' }`)?
* - Node cannot load `type: 'text'` import attributes it's Bun-specific.
* Using plain string literals keeps `@archon/workflows` importable from
* both runtimes, which removes SDK blocker #2.
* - Bun still embeds the data at compile time when building the CLI binary,
* so runtime behavior is unchanged.
*/
import { BUNDLED_IS_BINARY } from '@archon/paths';
// =============================================================================
// Default Commands (21 total)
// =============================================================================
import archonAssistCmd from '../../../../.archon/commands/defaults/archon-assist.md' with { type: 'text' };
import archonCodeReviewAgentCmd from '../../../../.archon/commands/defaults/archon-code-review-agent.md' with { type: 'text' };
import archonCommentQualityAgentCmd from '../../../../.archon/commands/defaults/archon-comment-quality-agent.md' with { type: 'text' };
import archonCreatePrCmd from '../../../../.archon/commands/defaults/archon-create-pr.md' with { type: 'text' };
import archonDocsImpactAgentCmd from '../../../../.archon/commands/defaults/archon-docs-impact-agent.md' with { type: 'text' };
import archonErrorHandlingAgentCmd from '../../../../.archon/commands/defaults/archon-error-handling-agent.md' with { type: 'text' };
import archonImplementIssueCmd from '../../../../.archon/commands/defaults/archon-implement-issue.md' with { type: 'text' };
import archonImplementReviewFixesCmd from '../../../../.archon/commands/defaults/archon-implement-review-fixes.md' with { type: 'text' };
import archonImplementCmd from '../../../../.archon/commands/defaults/archon-implement.md' with { type: 'text' };
import archonInvestigateIssueCmd from '../../../../.archon/commands/defaults/archon-investigate-issue.md' with { type: 'text' };
import archonPrReviewScopeCmd from '../../../../.archon/commands/defaults/archon-pr-review-scope.md' with { type: 'text' };
import archonRalphPrdCmd from '../../../../.archon/commands/defaults/archon-ralph-prd.md' with { type: 'text' };
import archonResolveMergeConflictsCmd from '../../../../.archon/commands/defaults/archon-resolve-merge-conflicts.md' with { type: 'text' };
import archonSyncPrWithMainCmd from '../../../../.archon/commands/defaults/archon-sync-pr-with-main.md' with { type: 'text' };
import archonSynthesizeReviewCmd from '../../../../.archon/commands/defaults/archon-synthesize-review.md' with { type: 'text' };
import archonTestCoverageAgentCmd from '../../../../.archon/commands/defaults/archon-test-coverage-agent.md' with { type: 'text' };
import archonValidatePrCodeReviewFeatureCmd from '../../../../.archon/commands/defaults/archon-validate-pr-code-review-feature.md' with { type: 'text' };
import archonValidatePrCodeReviewMainCmd from '../../../../.archon/commands/defaults/archon-validate-pr-code-review-main.md' with { type: 'text' };
import archonValidatePrE2eFeatureCmd from '../../../../.archon/commands/defaults/archon-validate-pr-e2e-feature.md' with { type: 'text' };
import archonValidatePrE2eMainCmd from '../../../../.archon/commands/defaults/archon-validate-pr-e2e-main.md' with { type: 'text' };
import archonValidatePrReportCmd from '../../../../.archon/commands/defaults/archon-validate-pr-report.md' with { type: 'text' };
// =============================================================================
// Default Workflows (13 total)
// =============================================================================
import archonAssistWf from '../../../../.archon/workflows/defaults/archon-assist.yaml' with { type: 'text' };
import archonComprehensivePrReviewWf from '../../../../.archon/workflows/defaults/archon-comprehensive-pr-review.yaml' with { type: 'text' };
import archonCreateIssueWf from '../../../../.archon/workflows/defaults/archon-create-issue.yaml' with { type: 'text' };
import archonFeatureDevelopmentWf from '../../../../.archon/workflows/defaults/archon-feature-development.yaml' with { type: 'text' };
import archonFixGithubIssueWf from '../../../../.archon/workflows/defaults/archon-fix-github-issue.yaml' with { type: 'text' };
import archonResolveConflictsWf from '../../../../.archon/workflows/defaults/archon-resolve-conflicts.yaml' with { type: 'text' };
import archonSmartPrReviewWf from '../../../../.archon/workflows/defaults/archon-smart-pr-review.yaml' with { type: 'text' };
import archonValidatePrWf from '../../../../.archon/workflows/defaults/archon-validate-pr.yaml' with { type: 'text' };
import archonRemotionGenerateWf from '../../../../.archon/workflows/defaults/archon-remotion-generate.yaml' with { type: 'text' };
import archonInteractivePrdWf from '../../../../.archon/workflows/defaults/archon-interactive-prd.yaml' with { type: 'text' };
import archonPivLoopWf from '../../../../.archon/workflows/defaults/archon-piv-loop.yaml' with { type: 'text' };
import archonAdversarialDevWf from '../../../../.archon/workflows/defaults/archon-adversarial-dev.yaml' with { type: 'text' };
import archonWorkflowBuilderWf from '../../../../.archon/workflows/defaults/archon-workflow-builder.yaml' with { type: 'text' };
// =============================================================================
// Exports
// =============================================================================
/**
* Bundled default commands - filename (without extension) -> content
*/
export const BUNDLED_COMMANDS: Record<string, string> = {
'archon-assist': archonAssistCmd,
'archon-code-review-agent': archonCodeReviewAgentCmd,
'archon-comment-quality-agent': archonCommentQualityAgentCmd,
'archon-create-pr': archonCreatePrCmd,
'archon-docs-impact-agent': archonDocsImpactAgentCmd,
'archon-error-handling-agent': archonErrorHandlingAgentCmd,
'archon-implement-issue': archonImplementIssueCmd,
'archon-implement-review-fixes': archonImplementReviewFixesCmd,
'archon-implement': archonImplementCmd,
'archon-investigate-issue': archonInvestigateIssueCmd,
'archon-pr-review-scope': archonPrReviewScopeCmd,
'archon-ralph-prd': archonRalphPrdCmd,
'archon-resolve-merge-conflicts': archonResolveMergeConflictsCmd,
'archon-sync-pr-with-main': archonSyncPrWithMainCmd,
'archon-synthesize-review': archonSynthesizeReviewCmd,
'archon-test-coverage-agent': archonTestCoverageAgentCmd,
'archon-validate-pr-code-review-feature': archonValidatePrCodeReviewFeatureCmd,
'archon-validate-pr-code-review-main': archonValidatePrCodeReviewMainCmd,
'archon-validate-pr-e2e-feature': archonValidatePrE2eFeatureCmd,
'archon-validate-pr-e2e-main': archonValidatePrE2eMainCmd,
'archon-validate-pr-report': archonValidatePrReportCmd,
};
/**
* Bundled default workflows - filename (without extension) -> content
*/
export const BUNDLED_WORKFLOWS: Record<string, string> = {
'archon-assist': archonAssistWf,
'archon-comprehensive-pr-review': archonComprehensivePrReviewWf,
'archon-create-issue': archonCreateIssueWf,
'archon-feature-development': archonFeatureDevelopmentWf,
'archon-fix-github-issue': archonFixGithubIssueWf,
'archon-resolve-conflicts': archonResolveConflictsWf,
'archon-smart-pr-review': archonSmartPrReviewWf,
'archon-validate-pr': archonValidatePrWf,
'archon-remotion-generate': archonRemotionGenerateWf,
'archon-interactive-prd': archonInteractivePrdWf,
'archon-piv-loop': archonPivLoopWf,
'archon-adversarial-dev': archonAdversarialDevWf,
'archon-workflow-builder': archonWorkflowBuilderWf,
};
export { BUNDLED_COMMANDS, BUNDLED_WORKFLOWS } from './bundled-defaults.generated';
/**
* Check if the current process is running as a compiled binary (not via Bun CLI).
@ -115,7 +35,7 @@ export const BUNDLED_WORKFLOWS: Record<string, string> = {
* so tests can use `spyOn(bundledDefaults, 'isBinaryBuild').mockReturnValue(...)`
* without resorting to `mock.module('@archon/paths', ...)` which is
* process-global and irreversible in Bun and would pollute other test files.
* See `.claude/rules/dx-quirks.md` and `loader.test.ts` for context.
* See `loader.test.ts` for context.
*/
export function isBinaryBuild(): boolean {
return BUNDLED_IS_BINARY;

View file

@ -21,6 +21,12 @@ OUTFILE="${OUTFILE:-}"
echo "Building Archon CLI v${VERSION} (commit: ${GIT_COMMIT})"
# Regenerate bundled defaults from .archon/{commands,workflows}/defaults/ so the
# compiled binary always embeds the current on-disk contents. CI also runs
# `bun run check:bundled` to catch committed drift.
echo "Regenerating bundled defaults..."
bun run scripts/generate-bundled-defaults.ts
# Update build-time constants in source before compiling.
# The file is restored via an EXIT trap so the dev tree is never left dirty,
# even if `bun build --compile` fails mid-way. See GitHub issue #979.

View file

@ -0,0 +1,172 @@
#!/usr/bin/env bun
/**
* Regenerates packages/workflows/src/defaults/bundled-defaults.generated.ts from
* the on-disk defaults in .archon/commands/defaults/ and .archon/workflows/defaults/.
*
* Emits inline string literals (via JSON.stringify) rather than Bun's
* `import X from '...' with { type: 'text' }` attributes so the module loads
* in Node too. This fixes two problems at once:
* - bundle drift (hand-maintained import list in bundled-defaults.ts)
* - SDK blocker #2 (type: 'text' import attributes are Bun-specific)
*
* Determinism: filenames are sorted before emission so `bun run check:bundled`
* (which regenerates into memory and compares to the committed file) catches
* unregenerated changes. Wired into `bun run validate` and CI.
*
* Usage:
* bun run scripts/generate-bundled-defaults.ts # write
* bun run scripts/generate-bundled-defaults.ts --check # verify (exit 2 if stale)
*
* Exit codes:
* 0 file generated (and unchanged, if --check)
* 1 unexpected error (missing dir, unreadable source, invalid filename, etc.)
* 2 --check was passed and the file would change
*/
import { access, readFile, readdir, writeFile } from 'fs/promises';
import { join, resolve } from 'path';
const REPO_ROOT = resolve(import.meta.dir, '..');
const COMMANDS_DIR = join(REPO_ROOT, '.archon/commands/defaults');
const WORKFLOWS_DIR = join(REPO_ROOT, '.archon/workflows/defaults');
const OUTPUT_PATH = join(
REPO_ROOT,
'packages/workflows/src/defaults/bundled-defaults.generated.ts'
);
const CHECK_ONLY = process.argv.includes('--check');
interface BundledFile {
name: string;
content: string;
}
async function ensureDir(dir: string, label: string): Promise<void> {
try {
await access(dir);
} catch {
throw new Error(
`${label} directory not found: ${dir}\n` +
`Run this script from the repo root (cwd was ${process.cwd()}), ` +
'or verify the .archon/ tree exists.'
);
}
}
async function collectFiles(dir: string, extensions: readonly string[]): Promise<BundledFile[]> {
const entries = await readdir(dir);
const matched = entries
.map(entry => {
const ext = extensions.find(e => entry.endsWith(e));
return ext ? { entry, ext } : undefined;
})
.filter((m): m is { entry: string; ext: string } => m !== undefined)
.sort((a, b) => a.entry.localeCompare(b.entry));
const files: BundledFile[] = [];
const seen = new Set<string>();
for (const { entry, ext } of matched) {
const name = entry.slice(0, -ext.length);
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) {
throw new Error(
`Bundled default has invalid filename "${entry}" in ${dir}. ` +
'Names must be kebab-case (lowercase letters, digits, hyphens).'
);
}
if (seen.has(name)) {
throw new Error(
`Bundled default name collision: "${name}" appears with multiple extensions in ${dir}. ` +
'Keep a single file per name (remove either the .yaml or .yml variant).'
);
}
seen.add(name);
const content = await readFile(join(dir, entry), 'utf-8');
if (!content.trim()) {
throw new Error(`Bundled default "${entry}" in ${dir} is empty.`);
}
files.push({ name, content });
}
return files;
}
function renderRecord(comment: string, exportName: string, files: BundledFile[]): string {
const entries = files
.map(f => ` ${JSON.stringify(f.name)}: ${JSON.stringify(f.content)},`)
.join('\n');
return [
`// ${comment} (${files.length} total)`,
`export const ${exportName}: Record<string, string> = {`,
entries,
'};',
].join('\n');
}
function renderFile(commands: BundledFile[], workflows: BundledFile[]): string {
const header = [
'/**',
' * AUTO-GENERATED — DO NOT EDIT.',
' *',
' * Regenerate with: bun run generate:bundled',
' * Verify up-to-date: bun run check:bundled',
' *',
' * Source of truth:',
' * .archon/commands/defaults/*.md',
' * .archon/workflows/defaults/*.{yaml,yml}',
' *',
' * Contents are inlined as plain string literals (JSON-escaped) so this',
' * module loads in both Bun and Node. Previous versions used',
" * `import X from '...' with { type: 'text' }` which is Bun-specific.",
' */',
'',
].join('\n');
return [
header,
renderRecord('Bundled default commands', 'BUNDLED_COMMANDS', commands),
'',
renderRecord('Bundled default workflows', 'BUNDLED_WORKFLOWS', workflows),
'',
].join('\n');
}
async function main(): Promise<void> {
await Promise.all([
ensureDir(COMMANDS_DIR, 'Commands defaults'),
ensureDir(WORKFLOWS_DIR, 'Workflows defaults'),
]);
const [commands, workflows] = await Promise.all([
collectFiles(COMMANDS_DIR, ['.md']),
collectFiles(WORKFLOWS_DIR, ['.yaml', '.yml']),
]);
const contents = renderFile(commands, workflows);
if (CHECK_ONLY) {
let existing = '';
try {
existing = await readFile(OUTPUT_PATH, 'utf-8');
} catch (e) {
const err = e as NodeJS.ErrnoException;
if (err.code !== 'ENOENT') throw err;
}
if (existing !== contents) {
console.error('bundled-defaults.generated.ts is stale.\n' + 'Run: bun run generate:bundled');
process.exit(2);
}
console.log(
`bundled-defaults.generated.ts is up to date (${commands.length} commands, ${workflows.length} workflows).`
);
return;
}
await writeFile(OUTPUT_PATH, contents, 'utf-8');
console.log(
`Wrote ${OUTPUT_PATH}\n ${commands.length} commands, ${workflows.length} workflows.`
);
}
main().catch((err: unknown) => {
const msg = err instanceof Error ? err.message : String(err);
console.error(msg);
process.exit(1);
});

11
scripts/tsconfig.json Normal file
View file

@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"declaration": false,
"declarationMap": false,
"sourceMap": false,
"types": ["bun-types"]
},
"include": ["*.ts"]
}