You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add .claude/rules/ for contextual convention enforcement
Extract coding conventions from CLAUDE.md into 4 modular rules files
(typescript, webview, yaml-operations, testing) that load contextually
when editing matching file patterns.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Applies when creating or modifying `.ts` files in `src/`.
4
+
5
+
## Hard Rules
6
+
7
+
-**Async file ops only** — Use `fs.promises` (readFile, writeFile, stat, etc.). Never use sync variants (`readFileSync`, `writeFileSync`, `statSync`, etc.). The extension runs in VS Code's extension host — sync I/O blocks the UI.
8
+
-**No `as any` type bypasses** — Use proper typing. If you need to attach metadata to an object, use a `WeakMap` (see `panelResolverKeys` pattern in `writerView/manager.ts`).
9
+
-**Path traversal validation** — Always call `isPathWithinWorkspace()` + `path.resolve()` before any file operation on user-provided paths. Import from `writerView/utils/helpers.ts`.
10
+
-**SVG excluded from import** — SVG is an XSS vector. Never add SVG to import/embed flows. SVGs are display-only via workspace scanner with CSP blocking scripts.
11
+
12
+
## Patterns
13
+
14
+
- Use `WeakMap` for panel metadata (resolver keys) instead of casting with `as any`
15
+
-`pendingDuplicateResolvers` Map + `panelResolverKeys` WeakMap for Promise-based webview dialogs
16
+
- Resolve pending promises in `onDidDispose` to prevent hangs
17
+
- Module-level state lives in `extensionState.ts` — access via `getDeps()`, not global variables
18
+
- Commands are registered in domain-specific modules under `commands/` — routed by `commands/register.ts`
Applies when creating or modifying files in `src/writerView/`.
4
+
5
+
## Security
6
+
7
+
-**HTML escape ALL interpolations** — Use `escapeHtml()` from `writerView/utils/helpers.ts`. It escapes `<`, `>`, `&`, `"`, and `'` (as `'`). Never interpolate user content into HTML without escaping.
8
+
-**CSP header required** — Every webview must include: `img-src ${webview.cspSource} data:;`. Scripts must use nonce-based CSP.
9
+
-**No inline scripts** — All JavaScript goes through nonce-gated `<script>` tags. CSP blocks inline event handlers.
10
+
11
+
## Image Handling
12
+
13
+
- SHA256 content hash with file-size pre-filter for image deduplication
14
+
- Images stored workspace-relative, served via `webview.asWebviewUri()`
15
+
-`data:` URIs allowed in CSP for small inline images only
16
+
17
+
## Webview Communication
18
+
19
+
- Use `safePostMessage()` from helpers for extension → webview messages
20
+
- Webview → extension communication via `vscode.postMessage()` with typed message objects
21
+
- Always validate message types in both directions
22
+
23
+
## File Structure
24
+
25
+
```
26
+
writerView/
27
+
├── manager.ts # Panel pool, file locking, lifecycle
Applies when creating or modifying code that reads or writes `.codex.yaml`, `.codex.json`, or `.codex.md` files.
4
+
5
+
## Mutation Safety
6
+
7
+
-**Use `withFileLock()` for all YAML writes** — Concurrent writes corrupt YAML. The file lock prevents race conditions when multiple operations (auto-save, structure editing, drag-drop) target the same file.
8
+
-**Preserve YAML comments and formatting** — `codexModel.ts` uses the `yaml` library's AST-preserving mode. Don't replace it with naive parse/stringify.
9
+
-**Validate after mutation** — After any structural change (add/remove/move nodes), run validation to catch broken references.
10
+
11
+
## Format Support
12
+
13
+
The extension handles three formats transparently:
0 commit comments