spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

[Feature]: Context Auto-Discovery from team-ai-directives using grep-based search

Open kanfil opened this issue 9 months ago • 0 comments

Problem Statement

Currently, when running /spec.specify, the context.md file is populated with mostly placeholder values ([NEEDS INPUT]). Users must manually identify and add relevant directives from their team-ai-directives repository. This is:

  1. Time-consuming - Users must manually search through team-ai-directives to find relevant personas, rules, and skills
  2. Error-prone - Users may miss relevant directives that could improve specification quality
  3. Inconsistent - Different team members may select different directives for similar features

The team-ai-directives repository already contains well-organized knowledge (constitution, personas, rules, skills), but there's no automatic mechanism to match this knowledge to feature descriptions.


Proposed Solution

Implement grep-based context auto-discovery that automatically searches team-ai-directives for relevant content based on the user's feature description.

Two-Layer Architecture

Layer 1: Bash Script (create-new-feature.sh)

  • Extract keywords from feature description
  • Use grep -ril to search team-ai-directives for matching files
  • Rank files by match count (files with more keyword matches rank higher)
  • Output discovered directives as JSON for the slash command to consume

Layer 2: Slash Command (specify.md and plan.md)

  • Parse the discovered directives from script output
  • Validate and refine results (AI can run additional searches if needed)
  • Populate context.md with discovered directive paths

Discovery Logic

# Extract keywords from feature description
keywords=$(echo "$feature_description" | tr '[:upper:]' '[:lower:]' | ...)

# Search team-ai-directives
for keyword in $keywords; do
    grep -ril "$keyword" "$team_directives_path" >> results
done

# Rank by match frequency, categorize by type
# Output: constitution, personas[], rules[], skills[], examples[]

Example

Input: /spec.specify Implement OAuth2 authentication for Python FastAPI

Grep finds:
- senior_python_developer.md (3 matches: oauth, python, fastapi)
- prevent_sql_injection.md (2 matches: oauth, authentication)  
- python_testing.md (1 match: python)

Output in context.md:
- Constitution: context_modules/constitution.md
- Persona: context_modules/personas/senior_python_developer.md
- Rules: security/prevent_sql_injection.md, testing/python_testing.md

Alternatives Considered

  1. Hardcoded keyword-to-file mappings - Requires constant maintenance, breaks when team-ai-directives structure changes
  2. AI-only discovery - Slower, less deterministic, depends on AI tool access
  3. Pre-computed index during specify init - Becomes stale when directives change

Grep-based search is self-updating, adapts to any team-ai-directives structure, and finds context-specific matches.


Component

  • Scripts (Bash/PowerShell utilities)
  • Spec templates (BDD, Testing Strategy, etc.)
  • Agent integrations (command files, workflows)

AI Agent

All agents


Use Cases

  1. New feature specification - When running /spec.specify, automatically discover relevant personas, rules, and skills from team-ai-directives
  2. Planning phase - /spec.plan can refresh/supplement context if feature scope changed during clarification
  3. Team consistency - Ensure all team members get consistent directive recommendations for similar features
  4. Onboarding - New team members automatically benefit from team knowledge without manually searching

Acceptance Criteria

  • [ ] create-new-feature.sh includes discover_directives() function with grep-based search
  • [ ] create-new-feature.ps1 includes equivalent PowerShell implementation
  • [ ] Script JSON output includes DISCOVERED_DIRECTIVES field
  • [ ] specify.md includes instructions for parsing and validating discovery results
  • [ ] plan.md includes optional context refresh capability
  • [ ] context-template.md updated with new structure for discovered directives
  • [ ] Constitution is always included when team-ai-directives exists
  • [ ] Files with no keyword matches are excluded (threshold: ≥1 match)
  • [ ] No limit on included directives (include all matches above threshold)
  • [ ] Graceful fallback when team-ai-directives not configured
  • [ ] Works in both build and spec modes

Additional Context

Files to Modify

File Changes
scripts/bash/create-new-feature.sh Add discover_directives() function
scripts/powershell/create-new-feature.ps1 Add Discover-Directives function
templates/commands/specify.md Add discovery validation instructions
templates/commands/plan.md Add context refresh section
templates/context-template.md Update structure for discovered directives

Related

  • Builds on existing Skills Auto-Discovery (src/specify_cli/skills/discovery.py)
  • Complements team-ai-directives integration (--team-ai-directives flag)

kanfil avatar Feb 27 '26 14:02 kanfil