Scan repository and update context files
You are updating the repository context files. These files serve as a knowledge base about the codebase for AI agents to reference.
Context files live in .opencode/context/. The structure is modular:
repo-structure.md (core overview).opencode/context/
āāā repo-structure.md # Always created - tech stack, structure, conventions
āāā frontend/ # Optional - if frontend-heavy
ā āāā components.md
ā āāā hooks.md
āāā backend/ # Optional - if backend-heavy
ā āāā api.md
ā āāā services.md
āāā shared/ # Optional - if significant shared code
āāā types.md
āāā utilities.md
BEFORE reading any source files, check if helper scripts exist and run the pre-analysis.
ls lib/scan-strategy.js 2>/dev/null
Run the scan strategy decision engine. This performs dependency graph analysis, auto-generates summaries, detects project capabilities, and outputs an action plan you should follow:
import { decideScanStrategy } from './lib/scan-strategy.js';
const strategy = await decideScanStrategy({
rootDir: process.cwd(),
forceFullScan: false, // Set true if user passed --full
rebuildGraph: false // Set true if user passed --rebuild-graph
});
// strategy.actionPlan contains a human-readable plan to follow
// strategy.mode is 'full' or 'incremental'
// strategy.preAnalysis contains auto-generated summaries and project capabilities
// strategy.filesToRead lists which files you need to read
The action plan output will tell you exactly:
Follow the action plan. It has already analyzed the dependency graph and determined the optimal update strategy.
If lib/ doesn't exist, fall back to manual analysis:
ls .opencode/context/repo-structure.md<!-- Context: branch@hash --> in first linegit diff --name-status -M <old-hash>..HEAD to see changesCheck if .opencode/context/ already exists. If it does, read ALL markdown files in it to understand the previous state. This helps you:
Important: Don't delete files the user may have manually added. Only update files you recognize as auto-generated context files.
For FULL SCAN mode, scan the repository using the pre-analysis as your guide:
If the pre-analysis is available, it already provides:
For each file in the "files to read" list:
Read 2-3 sample files from each major category (components, services, hooks, etc.) to detect:
Also scan for (much of this may already be in pre-analysis):
Tech Stack: package.json, requirements.txt, go.mod, etc.
Directory Structure: Map the high-level folder organization
Components: React/Vue/Svelte components with descriptions
Hooks: Custom hooks/composables
API Endpoints: Routes, methods, request/response shapes
Services: Business logic modules
Utilities: Helper functions
Types: TypeScript interfaces and types
Database: Schema, ORM, migrations
Auth: Strategy, providers, middleware
Integrations: Third-party APIs (Stripe, SendGrid, etc.)
State Management: Client and server state patterns
Deployment: Docker, CI/CD, hosting platform
Background Jobs: Queues, workers, cron
Realtime: WebSockets, SSE, pub/sub
i18n: Internationalization setup
Styling: CSS framework, component library, design tokens
Testing: Framework, patterns, utilities
Logging & Monitoring: Error tracking, APM
Security: CSRF, CSP, input sanitization
Performance: Caching, code splitting, SSR/SSG
Developer Experience: Linting, formatting, git hooks
Environment Variables: From .env.example only (NEVER read actual .env files)
Build Scripts: Key commands from package.json or Makefile
Conventions: Export style, naming, imports, error handling
Skip this step if doing a full scan.
The action plan lists exactly which files to re-read. For each:
When updating context files:
Don't assume standard paths. Instead:
**/*.{tsx,jsx,vue,svelte}**/use*.{ts,js,tsx,jsx}**/services/**/*.{ts,js}, **/api/**/*.{ts,js}**/types/**/*.{ts,d.ts}, **/*.d.tsYou MUST create repo-structure.md. Additional files are OPTIONAL.
Create a separate file when:
repo-structure.md unwieldy (>300 lines)Keep everything in repo-structure.md when:
| File | Contents |
|---|---|
repo-structure.md |
Tech stack, directory structure, conventions, build scripts, env vars, database, auth, integrations, deployment, high-level overview |
frontend/components.md |
Reusable UI components with descriptions and props |
frontend/hooks.md |
Custom hooks/composables with usage |
backend/api.md |
API endpoints, routes, request/response formats |
backend/services.md |
Business logic services and their responsibilities |
shared/types.md |
Key TypeScript types/interfaces used across the codebase |
shared/utilities.md |
Utility functions with descriptions |
Before generating context files, capture git state:
git branch --show-currentgit rev-parse --short HEAD[branch]@[hash] (e.g., main@abc1234)This will be embedded in each context file for staleness detection.
Always create this file with this structure:
<!-- Context: [branch]@[hash] -->
# Repository Context
Last updated: [current timestamp]
## Tech Stack
- **Language**: [language and version]
- **Framework**: [name and version]
- **Build Tool**: [tool and version]
- **Package Manager**: [npm, yarn, pnpm, etc.]
- **Key Dependencies**:
- [dependency]: [brief purpose]
## Directory Structure
\`\`\`
[tree-like structure with inline comments explaining each directory]
\`\`\`
## Core Architecture
[Key functions, classes, entry points, and how they connect]
## Database
[If applicable: type, ORM, schema overview, migration approach]
## Authentication
[If applicable: strategy, provider, middleware, token handling]
## Third-Party Integrations
[If applicable: services used and what they're for]
## Deployment & Infrastructure
[If applicable: hosting, CI/CD, environments, Docker]
## Conventions & Patterns
- **Exports**: [pattern observed]
- **Naming**: [conventions used]
- **File Organization**: [pattern]
- **Error Handling**: [approach]
- **State Management**: [approach if applicable]
## Environment Variables
- `[VAR_NAME]` - [purpose]
## Build & Scripts
- `[command]` - [what it does]
## Testing
[If applicable: framework, patterns, utilities]
## Additional Context Files
[List any additional context files you created and what they contain]
Follow the same patterns as repo-structure.md but focused on the specific category. Include relevant items with path, description, and key details.
Write all context files to .opencode/context/.
If the pre-analysis provided a file-summaries structure, update it with the AI-generated summaries for the files you read. Save to .opencode/analysis/codebase-analysis.json.
For Updates (existing context found):
Updated context files in .opencode/context/
repo-structure.md
~ Updated Tech Stack: added vitest dependency
~ Updated Directory Structure: added src/hooks/
frontend/components.md
+ Added: Modal, Tooltip (2 new components)
- Removed: OldButton (no longer exists)
Summary: Updated 2 files, created 0 new files.
Tokens used: ~8K (incremental mode, 97% savings)
For First Run:
Created context files in .opencode/context/
repo-structure.md
- Tech stack: Next.js 14 with TypeScript
- Directory structure mapped
- Database: PostgreSQL via Prisma
- Auth: Clerk
- 5 environment variables documented
frontend/components.md
- 12 reusable components documented
Summary: Created 2 context files.
Tokens used: ~120K (first run with pre-analysis)
When context files are loaded for use, AI agents should check staleness:
# Parse metadata from context file
head -1 .opencode/context/repo-structure.md
# ā <!-- Context: main@abc1234 -->
# Count commits behind
git log abc1234..HEAD --oneline | wc -l
# See what changed
git diff --name-only abc1234..HEAD | head -20
| Metric | Status | Action |
|---|---|---|
| Same commit | Current | Proceed normally |
| 1-5 commits behind | Recent | Proceed with awareness |
| 6-15 commits behind | Moderate | Warning: "Context may be outdated" |
| 16+ commits behind | Significant | Warning: "Run /context-update" |
| Branch mismatch | Unknown | Warning: "Context from different branch" |
.env files, only .env.example or template filesdist/, build/, .next/, etc..env.example but respect .gitignore patterns.opencode/context/notes.md, leave it alone