Skip to content

Commit e18a1ef

Browse files
committed
fix: tighten types, remove dead exports, prevent FD leak
1 parent 7594fa0 commit e18a1ef

5 files changed

Lines changed: 25 additions & 26 deletions

File tree

src/discovery-cache.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ async function atomicWriteJson(path: string, value: unknown): Promise<void> {
143143
} catch (err) {
144144
try {
145145
await unlink(temp)
146-
} catch {
147-
// ignore cleanup failures
148-
}
146+
} catch {}
149147
throw err
150148
}
151149
}

src/fs-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,7 @@ export async function* readSessionLinesFromOffset(filePath: string, startOffset:
117117
for await (const line of rl) yield line
118118
} catch (err) {
119119
warn(`stream read failed for ${filePath}: ${(err as NodeJS.ErrnoException).code ?? 'unknown'}`)
120+
} finally {
121+
stream.destroy()
120122
}
121123
}

src/parse-progress.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Chalk } from 'chalk'
21
import { stripVTControlCharacters } from 'node:util'
32

3+
import { Chalk } from 'chalk'
4+
45
import type { SourceProgressReporter } from './parser.js'
56
import { providerColor, providerLabel } from './provider-colors.js'
67

src/parser.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,19 @@ async function evaluateSourceManifestState(
655655

656656
if (!manifestEntry) {
657657
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'missing-entry' }
658-
logCacheDebug(source.provider, source.path, state.reason)
658+
logCacheDebug(source.provider, source.path, state.reason!)
659659
return state
660660
}
661661

662662
if (manifestEntry.lastSeenParserVersion !== parserVersion) {
663663
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'parser-version' }
664-
logCacheDebug(source.provider, source.path, state.reason)
664+
logCacheDebug(source.provider, source.path, state.reason!)
665665
return state
666666
}
667667

668668
if (source.cacheStrategy && manifestEntry.cacheStrategy && source.cacheStrategy !== manifestEntry.cacheStrategy) {
669669
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'parser-version' }
670-
logCacheDebug(source.provider, source.path, state.reason)
670+
logCacheDebug(source.provider, source.path, state.reason!)
671671
return state
672672
}
673673

@@ -678,14 +678,14 @@ async function evaluateSourceManifestState(
678678

679679
if (!manifestEntry.fingerprint || manifestEntry.fingerprintPath !== fingerprintPath) {
680680
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'fingerprint-miss' }
681-
logCacheDebug(source.provider, source.path, state.reason)
681+
logCacheDebug(source.provider, source.path, state.reason!)
682682
return state
683683
}
684684

685685
const currentFingerprint = await computeFileFingerprint(fingerprintPath).catch(() => null)
686686
if (!currentFingerprint) {
687687
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'fingerprint-miss' }
688-
logCacheDebug(source.provider, source.path, state.reason)
688+
logCacheDebug(source.provider, source.path, state.reason!)
689689
return state
690690
}
691691

@@ -720,7 +720,7 @@ async function evaluateSourceManifestState(
720720
}
721721

722722
const state: SourceManifestState = { source, parserVersion, manifestEntry, action: 'refresh', reason: 'fingerprint-miss', currentFingerprint }
723-
logCacheDebug(source.provider, source.path, state.reason)
723+
logCacheDebug(source.provider, source.path, state.reason!)
724724
return state
725725
}
726726

@@ -779,7 +779,7 @@ async function refreshClaudeCacheUnit(
779779
&& !!cached.appendState
780780
&& cached.sessions.length > 0
781781
&& !!state.currentFingerprint
782-
if (shouldUseAppendOnly && manifestAppendState) {
782+
if (shouldUseAppendOnly && manifestAppendState && cached?.appendState) {
783783
if (
784784
manifestAppendState.tailHash !== cached.appendState.tailHash
785785
|| manifestAppendState.endOffset !== cached.appendState.endOffset
@@ -789,7 +789,7 @@ async function refreshClaudeCacheUnit(
789789
}
790790
}
791791

792-
if (shouldUseAppendOnly && cached) {
792+
if (shouldUseAppendOnly && cached && cached.appendState) {
793793
addSeenDeduplicationKeysFromSessions(cached.sessions, localSeenMsgIds)
794794
const appendedLines: string[] = []
795795
for await (const line of readSessionLinesFromOffset(unit.path, cached.appendState.endOffset)) {

src/source-cache.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ function traceCacheRead(op: string, filePath: string, note?: string): void {
1616

1717
const APPEND_TAIL_WINDOW_BYTES = 16 * 1024
1818

19-
export type SourceCacheStrategy = 'full-reparse' | 'append-jsonl'
19+
type SourceCacheStrategy = 'full-reparse' | 'append-jsonl'
2020

21-
export type SourceFingerprint = {
21+
type SourceFingerprint = {
2222
mtimeMs: number
2323
sizeBytes: number
2424
}
2525

26-
export type AppendState = {
26+
type AppendState = {
2727
endOffset: number
2828
tailHash: string
2929
lastEntryType?: string
@@ -59,18 +59,16 @@ export type SourceCacheManifestEntry = {
5959
appendState?: AppendState
6060
}
6161

62-
export type ReadSourceCacheEntryOptions = {
62+
type ReadSourceCacheEntryOptions = {
6363
allowStaleFingerprint?: boolean
6464
}
6565

66-
export type SourceRange = {
66+
type SourceRange = {
6767
firstTimestamp?: string
6868
lastTimestamp?: string
6969
}
7070

71-
export type CachedSourcePlanHint = SourceCacheManifestEntry & SourceRange
72-
73-
export function sourceCacheKey(provider: string, logicalPath: string): string {
71+
function sourceCacheKey(provider: string, logicalPath: string): string {
7472
return `${provider}:${logicalPath}`
7573
}
7674

@@ -174,18 +172,20 @@ function isParsedTurn(value: unknown): boolean {
174172
&& typeof value.sessionId === 'string'
175173
}
176174

177-
function isModelBreakdownEntry(value: unknown): boolean {
175+
function isModelBreakdownEntry(value: unknown): value is { calls: number; costUSD: number; tokens: TokenUsage } {
178176
return isPlainObject(value)
179177
&& isFiniteNumber(value.calls)
180178
&& isFiniteNumber(value.costUSD)
181179
&& isTokenUsage(value.tokens)
182180
}
183181

184-
function isCallsBreakdownEntry(value: unknown): boolean {
182+
type TokenUsage = { inputTokens: number; outputTokens: number; cacheCreationInputTokens: number; cacheReadInputTokens: number; cachedInputTokens: number; reasoningTokens: number; webSearchRequests: number }
183+
184+
function isCallsBreakdownEntry(value: unknown): value is { calls: number } {
185185
return isPlainObject(value) && isFiniteNumber(value.calls)
186186
}
187187

188-
function isCategoryBreakdownEntry(value: unknown): boolean {
188+
function isCategoryBreakdownEntry(value: unknown): value is { turns: number; costUSD: number; retries: number; editTurns: number; oneShotTurns: number } {
189189
return isPlainObject(value)
190190
&& isFiniteNumber(value.turns)
191191
&& isFiniteNumber(value.costUSD)
@@ -343,9 +343,7 @@ async function atomicWriteJson(path: string, value: unknown): Promise<void> {
343343
} catch (err) {
344344
try {
345345
await unlink(temp)
346-
} catch {
347-
// ignore cleanup failures
348-
}
346+
} catch {}
349347
throw err
350348
}
351349
}

0 commit comments

Comments
 (0)