[SSE-Writer] Robust file vs directory detection for targetDirectory #297
Labels
No labels
good first issue
has-pr
help wanted
idea
priority
critical
priority
high
priority
low
priority
medium
status
blocked
status
in-progress
status
needs-review
status
ready
type
bug
type
docs
type
enhancement
type
feature
type
refactor
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
customable/claude-mem#297
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
The SSE-Writer receives
targetDirectoryvalues that are sometimes file paths instead of directory paths, causing CLAUDE.md write failures:Affected Paths (from logs)
.../ui/src/App.tsx.../ui/src/.../websocket/task-dispatcher.ts.../websocket/.../types/src/repository.ts.../src/Root Cause
The
targetDirectoryis being set from file paths in observation tasks without extracting the directory portion. This can happen when:path.dirname()Proposed Solution
1. SSE-Writer: Add file detection and auto-correction
Use a combination of filesystem checks and extension detection:
2. SSE-Writer: Fix error handling
Currently logs success even after failure. Fix the control flow:
3. Backend: Fix source of incorrect paths
In addition to SSE-Writer hardening, fix the root cause in the backend where
targetDirectoryis set:Package Options for Extension Detection
textextensionsis-text-pathlinguist-languagesRecommendation: Use
textextensionsfor its simplicity and small size, or implement a custom list if no dependencies are preferred.Files to Modify
packages/hooks/src/sse-writer.tsisLikelyFile()helper functionsanitizeTargetDirectory()wrapperwriteClaudeMd()Backend (root cause fix)
targetDirectoryis set forclaudemd:readyeventspath.dirname()is used when extracting from file pathsTesting
targetDirectoryAcceptance Criteria
isLikelyFile()functionFixed in commit
4bf6df2.SSE-Writer Fixes:
normalizeToDirectory()function - Detects file paths by extension and extracts the directory usingpath.dirname():Directory validation - Before writing, validates that the path is actually a directory
Directory creation - Creates the directory if it doesn't exist (using
mkdirSyncwithrecursive: true)Boolean return value -
writeClaudeMd()now returnstrue/falseto indicate success/failureFixed success logging - Only logs "CLAUDE.md written successfully" if the write actually succeeded
Note: The root cause (backend sending file paths instead of directory paths in
workingDirectory) may still need investigation, but this fix makes the SSE-Writer resilient to incorrect paths.[SSE-Writer] targetDirectory contains file paths instead of directoriesto [SSE-Writer] Robust file vs directory detection for targetDirectoryUpdate: Universellerer Ansatz
Statt nur Code-Extensions zu prüfen, sollten wir einen allgemeineren Ansatz verwenden:
Empfohlene Logik
Warum dieser Ansatz besser ist
.d,.git)Edge Cases
.gitignore→ Hat Extension.gitignore(weird aber ok)Makefile→ Keine Extension → wird als Verzeichnis behandelt (falsch, aber harmlos - würde nur nicht geschrieben)types.d→ Könnte Verzeichnis sein → Whitelist.vscode/→ Verstecktes Verzeichnis → WhitelistDer Filesystem-Check als erste Prüfung fängt die meisten Fälle ab. Die Extension-Heuristik ist nur Fallback für nicht-existierende Pfade.
Implementation Complete
Changes Made
1. Created shared path utility module (
packages/hooks/src/utils/path-utils.ts)isLikelyFile(path)- Checks if a path appears to be a file based on extensionnormalizeToDirectory(path)- Returns parent directory for file pathsCODE_EXTENSIONSlist with 40+ common file extensions2. Fixed root cause in
extractTargetDirectory()(packages/hooks/src/handlers/post-tool-use.ts)input.pathfor Glob/Grep tools usingnormalizeToDirectory()targetDirectory3. Refactored SSE-Writer (
packages/hooks/src/sse-writer.ts)normalizeToDirectory()to shared utility4. Added comprehensive test suite (
packages/hooks/src/__tests__/path-utils.test.ts)isLikelyFile()for various file typesnormalizeToDirectory()for file and directory pathsAcceptance Criteria
isLikelyFile()function (22 tests)Commits:
ce2cf6c,2e75d84