[Plugin] SSE-Writer: Validierung und Robustheit für claude-mem-context Tags #291

Closed
opened 2026-01-25 15:11:08 +00:00 by jack · 0 comments
Owner

Problem

Der SSE-Writer in packages/hooks/src/sse-writer.ts hat keine Validierung, dass der empfangene Content tatsächlich <claude-mem-context> Tags enthält.

Aktueller Code (Zeile 91-109)

function replaceContextSection(existing: string, newContent: string): string {
  const startTag = '<claude-mem-context>';
  const endTag = '</claude-mem-context>';

  const startIdx = existing.indexOf(startTag);
  const endIdx = existing.indexOf(endTag);

  if (startIdx === -1 || endIdx === -1) {
    // No existing section, append with newlines
    return existing.trimEnd() + '\n\n' + newContent + '\n';
  }

  // Replace existing section
  return (
    existing.slice(0, startIdx) +
    newContent +
    existing.slice(endIdx + endTag.length)
  );
}

Probleme

  1. Keine Validierung von newContent - Wenn der Content keine Tags enthält (Bug im Worker, Netzwerkfehler), wird ungültiger Content geschrieben

  2. Kein Fallback bei fehlenden Tags in bestehender Datei - User-erstellte CLAUDE.md ohne Tags funktioniert beim ersten Mal, aber nachfolgende Updates könnten fehlschlagen

  3. Keine Warnung bei Anomalien - Silent fail wenn etwas nicht stimmt

Vorgeschlagene Lösung

function replaceContextSection(existing: string, newContent: string): string {
  const startTag = '<claude-mem-context>';
  const endTag = '</claude-mem-context>';

  // 1. Validiere dass newContent die Tags enthält
  if (!newContent.includes(startTag) || !newContent.includes(endTag)) {
    console.error('[sse-writer] Invalid content: missing claude-mem-context tags');
    // Wrap content in tags as fallback
    newContent = `${startTag}\n${newContent}\n${endTag}`;
  }

  const startIdx = existing.indexOf(startTag);
  const endIdx = existing.indexOf(endTag);

  if (startIdx === -1 || endIdx === -1) {
    // No existing section - append at end
    // Content already has tags (validated above)
    console.log('[sse-writer] No existing claude-mem-context section, appending');
    return existing.trimEnd() + '\n\n' + newContent + '\n';
  }

  // Replace existing section
  return (
    existing.slice(0, startIdx) +
    newContent +
    existing.slice(endIdx + endTag.length)
  );
}

Zusätzliche Verbesserungen

Validierung beim Empfang (writeClaudeMd)

function writeClaudeMd(dir: string, content: string): void {
  const filePath = path.join(dir, 'CLAUDE.md');

  // Validate content has required tags
  if (!content.includes('<claude-mem-context>')) {
    console.error(`[sse-writer] Received content without claude-mem-context tags, skipping`);
    return;
  }

  // ... rest of function
}

Logging für Debugging

// Log when tags are found/not found
if (startIdx === -1 || endIdx === -1) {
  console.log(`[sse-writer] Creating new claude-mem-context section in ${filePath}`);
} else {
  console.log(`[sse-writer] Updating existing claude-mem-context section (${startIdx}-${endIdx})`);
}

Betroffene Dateien

  • packages/hooks/src/sse-writer.ts

Priorität

Niedrig - Der Worker generiert aktuell immer korrekte Tags, aber defensive Programmierung ist wichtig

## Problem Der SSE-Writer in `packages/hooks/src/sse-writer.ts` hat keine Validierung, dass der empfangene Content tatsächlich `<claude-mem-context>` Tags enthält. ### Aktueller Code (Zeile 91-109) ```typescript function replaceContextSection(existing: string, newContent: string): string { const startTag = '<claude-mem-context>'; const endTag = '</claude-mem-context>'; const startIdx = existing.indexOf(startTag); const endIdx = existing.indexOf(endTag); if (startIdx === -1 || endIdx === -1) { // No existing section, append with newlines return existing.trimEnd() + '\n\n' + newContent + '\n'; } // Replace existing section return ( existing.slice(0, startIdx) + newContent + existing.slice(endIdx + endTag.length) ); } ``` ### Probleme 1. **Keine Validierung von `newContent`** - Wenn der Content keine Tags enthält (Bug im Worker, Netzwerkfehler), wird ungültiger Content geschrieben 2. **Kein Fallback bei fehlenden Tags in bestehender Datei** - User-erstellte CLAUDE.md ohne Tags funktioniert beim ersten Mal, aber nachfolgende Updates könnten fehlschlagen 3. **Keine Warnung bei Anomalien** - Silent fail wenn etwas nicht stimmt ## Vorgeschlagene Lösung ```typescript function replaceContextSection(existing: string, newContent: string): string { const startTag = '<claude-mem-context>'; const endTag = '</claude-mem-context>'; // 1. Validiere dass newContent die Tags enthält if (!newContent.includes(startTag) || !newContent.includes(endTag)) { console.error('[sse-writer] Invalid content: missing claude-mem-context tags'); // Wrap content in tags as fallback newContent = `${startTag}\n${newContent}\n${endTag}`; } const startIdx = existing.indexOf(startTag); const endIdx = existing.indexOf(endTag); if (startIdx === -1 || endIdx === -1) { // No existing section - append at end // Content already has tags (validated above) console.log('[sse-writer] No existing claude-mem-context section, appending'); return existing.trimEnd() + '\n\n' + newContent + '\n'; } // Replace existing section return ( existing.slice(0, startIdx) + newContent + existing.slice(endIdx + endTag.length) ); } ``` ## Zusätzliche Verbesserungen ### Validierung beim Empfang (writeClaudeMd) ```typescript function writeClaudeMd(dir: string, content: string): void { const filePath = path.join(dir, 'CLAUDE.md'); // Validate content has required tags if (!content.includes('<claude-mem-context>')) { console.error(`[sse-writer] Received content without claude-mem-context tags, skipping`); return; } // ... rest of function } ``` ### Logging für Debugging ```typescript // Log when tags are found/not found if (startIdx === -1 || endIdx === -1) { console.log(`[sse-writer] Creating new claude-mem-context section in ${filePath}`); } else { console.log(`[sse-writer] Updating existing claude-mem-context section (${startIdx}-${endIdx})`); } ``` ## Betroffene Dateien - `packages/hooks/src/sse-writer.ts` ## Priorität Niedrig - Der Worker generiert aktuell immer korrekte Tags, aber defensive Programmierung ist wichtig
jack 2026-01-25 15:11:10 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
customable/claude-mem#291
No description provided.