Repository navigation
Expand file tree
/
Copy pathcompact.ts
More file actions
621 lines (537 loc) · 26.1 KB
/
Copy pathcompact.ts
File metadata and controls
621 lines (537 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/** Session manifest generator for compaction assist. Ports key functions from Python's `token_goat.compact` for TypeScript. */
import * as fs from 'node:fs'
import * as path from 'node:path'
import { detectHarness } from './bridges/registry.js'
import { isAutoTriggerMultiplierExplicit, loadConfig } from './config.js'
import { dataDir } from './constants.js'
import { DEFAULT_MAX_AGE_MS, tokenGoatHome } from './disk_cache.js'
import { ensureDirSync, atomicWriteText, normalizePathForwardSlash } from './util.js'
import { estimateTokens } from './overflow_guard.js'
import { readSessionStateFile, sessionFileStem, AGENT_SALT_MARKER } from './session_store.js'
import type { FileEntry } from './session.js'
import { readTranscriptTail } from './resident_context.js'
// --------------------------------------------------------------------------- Constants ---------------------------------------------------------------------------
export const CONTEXT_AUTOCOMPACT_TOKENS = 660_000
export const CATALOG_TOKENS = 10_800
export const CONTEXT_TIER_WARM = 0.5
export const CONTEXT_TIER_HOT = 0.7
export const CONTEXT_TIER_CRITICAL = 0.85
const NOISE_EXTS = new Set([
'.pyc', '.pyo', '.pyd',
'.class',
'.o', '.obj', '.a', '.lib', '.dll', '.so', '.dylib',
'.log',
'.jsonl',
'.tmp', '.temp', '.swp', '.swo',
'.bak',
'.pid',
'.lock',
'.map',
'.wasm',
'.gz', '.zip', '.tar', '.tgz',
'.db', '.sqlite', '.sqlite3', '.db3',
'.d.ts',
'.snap',
'.eot', '.ttf', '.woff', '.woff2',
'.ico',
'.pdb',
'.exe', '.bin',
])
const NOISE_BASENAMES = new Set([
'.ds_store', 'thumbs.db', 'desktop.ini',
'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml',
'poetry.lock', 'uv.lock', 'pdm.lock',
'cargo.lock',
'composer.lock', 'gemfile.lock',
'coverage.xml', '.coverage', 'lcov.info',
])
const NOISE_SEGMENTS = [
'/__pycache__/', '/.git/', '/node_modules/', '/.venv/', '/venv/',
'/dist/', '/build/', '/.mypy_cache/', '/.pytest_cache/', '/.ruff_cache/',
'/appdata/local/temp/', '/appdata/roaming/',
'/tmp/',
'/.next/', '/.nuxt/', '/.svelte-kit/', '/.turbo/', '/.parcel-cache/',
'/.cache/', '/.tox/',
'/coverage/', '/.nyc_output/',
'/site-packages/', '.egg-info/',
'/target/',
'/__snapshots__/',
]
// Per-harness auto-trigger multiplier defaults for getAutoTriggerMultiplier(). 'openclaw', 'pi', and 'hermes' have no dedicated tuning yet, so they match 'generic' until there's a clear reason to diverge (see bridges/registry.ts::detectHarness for the canonical harness-detection implementation this keys off of).
const HARNESS_MULTIPLIER_DEFAULTS: Record<string, number> = {
claudecode: 2.0,
codex: 1.5,
opencode: 2.5,
gemini: 3.0,
openclaw: 1.0,
pi: 1.0,
hermes: 1.0,
generic: 1.0,
}
// --------------------------------------------------------------------------- Types ---------------------------------------------------------------------------
export interface ContextPressure {
fillFraction: number
tier: 'cool' | 'warm' | 'hot' | 'critical'
}
export interface SessionCacheObject {
loadedSkillTotalTokens?: number
/** Matches the real on-disk shape session_store.ts::SerializedSession actually produces — an array of `[key, id]` pairs (see session.ts's `_webFetches`/`_bashOutputs` maps and their `recordWebFetch`/ `recordBashOutput` writers), not a `bashHistory`/`webHistory` dict shape no writer ever populated. */
webFetches?: Array<[string, string]>
bashOutputs?: Array<[string, string]>
/** Matches the real on-disk shape session_store.ts::saveSessionState writes (`SerializedSession.files: FileEntry[]`) — a flat array, not a path-keyed dict. Each entry's `wasEdited` flag distinguishes edited from read-only files; there is no separate `editedFiles` collection on disk. */
files?: FileEntry[]
symbolAccessCounts?: Record<string, number>
skillHistory?: Record<string, unknown>
/** Unix time in *seconds* the on-disk session cache was first created (written once by session_store.ts::saveSessionState). buildManifestAdaptive derives the session-age budget multiplier from it; undefined for a cache written before this field existed (age then treated as 0). */
created_ts?: number
}
// --------------------------------------------------------------------------- Helpers ---------------------------------------------------------------------------
// --------------------------------------------------------------------------- Core functions ---------------------------------------------------------------------------
// estimateTokens is re-exported from overflow_guard.ts (single canonical implementation) so existing `import { estimateTokens } from './compact.js'` call sites keep working.
export { estimateTokens }
/** Map a context-fill fraction to its qualitative pressure tier. */
export function tierForFraction(fill: number): 'cool' | 'warm' | 'hot' | 'critical' {
if (fill >= CONTEXT_TIER_CRITICAL) return 'critical'
if (fill >= CONTEXT_TIER_HOT) return 'hot'
if (fill >= CONTEXT_TIER_WARM) return 'warm'
return 'cool'
}
/** Return the estimated (never-measured) context pressure total for a cache. Used only when no real transcript measurement is available (see {@link measurePromptTokens}) -- e.g. a non-Claude-Code harness that never delivers a `transcript_path`, or a transcript that is missing/unreadable/carries no `usage` record yet. */
function pressureRawTotal(cache: SessionCacheObject): number {
const skillTokens = cache.loadedSkillTotalTokens ?? 0
const bashCount = (cache.bashOutputs ?? []).length
const webCount = (cache.webFetches ?? []).length
const files = cache.files ?? []
const readCount = files.length
return (
skillTokens +
CATALOG_TOKENS +
bashCount * 500 +
webCount * 1_000 +
readCount * 200
)
}
/** Tail window read for {@link measurePromptTokens}. Sized against a live 445 MB transcript on this machine: 275,377 records, largest single record 792,353 bytes, 11 records over 256 KB. A 256 KB window could therefore land entirely inside one record and find nothing, falling back to the fabricated estimate; 1 MiB clears the observed maximum with room to spare and still costs about half a millisecond per call, measured. It stays a bounded tail read, never a whole-file read. */
const PROMPT_MEASURE_TAIL_BYTES = 1_048_576
/** Pull the `usage` object out of one parsed transcript JSONL record, if present. Claude Code assistant records nest it at `message.usage` (verified against real transcript output from Claude Code 2.1.270 -- see `tests/fixtures/transcript_usage_capture.jsonl`), not at the record's top level. */
function extractUsageTotal(record: unknown): number | null {
if (record === null || typeof record !== 'object') return null
// Name the record being measured rather than taking any record that happens to carry a usage object. Scanned all 275,377 records of a live 445 MB transcript: every usage-bearing record is `type: "assistant"` and none is a sidechain. Re-checked on a second live transcript from a session that spawned many subagents: 59,883 usage records, still zero carrying `isSidechain`, so a subagent's usage does not land in its parent's transcript and no lane selector is needed here.
if ((record as Record<string, unknown>)['type'] !== 'assistant') return null
const message = (record as Record<string, unknown>)['message']
if (message === null || typeof message !== 'object') return null
const usage = (message as Record<string, unknown>)['usage']
if (usage === null || typeof usage !== 'object') return null
const u = usage as Record<string, unknown>
const input = u['input_tokens']
const cacheCreation = u['cache_creation_input_tokens']
const cacheRead = u['cache_read_input_tokens']
const output = u['output_tokens']
if (
typeof input !== 'number' ||
typeof cacheCreation !== 'number' ||
typeof cacheRead !== 'number' ||
typeof output !== 'number'
) {
return null
}
// The three input fields describe the prompt of the request that *produced* this record; the reply it produced is resident in the next one. `output_tokens` is required alongside them rather than defaulted to zero: it is present on all 59,900 usage records of a live transcript, so a record missing it is a shape this reader has never seen and should decline rather than silently under-report. Measured across 31,758 consecutive record pairs of a live transcript: 31,430 (99.0%) have the next total at or above this total plus this record's output, and the 328 below it are compactions and branch resets, where the whole total drops. Omitting output therefore reports a prompt one reply short of the real one -- typically 424 tokens, but up to 20,276 observed, which is 3% of the auto-compact window at the moment the tier matters most.
return input + cacheCreation + cacheRead + output
}
/** Measure the current prompt size (in tokens) from the harness's own transcript, rather than fabricating it from cumulative tool-call counts (see {@link pressureRawTotal}). Reads a bounded tail of `transcriptPath` (see {@link PROMPT_MEASURE_TAIL_BYTES}) and returns the sum of `input_tokens + cache_creation_input_tokens + cache_read_input_tokens + output_tokens` from the *last* parseable `usage` record found there -- that sum already reflects every compaction that has happened in the session, so no baseline subtraction is needed on top of it. Returns null (never throws) when the file is missing, empty, unreadable, or carries no usage record in the tail window. */
export function measurePromptTokens(transcriptPath: string): number | null {
try {
const lines = readTranscriptTail(transcriptPath, PROMPT_MEASURE_TAIL_BYTES)
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i]
if (line === undefined) continue
const trimmed = line.trim()
if (trimmed === '') continue
let parsed: unknown
try {
parsed = JSON.parse(trimmed)
} catch {
continue
}
const total = extractUsageTotal(parsed)
if (total !== null) return total
}
return null
} catch {
return null
}
}
/** Return the estimated context fill fraction and pressure tier. */
// Effective auto-compact window, scaled by the harness-tuned (or user-overridden) multiplier: different harnesses reach their own real auto-compact point at very different token counts, so CONTEXT_AUTOCOMPACT_TOKENS (Claude Code's own figure) needs scaling before it means anything for other harnesses.
function getEffectiveAutoTriggerWindow(): number {
const ca = loadConfig().compact_assist
const isConfigDefault = !isAutoTriggerMultiplierExplicit()
const multiplier = getAutoTriggerMultiplier(
ca.harness === 'auto'
? { configExplicitMultiplier: ca.auto_trigger_multiplier, isConfigDefault }
: { configExplicitMultiplier: ca.auto_trigger_multiplier, harness: ca.harness, isConfigDefault },
)
return CONTEXT_AUTOCOMPACT_TOKENS * multiplier
}
export function getContextPressure(cache?: SessionCacheObject, transcriptPath?: string): ContextPressure {
try {
const window = getEffectiveAutoTriggerWindow()
// A real measurement from the harness's own transcript IS the total: it already reflects every compaction and every loaded skill/catalog, so nothing is added or subtracted on top of it. Only fall back to the fabricated estimate below when no measurement is available at all (no transcript path given, file unreadable, no usage record in the tail window) -- that keeps non-Claude-Code harnesses and cache-only unit tests working.
if (transcriptPath) {
const measured = measurePromptTokens(transcriptPath)
if (measured !== null) {
const fill = measured / window
return { fillFraction: fill, tier: tierForFraction(fill) }
}
}
if (!cache) {
return { fillFraction: 0.0, tier: 'cool' }
}
const total = pressureRawTotal(cache)
const fill = total / window
return {
fillFraction: fill,
tier: tierForFraction(fill),
}
} catch {
return { fillFraction: 0.0, tier: 'cool' }
}
}
/**
* Give the PreCompact summarizer pressure-tiered rules for rolling context cleanup.
* The recovery channel is intentionally excluded: it runs after the summary is written
* and carries declarative metadata, not instructions.
*/
export function getRollingCompactionDirective(pressure: ContextPressure): string {
const ttlHours = DEFAULT_MAX_AGE_MS / (60 * 60 * 1000)
const ttl = Number.isInteger(ttlHours) ? `${ttlHours}h` : `${ttlHours.toFixed(1)}h`
const pressureRule: Record<ContextPressure['tier'], string> = {
cool: 'Retain recent stdout, test logs, and read-cache bodies; omit only byte-identical output duplicates.',
warm: 'Replace successful test logs with command, final result, and counts; retain failure output, failure diagnostics, and unique diagnostics. For repeated unchanged reads, keep only the latest unchanged whole-file read.',
hot: 'Discard superseded successful test logs and keep only a concise latest result per test command, plus unresolved failures and unique diagnostics. Collapse repeated reads of unchanged files, keeping only the latest unchanged whole-file read.',
critical: 'At critical pressure, discard raw test logs and stdout once their outcomes are summarized; retain unresolved failure evidence and unique diagnostics. Replace repeated unchanged read bodies with one concise summary.',
}
return [
`### Rolling compaction (${pressure.tier} context pressure)`,
`Remove expired tool stdout receipts (past the ${ttl} cache TTL) and receipts no longer recallable; do not preserve dead receipt IDs.`,
pressureRule[pressure.tier],
'Never prune edited-file state or surgical reads.',
'Always preserve core findings, decisions, notes, user constraints, active task state (goal, status, blockers, and next steps), and unique failure evidence.',
].join('\n')
}
/** Get the effective auto_trigger_multiplier for the detected harness. */
export function getAutoTriggerMultiplier(opts?: {
configExplicitMultiplier?: number
harness?: string
isConfigDefault?: boolean
}): number {
const config = opts?.configExplicitMultiplier ?? 2.0
let isDefault = opts?.isConfigDefault
if (isDefault === undefined) {
isDefault = config === 2.0
}
if (!isDefault && opts?.configExplicitMultiplier !== undefined) {
return Math.max(1.0, Math.min(10.0, opts.configExplicitMultiplier))
}
const harness = opts?.harness ?? detectHarness()
return Math.max(1.0, Math.min(10.0, HARNESS_MULTIPLIER_DEFAULTS[harness] ?? 1.0))
}
/** Return True when path should be excluded from the manifest as low-value noise. */
export function isNoisePath(inputPath: string): boolean {
if (!inputPath) {
return false
}
const p = normalizePathForwardSlash(inputPath, true)
for (const segment of NOISE_SEGMENTS) {
if (p.includes(segment)) {
return true
}
}
const slashIdx = p.lastIndexOf('/')
const basename = slashIdx >= 0 ? p.slice(slashIdx + 1) : p
if (NOISE_BASENAMES.has(basename)) {
return true
}
if (basename.startsWith('.improve-state-') || basename.startsWith('improve_commit_msg_')) {
return true
}
const dotIdx = basename.lastIndexOf('.')
if (dotIdx >= 0) {
const ext = basename.slice(dotIdx)
if (NOISE_EXTS.has(ext)) {
return true
}
}
for (const ext of NOISE_EXTS) {
if (ext.includes('.') && ext.split('.').length > 2) {
if (basename.endsWith(ext)) {
return true
}
}
}
return false
}
/** Return the session_id of the most-recently-modified session file. Excludes agent-salted blobs (filenames containing {@link AGENT_SALT_MARKER}, the sanitized form of relay.ts's `sessionStateKey` `:agent:` separator): a subagent's blob is frequently the newest file on disk (subagents run after the parent's own last tool call), so without this filter, "latest session" for a caller that gave no explicit session id could resolve to a narrow subagent-scoped ledger instead of the genuine parent/top-level session. */
export function findLatestSessionId(): string | null {
try {
const sessionsDir = path.join(tokenGoatHome(), 'sessions')
if (!fs.existsSync(sessionsDir)) {
return null
}
const files = fs.readdirSync(sessionsDir)
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.includes(AGENT_SALT_MARKER))
if (jsonFiles.length === 0) {
return null
}
const firstFile = jsonFiles[0]
if (!firstFile) {
return null
}
let latestFile = firstFile
let latestMtime = fs.statSync(path.join(sessionsDir, firstFile)).mtimeMs
for (const file of jsonFiles) {
const mtime = fs.statSync(path.join(sessionsDir, file)).mtimeMs
if (mtime > latestMtime) {
latestFile = file
latestMtime = mtime
}
}
return latestFile.replace(/\.json$/, '')
} catch {
return null
}
}
/** Count tracked events (reads + greps + edits + bash runs + web fetches) for a session. */
export function eventCount(cache: SessionCacheObject): number {
const files = cache.files ?? []
const editedCount = files.filter((f) => f.wasEdited).length
const bashCount = (cache.bashOutputs ?? []).length
const webCount = (cache.webFetches ?? []).length
const skillHistory = cache.skillHistory ?? {}
return (
files.length +
editedCount +
bashCount +
webCount +
Object.keys(skillHistory).length
)
}
/** Strip the trailing "# as-of: ..." line so two manifests built at different wall-clock times from identical session content compare as byte-equal. */
export function normalizeForCache(manifestText: string): string {
const lines = manifestText.trim().split('\n')
if (lines.length > 0) {
const lastLine = lines[lines.length - 1]
if (lastLine && lastLine.startsWith('# as-of:')) {
return lines.slice(0, -1).join('\n')
}
}
return manifestText
}
/** Write per-session manifest JSON for cross-session deduplication. */
export function writeSessionManifest(
projectHash: string,
sessionId: string,
manifestJson: Record<string, unknown>
): void {
// Spell the filename with session_store's stem builder rather than a bare 64-char slice. The key passed in is sessionStateKey(event), so for a subagent it is `<sessionId>:agent:<agentId>`, and slicing that at 64 cuts the agent id -- or, once the session id reaches 58 sanitized characters, the `_agent_` marker itself -- so sibling subagents collapse onto one manifest. sessionFileStem hashes the agent id instead, which is the fix session_store.ts already carries for its own blobs.
const safeSessionId = sessionFileStem(sessionId)
if (!safeSessionId) return
const sessionsDir = path.join(dataDir(), 'projects', projectHash, 'sessions')
if (!fs.existsSync(sessionsDir)) {
ensureDirSync(sessionsDir)
}
const dest = path.join(sessionsDir, `${safeSessionId}.json`)
atomicWriteText(dest, JSON.stringify(manifestJson))
manifestParseCache.delete(dest)
}
const MANIFEST_CACHE_MAX_ENTRIES = 100
const MAX_SESSIONS_READ = 20
const CLEANUP_THROTTLE_SECS = 300
interface CachedManifestEntry {
mtimeMs: number
ctimeMs: number
size: number
data: Record<string, unknown> | null
}
const manifestParseCache = new Map<string, CachedManifestEntry>()
const lastCleanupTimeByProject = new Map<string, number>()
/** Clear in-memory manifest cache (exposed for testing). */
export function _clearManifestParseCache(): void {
manifestParseCache.clear()
lastCleanupTimeByProject.clear()
}
function cacheManifest(fullPath: string, entry: CachedManifestEntry): void {
manifestParseCache.delete(fullPath)
if (manifestParseCache.size >= MANIFEST_CACHE_MAX_ENTRIES) {
const oldestKey = manifestParseCache.keys().next().value
if (oldestKey !== undefined) manifestParseCache.delete(oldestKey)
}
manifestParseCache.set(fullPath, entry)
}
/** Read all session manifest JSON files for projectHash, skipping stale and corrupt entries. */
export function readAllSessionManifests(
projectHash: string,
maxAgeSecs: number = 3600
): Record<string, unknown>[] {
const sessionsDir = path.join(dataDir(), 'projects', projectHash, 'sessions')
if (!fs.existsSync(sessionsDir)) {
return []
}
const now = Date.now() / 1000
const results: Record<string, unknown>[] = []
try {
const files = fs.readdirSync(sessionsDir)
const jsonFiles = files.filter((file) => file.endsWith('.json'))
const lastCleanup = lastCleanupTimeByProject.get(projectHash) ?? 0
const shouldCleanup = now - lastCleanup > CLEANUP_THROTTLE_SECS
if (shouldCleanup) {
lastCleanupTimeByProject.set(projectHash, now)
}
interface ManifestCandidate {
fullPath: string
mtimeMs: number
ctimeMs: number
size: number
}
const candidates: ManifestCandidate[] = []
for (const file of jsonFiles) {
const fullPath = path.join(sessionsDir, file)
try {
const stat = fs.statSync(fullPath)
const ageSecs = now - stat.mtimeMs / 1000
if (ageSecs > maxAgeSecs) {
if (shouldCleanup) {
try {
fs.unlinkSync(fullPath)
manifestParseCache.delete(fullPath)
} catch {
// ignore cleanup failure
}
}
continue
}
candidates.push({ fullPath, mtimeMs: stat.mtimeMs, ctimeMs: stat.ctimeMs, size: stat.size })
} catch {
// file unlinked or inaccessible
}
}
candidates.sort((a, b) => b.mtimeMs - a.mtimeMs)
const activeCandidates = candidates.slice(0, MAX_SESSIONS_READ)
for (const candidate of activeCandidates) {
const { fullPath, mtimeMs, ctimeMs, size } = candidate
const cached = manifestParseCache.get(fullPath)
if (cached && cached.mtimeMs === mtimeMs && cached.ctimeMs === ctimeMs && cached.size === size) {
// Map insertion order is the LRU order; refresh hot manifests before a later eviction.
manifestParseCache.delete(fullPath)
manifestParseCache.set(fullPath, cached)
if (cached.data !== null) {
results.push(cached.data)
}
continue
}
try {
const text = fs.readFileSync(fullPath, 'utf8')
const data = JSON.parse(text)
if (typeof data === 'object' && data !== null && 'files' in data) {
cacheManifest(fullPath, { mtimeMs, ctimeMs, size, data })
results.push(data)
} else {
cacheManifest(fullPath, { mtimeMs, ctimeMs, size, data: null })
}
} catch {
cacheManifest(fullPath, { mtimeMs, ctimeMs, size, data: null })
}
}
} catch {
// Silently fail if directory not accessible
}
return results
}
// --------------------------------------------------------------------------- Helpers for computing adaptive budget ---------------------------------------------------------------------------
function _editedFileCount(cache: SessionCacheObject): number {
return (cache.files ?? []).filter((f) => f.wasEdited).length
}
function _computeActivityMultiplier(ageSecs: number, editedCount: number): number {
const TEN_MIN_SECS = 600
const SIXTY_MIN_SECS = 3600
const EDITS_PER_MIN_DENSITY_THRESHOLD = 0.3
let tier: 'young' | 'active' | 'mature'
if (ageSecs < TEN_MIN_SECS) {
tier = 'young'
} else if (ageSecs < SIXTY_MIN_SECS) {
tier = 'active'
} else {
tier = 'mature'
}
const baseFactor: Record<typeof tier, number> = {
young: 0.6,
active: 1.0,
mature: 1.4,
}
let factor = baseFactor[tier]
if (ageSecs >= TEN_MIN_SECS) {
const editsPerMin = ageSecs > 0 ? editedCount / (ageSecs / 60) : 0
if (editsPerMin < EDITS_PER_MIN_DENSITY_THRESHOLD) {
factor = Math.min(factor, 1.0)
}
}
return factor
}
// --------------------------------------------------------------------------- Load session cache from disk ---------------------------------------------------------------------------
export function loadSessionCache(sessionId: string): SessionCacheObject | null {
// Reuse session_store.ts's own read/coercion (readSessionStateFile) instead of re-parsing the JSON here: it already normalizes both the current FileEntry[] array format and the legacy Python path-keyed dict format, and reads from the same tokenGoatHome()-based path saveSessionState writes to.
const disk = readSessionStateFile(sessionId)
if (!disk) {
return null
}
return {
files: disk.files,
webFetches: disk.webFetches,
bashOutputs: disk.bashOutputs,
...(disk.created_ts !== undefined ? { created_ts: disk.created_ts } : {}),
}
}
// --------------------------------------------------------------------------- Core functions ---------------------------------------------------------------------------
/** Compute adaptive token budget for manifest based on session complexity. Returns value in range [200, 800], capped by context pressure tier. */
export function computeAdaptiveBudget(
cache: SessionCacheObject,
ageSecs: number = 0.0,
opts?: {
hasPendingDiff?: boolean
hasUncommittedChanges?: boolean
staleCompactFraction?: number
contextPressure?: ContextPressure
}
): number {
const base = 200
const maxTotal = 800
const minTotal = 200
const editedCount = _editedFileCount(cache)
const editedBonus = Math.min(200, editedCount * 50)
const files = cache.files ?? []
const symbolFiles = files.filter((f) => {
const entry = f as unknown as Record<string, unknown>
return ((entry['symbols_read'] as unknown[]) ?? []).length > 0
}).length
const symbolsBonus = Math.min(150, symbolFiles * 30)
const bashCount = (cache.bashOutputs ?? []).length
const bashBonus = bashCount > 0 ? Math.min(100, Math.max(20, bashCount * 5)) : 0
const webBonus = (cache.webFetches ?? []).length > 0 ? 15 : 0
const diffBonus = opts?.hasPendingDiff ? 50 : 0
const uncommittedBonus = opts?.hasUncommittedChanges ? 10 : 0
const staleFrac = Math.max(0.0, Math.min(1.0, opts?.staleCompactFraction ?? 0.0))
const staleBonus = Math.min(60, Math.round(staleFrac * 60))
const rawTotal =
base + editedBonus + symbolsBonus + bashBonus + webBonus + diffBonus + uncommittedBonus + staleBonus
const factor = _computeActivityMultiplier(ageSecs, editedCount)
const total = Math.round(rawTotal * factor)
let capMax = maxTotal
if (opts?.contextPressure) {
if (opts.contextPressure.tier === 'critical') {
capMax = Math.min(capMax, 300)
} else if (opts.contextPressure.tier === 'hot') {
capMax = Math.min(capMax, 500)
}
}
return Math.max(minTotal, Math.min(capMax, total))
}