instructions-template.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Agent-instructions template — the markdown body each agent target
  3. * writes into its conventional instructions file (CLAUDE.md /
  4. * AGENTS.md / codegraph.mdc / etc.).
  5. *
  6. * The body content is identical across agents because the codegraph
  7. * usage advice is agent-agnostic — only the destination filename and
  8. * any optional frontmatter (Cursor `.mdc`) varies per target.
  9. *
  10. * The legacy `claude-md-template.ts` re-exports these names for
  11. * backwards compatibility with downstream importers.
  12. */
  13. /** Markers used by the marker-based section replacement. */
  14. export const CODEGRAPH_SECTION_START = '<!-- CODEGRAPH_START -->';
  15. export const CODEGRAPH_SECTION_END = '<!-- CODEGRAPH_END -->';
  16. /**
  17. * The full marker-delimited block written into each agent's
  18. * instructions file. Includes the start/end markers so the section
  19. * can be detected and replaced on re-install.
  20. */
  21. export const INSTRUCTIONS_TEMPLATE = `${CODEGRAPH_SECTION_START}
  22. ## CodeGraph
  23. This project has a CodeGraph MCP server (\`codegraph_*\` tools) configured. CodeGraph is a tree-sitter-parsed knowledge graph of every symbol, edge, and file. Reads are sub-millisecond and return structural information grep cannot.
  24. ### When to prefer codegraph over native search
  25. Use codegraph for **structural** questions — what calls what, what would break, where is X defined, what is X's signature. Use native grep/read only for **literal text** queries (string contents, comments, log messages) or after you already have a specific file open.
  26. | Question | Tool |
  27. |---|---|
  28. | "How does X work? / trace X / explain a system / architecture" | \`codegraph_explore\` (seed with symbol names) |
  29. | "Where is X defined?" / "Find symbol named X" | \`codegraph_search\` |
  30. | "What calls function Y?" | \`codegraph_callers\` |
  31. | "What does Y call?" | \`codegraph_callees\` |
  32. | "What would break if I changed Z?" | \`codegraph_impact\` |
  33. | "Show me Y's signature / source / docstring" | \`codegraph_node\` |
  34. | "Give me focused context for a task/area" | \`codegraph_context\` |
  35. | "What files exist under path/" | \`codegraph_files\` |
  36. | "Is the index healthy?" | \`codegraph_status\` |
  37. ### Rules of thumb
  38. - **\`codegraph_explore\` is the workhorse for understanding questions** ("how does X work", "trace…", "explain the Y system"). Feed it the key symbol/file names and read its output (line-numbered source from many files in one call). If the question names nothing concrete, do one quick \`codegraph_search\`/\`codegraph_context\` to surface the names, then explore with them. Fill gaps with \`codegraph_node\`/Read — don't grep-and-read your way through; that's the loop explore replaces.
  39. - **Delegating exploration to a subagent?** Tell it to call \`codegraph_explore\` first and trust the result. A generic "explore"-style agent defaults to grep+Read and treats codegraph as just a search index, throwing away the token savings.
  40. - **Trust codegraph results.** They come from a full AST parse. Do NOT re-verify them with grep — that's slower, less accurate, and wastes context.
  41. - **Don't grep first** when looking up a symbol by name. \`codegraph_search\` is faster and returns kind + location + signature in one call.
  42. - **Index lag**: the file watcher debounces ~500ms behind writes; don't re-query immediately after editing a file in the same turn.
  43. ### If \`.codegraph/\` doesn't exist
  44. The MCP server returns "not initialized." Ask the user: *"I notice this project doesn't have CodeGraph initialized. Want me to run \`codegraph init -i\` to build the index?"*
  45. ${CODEGRAPH_SECTION_END}`;
  46. /**
  47. * Backwards-compat alias. Existing downstream code may import
  48. * `CLAUDE_MD_TEMPLATE` from this module via the re-export shim in
  49. * `claude-md-template.ts`.
  50. */
  51. export const CLAUDE_MD_TEMPLATE = INSTRUCTIONS_TEMPLATE;