OpenSpec icon indicating copy to clipboard operation
OpenSpec copied to clipboard

[Proposal] Add hierarchical spec structure support

Open lsmonki opened this issue 9 months ago • 18 comments

Why

Working implementation: #660 Related to: #581

OpenSpec currently assumes a flat spec structure (specs/{capability}/spec.md) where each capability is a direct subdirectory under specs/. This works for simple projects but becomes limiting as projects grow. Teams need hierarchical organization for:

  • Domain-driven design: Grouping specs by bounded context (specs/payments/checkout/spec.md, specs/auth/oauth/spec.md)
  • Monorepos: Organizing specs by package or scope (specs/packages/core/spec.md)
  • Cross-cutting concerns: Separating global specs from feature-specific ones (specs/_global/testing/spec.md vs specs/features/export/spec.md)
  • Team namespaces: Large teams organizing specs by ownership (specs/team-platform/infra/spec.md)
  • Scale: Projects with 50+ specs become unmanageable in a flat structure

Commands like openspec list --specs, validation, sync, and archive fail to discover or work with hierarchically organized specs, forcing teams to use workarounds or manual processes.

What Changes

  • Core spec discovery: Implement recursive search for spec.md files at any depth, replacing the current single-level directory scan
  • Capability naming: Change capability identifier from directory name to full relative path from specs/ (e.g., _global/testing instead of just testing)
  • Delta mapping: Enable 1:1 path-based mapping between change deltas and main specs using replicated directory structure
  • Configuration: Add optional specStructure section in openspec/config.yaml or globally in ~/.config/openspec/config.json to control structure mode, depth limits, and validation behavior
    • structure: 'flat' | 'hierarchical' | 'auto' (default: auto - auto-detect)
    • maxDepth: Maximum hierarchy depth (default: 4, recommended: 2-3)
    • allowMixed: Allow mixing flat and hierarchical specs (default: true)
    • validatePaths: Enforce naming conventions (default: true)
  • Display: Update list and view commands to show hierarchical specs
  • Validation: Add validation rules to prevent orphaned specs (no spec.md at intermediate levels), enforce depth limits (warn at 4, hard limit at 10), and check naming conventions (lowercase alphanumeric with hyphens/underscores)
  • Backward compatibility: Auto-detect flat vs hierarchical structure and support both transparently
  • Templates: Update all skill prompts and templates to reference specs using relative paths
  • Commands: Adapt list, validate, sync, archive commands to work with hierarchical paths

Benefits:

  • Better organization: Group related specs by domain, team, or concern rather than forcing flat structure
  • Domain-driven design: Natural alignment with bounded contexts and domain models
  • Monorepo support: Organize specs by package, scope, or workspace
  • Scalability: Projects with 50+ specs remain navigable and maintainable
  • Team collaboration: Large teams can namespace their specs to avoid conflicts
  • Cross-cutting separation: Distinguish global concerns (_global/, shared/, utils/) from feature-specific specs
  • Code alignment: Spec structure can mirror code structure when beneficial
  • No workarounds: Eliminates need for custom schemas or hacks
  • Optional strictness: Configuration for validation during migrations or in large teams
  • Zero configuration: Works automatically with auto-detection, no setup required
  • Full compatibility: Existing flat projects continue working unchanged

Examples

Directory structures

Flat (current behavior):

openspec/specs/
  auth/spec.md
  api/spec.md
  payments/spec.md
  notifications/spec.md

Hierarchical (new):

openspec/specs/
  _global/
    testing/spec.md              # cross-cutting: testing standards
    security/spec.md             # cross-cutting: security requirements
    monitoring/spec.md           # cross-cutting: monitoring
  platform/
    services/
      api/spec.md                # service: API
      auth/spec.md               # service: authentication
      database/spec.md           # service: database
    infrastructure/
      deployment/spec.md         # infra: deployment
      scaling/spec.md            # infra: scaling
  frontend/
    components/
      forms/spec.md
      navigation/spec.md
    state-management/spec.md

Mixed (flat + hierarchical coexist):

openspec/specs/
  auth/spec.md                   # flat (depth 1)
  payments/spec.md               # flat (depth 1)
  _global/
    testing/spec.md              # hierarchical (depth 2)
    security/spec.md             # hierarchical (depth 2)
  platform/
    services/
      api/spec.md                # hierarchical (depth 3)

Change deltas mirror main structure 1:1

openspec/changes/add-rate-limiting/
  proposal.md
  specs/
    platform/
      services/
        api/spec.md              # delta for platform/services/api
    _global/
      security/spec.md           # delta for _global/security

CLI works with full paths

openspec validate platform/services/api    # validate a deep spec
openspec validate _global/testing          # validate a global spec
openspec validate --specs                  # validate all (auto-discovers)
openspec list --specs                      # list all with grouping

Output example (openspec list --specs)

Specs:
  _global/adr-workflow                  18 requirements
  _global/architecture                  12 requirements
  _global/testing                        8 requirements

  dev/mcp-server                        23 requirements
  dev/mcp-context-builder               14 requirements

  pel/expression-language                6 requirements

Configuration (optional, works without it)

In openspec/config.yaml:

specStructure:
  structure: auto        # auto | flat | hierarchical
  maxDepth: 4            # max nesting depth (1-10)
  allowMixed: true       # allow flat + hierarchical together
  validatePaths: true    # enforce naming conventions

Or globally in ~/.config/openspec/config.json:

{
  "specStructure": {
    "structure": "auto",
    "maxDepth": 4,
    "allowMixed": true,
    "validatePaths": true
  }
}

lsmonki avatar Feb 04 '26 11:02 lsmonki

This hierarchical support is a good step, but it seems to assume a centralized model (one openspec root for the entire monorepo). But what if we want to initialize openspec inside specific sub-projects (e.g., frontend/apps/my-app/openspec/) so that the specs and changes live alongside the code they affect, rather than being centralized in the repo root?

georgefromadyen avatar Feb 06 '26 16:02 georgefromadyen

This hierarchical support is a good step, but it seems to assume a centralized model (one openspec root for the entire monorepo). But what if we want to initialize openspec inside specific sub-projects (e.g., frontend/apps/my-app/openspec/) so that the specs and changes live alongside the code they affect, rather than being centralized in the repo root?

Step by step, this issue is meant to support the current state of OpenSpec and at least allow reading specs that are organized hierarchically. Where they are stored is a different problem (related, but still separate).

Take a look at this other issue #581, where what you’re mentioning is being discussed: the possibility of renaming openspec/ folder, or even moving the individual folders (specs, schemas, archive, changes) according to the configuration (#664 and #581) — and especially this other comment, which is directly related: https://github.com/Fission-AI/OpenSpec/issues/581#issuecomment-3858983471

lsmonki avatar Feb 06 '26 18:02 lsmonki

changes might need the structure as well;

for config, suggesting unlimited depth as default;

Piping avatar Feb 11 '26 04:02 Piping

STRONGLY need this for progressively opting in OpenSpec for x-large projects ( > 10million lines of code)

RiddMa avatar Feb 11 '26 09:02 RiddMa

+1 for this feature as well, i don't get how "validatePaths" works from the issue description

CosticaPuntaru avatar Feb 22 '26 14:02 CosticaPuntaru

+1 for this feature as well, i don't get how "validatePaths" works from the issue description

When enabled, validatePaths enforces naming conventions on spec paths during validation. Specifically, it checks for:

  • Kebab-case naming (e.g., my-feature not myFeature)
  • Valid characters in directory/file names
  • Path length limits
  • Reserved name conflicts

lsmonki avatar Feb 22 '26 15:02 lsmonki

Would love to see this feature implemented <3

brechtvhb avatar Mar 06 '26 12:03 brechtvhb

👍🏻 Also hoping for this feature. Although I wonder if it would not be a better approach to have multiple openspec folders in the project, like one for each subproject like mentioned above. maybe it would get confusing for the LLM to always specify which openspec folder it's referring to?

victorhsb avatar Mar 12 '26 22:03 victorhsb

This feature is essential. The flat structure doesn't really work for real-world enterprise applications.

max-arshinov avatar Mar 20 '26 12:03 max-arshinov

+1 for this feature as well

vncpetrov avatar Mar 30 '26 15:03 vncpetrov

This feature is a must for my organization - we would like to incorporate openspec into some rather large backend applications and the flat spec file structure will quickly become too crowded.

Scott-Selke avatar Mar 30 '26 22:03 Scott-Selke

I am in a startup, can verify this being a problem for large codebases. We made our own called Unispec, same concept, but adds hierarchical spec structures, spec to code relations, etc... The prompting needs some help but it speaks MCP so it is fully autonomous if you want it to be.

uwzis avatar Apr 07 '26 06:04 uwzis

I've proposed a hierarchical 'Control Plane' pattern for multi-repo architectures over in #725 here. It addresses some of the organizational needs for hierarchical spec discovery discussed here. Curious to see if this aligns with the roadmap for this issue.

rajashree-n avatar Apr 11 '26 08:04 rajashree-n

Side note: we would need this hierarchical spec structure less, if we could already structure specs simply as specs/[topic]/[feature].md

I do not get this constraint of only 1 file (always a "spec.md") per directory. It clutters the workspace with quasi-empty folders.

It's not like we store other assets (images...) as complements next to the spec.md :? Or is it planned to be so one day ?

pakal avatar May 28 '26 16:05 pakal

Strongly agree with @pakal. Would love to be able to do something like

openspec/
  specs/
    blog/
      posts.md
      recommended-posts.md
      comments.md

And I see no reason it wouldn't be possible

bessey avatar May 29 '26 15:05 bessey

I've patched openspec to support hierarchical, for-humans, specs, it's a life changer!

The problem is, it breaks compatibility with tools like openspecui (I'm patching them too currently).

Image

Would this flexibility be welcome by the openspec devs ? Or are there rationales taht I don't understand, behind the flat and single-fil-per-directory layout of today ?

pakal avatar Jul 02 '26 08:07 pakal

@pakal Yep we're considering something like this. I think we're realising that what's actually needed here is some flexible way for people to define folder structures (or even what folders should even exist for that matter)

TabishB avatar Jul 03 '26 06:07 TabishB

Closing as resolved by #1355, which delivered the functional core of this proposal: recursive spec discovery at any depth (src/utils/spec-discovery.ts), capability identifiers as full relative paths from specs/, and 1:1 path-based delta mapping across parse, apply, and archive. Flat and nested layouts coexist with no configuration needed.

Verified on current main with a spec at specs/platform/services/api/spec.md: list, validate, and archive all handle it. The optional specStructure config block (structure mode, maxDepth, allowMixed) was not implemented; auto-detection turned out to be enough, and nobody has missed the knobs yet. If a concrete need for them appears, that would be a fresh, narrower issue. On main, ships with the next release.

clay-good avatar Jul 22 '26 18:07 clay-good