Skip to content

Commit 1918435

Browse files
efoutsclaude
andcommitted
feat: AWS Bedrock context status line for Claude Code
Complete implementation of a context window usage display for AWS Bedrock users who lack access to the `/context` command in Claude Code. - Real-time context token display in status line (e.g., "Context: 125.4k") - Single-script Node.js solution (src/context-status.js) - Comprehensive Jest test suite with 100% pass rate - MIT licensed for broad compatibility 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
0 parents  commit 1918435

File tree

12 files changed

+4200
-0
lines changed

12 files changed

+4200
-0
lines changed

.claude/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"statusLine": {
3+
"type": "command",
4+
"command": "node ./src/context-status.js"
5+
}
6+
}

.eslintignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules/
2+
*.min.js
3+
coverage/
4+
dist/
5+
scripts/build.js
6+
scripts/test*.js
7+
scripts/lint.js
8+
scripts/benchmark.js

.eslintrc.cjs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
module.exports = {
2+
env: {
3+
node: true,
4+
es2022: true,
5+
jest: true
6+
},
7+
extends: [
8+
'eslint:recommended'
9+
],
10+
parserOptions: {
11+
ecmaVersion: 2022,
12+
sourceType: 'module'
13+
},
14+
rules: {
15+
// Security-focused rules (manual configuration for better control)
16+
'no-eval': 'error',
17+
'no-implied-eval': 'error',
18+
'no-new-func': 'error',
19+
'no-script-url': 'error',
20+
'no-proto': 'error',
21+
'no-iterator': 'error',
22+
'no-with': 'error',
23+
24+
// Prevent common vulnerabilities
25+
'no-console': ['warn', { allow: ['error', 'warn'] }],
26+
'no-debugger': 'error',
27+
'no-alert': 'error',
28+
29+
// Variable declarations - strict
30+
'no-undef': 'error',
31+
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
32+
'no-use-before-define': ['error', { functions: false }],
33+
'prefer-const': 'error',
34+
'no-var': 'error',
35+
36+
// Functions - security and quality
37+
'no-param-reassign': 'warn',
38+
'consistent-return': 'error',
39+
'no-return-assign': 'error',
40+
'no-return-await': 'error',
41+
42+
// Async/await best practices
43+
'require-await': 'error',
44+
'no-async-promise-executor': 'error',
45+
'no-await-in-loop': 'warn',
46+
'prefer-promise-reject-errors': 'error',
47+
48+
// Error handling - security critical
49+
'handle-callback-err': 'error',
50+
'no-throw-literal': 'error',
51+
52+
// Best practices - prevent bugs and security issues
53+
'eqeqeq': ['error', 'always'],
54+
'no-eq-null': 'error',
55+
'curly': ['error', 'all'],
56+
'dot-notation': 'error',
57+
'guard-for-in': 'error',
58+
'no-caller': 'error',
59+
'no-extend-native': 'error',
60+
'no-extra-bind': 'error',
61+
'no-fallthrough': 'error',
62+
'no-floating-decimal': 'error',
63+
'no-implicit-coercion': 'error',
64+
'no-implicit-globals': 'error',
65+
'no-lone-blocks': 'error',
66+
'no-loop-func': 'error',
67+
'no-multi-spaces': 'error',
68+
'no-new': 'error',
69+
'no-new-wrappers': 'error',
70+
'no-octal-escape': 'error',
71+
'no-self-compare': 'error',
72+
'no-sequences': 'error',
73+
'no-unmodified-loop-condition': 'error',
74+
'no-useless-call': 'error',
75+
'no-useless-concat': 'error',
76+
'radix': 'error',
77+
'wrap-iife': 'error',
78+
'yoda': 'error',
79+
80+
// Code style - minimal but important
81+
'indent': ['error', 2, { SwitchCase: 1 }],
82+
'quotes': ['error', 'single', { avoidEscape: true }],
83+
'semi': ['error', 'always'],
84+
'comma-dangle': ['error', 'never'],
85+
'max-len': ['warn', {
86+
code: 120,
87+
ignoreUrls: true,
88+
ignoreStrings: true,
89+
ignoreTemplateLiterals: true
90+
}],
91+
92+
// Import/Export
93+
'no-duplicate-imports': 'error',
94+
95+
// Node.js specific security
96+
'no-process-exit': 'warn',
97+
'no-sync': 'warn'
98+
},
99+
overrides: [
100+
{
101+
// Test files can be more lenient
102+
files: ['**/tests/**/*.js', '**/*.test.js', '**/*.spec.js'],
103+
rules: {
104+
'no-console': 'off',
105+
'max-len': 'off',
106+
'no-sync': 'off'
107+
}
108+
}
109+
]
110+
};

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dependencies
2+
node_modules/
3+
.pnpm-store/
4+
5+
# Test artifacts and reports
6+
coverage/
7+
test-results/
8+
.nyc_output/
9+
junit.xml
10+
11+
# Test files generated during test runs
12+
*.jsonl
13+
14+
# Lock files (keep only pnpm-lock.yaml)
15+
package-lock.json
16+
yarn.lock
17+
18+
# IDE/Editor Files
19+
.vscode/
20+
.idea/
21+
*.swp
22+
*.swo
23+
settings.local.json
24+
25+
# System Files
26+
.DS_Store
27+
28+
# Logs
29+
*.log

CLAUDE.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 This Dot, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Claude Code Context Status Line for AWS Bedrock Users
2+
3+
A minimal, self-contained JavaScript script for displaying context window usage in Claude Code's status line. **Specifically designed for AWS Bedrock users who have lost access to the `/context` command** in Claude Code and need to restore visibility into their context window usage.
4+
5+
## What It Does
6+
7+
Displays your current context window usage in a clean, minimal format directly in your status line:
8+
9+
```
10+
Context: 125.4k
11+
```
12+
13+
## Installation
14+
15+
### Method 1: NPX (Recommended)
16+
17+
Add this to your Claude Code settings to use the package directly:
18+
19+
```json
20+
{
21+
"statusLine": {
22+
"type": "command",
23+
"command": "npx @thisdot/claude-code-context-status-line"
24+
}
25+
}
26+
```
27+
28+
This runs the script directly from npm each time without creating local files.
29+
30+
### Method 2: Manual Installation
31+
32+
1. Download the script directly:
33+
34+
```bash
35+
curl -o context-status.js https://raw.githubusercontent.com/thisdot/claude-code-context-status-line/main/src/context-status.js
36+
```
37+
38+
2. Add to your `~/.claude/settings.json`:
39+
40+
```json
41+
{
42+
"statusLine": {
43+
"type": "command",
44+
"command": "node /full/path/to/src/context-status.js"
45+
}
46+
}
47+
```
48+
49+
## Requirements
50+
51+
- **Node.js**: Version 18 or higher (same as Claude Code)
52+
- **Claude Code**: Any version supporting status line configuration
53+
54+
## License
55+
56+
MIT License - see [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)