Files
Ryan de Melo 2fa679f180 fix(schema): make schema init --default actually set the default (#1709)
* fix(schema): make schema init --default actually set the default

--default wrote defaultSchema to openspec/config.yaml, but the config
loader only reads schema, so new changes kept using spec-driven while
the command reported success. Write the key that is read, and drop the
dead one a previous run may have left behind.

Fixes #1708

* fix(schema): preserve supported configs when setting default

* docs(schema): specify default config file handling

* docs(schema): protect default config migration

* fix(schema): make default initialization atomic

* fix(schema): hide init staging and backup dirs from discovery

`schema init` stages into `.init-staging-<rand>` and moves an existing
schema aside to `<name>.init-backup-<pid>-<ts>`, both inside the schemas
dir. `schema fork` already did this and the resolver filters its temp
names out of discovery; the init names were never added, so `listSchemas`
and `listSchemasWithInfo` surfaced them as real schemas -- in shell
completions, "available schemas" error lists, and change-metadata
validation.

The backup is the durable case: cleanup failure is deliberately tolerated
with a warning, so a blocked cleanup (or a crash mid-transaction) leaves a
permanent phantom schema behind.

Generalize the fork-only filter to cover both commands' staging and backup
names. Real schema names are kebab-case, so excluding these dot-bearing
names can never hide a legitimate schema.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Clay Good <hi@claygood.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 20:42:30 +00:00

6.3 KiB

schema-init-command Specification

Purpose

Define openspec schema init behavior for creating project-local schema skeletons in interactive and non-interactive modes.

Requirements

Requirement: Schema init command creates project-local schema

The CLI SHALL provide an openspec schema init <name> command that creates a new schema directory under openspec/schemas/<name>/ with a valid schema.yaml file and default template files.

Scenario: Create schema with valid name

  • WHEN user runs openspec schema init my-workflow
  • THEN system creates directory openspec/schemas/my-workflow/
  • AND creates schema.yaml with name, version, description, and artifacts array
  • AND creates template files referenced by artifacts
  • AND displays success message with created path

Scenario: Reject invalid schema name

  • WHEN user runs openspec schema init "My Workflow" (contains space)
  • THEN system displays error about invalid schema name
  • AND suggests using kebab-case format
  • AND exits with non-zero code

Scenario: Schema name already exists

  • WHEN user runs openspec schema init existing-schema and openspec/schemas/existing-schema/ already exists
  • THEN system displays error that schema already exists
  • AND suggests using --force to overwrite or schema fork to copy
  • AND exits with non-zero code

Requirement: Schema init supports interactive mode

The CLI SHALL prompt for schema configuration when run in an interactive terminal without explicit flags.

Scenario: Interactive prompts for description

  • WHEN user runs openspec schema init my-workflow in an interactive terminal
  • THEN system prompts for schema description
  • AND uses provided description in generated schema.yaml

Scenario: Interactive prompts for artifact selection

  • WHEN user runs openspec schema init my-workflow in an interactive terminal
  • THEN system displays multi-select prompt with common artifacts (proposal, specs, design, tasks)
  • AND each option includes a brief description
  • AND uses selected artifacts in generated schema.yaml

Scenario: Non-interactive mode with flags

  • WHEN user runs openspec schema init my-workflow --description "My workflow" --artifacts proposal,tasks
  • THEN system creates schema without prompting
  • AND uses flag values for configuration

Requirement: Schema init supports setting project default

The CLI SHALL offer to set the newly created schema as the project default.

Scenario: Set as default interactively

  • WHEN user runs openspec schema init my-workflow in interactive mode
  • AND user confirms setting as default
  • THEN system updates an existing openspec/config.yaml or openspec/config.yml in place with schema: my-workflow
  • AND removes the legacy defaultSchema key when updating an existing configuration
  • AND creates openspec/config.yaml when neither configuration file exists

Scenario: Set as default via flag

  • WHEN user runs openspec schema init my-workflow --default
  • THEN system creates the schema and updates an existing openspec/config.yaml or openspec/config.yml in place with schema: my-workflow
  • AND removes the legacy defaultSchema key when updating an existing configuration
  • AND creates openspec/config.yaml when neither configuration file exists

Scenario: Skip setting default

  • WHEN user runs openspec schema init my-workflow --no-default
  • THEN system creates schema without modifying openspec/config.yaml

Scenario: Invalid config prevents schema creation

  • GIVEN openspec/config.yaml or openspec/config.yml is invalid YAML, is not a YAML object, is not a regular file, or is not writable
  • WHEN user runs openspec schema init my-workflow --default
  • THEN the command exits with a non-zero status
  • AND does not create openspec/schemas/my-workflow/
  • AND leaves the config byte-for-byte unchanged

Scenario: Config failure preserves a schema during forced replacement

  • GIVEN openspec/schemas/my-workflow/ already contains user-authored files
  • AND the project config cannot be validated or atomically replaced
  • WHEN user runs openspec schema init my-workflow --force --default
  • THEN the command exits with a non-zero status
  • AND restores the existing schema and config byte-for-byte

Requirement: Schema init outputs JSON format

The CLI SHALL support --json flag for machine-readable output.

Scenario: JSON output on success

  • WHEN user runs openspec schema init my-workflow --json --description "Test" --artifacts proposal
  • THEN system outputs JSON with created: true, path, and schema fields
  • AND does not display interactive prompts or spinners

Scenario: JSON output on error

  • WHEN user runs openspec schema init "invalid name" --json
  • THEN system outputs JSON with error field describing the issue
  • AND exits with non-zero code

Requirement: Schema init validates artifacts before forced replacement

The CLI SHALL validate all requested artifact IDs before replacing an existing project-local schema. If artifact validation fails, the CLI SHALL leave the existing schema directory and all of its contents unchanged on every supported platform.

Scenario: Unknown artifact preserves existing schema

  • GIVEN openspec/schemas/tdd-driven/ already exists with user-authored files
  • WHEN the user runs schema init tdd-driven with --force and an artifact list containing the unknown ID task
  • THEN the command exits with a non-zero status and reports the unknown artifact
  • AND the existing tdd-driven schema directory and its contents remain unchanged

Scenario: Unknown artifact preserves a schema at a Windows project path

  • GIVEN an existing project-local schema is resolved from a Windows filesystem path
  • WHEN forced schema initialization fails artifact validation
  • THEN the resolved schema directory and its contents remain unchanged

Scenario: Valid artifacts allow forced replacement

  • GIVEN a project-local schema already exists
  • WHEN the user runs schema init with --force and only valid artifact IDs
  • THEN the command replaces the existing schema with the newly generated schema
  • AND reports successful creation