fix(install): chmod sdk dist/cli.js executable; fix context monitor over-reporting (#2460)
Bug #2453: After tsc builds sdk/dist/cli.js, npm install -g from a local directory does not chmod the bin-script target (unlike tarball extraction). The file lands at mode 644, the gsd-sdk symlink points at a non-executable file, and command -v gsd-sdk fails on every first install. Fix: explicitly chmodSync(cliPath, 0o755) immediately after npm install -g completes, mirroring the pattern used for hook files throughout the installer. Bug #2451: gsd-context-monitor warning messages over-reported usage by ~13 percentage points vs CC native /context. Root cause: gsd-statusline.js wrote a buffer-normalized used_pct (accounting for the 16.5% autocompact reserve) to the bridge file, inflating values. The bridge used_pct is now raw (Math.round(100 - remaining_percentage)), consistent with what CC's native /context command reports. The statusline progress bar continues to display the normalized value; only the bridge value changes. Updated the existing #2219 tests to check the normalized display via hook stdout rather than bridge.used_pct, and added a new assertion that bridge.used_pct is raw. Closes #2453 Closes #2451 Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -255,8 +255,12 @@ describe('context meter respects CLAUDE_CODE_AUTO_COMPACT_WINDOW (#2219)', () =>
|
||||
const hookPath = path.join(__dirname, '..', 'hooks', 'gsd-statusline.js');
|
||||
|
||||
/**
|
||||
* Run the statusline hook with a synthetic context_window payload and
|
||||
* return the used_pct written to the bridge file.
|
||||
* Run the statusline hook with a synthetic context_window payload.
|
||||
* Returns { normalizedUsed, rawUsedPct } where:
|
||||
* - normalizedUsed: the buffer-adjusted % shown in the statusline bar
|
||||
* (parsed from the hook's stdout ANSI output, e.g. "60%")
|
||||
* - rawUsedPct: the raw value written to the bridge file (100 - remaining,
|
||||
* CC-consistent per #2451 fix)
|
||||
*/
|
||||
function runHook(remainingPct, totalTokens, acwEnv) {
|
||||
const sessionId = `test-2219-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
@@ -277,48 +281,70 @@ describe('context meter respects CLAUDE_CODE_AUTO_COMPACT_WINDOW (#2219)', () =>
|
||||
delete env.CLAUDE_CODE_AUTO_COMPACT_WINDOW;
|
||||
}
|
||||
|
||||
let stdout = '';
|
||||
try {
|
||||
execFileSync(process.execPath, [hookPath], {
|
||||
stdout = execFileSync(process.execPath, [hookPath], {
|
||||
input: payload,
|
||||
env,
|
||||
encoding: 'utf8',
|
||||
timeout: 4000,
|
||||
});
|
||||
} catch (e) {
|
||||
// Non-zero exit is fine — hook exits 0 on success, but we're reading
|
||||
// the bridge file, not the exit code. Ignore exit failures here.
|
||||
stdout = e.stdout || '';
|
||||
}
|
||||
|
||||
// Parse normalized used% from the statusline bar output (e.g. "60%")
|
||||
// Strip ANSI escape codes then extract the percentage digit(s) before "%"
|
||||
const clean = stdout.replace(/\x1b\[[0-9;]*m/g, '');
|
||||
const match = clean.match(/(\d+)%/);
|
||||
const normalizedUsed = match ? parseInt(match[1], 10) : null;
|
||||
|
||||
// Read raw used_pct from the bridge file (#2451: bridge stores raw CC value)
|
||||
const bridgePath = path.join(os.tmpdir(), `claude-ctx-${sessionId}.json`);
|
||||
const bridge = JSON.parse(fs.readFileSync(bridgePath, 'utf8'));
|
||||
fs.unlinkSync(bridgePath);
|
||||
return bridge.used_pct;
|
||||
let rawUsedPct = null;
|
||||
try {
|
||||
const bridge = JSON.parse(fs.readFileSync(bridgePath, 'utf8'));
|
||||
rawUsedPct = bridge.used_pct;
|
||||
fs.unlinkSync(bridgePath);
|
||||
} catch { /* bridge may not exist if hook exited early */ }
|
||||
|
||||
return { normalizedUsed, rawUsedPct };
|
||||
}
|
||||
|
||||
test('default buffer (no env var): 50% remaining → ~60% used', () => {
|
||||
test('default buffer (no env var): 50% remaining → ~60% normalized bar display', () => {
|
||||
// Default 16.5% buffer: usableRemaining = (50 - 16.5) / (100 - 16.5) * 100 ≈ 40.12%
|
||||
// used ≈ 100 - 40.12 = 59.88 → rounded 60
|
||||
const used = runHook(50, 1_000_000, null);
|
||||
assert.strictEqual(used, 60);
|
||||
// normalized used ≈ 100 - 40.12 = 59.88 → rounded 60 (shown in statusline bar)
|
||||
const { normalizedUsed } = runHook(50, 1_000_000, null);
|
||||
assert.strictEqual(normalizedUsed, 60);
|
||||
});
|
||||
|
||||
test('CLAUDE_CODE_AUTO_COMPACT_WINDOW=400000: 50% remaining → ~83% used', () => {
|
||||
test('CLAUDE_CODE_AUTO_COMPACT_WINDOW=400000: 50% remaining → ~83% normalized bar display', () => {
|
||||
// With 1M total, 400k window → buffer = 40%. usableRemaining = (50 - 40) / (100 - 40) * 100 ≈ 16.67%
|
||||
// used ≈ 100 - 16.67 = 83.33 → rounded 83
|
||||
const used = runHook(50, 1_000_000, 400_000);
|
||||
assert.strictEqual(used, 83);
|
||||
// normalized used ≈ 100 - 16.67 = 83.33 → rounded 83 (shown in statusline bar)
|
||||
const { normalizedUsed } = runHook(50, 1_000_000, 400_000);
|
||||
assert.strictEqual(normalizedUsed, 83);
|
||||
});
|
||||
|
||||
test('CLAUDE_CODE_AUTO_COMPACT_WINDOW=0 falls back to default buffer', () => {
|
||||
// Explicit "0" means unset — should behave like no env var (16.5% buffer)
|
||||
const used = runHook(50, 1_000_000, 0);
|
||||
assert.strictEqual(used, 60);
|
||||
const { normalizedUsed } = runHook(50, 1_000_000, 0);
|
||||
assert.strictEqual(normalizedUsed, 60);
|
||||
});
|
||||
|
||||
test('buffer capped at 100% when ACW exceeds total context', () => {
|
||||
// Pathological: ACW > totalCtx → buffer = 100%. With no usable range left,
|
||||
// usableRemaining = max(0, (50-100)/(100-100)*100) = max(0, -Inf) = 0,
|
||||
// so used = 100 (context reported as completely full).
|
||||
const used = runHook(50, 1_000_000, 2_000_000);
|
||||
assert.strictEqual(used, 100);
|
||||
// so normalized used = 100 (context reported as completely full in bar).
|
||||
const { normalizedUsed } = runHook(50, 1_000_000, 2_000_000);
|
||||
assert.strictEqual(normalizedUsed, 100);
|
||||
});
|
||||
|
||||
test('bridge used_pct is raw (CC-consistent) regardless of ACW setting (#2451)', () => {
|
||||
// Fix for #2451: bridge used_pct must be raw (100 - remaining), not normalized.
|
||||
// This ensures gsd-context-monitor warning messages match CC native /context.
|
||||
// The ACW normalization only affects the statusline bar display, not the bridge.
|
||||
const { rawUsedPct } = runHook(50, 1_000_000, 400_000);
|
||||
assert.strictEqual(rawUsedPct, 50,
|
||||
'bridge used_pct must be raw (100-50=50) regardless of CLAUDE_CODE_AUTO_COMPACT_WINDOW');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user