Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: optimize CLAUDE.md template and MCP enforcement rules

riley022793a781f37076
diff --git a/ix-cli/src/cli/__tests__/init.test.ts b/ix-cli/src/cli/__tests__/init.test.ts
new file mode 100644
index 00000000..f220afeb
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/init.test.ts
@@ -0,0 +1,54 @@
+import { describe, it, expect, vi, beforeAll } from "vitest";
+
+// Mock dependencies that init.ts imports so we can load it without side effects
+vi.mock("../../client/api.js", () => ({
+ IxClient: vi.fn().mockImplementation(() => ({
+ health: vi.fn().mockResolvedValue(true),
+ })),
+}));
+
+vi.mock("../config.js", () => ({
+ getEndpoint: vi.fn().mockReturnValue("http://localhost:8090"),
+}));
+
+let CLAUDE_MD: string;
+
+beforeAll(async () => {
+ const mod = await import("../commands/init.js");
+ CLAUDE_MD = mod.CLAUDE_MD;
+});
+
+describe("CLAUDE_MD template", () => {
+ it("contains all 7 mandatory rules", () => {
+ expect(CLAUDE_MD).toContain("ix_query");
+ expect(CLAUDE_MD).toContain("ix_decide");
+ expect(CLAUDE_MD).toContain("ix_conflicts");
+ expect(CLAUDE_MD).toContain("NEVER guess");
+ expect(CLAUDE_MD).toContain("ix_ingest");
+ expect(CLAUDE_MD).toContain("ix://session/context");
+ expect(CLAUDE_MD).toContain("ix_truth");
+ });
+
+ it("contains a Workflow section", () => {
+ expect(CLAUDE_MD).toContain("## Workflow");
+ });
+
+ it("contains a What NOT to Do section", () => {
+ expect(CLAUDE_MD).toContain("## What NOT to Do");
+ });
+
+ it("contains a Confidence Scores section", () => {
+ expect(CLAUDE_MD).toContain("## Confidence Scores");
+ });
+
+ it("is under 80 lines to keep it concise", () => {
+ const lines = CLAUDE_MD.split("\n");
+ expect(lines.length).toBeLessThan(80);
+ });
+
+ it("has numbered rules 1-7 in the MANDATORY RULES section", () => {
+ for (let i = 1; i <= 7; i++) {
+ expect(CLAUDE_MD).toContain(`${i}.`);
+ }
+ });
+});
diff --git a/ix-cli/src/cli/commands/init.ts b/ix-cli/src/cli/commands/init.ts
index 5e2af438..a42c1df3 100644
--- a/ix-cli/src/cli/commands/init.ts
+++ b/ix-cli/src/cli/commands/init.ts
@@ -5,18 +5,38 @@ import type { Command } from "commander";
import { IxClient } from "../../client/api.js";
import { getEndpoint } from "../config.js";
-const CLAUDE_MD = `# Ix Memory System
+export const CLAUDE_MD = `# Ix Memory System
-This project uses Ix Memory for persistent, time-aware context.
+This project uses Ix Memory — persistent, time-aware context for LLM assistants.
## MANDATORY RULES
-1. BEFORE answering any question about this codebase → call ix_query
-2. AFTER making any design/architecture decision → call ix_decide
-3. When noticing contradictory information → call ix_conflicts
-4. NEVER guess about codebase facts when Ix has structured data
-5. After modifying code → call ix_ingest on changed files
-6. At start of each session → review ix://session/context for prior work
-7. When the user states a goal → call ix_truth to record it
+1. BEFORE answering any question about the codebase → call \`ix_query\` first. Do NOT answer from training data alone.
+2. AFTER every design or architecture decision → call \`ix_decide\` with both the conclusion AND the reasoning.
+3. When you notice contradictory information → call \`ix_conflicts\` and present conflicts to the user before continuing.
+4. NEVER guess about codebase facts — if Ix has structured data, use it. Say "according to Ix" when citing results.
+5. IMMEDIATELY after modifying code → call \`ix_ingest\` on changed files. Do not batch this to the end of the session.
+6. At start of each session → review \`ix://session/context\` to load prior work and decisions.
+7. When the user states a goal → call \`ix_truth\` to record the intent so all decisions can trace back to it.
+
+## Workflow
+A typical session follows this flow:
+1. **Start** — Read \`ix://session/context\` and \`ix://project/intent\` to understand prior state.
+2. **Query** — Before any task, call \`ix_query\` to get relevant context from the knowledge graph.
+3. **Work** — Implement changes, making decisions as needed.
+4. **Ingest** — After each file change, call \`ix_ingest\` immediately to keep the graph current.
+5. **Decide** — Record any design decisions with \`ix_decide\` so future sessions can understand why.
+
+## What NOT to Do
+- Do NOT answer architecture questions from training data alone — always check Ix first.
+- Do NOT skip \`ix_ingest\` after modifying files — stale memory leads to wrong answers next session.
+- Do NOT record a decision without rationale — "we chose X" is useless without "because Y".
+- Do NOT ignore conflicts — if \`ix_conflicts\` returns results, surface them to the user before proceeding.
+
+## Confidence Scores
+Ix returns confidence scores with query results. When data has low confidence:
+- Mention the uncertainty to the user (e.g., "Ix has low confidence on this — it may be outdated").
+- Suggest re-ingesting the relevant files to improve confidence.
+- Never present low-confidence data as established fact.
`;
export function registerInitCommand(program: Command): void {
diff --git a/ix-cli/src/mcp/__tests__/server.test.ts b/ix-cli/src/mcp/__tests__/server.test.ts
new file mode 100644
index 00000000..ae8113c7
--- /dev/null
+++ b/ix-cli/src/mcp/__tests__/server.test.ts
@@ -0,0 +1,55 @@
+import { describe, it, expect, vi, beforeAll } from "vitest";
+
+// Mock the MCP SDK to prevent server startup side effects
+vi.mock("@modelcontextprotocol/sdk/server/mcp.js", () => ({
+ McpServer: vi.fn().mockImplementation(() => ({
+ tool: vi.fn(),
+ resource: vi.fn(),
+ connect: vi.fn().mockResolvedValue(undefined),
+ })),
+}));
+
+vi.mock("@modelcontextprotocol/sdk/server/stdio.js", () => ({
+ StdioServerTransport: vi.fn(),
+}));
+
+vi.mock("../../client/api.js", () => ({
+ IxClient: vi.fn().mockImplementation(() => ({})),
+}));
+
+let INSTRUCTIONS: string;
+
+beforeAll(async () => {
+ const mod = await import("../server.js");
+ INSTRUCTIONS = mod.INSTRUCTIONS;
+});
+
+describe("MCP server INSTRUCTIONS", () => {
+ it("contains all 7 mandatory rules", () => {
+ expect(INSTRUCTIONS).toContain("ix_query");
+ expect(INSTRUCTIONS).toContain("ix_decide");
+ expect(INSTRUCTIONS).toContain("ix_conflicts");
+ expect(INSTRUCTIONS).toContain("NEVER answer from training data alone");
+ expect(INSTRUCTIONS).toContain("ix_ingest");
+ expect(INSTRUCTIONS).toContain("ix://session/context");
+ expect(INSTRUCTIONS).toContain("ix_truth");
+ });
+
+ it("is under 30 lines to stay concise", () => {
+ const lines = INSTRUCTIONS.split("\n");
+ expect(lines.length).toBeLessThan(30);
+ });
+
+ it("contains behavioral checks section", () => {
+ expect(INSTRUCTIONS).toContain("BEHAVIORAL CHECKS");
+ });
+
+ it("contains a stop-and-query nudge", () => {
+ expect(INSTRUCTIONS).toContain("STOP and call it");
+ });
+
+ it("describes what Ix returns", () => {
+ expect(INSTRUCTIONS).toContain("nodes, edges, claims, conflicts, and decisions");
+ expect(INSTRUCTIONS).toContain("confidence scores");
+ });
+});
diff --git a/ix-cli/src/mcp/server.ts b/ix-cli/src/mcp/server.ts
index 60a6e426..596e39bc 100644
--- a/ix-cli/src/mcp/server.ts
+++ b/ix-cli/src/mcp/server.ts
@@ -13,16 +13,24 @@ import { IxClient } from "../client/api.js";
// ---------------------------------------------------------------------------
// Server instructions — sent to the LLM at connection time
// ---------------------------------------------------------------------------
-const INSTRUCTIONS = `You have access to Ix Memory — a persistent, time-aware memory system for this codebase.
+export const INSTRUCTIONS = `You have access to Ix Memory — a persistent, time-aware knowledge graph for this codebase.
+Ix returns structured context: nodes, edges, claims, conflicts, and decisions — each with confidence scores.
MANDATORY RULES:
-1. ALWAYS call ix_query before answering questions about the codebase
-2. AFTER any design decision, call ix_decide to record it
-3. When you notice conflicting information, call ix_conflicts
-4. NEVER answer from memory alone when Ix has data
-5. After modifying code, call ix_ingest on changed files
-6. At start of each session, review ix://session/context
-7. When the user states a goal, call ix_truth to record it`;
+1. ALWAYS call ix_query before answering questions about the codebase — if you are about to answer and have not called ix_query yet, STOP and call it first
+2. AFTER any design decision, call ix_decide to record it with full reasoning
+3. When you notice conflicting information, call ix_conflicts and present results to the user
+4. NEVER answer from training data alone when Ix has structured data available
+5. After modifying code, call ix_ingest IMMEDIATELY on changed files — not at end of session
+6. At start of each session, review ix://session/context for prior work and decisions
+7. When the user states a goal, call ix_truth to record the intent so decisions trace back to it
+
+BEHAVIORAL CHECKS:
+- About to answer a codebase question? → Did you call ix_query? If not, STOP and call it.
+- Just made a design choice? → Call ix_decide before moving on.
+- Just edited a file? → Call ix_ingest now, not later.
+- See something contradictory? → Call ix_conflicts before proceeding.
+- Low confidence in Ix results? → Tell the user and suggest re-ingesting.`;
// ---------------------------------------------------------------------------
// Client – uses IX_ENDPOINT env var or defaults to localhost:8090
@@ -42,7 +50,7 @@ const server = new McpServer(
// --- ix_query ---------------------------------------------------------------
server.tool(
"ix_query",
- "ALWAYS call this tool BEFORE answering any question about the codebase, architecture, or technical decisions. Ix Memory contains structured, versioned knowledge with confidence scores. Never rely on your training data alone when Ix has data available.",
+ "ALWAYS call this tool BEFORE answering any question about the codebase, architecture, or technical decisions. Ix Memory contains structured, versioned knowledge with confidence scores. Never rely on your training data alone when Ix has data available. Results include confidence scores — mention uncertainty to the user when confidence is low.",
{
question: z.string().describe("The question to ask the knowledge graph"),
asOfRev: z.optional(z.number()).describe("Optional revision snapshot"),
@@ -68,7 +76,7 @@ server.tool(
// --- ix_ingest ---------------------------------------------------------------
server.tool(
"ix_ingest",
- "Ingest source files into the Ix knowledge graph. Call after modifying code so the memory stays current.",
+ "Ingest source files into the Ix knowledge graph. Do this IMMEDIATELY after file changes, not at the end of the session. Stale memory causes wrong answers in future sessions.",
{
path: z.string().describe("File or directory path to ingest"),
recursive: z.optional(z.boolean()).describe("Recurse into subdirectories"),
@@ -95,7 +103,7 @@ server.tool(
// --- ix_decide ---------------------------------------------------------------
server.tool(
"ix_decide",
- "Record a design decision in the knowledge graph. Always call after making an architectural or design choice.",
+ "Record a design decision in the knowledge graph. Always call after making an architectural or design choice. This creates a permanent record — include the reasoning, not just the conclusion.",
{
title: z.string().describe("Short title of the decision"),
rationale: z.string().describe("Why this decision was made"),
@@ -235,7 +243,7 @@ server.tool(
// --- ix_conflicts ------------------------------------------------------------
server.tool(
"ix_conflicts",
- "List all detected conflicts in the knowledge graph. Call when you notice contradictory information.",
+ "List all detected conflicts in the knowledge graph. Call when you notice contradictory information. If any conflicts are found, present them to the user before proceeding.",
{},
async () => {
try {
@@ -349,7 +357,7 @@ server.tool(
// --- ix_truth ----------------------------------------------------------------
server.tool(
"ix_truth",
- "Manage project intents (goals). Use action 'list' to see current intents, or 'add' to record a new goal.",
+ "Manage project intents (goals). Use action 'list' to see current intents, or 'add' to record a new goal. The user's stated goals guide all decisions — always check intents before major work.",
{
action: z.enum(["list", "add"]).describe("Action: list or add"),
statement: z