Skip to main content

subcog/mcp/
prompt_understanding.rs

1//! Guidance content for the `prompt_understanding` tool.
2
3pub const PROMPT_UNDERSTANDING: &str = r#"# SUBCOG.MCP-SERVER - How to use Subcog memory tools effectively
4
5## 1. Session Start Protocol
6
7When starting a session, establish context and tool availability.
8
9**Preferred approach**: Call `subcog_init` - it combines guidance, status, and recall in one call.
10
11```yaml
12subcog_init:
13  include_recall: true
14  recall_query: "project setup OR architecture OR conventions"
15  recall_limit: 5
16```
17
18**Manual alternative** (if you need finer control):
19
201) Call `prompt_understanding` to load usage guidance (you're reading it now).
212) Call `subcog_status` to confirm memory system health and get statistics.
223) For first project interaction:
23   - `subcog_recall` with query: "project setup OR architecture OR conventions"
24   - Check for existing patterns, decisions, and context.
25
26**Important**: `subcog_init` uses `recall_limit` (not `limit`). The `limit` parameter is for `subcog_recall`.
27
28## 2. Core Concepts
29
30### 2.1 Memory System
31
32Subcog provides **persistent memory** across coding sessions with:
33- **Semantic search** (vector + BM25 hybrid ranking)
34- **Multi-domain scoping** (project, user, org)
35- **Knowledge graph** for entity-centric retrieval
36- **Automatic deduplication** to prevent duplicates
37- **Memory consolidation** to summarize related memories
38
39### 2.2 Domain Scoping
40
41Memories are scoped by domain:
42- **project**: Repository-scoped (default when in git repo)
43- **user**: User-wide memories (shared across all projects)
44- **org**: Organization-wide (if enabled/configured)
45
46Domain resolution for lookups: project → user → org
47
48### 2.3 Namespaces
49
50Memories are categorized by namespace:
51| Namespace | Purpose |
52|-----------|---------|
53| `decisions` | Architecture and design choices |
54| `patterns` | Coding conventions and standards |
55| `learnings` | Insights and discoveries |
56| `context` | Project background and state |
57| `tech-debt` | Known issues and TODOs |
58| `apis` | API documentation and contracts |
59| `config` | Configuration details |
60| `security` | Security policies and findings |
61| `performance` | Performance observations |
62| `testing` | Test strategies and edge cases |
63
64## 3. Tool Catalog
65
66### 3.0 Session Initialization
67
68| Tool | Description |
69|------|-------------|
70| `subcog_init` | Initialize session by combining guidance, status check, and context recall in one call |
71
72**Parameters**:
73- `include_recall` (boolean, default: `true`): Whether to recall project context
74- `recall_query` (string, optional): Custom query for recall (default: "project setup OR architecture OR conventions")
75- `recall_limit` (integer, default: 5, max: 20): Maximum memories to recall
76
77**Warning**: Use `recall_limit`, not `limit`. The `limit` parameter belongs to `subcog_recall`, not `subcog_init`.
78
79### 3.1 Memory CRUD Tools
80
81| Tool | Description |
82|------|-------------|
83| `subcog_capture` | Create a memory (required: content, namespace) |
84| `subcog_get` | Retrieve a memory by ID |
85| `subcog_update` | Update memory content and/or tags |
86| `subcog_delete` | Delete a memory (soft by default, hard optional) |
87| `subcog_delete_all` | Bulk delete with filter (dry-run by default) |
88| `subcog_restore` | Restore a soft-deleted memory |
89| `subcog_history` | View audit trail for a memory |
90
91### 3.2 Search & Recall Tools
92
93| Tool | Description |
94|------|-------------|
95| `subcog_recall` | Semantic + text search, or list all (when no query) |
96| `subcog_status` | System health and statistics |
97| `subcog_namespaces` | List namespace descriptions |
98| `subcog_reindex` | Rebuild search index |
99
100**Note**: `subcog_recall` now subsumes `subcog_list`. Omit the `query` parameter to list all memories with filtering and pagination support.
101
102**Filter syntax** (GitHub-style):
103```
104ns:decisions tag:rust -tag:deprecated since:7d source:src/*
105```
106
107**Search modes**: `hybrid` (default), `vector`, `text`
108**Detail levels**: `light`, `medium` (default), `everything`
109
110### 3.3 Knowledge Graph Tools
111
112| Tool | Description |
113|------|-------------|
114| `subcog_entities` | CRUD + extract + merge for entities |
115| `subcog_relationships` | CRUD + infer for entity relationships |
116| `subcog_graph` | **Consolidated**: Query (neighbors, paths, stats) + visualize |
117
118**Entity actions**: `create`, `get`, `list`, `delete`, `extract`, `merge`
119**Relationship actions**: `create`, `get`, `list`, `delete`, `infer`
120**Graph operations**: `neighbors`, `path`, `stats`, `visualize`
121
122**Entity types**: Person, Organization, Technology, Concept, File
123**Relationship types**: WorksAt, Created, Uses, Implements, PartOf, RelatesTo, MentionedIn, Supersedes, ConflictsWith
124
125### 3.4 Consolidation & Maintenance Tools
126
127| Tool | Description |
128|------|-------------|
129| `subcog_consolidate` | Group and summarize related memories (LLM-powered) |
130| `subcog_get_summary` | Retrieve a summary with its source memories |
131| `subcog_enrich` | Improve memory structure/tags via LLM |
132| `subcog_sync` | **DEPRECATED** - SQLite is now authoritative |
133
134### 3.5 Prompt Management Tools
135
136| Tool | Description |
137|------|-------------|
138| `subcog_prompts` | **Consolidated**: Prompt template management |
139
140**Prompt actions**: `save`, `list`, `get`, `run`, `delete`
141**Variable syntax**: `{{variable_name}}`
142**Domain resolution**: project → user → org
143
144### 3.6 Context Template Tools
145
146| Tool | Description |
147|------|-------------|
148| `subcog_templates` | **Consolidated**: Context template management |
149
150**Template actions**: `save`, `list`, `get`, `render`, `delete`
151
152### 3.7 Import/Export CLI Commands
153
154Subcog provides bulk memory import and export via CLI commands:
155
156| Command | Description |
157|---------|-------------|
158| `subcog import <file>` | Import memories from JSON, YAML, or CSV |
159| `subcog export <file>` | Export memories to JSON, YAML, CSV, or Parquet |
160
161**Import options**:
162- `--format`: Force format (auto-detected from extension)
163- `--namespace`: Default namespace for imported memories
164- `--domain`: Default domain (project, user, org)
165- `--skip-duplicates`: Skip content-hash duplicates (default: true)
166- `--dry-run`: Validate without storing
167
168**Export options**:
169- `--format`: Force format (auto-detected from extension)
170- `--filter`: GitHub-style filter query (e.g., `ns:decisions tag:rust`)
171- `--limit`: Maximum memories to export
172- `--domain`: Filter by domain
173
174**Supported formats**:
175| Format | Import | Export | Extension |
176|--------|--------|--------|-----------|
177| JSON | Yes | Yes | `.json`, `.ndjson`, `.jsonl` |
178| YAML | Yes | Yes | `.yaml`, `.yml` |
179| CSV | Yes | Yes | `.csv`, `.tsv` |
180| Parquet | No | Yes (feature-gated) | `.parquet`, `.pq` |
181
182**Import file structure** (JSON example):
183```json
184{"content": "Memory content here", "namespace": "decisions", "tags": ["rust", "api"]}
185{"content": "Another memory", "tags": ["frontend"]}
186```
187
188### 3.7 Privacy & Compliance Tools
189
190| Tool | Description |
191|------|-------------|
192| `subcog_gdpr_export` | Export all user data (GDPR Article 20) |
193
194### 3.8 Template Syntax Reference
195
196Context templates format memories and statistics for hooks and tool responses.
197
198Use `subcog_templates` with appropriate action (see Section 3.6).
199
200**Template syntax**:
201- **Variables**: `{{variable_name}}` - substituted at render time
202- **Iteration**: `{{#each memories}}...{{memory.field}}...{{/each}}`
203- **Output formats**: markdown (default), json, xml
204
205**Auto-variables** (populated automatically):
206- `{{memories}}` - List of memories for iteration
207- `{{memory.id}}`, `{{memory.content}}`, `{{memory.namespace}}`, `{{memory.tags}}`, `{{memory.score}}`
208- `{{total_count}}`, `{{namespace_counts}}`, `{{statistics}}`
209
210**Versioning**: Templates auto-increment version on save (v1, v2, v3...).
211
212## 4. Key Features
213
214### 4.1 Search Intent Detection
215
216Subcog automatically detects search intent and surfaces relevant memories:
217
218| Intent | Triggers | Prioritized Namespaces |
219|--------|----------|----------------------|
220| HowTo | "how do I...", "implement..." | patterns, learnings |
221| Troubleshoot | "error", "fix", "not working" | blockers, learnings, tech-debt |
222| Location | "where is...", "find..." | decisions, context |
223| Explanation | "what is...", "explain..." | decisions, context |
224| Comparison | "vs", "difference between" | decisions, patterns |
225
226### 4.2 Deduplication
227
228The pre-compact hook automatically prevents duplicate captures using:
2291. **Exact match** - SHA256 hash lookup (<5ms)
2302. **Semantic similarity** - Embedding comparison (<50ms)
2313. **Recent capture** - LRU cache with 5-minute TTL (<1ms)
232
233Per-namespace thresholds: decisions (92%), patterns (90%), learnings (88%)
234
235### 4.3 Entity Extraction
236
237When enabled (`auto_extract_entities: true`), memories are analyzed for:
238- **People** (Alice, Bob Smith)
239- **Organizations** (Anthropic, Rust Foundation)
240- **Technologies** (Rust, PostgreSQL, React)
241- **Concepts** (Microservices, CQRS)
242- **Files** (src/main.rs, README.md)
243
244Use `subcog_entities` with `action: extract` for manual extraction.
245
246### 4.4 Memory Consolidation
247
248The consolidation service groups related memories and creates summary nodes:
2491. **Semantic clustering** by namespace (configurable threshold: 0.7)
2502. **LLM summarization** preserving key details
2513. **Edge relationships** linking summaries to source memories
252
253Use `subcog_consolidate --dry-run` to preview before applying.
254
255### 4.5 Prompt Enrichment
256
257When saving prompts, LLM can auto-generate metadata:
258- **Description** from content analysis
259- **Tags** inferred from context
260- **Variable definitions** with descriptions and defaults
261
262Use `skip_enrichment: true` to disable.
263
264## 5. When to Capture
265
266Capture memories when you detect:
267- **Decisions**: "we decided", "going with", "choosing", "agreed on"
268- **Patterns**: "always", "never", "standard", "convention", "rule"
269- **Learnings**: "turns out", "gotcha", "realized", "discovered"
270- **Fixes**: "resolved", "the issue was", "workaround", "solution"
271- **Tech debt**: "TODO", "temporary", "needs refactor", "hack"
272- **Context**: project background, architecture overview, team agreements
273
274**Quality guidelines**:
275- Include the "why" (rationale), not just the "what"
276- Add relevant file paths via `source`
277- Use descriptive, searchable tags
278- Keep content concise (1-3 paragraphs)
279
280## 6. Recall Strategy
281
282Use `subcog_recall` before:
283- **Implementing features**: search decisions + patterns
284- **Debugging**: search context + learnings + tech-debt
285- **Architecture changes**: search decisions + patterns
286- **Onboarding**: search context + decisions
287
288**Recommended defaults**:
289- `mode`: "hybrid"
290- `detail`: "medium"
291- `limit`: 5-10
292
293**Advanced filtering**:
294```
295ns:decisions tag:database since:30d        # Recent DB decisions
296ns:patterns source:src/api/*              # API patterns
297ns:learnings -tag:deprecated              # Active learnings
298tag:security tag:auth                     # Security + auth
299```
300
301## 7. Knowledge Graph Best Practices
302
303### 7.1 When to Use Graph Tools
304
305- **Entity tracking**: Track people, projects, technologies across memories
306- **Relationship mapping**: Understand connections (who works on what)
307- **Graph-augmented search**: Find memories by entity, not just text
308- **Visualization**: Generate architecture diagrams from knowledge
309
310### 7.2 Entity Management
311
312```
313# List all technology entities
314subcog_entities: action=list, entity_type=Technology
315
316# Create an entity manually
317subcog_entities: action=create, name="PostgreSQL", entity_type=Technology
318
319# Extract entities from text (LLM-powered)
320subcog_entities: action=extract, content="Alice uses PostgreSQL for the API", store=true
321
322# Find duplicates before merging
323subcog_entities: action=merge, entity_id="...", dry_run=true
324
325# Merge duplicate entities
326subcog_entities: action=merge, entity_ids=["id1", "id2"], canonical_name="PostgreSQL"
327```
328
329### 7.3 Graph Traversal
330
331```
332# Get neighbors of an entity
333subcog_graph: operation=neighbors, entity_id="...", depth=2
334
335# Find path between entities
336subcog_graph: operation=path, from_entity="...", to_entity="..."
337
338# Get graph statistics
339subcog_graph: operation=stats
340```
341
342### 7.4 Visualization
343
344```
345# Generate Mermaid diagram centered on entity
346subcog_graph: operation=visualize, format=mermaid, entity_id="...", depth=2
347
348# Full graph as DOT format
349subcog_graph: operation=visualize, format=dot, limit=50
350```
351
352## 8. Prompt Template Best Practices
353
354### 8.1 Creating Templates
355
356- Use **kebab-case** names (code-review, bug-triage)
357- Include `{{variable}}` placeholders for dynamic content
358- Provide descriptive tags for searchability
359- Use **user** domain for cross-project reuse
360
361### 8.2 Variable Syntax
362
363Valid: `{{file}}`, `{{issue_type}}`, `{{focus_area}}`
364Reserved prefixes: `subcog_`, `system_`, `__`
365
366Variables inside fenced code blocks are treated as literal examples:
367```markdown
368Use {{active_var}} here.
369
370\`\`\`python
371template = "{{code_example}}"  # This is NOT extracted
372\`\`\`
373```
374
375### 8.3 Example Templates
376
377```yaml
378# code-review template
379prompt_save:
380  name: code-review
381  content: |
382    Review {{file}} focusing on:
383    - {{focus_area}} issues
384    - Best practices
385    - Edge cases
386  tags: [review, quality]
387  domain: user
388```
389
390## 9. Example Workflows
391
392### 9.1 Capture a Decision
393
394```yaml
395subcog_capture:
396  content: "Decided to use SQLite for local dev to simplify onboarding. Production uses PostgreSQL."
397  namespace: decisions
398  tags: [database, sqlite, postgresql, development]
399  source: docker-compose.yml
400```
401
402### 9.2 Search with Intent
403
404```yaml
405subcog_recall:
406  query: "how do we handle authentication errors"
407  filter: "ns:patterns ns:learnings"
408  detail: medium
409  limit: 10
410```
411
412### 9.3 Get Memory by ID
413
414```yaml
415subcog_get:
416  memory_id: "abc123def456"
417```
418
419### 9.4 Update Memory Tags
420
421```yaml
422subcog_update:
423  memory_id: "abc123def456"
424  tags: [database, postgresql, production, updated]
425```
426
427### 9.5 Consolidate Decisions
428
429```yaml
430subcog_consolidate:
431  namespaces: [decisions]
432  days: 30
433  similarity: 0.8
434  dry_run: true
435```
436
437### 9.6 Extract Entities from Text
438
439```yaml
440subcog_entities:
441  action: extract
442  content: "Alice from Anthropic uses Rust to build the Claude API integration."
443  store: true
444  min_confidence: 0.6
445```
446
447### 9.7 Visualize Entity Relationships
448
449```yaml
450subcog_graph:
451  operation: visualize
452  format: mermaid
453  entity_types: [Person, Technology]
454  depth: 2
455```
456
457### 9.8 Infer Relationships Between Entities
458
459```yaml
460subcog_relationships:
461  action: infer
462  entity_ids: ["entity_alice", "entity_postgres"]
463  store: true
464  min_confidence: 0.7
465```
466
467### 9.9 Export User Data (GDPR)
468
469```yaml
470subcog_gdpr_export: {}
471```
472
473### 9.10 Create a Context Template
474
475```yaml
476subcog_templates:
477  action: save
478  name: search-results
479  content: |
480    # {{title}}
481
482    Found {{total_count}} memories:
483
484    {{#each memories}}
485    - **{{memory.namespace}}**: {{memory.content}}
486      _Score: {{memory.score}}_
487    {{/each}}
488  description: Format search results for display
489  tags: [search, formatting]
490  domain: user
491```
492
493### 9.11 Render a Context Template with Query
494
495```yaml
496subcog_templates:
497  action: render
498  name: search-results
499  query: "authentication patterns"
500  limit: 10
501  format: markdown
502  variables:
503    title: "Authentication Patterns"
504```
505
506### 9.12 List Context Templates
507
508```yaml
509subcog_templates:
510  action: list
511  domain: user
512  tags: [formatting]
513  limit: 20
514```
515
516### 9.13 Import Memories from File (CLI)
517
518```bash
519# Import from JSON (newline-delimited)
520subcog import memories.json --namespace decisions
521
522# Import from YAML with dry-run
523subcog import exported.yaml --dry-run
524
525# Import from CSV with domain override
526subcog import data.csv --domain user --namespace learnings
527```
528
529### 9.14 Export Memories to File (CLI)
530
531```bash
532# Export all decisions to JSON
533subcog export decisions.json --filter "ns:decisions"
534
535# Export recent learnings to YAML
536subcog export learnings.yaml --filter "ns:learnings since:30d" --limit 100
537
538# Export with tags to CSV
539subcog export tagged.csv --filter "tag:rust tag:api"
540```
541
542## 10. Safety and Integrity
543
544- Keep captures **concise and contextual**
545- Use `source` to link memories to files or URLs
546- **Avoid sensitive data** in memory content (secrets auto-filtered)
547- Use `subcog_reindex` if search results seem stale
548- Use **soft delete** (default) to allow recovery
549- Run `subcog_consolidate --dry-run` before actual consolidation
550
551## 11. Troubleshooting
552
553| Issue | Solution |
554|-------|----------|
555| Memories not found | Check `subcog_status`, try broader query, remove restrictive filters |
556| Prompts missing | Verify domain scope, check storage config |
557| Search slow | Use `subcog_reindex` to rebuild index |
558| Duplicates appearing | Deduplication may be disabled; check config |
559| Graph empty | Enable `auto_extract_entities` or use `subcog_entities` with `action: extract` |
560| Consolidation fails | Check LLM provider config; falls back to edge-only mode |
561| Stale index | Run `subcog_reindex` after direct DB changes |
562
563## 12. Configuration Reference
564
565Key environment variables:
566- `SUBCOG_SEARCH_INTENT_ENABLED` - Enable intent detection
567- `SUBCOG_DEDUP_ENABLED` - Enable deduplication
568- `SUBCOG_AUTO_EXTRACT_ENTITIES` - Enable entity extraction
569- `SUBCOG_CONSOLIDATION_ENABLED` - Enable consolidation
570- `SUBCOG_LLM_PROVIDER` - LLM provider (anthropic, openai, ollama, lmstudio)
571
572See `~/.config/subcog/config.toml` for full configuration options.
573"#;