Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ Next: Create design using /opsx:continue

### `openspec instructions`

Get enriched instructions for creating an artifact or applying tasks. Used by AI agents to understand what to create next.
Get enriched instructions for creating an artifact, applying tasks, or retrieving project context. Used by AI agents to understand what to create next.

```
openspec instructions [artifact] [options]
Expand All @@ -476,10 +476,21 @@ openspec instructions [artifact] [options]
|--------|-------------|
| `--change <id>` | Change name (required in non-interactive mode) |
| `--schema <name>` | Schema override |
| `--context` | Output project context from `config.yaml` (incompatible with `--change`, `--schema`, artifact) |
| `--json` | Output as JSON |

**Special case:** Use `apply` as the artifact to get task implementation instructions.

**Modes:**

This command operates in three modes:

1. **Artifact instructions** (default): Get instructions for creating a specific artifact
2. **Apply instructions** (`apply` argument): Get task implementation instructions with progress tracking
3. **Context-only** (`--context` flag): Return just the project context from `config.yaml`

The `--context` flag is exclusive — it cannot be combined with `--change`, `--schema`, or an artifact argument.

**Examples:**

```bash
Expand All @@ -494,14 +505,30 @@ openspec instructions apply --change add-dark-mode

# JSON for agent consumption
openspec instructions design --change add-dark-mode --json

# Get project context (text)
openspec instructions --context

# Get project context (JSON)
openspec instructions --context --json
```

**Output includes:**

- Template content for the artifact
- Project context from config
- Project context from `config.yaml`
- Content from dependency artifacts
- Per-artifact rules from config
- Per-artifact rules from `config.yaml`

**Context-only output (JSON):**

```json
{
"context": "This is a TypeScript CLI tool that runs on Node.js 18+..."
}
```

If no `config.yaml` exists or it has no `context` field, returns `{"context": null}` in JSON mode or no output in text mode.

---

Expand Down
2 changes: 2 additions & 0 deletions openspec/changes/project-context-all-skills/.openspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-02-12
120 changes: 120 additions & 0 deletions openspec/changes/project-context-all-skills/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
## Context

Project context (`config.yaml` → `context` field) reaches only skills that call `openspec instructions <artifact>` — Continue, FF, New, Onboard. Skills that call `openspec instructions apply` (Apply, Verify) or no instructions at all (Explore, Archive, Bulk-archive, Sync) operate blind to project constraints.

Additionally, skills that do receive `context`, `rules`, and `instruction` use weak enforcement language ("guidance", "apply as constraints") rather than mandatory directives.

Current architecture:

```
readProjectConfig()
└──► generateInstructions() ──► ArtifactInstructions { context, rules }
└──► `instructions <artifact>` command
└──► Continue, FF, New, Onboard ✅

╳ generateApplyInstructions() ──► ApplyInstructions { NO context }
└──► `instructions apply` command
└──► Apply, Verify ❌

╳ (no pathway) ──► Explore, Archive, Bulk-archive, Sync ❌
```

## Goals / Non-Goals

**Goals:**
- Every skill that generates, validates, or reasons about code/artifacts has access to project context
- `context`, `rules`, and `instruction` are communicated as mandatory constraints in all skill prompts
- Minimal CLI API surface change — reuse existing `instructions` command

**Non-Goals:**
- Changing how `rules` work (they remain per-artifact, only relevant to artifact-creating skills)
- Adding context to the Feedback skill (not project-work related)
- Caching or performance optimization of `readProjectConfig()` (already benchmarked as fast enough)

## Decisions

### 1. Add `--context` flag to existing `instructions` command

**Choice:** New `--context` flag on `openspec instructions` rather than a separate `openspec context` command.

**Rationale:** The `instructions` command is already the single entry point for AI agents to get guidance. Adding `--context` keeps the API surface small. The flag works without `--change` or artifact arguments since project context is change-independent.

**Usage:**
```bash
openspec instructions --context # text output
openspec instructions --context --json # { "context": "..." }
```

**Behavior:**
- `--context` can be used alone (text output) or with `--json` (structured output)
- `--context` is incompatible with `--change`, `--schema`, and artifact arguments. Error if any are combined.
- Reads `config.yaml` via `readProjectConfig()`
- Returns only the `context` field (not `rules` — those are per-artifact)
- Returns empty/null gracefully if no config or no context defined

**Alternative considered:** Separate `openspec context` command. Rejected because it fragments the instruction pathway and adds a command that only returns one field.

### 2. Add `context` to `ApplyInstructions`

**Choice:** Enrich `generateApplyInstructions()` to call `readProjectConfig()` and include `context` in the response, same pattern as `generateInstructions()`.

**Rationale:** Apply and Verify already call `instructions apply --json`. Adding `context` to the response means these skills get project context without changing their flow.

**Changes:**
- `ApplyInstructions` interface: add `context?: string`
- `generateApplyInstructions()`: call `readProjectConfig()`, extract `context`
- `printApplyInstructionsText()`: print `<project_context>` block (same format as artifact instructions)

### 3. Standardize enforcement language across all skill prompts

**Choice:** Define a consistent block of text that every skill uses when describing how to handle `context`, `rules`, and `instruction`.

**Pattern for artifact-creating skills** (Continue, FF, New, Onboard):
```
**You MUST follow these fields from the instructions output:**
- `context`: Project constraints. You MUST follow these when creating artifacts. Do NOT include in output.
- `rules`: Artifact-specific rules. You MUST follow these. Do NOT include in output.
- `instruction`: Directives for how to create this artifact. You MUST follow these.
```

**Pattern for code-operating skills** (Apply, Verify):
```
**You MUST follow the `context` field** from the instructions output.
This contains project constraints (tech stack, conventions, cross-platform rules)
that you MUST respect when implementing/verifying code. Do NOT include in output.
```

**Pattern for change-independent skills** (Explore, Archive, Bulk-archive, Sync):
```
At the start, load project context:
\`\`\`bash
openspec instructions --context --json
\`\`\`
If it returns a `context` field, you MUST follow these project constraints
throughout the session.
```

### 4. Per-skill context consumption

| Skill | Context source | What changes |
|-------|---------------|--------------|
| **Continue** | `instructions <artifact> --json` | Strengthen enforcement language |
| **FF** | `instructions <artifact> --json` | Strengthen enforcement language |
| **New** | `instructions <artifact> --json` | Strengthen enforcement language |
| **Onboard** | `instructions <artifact> --json` | Strengthen enforcement language |
| **Apply** | `instructions apply --json` | Add context consumption + enforcement |
| **Verify** | `instructions apply --json` | Add context consumption + enforcement |
| **Explore** | `instructions --context --json` | Add context loading at session start |
| **Archive** | `instructions --context --json` | Add context loading at session start |
| **Bulk-archive** | `instructions --context --json` | Add context loading at session start |
| **Sync** | `instructions --context --json` | Add context loading at session start |
| **Feedback** | N/A | No changes (not project-work related) |

## Risks / Trade-offs

**[Prompt length increase]** → Every skill prompt grows by a few lines. Acceptable — clarity is worth the tokens.

**[`--context` flag overlap with other options]** → `--context` errors if combined with `--change`, `--schema`, or an artifact argument. Strict validation, no ambiguity.

**[No config file]** → Skills calling `--context` when no `config.yaml` exists. → Return gracefully (empty context), skill continues without constraints. Same behavior as `generateInstructions()` today.
30 changes: 30 additions & 0 deletions openspec/changes/project-context-all-skills/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Why

The project context defined in `openspec/config.yaml` (tech stack, conventions, cross-platform rules) doesn't reach most skills. Only skills that call `openspec instructions <artifact>` (Continue, FF, New, Onboard) receive it. Skills that call `openspec instructions apply` (Apply, Verify) or don't call instructions at all (Explore, Archive, Bulk-archive, Sync) operate without knowing basic project constraints. Additionally, even skills that do receive `context`, `rules`, and `instruction` describe them with weak language ("guidance", "apply as constraints") rather than treating them as mandatory directives.

Ref: [GitHub Issue #696](https://github.com/Fission-AI/OpenSpec/issues/696)

## What Changes

- Add `context` field to `ApplyInstructions` interface and `generateApplyInstructions()` function, reading it from `readProjectConfig()` — same pattern as `generateInstructions()` already does for artifact instructions
- Add `openspec instructions --context` standalone flag that returns the project context from `config.yaml` without requiring a change name or artifact ID. Supports `--json` for structured output. Incompatible with `--change`, `--schema`, and artifact arguments; error if combined
- Update **all** skill prompts to consume project context and to use mandatory language ("you MUST follow", not "apply as constraints") for `context`, `rules`, and `instruction` fields
- Document the new `--context` flag in CLI documentation

## Capabilities

### New Capabilities
- `cli-instructions-context`: Standalone `--context` flag on the `instructions` command that returns project context without requiring a change or artifact
- `skill-context-enforcement`: Consistent, mandatory language across all skill prompts for how `context`, `rules`, and `instruction` fields must be followed

### Modified Capabilities
- `instruction-loader`: Add `context` field to `ApplyInstructions` so `instructions apply` returns project context alongside tasks and progress

## Impact

- `src/commands/workflow/instructions.ts``generateApplyInstructions()` must call `readProjectConfig()` and include `context` in output; `instructionsCommand()` must handle `--context` flag
- `src/commands/workflow/shared.ts``ApplyInstructions` interface gets `context` field
- `src/core/templates/skill-templates.ts` — All skill template prompts updated for context consumption and enforcement language
- `.claude/commands/opsx/*.md` — All generated slash command files updated accordingly
- CLI documentation for the `--context` flag
- Tests for instructions command and apply instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## ADDED Requirements

### Requirement: Standalone context flag
The `openspec instructions` command SHALL support a `--context` flag that returns the project context from `config.yaml` without requiring a change name or artifact ID.

#### Scenario: Return project context as text
- **WHEN** `openspec instructions --context` is called
- **THEN** the command reads `openspec/config.yaml` via `readProjectConfig()` and outputs the `context` field as plain text

#### Scenario: Return project context as JSON
- **WHEN** `openspec instructions --context --json` is called
- **THEN** the command outputs `{ "context": "<context string>" }`

#### Scenario: No config file exists
- **WHEN** `openspec instructions --context` is called and no `openspec/config.yaml` exists
- **THEN** the command outputs nothing (text mode) or `{ "context": null }` (JSON mode) and exits with code 0

#### Scenario: Config exists but no context field
- **WHEN** `openspec instructions --context` is called and `config.yaml` exists but has no `context` field
- **THEN** the command outputs nothing (text mode) or `{ "context": null }` (JSON mode) and exits with code 0

### Requirement: Context flag exclusivity
The `--context` flag SHALL be incompatible with change-specific and artifact-specific options.

#### Scenario: Combined with --change
- **WHEN** `openspec instructions --context --change "some-change"` is called
- **THEN** the command outputs an error message and exits with non-zero code

#### Scenario: Combined with --schema
- **WHEN** `openspec instructions --context --schema "some-schema"` is called
- **THEN** the command outputs an error message and exits with non-zero code

#### Scenario: Combined with artifact argument
- **WHEN** `openspec instructions proposal --context` is called
- **THEN** the command outputs an error message and exits with non-zero code

### Requirement: CLI documentation
The `--context` flag SHALL be documented in the CLI reference documentation.

#### Scenario: Documentation includes context flag
- **WHEN** a user reads `docs/cli.md`
- **THEN** the `openspec instructions` section documents the `--context` flag with usage examples and behavior
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## MODIFIED Requirements

### Requirement: Template Enrichment
The system SHALL enrich templates with change-specific context.

#### Scenario: Include artifact metadata
- **WHEN** instructions are generated for an artifact
- **THEN** the output includes change name, artifact ID, schema name, and output path

#### Scenario: Include dependency status
- **WHEN** an artifact has dependencies
- **THEN** the output shows each dependency with completion status (done/missing)

#### Scenario: Include unlocked artifacts
- **WHEN** instructions are generated
- **THEN** the output includes which artifacts become available after this one

#### Scenario: Root artifact indicator
- **WHEN** an artifact has no dependencies
- **THEN** the dependency section indicates this is a root artifact

#### Scenario: Include project context in apply instructions
- **WHEN** `generateApplyInstructions()` is called for a change
- **THEN** the output includes a `context` field read from `config.yaml` via `readProjectConfig()`

#### Scenario: Apply instructions with no config
- **WHEN** `generateApplyInstructions()` is called and no `config.yaml` exists
- **THEN** the `context` field is `undefined` and the rest of the apply instructions are unaffected

#### Scenario: Apply instructions text output includes project context
- **WHEN** `printApplyInstructionsText()` is called with a non-empty `context` field
- **THEN** the text output includes a `<project_context>` block with the context content, matching the format used by artifact instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## ADDED Requirements

### Requirement: Mandatory enforcement language for artifact-creating skills
Skills that create artifacts (Continue, FF, New, Onboard) SHALL use mandatory language ("you MUST follow") when describing how to handle `context`, `rules`, and `instruction` fields from the instructions output.

#### Scenario: Continue skill enforcement
- **WHEN** the Continue skill prompt describes the `context`, `rules`, and `instruction` fields
- **THEN** it uses "you MUST follow" language and explicitly states these are mandatory constraints, not suggestions

#### Scenario: FF skill enforcement
- **WHEN** the FF skill prompt describes the `context`, `rules`, and `instruction` fields
- **THEN** it uses "you MUST follow" language and explicitly states these are mandatory constraints, not suggestions

#### Scenario: New skill enforcement
- **WHEN** the New skill prompt describes the `context`, `rules`, and `instruction` fields
- **THEN** it uses "you MUST follow" language and explicitly states these are mandatory constraints, not suggestions

#### Scenario: Onboard skill enforcement
- **WHEN** the Onboard skill prompt describes the `context`, `rules`, and `instruction` fields
- **THEN** it uses "you MUST follow" language and explicitly states these are mandatory constraints, not suggestions

### Requirement: Context consumption for code-operating skills
Skills that operate on code (Apply, Verify) SHALL consume the `context` field from `instructions apply --json` and use mandatory enforcement language.

#### Scenario: Apply skill consumes context
- **WHEN** the Apply skill reads the output of `openspec instructions apply --json`
- **THEN** it treats the `context` field as mandatory project constraints that MUST be followed when implementing code

#### Scenario: Verify skill consumes context
- **WHEN** the Verify skill reads the output of `openspec instructions apply --json`
- **THEN** it treats the `context` field as mandatory project constraints that MUST be followed when verifying code

### Requirement: Context loading for change-independent skills
Skills that operate without a change context (Explore, Archive, Bulk-archive, Sync) SHALL load project context at session start via `openspec instructions --context --json`.

#### Scenario: Explore skill loads context
- **WHEN** the Explore skill starts a session
- **THEN** it calls `openspec instructions --context --json` and treats the `context` field as mandatory project constraints

#### Scenario: Archive skill loads context
- **WHEN** the Archive skill starts a session
- **THEN** it calls `openspec instructions --context --json` and treats the `context` field as mandatory project constraints

#### Scenario: Bulk-archive skill loads context
- **WHEN** the Bulk-archive skill starts a session
- **THEN** it calls `openspec instructions --context --json` and treats the `context` field as mandatory project constraints

#### Scenario: Sync skill loads context
- **WHEN** the Sync skill starts a session
- **THEN** it calls `openspec instructions --context --json` and treats the `context` field as mandatory project constraints

### Requirement: Generated slash commands match skill templates
The generated `.claude/commands/opsx/*.md` files SHALL reflect the same enforcement language as their corresponding skill templates in `skill-templates.ts`.

#### Scenario: Slash commands are regenerated
- **WHEN** skills are generated via `openspec skills generate`
- **THEN** all `.claude/commands/opsx/*.md` files contain the updated mandatory enforcement language matching their skill template
Loading