在反向遍历过程中,VE 函数调用 HY5 函数来确保获取的 Token 使用信息是有效和准确的。HY5 函数实现了一套严格的三重检查机制:
// Claude Code 短期记忆核心逻辑class ShortTermMemory {
constructor() {
this.messages = []; // O(1) 访问的消息队列this.maxTokens = 200000; // 动态Token限制this.compressionThreshold = 0.92; // 92% 压缩触发阈值
}
// VE函数的核心:从最新消息反向查找Token使用情况getCurrentUsage() {
console.log('🔍 Checking memory usage...');
let totalTokens = 0;
// 从后往前遍历,因为usage信息通常在最近的AI回复里for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (message.usage) {
totalTokens += this.calculateTotalTokens(message.usage);
break; // 找到即停止,避免不必要的遍历
}
}
return {
used: totalTokens,
total: this.maxTokens,
percentage: totalTokens / this.maxTokens
};
}
// yW5函数:检查是否需要启动压缩needsCompression() {
const usage = this.getCurrentUsage();
if (usage.percentage >= this.compressionThreshold) {
console.log(`🚨 Memory usage at ${Math.round(usage.percentage * 100)}%, triggering compression!`);
return true;
}
return false;
}
// zY5函数:精确的Token计算calculateTotalTokens(usage) {
return usage.input_tokens +
(usage.cache_creation_input_tokens || 0) +
(usage.cache_read_input_tokens || 0) +
usage.output_tokens;
}
}// Claude Code 短期记忆核心逻辑class ShortTermMemory {
constructor() {
this.messages= [];// O(1) 访问的消息队列this.maxTokens = 200000; // 动态Token限制this.compressionThreshold = 0.92; // 92% 压缩触发阈值
}
// VE函数的核心:从最新消息反向查找Token使用情况getCurrentUsage() {
console.log('🔍 Checking memory usage...');
lettotalTokens =0;
// 从后往前遍历,因为usage信息通常在最近的AI回复里for (let i = this.messages.length - 1; i >= 0; i--) {
constmessage =this.messages[i];
if(message.usage) {
totalTokens +=this.calculateTotalTokens(message.usage);
break;// 找到即停止,避免不必要的遍历
}
}
return{
used: totalTokens,
total:this.maxTokens,
percentage: totalTokens /this.maxTokens
};
}
// yW5函数:检查是否需要启动压缩needsCompression() {
constusage =this.getCurrentUsage();
if(usage.percentage>=this.compressionThreshold) {
console.log(🚨Memoryusage at ${Math.round(usage.percentage*100)}%, triggering compression!);
returntrue;
}
returnfalse;
}
// zY5函数:精确的Token计算calculateTotalTokens(usage) {
returnusage.input_tokens+
(usage.cache_creation_input_tokens||0) +
(usage.cache_read_input_tokens||0) +
usage.output_tokens;
}
}当短期记忆的使用率触及92%的阈值时,中期记忆层便会启动。它的核心是wU2压缩器,其工作不是粗暴地丢弃数据,而是进行“智能蒸馏”——调用一个专门的 LLM,将冗长的对话历史提炼成一份结构化的摘要。
这个过程的精髓在于AU2函数生成的压缩指令。它要求 LLM 按照一个精心设计的8段式结构来组织摘要。
8段式结构化总结 (The 8-Section Summary)
这个结构的设计并非随意,它模拟了开发者回顾项目时的思维模式,确保了上下文的完整性:
Primary Request and Intent (主要请求和意图): 用户的核心目标是什么?
Key Technical Concepts (关键技术概念): 对话中涉及的框架、算法、库等。
Files and Code Sections (文件和代码片段): 所有被提及或修改过的代码和文件路径。
Errors and fixes (错误和修复): 记录遇到的错误信息和最终的解决方案。
Problem Solving (问题解决过程): 解决问题的完整思路和决策路径。
All user messages (所有用户消息): 保留用户的关键指令和反馈。
Pending Tasks (待处理任务): 未完成的工作项,形成待办清单。
Current Work (当前工作状态): 明确记录当前对话中断时的进度。
这种结构化的方式,将无序的对话历史转化为一份有序、高信息密度的“项目文档”,为后续的对话提供了充分的背景信息。
wU2 压缩器在生成摘要后,不会直接应用压缩结果。相反,它实施了一套严格的质量验证机制,确保压缩过程不会损害关键信息的完整性。
// 压缩质量验证系统
classCompressionQualityValidator{
constructor() {
this.qualityThresholds= {
minFidelityScore:80, // 最低信息保真度 80%
maxCompressionRatio:0.15,// 最大压缩比 15%
minSectionCoverage:0.875,// 最低段落覆盖度 87.5%
maxKeywordLoss:0.20 // 最大关键词丢失率 20%
};
}
// 执行完整的质量验证流程
asyncvalidateCompressionQuality(compressedSummary, originalMessages) {
console.log('🔍 开始压缩质量验证...');
constchecks = {
// 检查 1:验证所有8个段落是否完整存在
sectionsComplete:this.validateSectionCompleteness(compressedSummary),
// 检查 2:验证关键信息保留情况
keyInfoPreserved:awaitthis.validateKeyInformationPreservation(
compressedSummary, originalMessages
),
// 检查 3:验证上下文连续性
contextContinuous:this.validateContextContinuity(compressedSummary),
// 检查 4:验证压缩比例是否合理
compressionRatioValid:this.validateCompressionRatio(
compressedSummary, originalMessages
)
};
// 计算综合保真度评分
constfidelityScore =this.calculateFidelityScore(checks);
constvalidationResult = {
isValid: fidelityScore >=this.qualityThresholds.minFidelityScore,
fidelityScore,
checks,
recommendations:this.generateImprovementRecommendations(checks)
};
console.log(`📊 压缩质量评估完成 - 保真度
{fidelityScore}%`);
returnvalidationResult;
}
// 检查 1:验证段落完整性
validateSectionCompleteness(summary) {
constrequiredSections = [
'Primary Request and Intent',
'Key Technical Concepts',
'Files and Code Sections',
'Errors and fixes',
'Problem Solving',
'All user messages',
'Pending Tasks',
'Current Work'
];
constfoundSections = requiredSections.filter(section=>
summary.toLowerCase().includes(section.toLowerCase()) ||
this.findSectionByKeywords(summary, section)
);
constcompleteness = foundSections.length/ requiredSections.length;
return{
score: completeness *100,
missingCount: requiredSections.length- foundSections.length,
missingSections: requiredSections.filter(s=>!foundSections.includes(s)),
isValid: completeness >=this.qualityThresholds.minSectionCoverage
};
}
// 检查 2:关键信息保留验证
asyncvalidateKeyInformationPreservation(summary, originalMessages) {
// 提取原始对话中的关键信息
constkeyInfo =this.extractKeyInformation(originalMessages);
// 检查摘要中保留的关键信息比例
constpreservedInfo = {
fileNames:this.checkFileNamePreservation(summary, keyInfo.fileNames),
errorMessages:this.checkErrorMessagePreservation(summary, keyInfo.errorMessages),
userCommands:this.checkUserCommandPreservation(summary, keyInfo.userCommands),
technicalTerms:this.checkTechnicalTermPreservation(summary, keyInfo.technicalTerms)
};
constoverallPreservation =Object.values(preservedInfo)
.reduce((sum, item) =>sum + item.preservationRate,0) /4;
return{
score: overallPreservation *100,
details: preservedInfo,
isValid: overallPreservation >= (1-this.qualityThresholds.maxKeywordLoss)
};
}
// 检查 3:上下文连续性验证
validateContextContinuity(summary) {
constcontinuityIndicators = [
'用户首先','然后','接下来','最后',
'问题出现','解决方案','结果',
'当前状态','下一步'
];
constfoundIndicators = continuityIndicators.filter(indicator=>
summary.includes(indicator)
);
constcontinuityScore =Math.min(100, (foundIndicators.length/5) *100);
return{
score: continuityScore,
foundIndicators: foundIndicators.length,
isValid: continuityScore >=60
};
}
// 检查 4:压缩比例验证
validateCompressionRatio(summary, originalMessages) {
constoriginalLength = originalMessages
.map(msg=>JSON.stringify(msg).length)
.reduce((sum, len) =>sum + len,0);
constcompressedLength = summary.length;
constcompressionRatio = compressedLength / originalLength;
return{
originalLength,
compressedLength,
compressionRatio,
isValid: compressionRatio <=this.qualityThresholds.maxCompressionRatio
};
}
// 综合保真度评分计算
calculateFidelityScore(checks) {
constweights = {
sectionsComplete:0.3, // 段落完整性权重 30%
keyInfoPreserved:0.4, // 关键信息保留权重 40%
contextContinuous:0.2, // 上下文连续性权重 20%
compressionRatioValid:0.1// 压缩比例权重 10%
};
returnMath.round(
checks.sectionsComplete.score* weights.sectionsComplete+
checks.keyInfoPreserved.score* weights.keyInfoPreserved+
checks.contextContinuous.score* weights.contextContinuous+
(checks.compressionRatioValid.isValid?100:50) * weights.compressionRatioValid
);
}
}当压缩质量验证失败时,Claude Code 不会简单地放弃压缩,而是采用优雅降级策略:
策略 1:自适应重压缩
策略 2:分段保留
策略 3:降级到简单截断
长期记忆是跨会话(cross-session)的知识存储层,通常以一个CLAUDE.md文件的形式存在。它存储的是那些经过中期记忆提炼后,被认为具有长期价值的信息,比如用户偏好、项目配置、通用解决方案等。
这一层不仅仅是简单的文件读写,更重要的是它支持向量化搜索(Vector Search)。当新的对话开始时,系统可以将用户的问题转换成向量,在长期记忆库中进行相似度检索,从而“回忆”起过去相关的经验,让 AI 具备跨越时间窗口解决问题的能力。
在 Claude Code 的设计中,92%这个压缩阈值是一个非常有意思的细节。它并非工程师拍脑袋想出的数字,而是大量 A/B 测试和多目标优化的结果。
// 多目标优化权重配置
constOptimizationWeights= {
user_experience:0.4, // 用户体验权重最高
performance_overhead:0.3,// 性能开销权重次之
information_fidelity:0.2,// 信息保真度
compression_frequency:0.1// 压缩频率权重最低
};
functioncalculateOptimalThreshold(testResults) {
letbestScore =0;
letoptimalThreshold =null;
for(const[threshold, metrics]ofObject.entries(testResults)) {
constweightedScore =
metrics.userExperience*OptimizationWeights.user_experience+
metrics.performance*OptimizationWeights.performance_overhead+
metrics.fidelity*OptimizationWeights.information_fidelity+
metrics.frequency*OptimizationWeights.compression_frequency;
if(weightedScore > bestScore) {
bestScore = weightedScore;
optimalThreshold = threshold;
}
}
return{threshold: optimalThreshold,score: bestScore };
}
ClaudeCode不会在达到92%时才突然行动。它设计了一套渐进式的警告系统,提前与用户沟通内存状态:
Level 1:Warning 状态 (60% - _W5 函数触发)- 触发条件:Token 使用率达到 60% - 显示信息:🟡 记忆使用量较高 (60%) - 用户操作:友好提醒,无需立即行动 - 系统行为:开始监控频率增加,从每 5 次对话检查一次变为每 2 次检查一次 - 预估剩余对话轮数:约 25-30 轮Level 2: Urgent 状态 (80% - jW5 函数触发)- 触发条件:Token 使用率达到 80% - 显示信息:🟠 记忆空间紧张,建议手动整理 - 用户操作:建议用户主动结束某些话题或重新开始对话 - 系统行为:每次对话后都进行 Token 检查,同时预热压缩系统 - 预估剩余对话轮数:约 8-12 轮Level 3: Critical 状态 (92% - h11 函数触发)- 触发条件:Token 使用率达到 92% - 显示信息:🔴 记忆空间已满,正在整理... - 用户操作:系统自动处理,用户无需操作 - 系统行为:立即触发 wU2 压缩器,显示压缩进度 - 压缩完成后:显示 ✅ 记忆整理完成,对话可以继续
// m11 函数:渐进式警告系统核心逻辑
classProgressiveWarningSystem{
constructor() {
this.thresholds= {
normal:0.0, // 正常状态
warning:0.6, // _W5 - 60% 警告阈值
urgent:0.8, // jW5 - 80% 紧急阈值
critical:0.92 // h11 - 92% 临界阈值
};
this.currentLevel='normal';
this.lastWarningTime=0;
this.warningCooldown=300000;// 5分钟警告冷却时间
}
// 计算当前警告等级和对应操作
assessMemoryStatus(currentUsage, maxTokens) {
constpercentage = currentUsage / maxTokens;
constnow =Date.now();
// 防止警告信息过于频繁
if(now -this.lastWarningTime<this.warningCooldown&&
percentage <this.thresholds.critical) {
return{level:this.currentLevel,shouldDisplay:false};
}
letnewLevel, message, action, estimatedRounds;
if(percentage >=this.thresholds.critical) {
newLevel ='critical';
message ='🔴 记忆空间已满,正在整理...';
action ='auto_compress';
estimatedRounds =0;
}elseif(percentage >=this.thresholds.urgent) {
newLevel ='urgent';
message ='🟠 记忆空间紧张,建议手动整理';
action ='suggest_manual_compress';
estimatedRounds =this.calculateRemainingRounds(percentage,0.92);
}elseif(percentage >=this.thresholds.warning) {
newLevel ='warning';
message ='🟡 记忆使用量较高';
action ='gentle_reminder';
estimatedRounds =this.calculateRemainingRounds(percentage,0.92);
}else{
newLevel ='normal';
message = ✅ 记忆健康 (${Math.round(percentage *100)}%);
action ='none';
estimatedRounds =this.calculateRemainingRounds(percentage,0.92);
}
// 更新状态
if(newLevel !==this.currentLevel) {
this.currentLevel= newLevel;
this.lastWarningTime= now;
}
return{
level: newLevel,
message,
action,
percentage:Math.round(percentage *100),
estimatedRounds,
shouldDisplay:true
};
}
// 估算剩余对话轮数
calculateRemainingRounds(currentRatio, targetRatio) {
if(currentRatio >= targetRatio)return0;
// 基于历史数据:平均每轮对话消耗约 1.5-2% 的 Token
constavgTokenPerRound =0.0175;// 1.75%
constremainingRatio = targetRatio - currentRatio;
returnMath.floor(remainingRatio / avgTokenPerRound);
}
// 生成用户友好的进度条显示
generateProgressBar(percentage) {
constbarLength =20;
constfilled =Math.round((percentage /100) * barLength);
constempty = barLength - filled;
constprogressBar ='█'.repeat(filled) +'░'.repeat(empty);
return[${progressBar}] ${percentage}%;
}
}wU2压缩器最智能的一点,可能在于压缩之后的操作。它并没有将所有信息都“压扁”,而是通过TW5函数执行一个关键步骤:智能文件恢复 (Intelligent File Restoration)。
对话历史被压缩了,但那些在对话中被频繁读写、修改的核心代码文件,其内容会被智能地重新加载回上下文中。这就像一个程序员清理了桌面,但把最重要的几份文件留在了手边。
TW5函数通过一套评分算法来决定恢复哪些文件,其评分标准包括:
这个机制确保了即使在上下文被大幅压缩后,AI 依然能无缝地继续之前的工作,因为它最重要的“工作文件”并未丢失。
// TW5 函数:智能文件恢复评分系统
classIntelligentFileRestorer{
constructor() {
// qW5: 文件数量限制配置
this.maxFiles=20;
// LW5: 单文件Token限制配置
this.maxTokensPerFile=8192;
// MW5: 总恢复Token限制配置
this.totalTokenLimit=32768;
// 评分权重配置
this.scoringWeights= {
temporal:0.35, // 时间因素权重 35%
frequency:0.25, // 频率因素权重 25%
operation:0.20, // 操作类型权重 20%
fileType:0.15, // 文件类型权重 15%
project:0.05 // 项目关联度权重 5%
};
}
// 核心评分算法
calculateImportanceScore(fileMetadata) {
lettotalScore =0;
// 1. 时间因素评分 (0-100分)
consttemporalScore =this.calculateTemporalScore(fileMetadata);
totalScore += temporalScore *this.scoringWeights.temporal;
// 2. 频率因素评分 (0-100分)
constfrequencyScore =this.calculateFrequencyScore(fileMetadata);
totalScore += frequencyScore *this.scoringWeights.frequency;
// 3. 操作类型评分 (0-100分)
constoperationScore =this.calculateOperationScore(fileMetadata);
totalScore += operationScore *this.scoringWeights.operation;
// 4. 文件类型评分 (0-100分)
constfileTypeScore =this.calculateFileTypeScore(fileMetadata);
totalScore += fileTypeScore *this.scoringWeights.fileType;
// 5. 项目关联度评分 (0-100分)
constprojectScore =this.calculateProjectRelevanceScore(fileMetadata);
totalScore += projectScore *this.scoringWeights.project;
returnMath.round(totalScore);
}
// 1. 时间因素评分:最近访问的文件优先级更高
calculateTemporalScore(file) {
constnow =Date.now();
consthoursSinceLastAccess = (now - file.lastAccessTime) / (1000*60*60);
// 时间衰减函数:24小时内满分,之后指数衰减
if(hoursSinceLastAccess <=1) {
return100;// 1小时内 = 满分
}elseif(hoursSinceLastAccess <=6) {
return90; // 6小时内 = 90分
}elseif(hoursSinceLastAccess <=24) {
return75; // 24小时内 = 75分
}else{
// 24小时后开始衰减:75 * e^(-0.1 * (hours - 24))
returnMath.max(10,75*Math.exp(-0.1* (hoursSinceLastAccess -24)));
}
}
// 2. 频率因素评分:操作越频繁优先级越高
calculateFrequencyScore(file) {
consttotalOperations = file.readCount+ file.writeCount+ file.editCount;
// 基于操作总数的评分
letscore =Math.min(80, totalOperations *5);// 最高80分
// 最近操作频率加成
constrecentOperations = file.operationsInLastHour||0;
score +=Math.min(20, recentOperations *10);// 最高20分加成
returnMath.min(100, score);
}
// 3. 操作类型评分:写操作 > 编辑操作 > 读操作
calculateOperationScore(file) {
letscore =0;
// 写操作权重最高
score += file.writeCount*15;
// 编辑操作权重中等
score += file.editCount*10;
// 读操作权重较低
score += file.readCount*3;
// 如果最后一次操作是写操作,额外加分
if(file.lastOperation==='write') {
score +=25;
}elseif(file.lastOperation==='edit') {
score +=15;
}
returnMath.min(100, score);
}
// 4. 文件类型评分:代码文件 > 配置文件 > 文档文件
calculateFileTypeScore(file) {
constextension = file.path.split('.').pop().toLowerCase();
// 编程语言文件优先级最高
constcodeExtensions = {
'js':100,'ts':100,'jsx':95,'tsx':95,
'py':90,'java':85,'cpp':85,'c':85,
'go':80,'rs':80,'php':75,'rb':75
};
// 配置文件优先级中等
constconfigExtensions = {
'json':70,'yaml':65,'yml':65,'toml':60,
'xml':55,'ini':50,'env':50,'config':50
};
// 文档文件优先级较低
constdocExtensions = {
'md':40,'txt':30,'doc':25,'docx':25,
'pdf':20,'html':35,'css':45
};
if(codeExtensions[extension]) {
returncodeExtensions[extension];
}elseif(configExtensions[extension]) {
returnconfigExtensions[extension];
}elseif(docExtensions[extension]) {
returndocExtensions[extension];
}
return30;// 未知类型默认30分
}
// 5. 项目关联度评分:与当前项目相关性
calculateProjectRelevanceScore(file) {
letscore =50;// 基础分50
// 检查是否在项目根目录或核心目录中
constpathLower = file.path.toLowerCase();
if(pathLower.includes('/src/') || pathLower.includes('\\src\\')) {
score +=30;// 源代码目录加分
}
if(pathLower.includes('/components/') || pathLower.includes('/modules/')) {
score +=20;// 组件/模块目录加分
}
if(pathLower.includes('/test/') || pathLower.includes('/spec/')) {
score +=10;// 测试目录适度加分
}
// 检查是否为关键文件
constfileName = file.path.split('/').pop().toLowerCase();
constcriticalFiles = [
'package.json','pom.xml','cargo.toml','go.mod',
'dockerfile','docker-compose.yml','makefile',
'readme.md','index.js','main.py','app.js'
];
if(criticalFiles.includes(fileName)) {
score +=25;// 关键文件大幅加分
}
returnMath.min(100, score);
}
// 智能选择算法:在约束条件下选择最优文件组合
selectOptimalFileSet(rankedFiles) {
constselectedFiles = [];
lettotalTokens =0;
letfileCount =0;
// 按分数排序
constsortedFiles = rankedFiles.sort((a, b) =>b.score- a.score);
for(constfileofsortedFiles) {
// 检查约束条件
if(fileCount >=this.maxFiles) {
console.log(📊 达到文件数量限制: ${this.maxFiles});
break;
}
if(file.estimatedTokens>this.maxTokensPerFile) {
console.log(⚠️ 文件 ${file.path} 超出单文件限制,跳过);
continue;
}
if(totalTokens + file.estimatedTokens>this.totalTokenLimit) {
console.log(📊 添加 ${file.path} 将超出总Token限制);
// 尝试背包问题优化:寻找更小的高分文件
constremainingTokens =this.totalTokenLimit- totalTokens;
constalternativeFile =this.findBestFitFile(
sortedFiles.slice(sortedFiles.indexOf(file) +1),
remainingTokens
);
if(alternativeFile) {
selectedFiles.push(alternativeFile);
totalTokens += alternativeFile.estimatedTokens;
fileCount++;
}
continue;
}
selectedFiles.push(file);
totalTokens += file.estimatedTokens;
fileCount++;
}
return{
files: selectedFiles,
totalFiles: fileCount,
totalTokens,
efficiency: (totalTokens /this.totalTokenLimit) *100
};
}
}Claude Code 的上下文管理系统为我们构建复杂的 AI 应用提供了宝贵的经验:
分层思想是王道: 不要试图用一个系统解决所有问题。将记忆/上下文管理分解为高速的短期层、负责整理的中期层和用于持久化的长期层,是一种非常清晰和可扩展的架构。
优雅降级,而非硬性限制: 与其在达到 Token 上限时粗暴地截断,不如设计一套智能压缩和摘要机制。这能显著提升应用的“智能感”和鲁棒性。
性能优化在于细节: 反向遍历查找 Token、结构化摘要、向量化检索等技术,都是在细节之处提升系统性能和用户体验的典范。
透明化是最好的交互: 将系统内部状态(如内存使用率)通过渐进式警告的方式暴露给用户,可以建立信任,并将潜在的负面体验转化为正向的互动。
最终,Claude Code 告诉我们,真正的智能记忆,不在于无限的容量,而在于管理的智慧——知道什么该记住,什么该忘记,以及如何优雅地忘记。这不仅是 AI 设计的哲学,或许也是我们程序员日常工作中值得借鉴的思考。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |