|
| 1 | +## Repository Overview |
| 2 | + |
| 3 | +**Purpose:** Restore context window visibility for AWS Bedrock users in Claude Code by displaying token usage in the status line. |
| 4 | + |
| 5 | +**Architecture:** Single-script Node.js project with comprehensive testing and security hardening. |
| 6 | + |
| 7 | +## Package Manager |
| 8 | + |
| 9 | +**IMPORTANT:** This project uses **pnpm**, not npm. |
| 10 | + |
| 11 | +## Project Structure |
| 12 | + |
| 13 | +**Key files:** |
| 14 | +- `src/context-status.js` - Main executable script |
| 15 | +- `tests/` - Jest test suite (functionality, security, benchmarks) |
| 16 | +- `package.json` - Uses pnpm (not npm) |
| 17 | +- `.eslintrc.cjs` - Security-focused linting rules |
| 18 | + |
| 19 | +## Key Design Decisions |
| 20 | + |
| 21 | +### 1. Build Process |
| 22 | +- **No build step required** - single script approach |
| 23 | +- Main script lives in src/ directory (`src/context-status.js`) |
| 24 | +- Direct execution with Node.js 18+ |
| 25 | +- Simple src/ structure for better organization |
| 26 | + |
| 27 | +### 2. Testing Strategy |
| 28 | +- **Jest with ES modules** support (`node --experimental-vm-modules`) |
| 29 | +- **Three test categories:** |
| 30 | + - Functionality tests (core features) |
| 31 | + - Security tests (path traversal, validation, etc.) |
| 32 | + - Performance benchmarks (using `benchmark` library) |
| 33 | +- **Coverage targets:** 70% for branches, functions, lines, statements |
| 34 | + |
| 35 | +### 3. Performance Features |
| 36 | +- **Clean formatting:** Consistent token display across all environments |
| 37 | +- **Memory efficient:** Minimal memory footprint for status line updates |
| 38 | + |
| 39 | +## Development Commands |
| 40 | + |
| 41 | +```bash |
| 42 | +# Setup |
| 43 | +pnpm install |
| 44 | + |
| 45 | +# Development workflow |
| 46 | +pnpm test # Run all tests |
| 47 | +pnpm run test:coverage # Generate coverage report |
| 48 | +pnpm run lint # Check code quality and security |
| 49 | +pnpm run lint:fix # Auto-fix linting issues |
| 50 | +pnpm run benchmark # Run performance tests |
| 51 | +``` |
| 52 | + |
| 53 | +## Performance Targets |
| 54 | + |
| 55 | +The benchmark suite validates basic performance expectations: |
| 56 | + |
| 57 | +- **Token formatting:** Efficient number formatting with Intl.NumberFormat |
| 58 | +- **File processing:** Quick parsing of JSONL transcript files |
| 59 | +- **Memory usage:** Minimal memory footprint for status line display |
| 60 | + |
| 61 | +## Security Considerations |
| 62 | + |
| 63 | +### Input Validation |
| 64 | +- JSON input parsing with error handling |
| 65 | +- Basic file path resolution |
| 66 | + |
| 67 | +### Error Handling |
| 68 | +- No sensitive information exposed in error messages |
| 69 | +- Safe fallback output (`Context: -`) on all errors |
| 70 | + |
| 71 | + |
| 72 | +## ESLint Configuration |
| 73 | + |
| 74 | +Strict security-focused configuration includes: |
| 75 | +- No `eval()`, `new Function()`, or similar dangerous patterns |
| 76 | +- Strict equality checks (`===`) |
| 77 | +- Async/await best practices |
| 78 | +- No unused variables or dead code |
| 79 | +- Consistent code style for maintainability |
| 80 | + |
| 81 | +## Integration Testing |
| 82 | + |
| 83 | +```bash |
| 84 | +# Test with Claude Code (manual) |
| 85 | +echo '{"transcript_path":"/path/to/actual/transcript.jsonl"}' | node src/context-status.js |
| 86 | + |
| 87 | +# Test security (should be blocked) |
| 88 | +echo '{"transcript_path":"../../../etc/passwd.jsonl"}' | node src/context-status.js |
| 89 | +# Expected: Context: 0 (with security logging to stderr) |
| 90 | +``` |
| 91 | + |
| 92 | +## Version Management |
| 93 | + |
| 94 | +- **Current version:** 2.0.0 |
| 95 | +- **Semantic versioning:** MAJOR.MINOR.PATCH |
| 96 | +- **Breaking changes:** Increment MAJOR |
| 97 | +- **New features:** Increment MINOR |
| 98 | +- **Bug fixes:** Increment PATCH |
| 99 | + |
| 100 | +## Common Issues |
| 101 | + |
| 102 | +1. **ES Module errors:** Ensure using `node --experimental-vm-modules` for Jest |
| 103 | +2. **pnpm not found:** Install with `npm install -g pnpm` |
| 104 | +3. **Permission errors:** Ensure `src/context-status.js` is executable (`chmod +x`) |
| 105 | +4. **Path issues:** Use absolute paths in Claude Code configuration |
| 106 | + |
| 107 | +## Contributing Guidelines |
| 108 | + |
| 109 | +When making changes: |
| 110 | + |
| 111 | +1. **Run tests first:** `pnpm test` |
| 112 | +2. **Check security:** Review any new file access or input handling |
| 113 | +3. **Lint code:** `pnpm run lint:fix` |
| 114 | +4. **Update tests:** Add tests for new functionality |
| 115 | +5. **Performance check:** Run `pnpm run benchmark` if affecting performance |
| 116 | +6. **Update docs:** Update this file and README.md as needed |
| 117 | + |
| 118 | +## Debugging Methodology |
| 119 | + |
| 120 | +When tests fail unexpectedly (especially when functions return 0 instead of expected values): |
| 121 | + |
| 122 | +1. **Systematic Layer-by-Layer Debugging:** |
| 123 | + - Add `console.error('[DEBUG]')` logging at each major function entry/exit |
| 124 | + - Trace the exact failure point rather than assuming the problem location |
| 125 | + - Verify inputs, intermediate values, and outputs at each stage |
| 126 | + |
| 127 | +2. **Common Test Issues:** |
| 128 | + - **Newline characters**: Ensure test files use `'\n'` not `'\\n'` for proper JSONL format |
| 129 | + - **File paths**: Verify test files are within security boundaries (project directory) |
| 130 | + - **File content**: Log actual file contents vs expected format during debugging |
| 131 | + |
| 132 | +3. **Isolation Testing:** |
| 133 | + - Create minimal reproduction scripts outside Jest environment |
| 134 | + - Test core functions with known-good inputs to verify baseline functionality |
| 135 | + - Use single-test execution: `--testNamePattern="specific test name"` |
| 136 | + |
| 137 | +4. **Example Debug Session:** |
| 138 | + ```bash |
| 139 | + # Add debug logging to problematic function |
| 140 | + # Run isolated test to see exact failure point |
| 141 | + node --experimental-vm-modules node_modules/jest/bin/jest.js --testNamePattern="failing test" |
| 142 | + # Remove debug logging after fix is confirmed |
| 143 | + ``` |
| 144 | + |
| 145 | +## AI Assistant Notes |
| 146 | + |
| 147 | +- This is a security-hardened project - be cautious with file access and input validation |
| 148 | +- Performance is critical - the script runs frequently in Claude Code's status line |
| 149 | +- ES modules with Jest require the `--experimental-vm-modules` flag |
| 150 | +- Always use pnpm, not npm, for package management |
| 151 | +- The main script must remain at root level for proper package.json bin configuration |
0 commit comments