feat: 添加令牌消耗诊断功能,优化输出和状态管理

This commit is contained in:
catlog22
2026-01-14 21:40:00 +08:00
parent 266f6f11ec
commit f9c1216eec
7 changed files with 540 additions and 60 deletions

View File

@@ -93,6 +93,14 @@
"detection_focus": null,
"needs_gemini_analysis": true,
"priority_order": [1, 2, 3, 4]
},
"token_consumption": {
"pattern_ids": ["TKN-001", "TKN-002", "TKN-003", "TKN-004", "TKN-005"],
"severity_hint": "medium",
"strategies": ["prompt_compression", "lazy_loading", "output_minimization", "state_field_reduction", "sliding_window"],
"risk_levels": ["low", "low", "low", "low", "low"],
"detection_focus": "Verbose prompts, excessive state fields, full content passing, unbounded arrays, redundant I/O",
"priority_order": [1, 2, 3, 4, 5]
}
},
"keywords": {
@@ -149,7 +157,12 @@
"中间文件": "authoring_principles_violation",
"临时文件": "authoring_principles_violation",
"文件中转": "authoring_principles_violation",
"state膨胀": "authoring_principles_violation"
"state膨胀": "authoring_principles_violation",
"token消耗": "token_consumption",
"token优化": "token_consumption",
"产出简化": "token_consumption",
"冗长": "token_consumption",
"精简": "token_consumption"
},
"english": {
"token": "context_explosion",
@@ -198,7 +211,12 @@
"feedback": "user_experience",
"intermediate": "authoring_principles_violation",
"temp": "authoring_principles_violation",
"relay": "authoring_principles_violation"
"relay": "authoring_principles_violation",
"verbose": "token_consumption",
"minimize": "token_consumption",
"compress": "token_consumption",
"simplify": "token_consumption",
"reduction": "token_consumption"
}
},
"category_labels": {
@@ -213,6 +231,7 @@
"output_quality": "Output Quality",
"user_experience": "User Experience",
"authoring_principles_violation": "Authoring Principles Violation",
"token_consumption": "Token Consumption",
"custom": "Custom"
},
"category_labels_chinese": {
@@ -227,6 +246,7 @@
"output_quality": "Output Quality",
"user_experience": "User Experience",
"authoring_principles_violation": "Authoring Principles Violation",
"token_consumption": "Token Consumption Optimization",
"custom": "Other Issues"
},
"category_descriptions": {
@@ -241,12 +261,13 @@
"output_quality": "Output validation or completeness issues",
"user_experience": "Interaction or feedback clarity issues",
"authoring_principles_violation": "Violation of skill authoring principles",
"token_consumption": "Excessive token usage from verbose prompts, large state objects, or redundant I/O patterns",
"custom": "Requires custom analysis"
},
"fix_priority_order": {
"P0": ["dataflow_break", "authoring_principles_violation"],
"P1": ["agent_failure"],
"P2": ["context_explosion"],
"P2": ["context_explosion", "token_consumption"],
"P3": ["memory_loss"]
},
"cross_category_dependencies": {

View File

@@ -180,7 +180,35 @@ Classification of skill execution issues with detection patterns and severity cr
---
### 6. Documentation Conflict (P6)
### 6. Token Consumption (P6)
**Definition**: Excessive token usage from verbose prompts, large state objects, or inefficient I/O patterns.
**Root Causes**:
- Long static prompts without compression
- State schema with too many fields
- Full content embedding instead of path references
- Arrays growing unbounded without sliding windows
- Write-then-read file relay patterns
**Detection Patterns**:
| Pattern ID | Regex/Check | Description |
|------------|-------------|-------------|
| TKN-001 | File size > 4KB | Verbose prompt files |
| TKN-002 | State fields > 15 | Excessive state schema |
| TKN-003 | `/Read\([^)]+\)\s*[\+,]/` | Full content passing |
| TKN-004 | `/.push\|concat(?!.*\.slice)/` | Unbounded array growth |
| TKN-005 | `/Write\([^)]+\)[\s\S]{0,100}Read\([^)]+\)/` | Write-then-read pattern |
**Impact Levels**:
- **High**: Multiple TKN-003/TKN-004 issues causing significant token waste
- **Medium**: Several verbose files or state bloat
- **Low**: Minor optimization opportunities
---
### 7. Documentation Conflict (P7)
**Definition**: 同一概念在不同文件中定义不一致,导致行为不可预测和文档误导。
@@ -262,6 +290,7 @@ function calculateIssueSeverity(issue) {
| Long-tail Forgetting | constraint_injection, state_constraints_field, checkpoint | 1, 2, 3 |
| Data Flow Disruption | state_centralization, schema_enforcement, field_normalization | 1, 2, 3 |
| Agent Coordination | error_wrapping, result_validation, flatten_nesting | 1, 2, 3 |
| **Token Consumption** | prompt_compression, lazy_loading, output_minimization, state_field_reduction | 1, 2, 3, 4 |
| **Documentation Redundancy** | consolidate_to_ssot, centralize_mapping_config | 1, 2 |
| **Documentation Conflict** | reconcile_conflicting_definitions | 1 |

View File

@@ -1308,3 +1308,230 @@ async function checkpoint(name, summary, options) {
return response;
}
```
---
## Token Consumption Strategies
Strategies for reducing token usage and simplifying skill outputs.
---
### Strategy: prompt_compression
**Purpose**: Reduce verbose prompts by extracting static text and using templates.
**Implementation**:
```javascript
// Before: Long inline prompt
const prompt = `
You are an expert code analyzer specializing in identifying patterns.
Your role is to examine the provided code and identify any issues.
Please follow these detailed instructions carefully:
1. Read the code thoroughly
2. Identify any anti-patterns
3. Check for security vulnerabilities
... (continues for many lines)
Code to analyze:
${fullCodeContent}
`;
// After: Compressed with template reference
const PROMPT_TEMPLATE_PATH = 'templates/analyzer-prompt.md';
const prompt = `
[TEMPLATE: ${PROMPT_TEMPLATE_PATH}]
[CODE_PATH: ${codePath}]
[FOCUS: patterns, security]
`;
// Or use key instructions only
const prompt = `
Analyze ${codePath} for: patterns, security, performance.
Return JSON: { issues: [], severity: string }
`;
```
**Risk**: Low
**Verification**: Compare token count before/after compression
---
### Strategy: lazy_loading
**Purpose**: Pass file paths instead of full content, let consumers load if needed.
**Implementation**:
```javascript
// Before: Full content in prompt
const content = Read(filePath);
const prompt = `Analyze this content:\n${content}`;
// After: Path reference with lazy loading instruction
const prompt = `
Analyze file at: ${filePath}
(Read the file content if you need to examine it)
Return: { summary: string, issues: [] }
`;
// For agent calls
const result = await Task({
subagent_type: 'universal-executor',
prompt: `
[FILE_PATH]: ${dataPath}
[TASK]: Analyze the file and extract key metrics.
[NOTE]: Read the file only if needed for your analysis.
`
});
```
**Risk**: Low
**Verification**: Verify agents can still access required data
---
### Strategy: output_minimization
**Purpose**: Configure agents to return minimal, structured output instead of verbose text.
**Implementation**:
```javascript
// Before: Verbose output expectation
const prompt = `
Analyze the code and provide a detailed report including:
- Executive summary
- Detailed findings with explanations
- Code examples for each issue
- Recommendations with rationale
...
`;
// After: Minimal structured output
const prompt = `
Analyze the code. Return ONLY this JSON:
{
"status": "pass|review|fail",
"issues": [{ "id": string, "severity": string, "file": string, "line": number }],
"summary": "one sentence"
}
Do not include explanations or code examples.
`;
// Validation
const result = JSON.parse(agentOutput);
if (!result.status || !Array.isArray(result.issues)) {
throw new Error('Invalid output format');
}
```
**Risk**: Low
**Verification**: JSON.parse succeeds, output size reduced
---
### Strategy: state_field_reduction
**Purpose**: Audit and consolidate state fields to minimize serialization overhead.
**Implementation**:
```typescript
// Before: Bloated state
interface State {
status: string;
target: TargetInfo;
user_input: string;
parsed_input: ParsedInput; // Remove - temporary
intermediate_result: any; // Remove - not persisted
debug_info: DebugInfo; // Remove - debugging only
analysis_cache: any; // Remove - session cache
full_history: HistoryEntry[]; // Remove - unbounded
step1_output: any; // Remove - intermediate
step2_output: any; // Remove - intermediate
step3_output: any; // Remove - intermediate
final_result: FinalResult;
error_log: string[]; // Remove - debugging
metrics: Metrics; // Remove - optional
}
// After: Minimal state (≤15 fields)
interface State {
status: 'pending' | 'running' | 'completed' | 'failed';
target: { name: string; path: string };
input_summary: string; // Summarized user input
result_path: string; // Path to final result
quality_gate: 'pass' | 'fail';
error?: string; // Only if failed
}
```
**Audit Checklist**:
```javascript
function auditStateFields(stateSchema) {
const removeCandidates = [];
for (const [key, type] of Object.entries(stateSchema)) {
// Identify removal candidates
if (key.startsWith('debug_')) removeCandidates.push(key);
if (key.endsWith('_cache')) removeCandidates.push(key);
if (key.endsWith('_temp')) removeCandidates.push(key);
if (key.includes('intermediate')) removeCandidates.push(key);
if (key.includes('step') && key.includes('output')) removeCandidates.push(key);
}
return {
total_fields: Object.keys(stateSchema).length,
remove_candidates: removeCandidates,
estimated_reduction: removeCandidates.length
};
}
```
**Risk**: Medium (ensure no essential data removed)
**Verification**: State field count ≤ 15, all essential data preserved
---
### Strategy: in_memory_consolidation
**Purpose**: Consolidate outputs into single file, eliminate redundant report files.
**Implementation**:
```javascript
// Before: Multiple output files
Write(`${workDir}/diagnosis-report.md`, reportMarkdown);
Write(`${workDir}/diagnosis-summary.json`, summaryJson);
Write(`${workDir}/state.json`, JSON.stringify(state));
Write(`${workDir}/tuning-report.md`, tuningReport);
Write(`${workDir}/tuning-summary.md`, finalSummary);
// After: Single source of truth
const consolidatedState = {
...state,
final_report: {
summary: summaryJson,
details_available_in_state: true,
generated_at: new Date().toISOString()
}
};
Write(`${workDir}/state.json`, JSON.stringify(consolidatedState, null, 2));
// Report can be rendered from state on-demand
function renderReport(state) {
return `
# Tuning Report: ${state.target_skill.name}
Status: ${state.status}
Quality: ${state.quality_gate}
Issues: ${state.issues.length}
...
`;
}
```
**Benefits**:
- Single file to read/write
- No data duplication
- On-demand rendering
**Risk**: Low
**Verification**: Only state.json exists as output, rendering works correctly