Skip to main content
Glama

prevention_rules

Destructive

Generate rules to prevent repeated mistakes by analyzing patterns of errors and creating actionable guidelines for improvement.

Instructions

Generate prevention rules from repeated mistake patterns

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
minOccurrencesNo
outputPathNo

Implementation Reference

  • The implementation of the `prevention_rules` tool handler, which triggers the generation and registration of prevention rules.
    function writePreventionRules(filePath, minOccurrences = 2) {
      const { PREVENTION_RULES_PATH } = getFeedbackPaths();
      const outPath = filePath || PREVENTION_RULES_PATH;
      const markdown = buildPreventionRules(minOccurrences);
      ensureDir(path.dirname(outPath));
      fs.writeFileSync(outPath, `${markdown}\n`);
    
      const contextFs = getContextFsModule();
      if (contextFs && typeof contextFs.registerPreventionRules === 'function') {
        try {
          contextFs.registerPreventionRules(markdown, { minOccurrences, outputPath: outPath });
        } catch {
          // Non-critical
        }
      }
      return { path: outPath, markdown };
    }
  • The logic for building the prevention rules content based on logged mistakes and diagnostic entries.
    function buildPreventionRules(minOccurrences = 2, options = {}) {
      const { MEMORY_LOG_PATH, DIAGNOSTIC_LOG_PATH } = getFeedbackPaths();
      const memories = readJSONL(MEMORY_LOG_PATH).filter((m) => m.category === 'error');
      const diagnosticEntries = readDiagnosticEntries(DIAGNOSTIC_LOG_PATH);
      if (memories.length === 0) {
        if (diagnosticEntries.length === 0) {
          return '# Prevention Rules\n\nNo mistake memories recorded yet.';
        }
      }
    
      // Time-weighted decay: recent mistakes count more (#202)
      const decayHalfLifeDays = options.decayHalfLifeDays || 7;
      const lambda = Math.LN2 / decayHalfLifeDays;
      const now = Date.now();
    
      function decayWeight(memory) {
        const ts = memory.timestamp ? new Date(memory.timestamp).getTime() : now;
        const daysSince = (now - ts) / (24 * 60 * 60 * 1000);
        return Math.exp(-lambda * daysSince);
      }
    
      const buckets = {};
      const rubricBuckets = {};
      const diagnosisBuckets = {};
      const repeatedViolationBuckets = {};
      for (const m of memories) {
        const key = (m.tags || []).find((t) => !['feedback', 'negative', 'positive'].includes(t)) || 'general';
        if (!buckets[key]) buckets[key] = { items: [], weightedCount: 0 };
        const w = decayWeight(m);
        buckets[key].items.push(m);
        buckets[key].weightedCount += w;
    
        const failed = m.rubricSummary && Array.isArray(m.rubricSummary.failingCriteria)
          ? m.rubricSummary.failingCriteria
          : [];
        failed.forEach((criterion) => {
          if (!rubricBuckets[criterion]) rubricBuckets[criterion] = [];
          rubricBuckets[criterion].push(m);
        });
    
        if (m.diagnosis && m.diagnosis.rootCauseCategory) {
          if (!diagnosisBuckets[m.diagnosis.rootCauseCategory]) diagnosisBuckets[m.diagnosis.rootCauseCategory] = [];
          diagnosisBuckets[m.diagnosis.rootCauseCategory].push(m);
        }
    
        (m.diagnosis && Array.isArray(m.diagnosis.violations) ? m.diagnosis.violations : []).forEach((violation) => {
          const key = violation.constraintId || violation.message;
          if (!key) return;
          if (!repeatedViolationBuckets[key]) repeatedViolationBuckets[key] = [];
          repeatedViolationBuckets[key].push(m);
        });
      }
    
      for (const entry of diagnosticEntries) {
        const diagnosis = entry && entry.diagnosis ? entry.diagnosis : null;
        if (!diagnosis || !diagnosis.rootCauseCategory) continue;
        if (!diagnosisBuckets[diagnosis.rootCauseCategory]) diagnosisBuckets[diagnosis.rootCauseCategory] = [];
        diagnosisBuckets[diagnosis.rootCauseCategory].push(entry);
    
        (Array.isArray(diagnosis.violations) ? diagnosis.violations : []).forEach((violation) => {
          const key = violation.constraintId || violation.message;
          if (!key) return;
          if (!repeatedViolationBuckets[key]) repeatedViolationBuckets[key] = [];
          repeatedViolationBuckets[key].push(entry);
        });
      }
    
      const lines = ['# Prevention Rules', '', 'Generated from negative feedback memories (time-weighted, half-life: ' + decayHalfLifeDays + 'd).'];
    
      Object.entries(buckets)
        .sort((a, b) => b[1].weightedCount - a[1].weightedCount)
        .forEach(([domain, { items, weightedCount }]) => {
          const effectiveOccurrences = Math.round(weightedCount);
          if (effectiveOccurrences < minOccurrences) return;
          const latest = items[items.length - 1];
          const avoid = (latest.content || '').split('\n').find((l) => l.toLowerCase().startsWith('how to avoid:')) || 'How to avoid: Investigate and prevent recurrence';
          lines.push('');
          lines.push(`## ${domain}`);
          lines.push(`- Recurrence count: ${items.length} (weighted: ${weightedCount.toFixed(1)})`);
          lines.push(`- Rule: ${avoid.replace(/^How to avoid:\s*/i, '')}`);
          lines.push(`- Latest mistake: ${latest.title}`);
        });
    
      const rubricEntries = Object.entries(rubricBuckets)
        .sort((a, b) => b[1].length - a[1].length)
        .filter(([, items]) => items.length >= minOccurrences);
      if (rubricEntries.length > 0) {
        lines.push('');
        lines.push('## Rubric Failure Dimensions');
        rubricEntries.forEach(([criterion, items]) => {
          lines.push(`- ${criterion}: ${items.length} failures`);
        });
      }
    
      const diagnosisEntries = Object.entries(diagnosisBuckets)
        .sort((a, b) => b[1].length - a[1].length)
        .filter(([, items]) => items.length >= minOccurrences);
      if (diagnosisEntries.length > 0) {
        lines.push('');
        lines.push('## Root Cause Categories');
        diagnosisEntries.forEach(([category, items]) => {
          lines.push(`- ${category}: ${items.length} failures`);
        });
      }
    
      const repeatedViolationEntries = Object.entries(repeatedViolationBuckets)
        .sort((a, b) => b[1].length - a[1].length)
        .filter(([, items]) => items.length >= minOccurrences);
      if (repeatedViolationEntries.length > 0) {
        lines.push('');
        lines.push('## Repeated Failure Constraints');
        repeatedViolationEntries.forEach(([constraintId, items]) => {
          lines.push(`- ${constraintId}: ${items.length} failures`);
        });
      }
    
      if (lines.length === 3) {
        lines.push('');
        lines.push(`No domain has reached the threshold (${minOccurrences}) yet.`);
      }
    
      return lines.join('\n');
    }
  • The tool registration/dispatch handler in the MCP server adapter that calls `writePreventionRules`.
    case 'prevention_rules': {
      const outputPath = args.outputPath ? resolveSafePath(args.outputPath) : undefined;
      return toTextResult(writePreventionRules(outputPath, Number(args.minOccurrences || 2)));
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The annotations indicate destructiveHint=true, which the description doesn't explicitly mention. However, the description doesn't contradict this annotation, and it adds some behavioral context by explaining what the tool generates (prevention rules) and from what source (repeated mistake patterns). With annotations covering the destructive nature, the description adds modest value.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that communicates the core purpose without any wasted words. It's appropriately sized and front-loaded with the essential information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with destructiveHint=true, 2 undocumented parameters, and no output schema, the description is insufficient. It doesn't explain what format the prevention rules take, what happens during generation, or provide any guidance about the parameters. The description leaves too many gaps given the tool's complexity and annotation profile.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage and 2 parameters (minOccurrences and outputPath), the description provides no information about what these parameters mean or how they should be used. The description doesn't compensate for the complete lack of parameter documentation in the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Generate prevention rules from repeated mistake patterns' clearly states the tool's function with a specific verb ('Generate') and resource ('prevention rules'), and explains the input source ('from repeated mistake patterns'). However, it doesn't differentiate this tool from its many siblings, which would require a 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives, nor does it mention any prerequisites or exclusions. With 24 sibling tools available, this lack of differentiation leaves the agent without context for tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/IgorGanapolsky/mcp-memory-gateway'

If you have feedback or need assistance with the MCP directory API, please join our Discord server