@ginkoai/cli 1.6.2 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/agent/agent-client.d.ts +150 -0
- package/dist/commands/agent/agent-client.d.ts.map +1 -0
- package/dist/commands/agent/agent-client.js +170 -0
- package/dist/commands/agent/agent-client.js.map +1 -0
- package/dist/commands/agent/index.d.ts +22 -0
- package/dist/commands/agent/index.d.ts.map +1 -0
- package/dist/commands/agent/index.js +121 -0
- package/dist/commands/agent/index.js.map +1 -0
- package/dist/commands/agent/list.d.ts +22 -0
- package/dist/commands/agent/list.d.ts.map +1 -0
- package/dist/commands/agent/list.js +119 -0
- package/dist/commands/agent/list.js.map +1 -0
- package/dist/commands/agent/register.d.ts +21 -0
- package/dist/commands/agent/register.d.ts.map +1 -0
- package/dist/commands/agent/register.js +97 -0
- package/dist/commands/agent/register.js.map +1 -0
- package/dist/commands/agent/status.d.ts +19 -0
- package/dist/commands/agent/status.d.ts.map +1 -0
- package/dist/commands/agent/status.js +271 -0
- package/dist/commands/agent/status.js.map +1 -0
- package/dist/commands/agent/work.d.ts +22 -0
- package/dist/commands/agent/work.d.ts.map +1 -0
- package/dist/commands/agent/work.js +459 -0
- package/dist/commands/agent/work.js.map +1 -0
- package/dist/commands/checkpoint/create.d.ts +27 -0
- package/dist/commands/checkpoint/create.d.ts.map +1 -0
- package/dist/commands/checkpoint/create.js +82 -0
- package/dist/commands/checkpoint/create.js.map +1 -0
- package/dist/commands/checkpoint/index.d.ts +23 -0
- package/dist/commands/checkpoint/index.d.ts.map +1 -0
- package/dist/commands/checkpoint/index.js +91 -0
- package/dist/commands/checkpoint/index.js.map +1 -0
- package/dist/commands/checkpoint/list.d.ts +27 -0
- package/dist/commands/checkpoint/list.d.ts.map +1 -0
- package/dist/commands/checkpoint/list.js +115 -0
- package/dist/commands/checkpoint/list.js.map +1 -0
- package/dist/commands/checkpoint/show.d.ts +23 -0
- package/dist/commands/checkpoint/show.d.ts.map +1 -0
- package/dist/commands/checkpoint/show.js +102 -0
- package/dist/commands/checkpoint/show.js.map +1 -0
- package/dist/commands/dlq.d.ts +24 -0
- package/dist/commands/dlq.d.ts.map +1 -0
- package/dist/commands/dlq.js +172 -0
- package/dist/commands/dlq.js.map +1 -0
- package/dist/commands/escalation/create.d.ts +22 -0
- package/dist/commands/escalation/create.d.ts.map +1 -0
- package/dist/commands/escalation/create.js +122 -0
- package/dist/commands/escalation/create.js.map +1 -0
- package/dist/commands/escalation/escalation-client.d.ts +101 -0
- package/dist/commands/escalation/escalation-client.d.ts.map +1 -0
- package/dist/commands/escalation/escalation-client.js +129 -0
- package/dist/commands/escalation/escalation-client.js.map +1 -0
- package/dist/commands/escalation/index.d.ts +22 -0
- package/dist/commands/escalation/index.d.ts.map +1 -0
- package/dist/commands/escalation/index.js +94 -0
- package/dist/commands/escalation/index.js.map +1 -0
- package/dist/commands/escalation/list.d.ts +24 -0
- package/dist/commands/escalation/list.d.ts.map +1 -0
- package/dist/commands/escalation/list.js +170 -0
- package/dist/commands/escalation/list.js.map +1 -0
- package/dist/commands/escalation/resolve.d.ts +20 -0
- package/dist/commands/escalation/resolve.d.ts.map +1 -0
- package/dist/commands/escalation/resolve.js +102 -0
- package/dist/commands/escalation/resolve.js.map +1 -0
- package/dist/commands/graph/api-client.d.ts +21 -1
- package/dist/commands/graph/api-client.d.ts.map +1 -1
- package/dist/commands/graph/api-client.js +23 -0
- package/dist/commands/graph/api-client.js.map +1 -1
- package/dist/commands/handoff.d.ts.map +1 -1
- package/dist/commands/handoff.js +9 -1
- package/dist/commands/handoff.js.map +1 -1
- package/dist/commands/log.d.ts +3 -0
- package/dist/commands/log.d.ts.map +1 -1
- package/dist/commands/log.js +73 -14
- package/dist/commands/log.js.map +1 -1
- package/dist/commands/notifications/history.d.ts +21 -0
- package/dist/commands/notifications/history.d.ts.map +1 -0
- package/dist/commands/notifications/history.js +160 -0
- package/dist/commands/notifications/history.js.map +1 -0
- package/dist/commands/notifications/index.d.ts +22 -0
- package/dist/commands/notifications/index.d.ts.map +1 -0
- package/dist/commands/notifications/index.js +87 -0
- package/dist/commands/notifications/index.js.map +1 -0
- package/dist/commands/notifications/list.d.ts +19 -0
- package/dist/commands/notifications/list.d.ts.map +1 -0
- package/dist/commands/notifications/list.js +132 -0
- package/dist/commands/notifications/list.js.map +1 -0
- package/dist/commands/notifications/test.d.ts +19 -0
- package/dist/commands/notifications/test.d.ts.map +1 -0
- package/dist/commands/notifications/test.js +217 -0
- package/dist/commands/notifications/test.js.map +1 -0
- package/dist/commands/orchestrate.d.ts +25 -0
- package/dist/commands/orchestrate.d.ts.map +1 -0
- package/dist/commands/orchestrate.js +858 -0
- package/dist/commands/orchestrate.js.map +1 -0
- package/dist/commands/sprint/deps.d.ts +29 -0
- package/dist/commands/sprint/deps.d.ts.map +1 -0
- package/dist/commands/sprint/deps.js +269 -0
- package/dist/commands/sprint/deps.js.map +1 -0
- package/dist/commands/sprint/index.d.ts +10 -5
- package/dist/commands/sprint/index.d.ts.map +1 -1
- package/dist/commands/sprint/index.js +26 -5
- package/dist/commands/sprint/index.js.map +1 -1
- package/dist/commands/start/index.d.ts.map +1 -1
- package/dist/commands/start/index.js +6 -0
- package/dist/commands/start/index.js.map +1 -1
- package/dist/commands/start/start-reflection.d.ts.map +1 -1
- package/dist/commands/start/start-reflection.js +8 -0
- package/dist/commands/start/start-reflection.js.map +1 -1
- package/dist/commands/verify.d.ts +17 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +232 -0
- package/dist/commands/verify.js.map +1 -0
- package/dist/core/session-log-manager.d.ts +1 -1
- package/dist/core/session-log-manager.d.ts.map +1 -1
- package/dist/index.js +78 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/__tests__/task-timeout.test.d.ts +12 -0
- package/dist/lib/__tests__/task-timeout.test.d.ts.map +1 -0
- package/dist/lib/__tests__/task-timeout.test.js +278 -0
- package/dist/lib/__tests__/task-timeout.test.js.map +1 -0
- package/dist/lib/agent-heartbeat.d.ts +68 -0
- package/dist/lib/agent-heartbeat.d.ts.map +1 -0
- package/dist/lib/agent-heartbeat.js +117 -0
- package/dist/lib/agent-heartbeat.js.map +1 -0
- package/dist/lib/checkpoint.d.ts +85 -0
- package/dist/lib/checkpoint.d.ts.map +1 -0
- package/dist/lib/checkpoint.js +323 -0
- package/dist/lib/checkpoint.js.map +1 -0
- package/dist/lib/context-metrics.d.ts +230 -0
- package/dist/lib/context-metrics.d.ts.map +1 -0
- package/dist/lib/context-metrics.js +372 -0
- package/dist/lib/context-metrics.js.map +1 -0
- package/dist/lib/dead-letter-queue.d.ts +108 -0
- package/dist/lib/dead-letter-queue.d.ts.map +1 -0
- package/dist/lib/dead-letter-queue.js +378 -0
- package/dist/lib/dead-letter-queue.js.map +1 -0
- package/dist/lib/event-logger.d.ts +9 -1
- package/dist/lib/event-logger.d.ts.map +1 -1
- package/dist/lib/event-logger.js +45 -3
- package/dist/lib/event-logger.js.map +1 -1
- package/dist/lib/event-queue.d.ts.map +1 -1
- package/dist/lib/event-queue.js +13 -2
- package/dist/lib/event-queue.js.map +1 -1
- package/dist/lib/examples/timeout-demo.d.ts +13 -0
- package/dist/lib/examples/timeout-demo.d.ts.map +1 -0
- package/dist/lib/examples/timeout-demo.js +102 -0
- package/dist/lib/examples/timeout-demo.js.map +1 -0
- package/dist/lib/examples/timeout-integration-example.d.ts +17 -0
- package/dist/lib/examples/timeout-integration-example.d.ts.map +1 -0
- package/dist/lib/examples/timeout-integration-example.js +223 -0
- package/dist/lib/examples/timeout-integration-example.js.map +1 -0
- package/dist/lib/notification-hooks.d.ts +103 -0
- package/dist/lib/notification-hooks.d.ts.map +1 -0
- package/dist/lib/notification-hooks.js +223 -0
- package/dist/lib/notification-hooks.js.map +1 -0
- package/dist/lib/notifications/discord.d.ts +20 -0
- package/dist/lib/notifications/discord.d.ts.map +1 -0
- package/dist/lib/notifications/discord.js +140 -0
- package/dist/lib/notifications/discord.js.map +1 -0
- package/dist/lib/notifications/index.d.ts +66 -0
- package/dist/lib/notifications/index.d.ts.map +1 -0
- package/dist/lib/notifications/index.js +120 -0
- package/dist/lib/notifications/index.js.map +1 -0
- package/dist/lib/notifications/slack.d.ts +20 -0
- package/dist/lib/notifications/slack.d.ts.map +1 -0
- package/dist/lib/notifications/slack.js +186 -0
- package/dist/lib/notifications/slack.js.map +1 -0
- package/dist/lib/notifications/teams.d.ts +20 -0
- package/dist/lib/notifications/teams.d.ts.map +1 -0
- package/dist/lib/notifications/teams.js +146 -0
- package/dist/lib/notifications/teams.js.map +1 -0
- package/dist/lib/notifications/webhook.d.ts +23 -0
- package/dist/lib/notifications/webhook.d.ts.map +1 -0
- package/dist/lib/notifications/webhook.js +65 -0
- package/dist/lib/notifications/webhook.js.map +1 -0
- package/dist/lib/orchestrator-state.d.ts +194 -0
- package/dist/lib/orchestrator-state.d.ts.map +1 -0
- package/dist/lib/orchestrator-state.js +332 -0
- package/dist/lib/orchestrator-state.js.map +1 -0
- package/dist/lib/realtime-cursor.d.ts +107 -0
- package/dist/lib/realtime-cursor.d.ts.map +1 -0
- package/dist/lib/realtime-cursor.js +260 -0
- package/dist/lib/realtime-cursor.js.map +1 -0
- package/dist/lib/rollback.d.ts +86 -0
- package/dist/lib/rollback.d.ts.map +1 -0
- package/dist/lib/rollback.js +405 -0
- package/dist/lib/rollback.js.map +1 -0
- package/dist/lib/sprint-loader.d.ts +39 -2
- package/dist/lib/sprint-loader.d.ts.map +1 -1
- package/dist/lib/sprint-loader.js +269 -5
- package/dist/lib/sprint-loader.js.map +1 -1
- package/dist/lib/stale-agent-detector.d.ts +102 -0
- package/dist/lib/stale-agent-detector.d.ts.map +1 -0
- package/dist/lib/stale-agent-detector.js +156 -0
- package/dist/lib/stale-agent-detector.js.map +1 -0
- package/dist/lib/task-dependencies.d.ts +143 -0
- package/dist/lib/task-dependencies.d.ts.map +1 -0
- package/dist/lib/task-dependencies.js +357 -0
- package/dist/lib/task-dependencies.js.map +1 -0
- package/dist/lib/task-timeout.d.ts +153 -0
- package/dist/lib/task-timeout.d.ts.map +1 -0
- package/dist/lib/task-timeout.js +505 -0
- package/dist/lib/task-timeout.js.map +1 -0
- package/dist/lib/verification/build-check.d.ts +55 -0
- package/dist/lib/verification/build-check.d.ts.map +1 -0
- package/dist/lib/verification/build-check.js +111 -0
- package/dist/lib/verification/build-check.js.map +1 -0
- package/dist/lib/verification/index.d.ts +19 -0
- package/dist/lib/verification/index.d.ts.map +1 -0
- package/dist/lib/verification/index.js +17 -0
- package/dist/lib/verification/index.js.map +1 -0
- package/dist/lib/verification/lint-check.d.ts +34 -0
- package/dist/lib/verification/lint-check.d.ts.map +1 -0
- package/dist/lib/verification/lint-check.js +215 -0
- package/dist/lib/verification/lint-check.js.map +1 -0
- package/dist/lib/verification/test-runner.d.ts +50 -0
- package/dist/lib/verification/test-runner.d.ts.map +1 -0
- package/dist/lib/verification/test-runner.js +225 -0
- package/dist/lib/verification/test-runner.js.map +1 -0
- package/dist/utils/command-helpers.d.ts.map +1 -1
- package/dist/utils/command-helpers.js +7 -0
- package/dist/utils/command-helpers.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileType: utility
|
|
3
|
+
* @status: current
|
|
4
|
+
* @updated: 2025-12-07
|
|
5
|
+
* @tags: [context-metrics, token-estimation, pressure-monitoring, epic-004, sprint-4]
|
|
6
|
+
* @related: [orchestrate.ts, event-logger.ts, agent-heartbeat.ts]
|
|
7
|
+
* @priority: high
|
|
8
|
+
* @complexity: medium
|
|
9
|
+
* @dependencies: []
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Model context limits configuration
|
|
13
|
+
* Values are approximate and based on model documentation
|
|
14
|
+
*/
|
|
15
|
+
export const MODEL_LIMITS = {
|
|
16
|
+
// Claude models
|
|
17
|
+
'claude-opus-4-5-20251101': 200000,
|
|
18
|
+
'claude-sonnet-4-20250514': 200000,
|
|
19
|
+
'claude-3-5-sonnet-20241022': 200000,
|
|
20
|
+
'claude-3-opus-20240229': 200000,
|
|
21
|
+
'claude-3-sonnet-20240229': 200000,
|
|
22
|
+
'claude-3-haiku-20240307': 200000,
|
|
23
|
+
// OpenAI models
|
|
24
|
+
'gpt-4-turbo': 128000,
|
|
25
|
+
'gpt-4-turbo-preview': 128000,
|
|
26
|
+
'gpt-4o': 128000,
|
|
27
|
+
'gpt-4o-mini': 128000,
|
|
28
|
+
'gpt-4': 8192,
|
|
29
|
+
'gpt-3.5-turbo': 16385,
|
|
30
|
+
// Google models
|
|
31
|
+
'gemini-pro': 1000000,
|
|
32
|
+
'gemini-1.5-pro': 1000000,
|
|
33
|
+
'gemini-1.5-flash': 1000000,
|
|
34
|
+
// Default fallback
|
|
35
|
+
default: 128000,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Default pressure thresholds
|
|
39
|
+
*/
|
|
40
|
+
export const DEFAULT_THRESHOLDS = {
|
|
41
|
+
optimal: 0.5,
|
|
42
|
+
elevated: 0.7,
|
|
43
|
+
warning: 0.85,
|
|
44
|
+
};
|
|
45
|
+
// ============================================================
|
|
46
|
+
// Token Estimation
|
|
47
|
+
// ============================================================
|
|
48
|
+
/**
|
|
49
|
+
* Estimate token count from text using character-based heuristic.
|
|
50
|
+
*
|
|
51
|
+
* Heuristic: ~4 characters per token for English text.
|
|
52
|
+
* This is a rough approximation that works well for typical code/prose.
|
|
53
|
+
*
|
|
54
|
+
* More accurate methods:
|
|
55
|
+
* - tiktoken (OpenAI's tokenizer)
|
|
56
|
+
* - Anthropic SDK count_tokens
|
|
57
|
+
*
|
|
58
|
+
* We use character heuristic for:
|
|
59
|
+
* - Zero dependencies
|
|
60
|
+
* - Fast calculation
|
|
61
|
+
* - Good enough for pressure monitoring (within ~10%)
|
|
62
|
+
*
|
|
63
|
+
* @param text - Text to estimate tokens for
|
|
64
|
+
* @returns Estimated token count
|
|
65
|
+
*/
|
|
66
|
+
export function estimateTokens(text) {
|
|
67
|
+
if (!text)
|
|
68
|
+
return 0;
|
|
69
|
+
// Heuristic: ~4 characters per token for English/code
|
|
70
|
+
// This aligns with typical tokenizer behavior for mixed content
|
|
71
|
+
const charCount = text.length;
|
|
72
|
+
const estimated = Math.ceil(charCount / 4);
|
|
73
|
+
return estimated;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Estimate tokens for structured content (messages, tool calls, etc.)
|
|
77
|
+
*
|
|
78
|
+
* @param content - Structured content to estimate
|
|
79
|
+
* @returns Estimated token count with breakdown
|
|
80
|
+
*/
|
|
81
|
+
export function estimateStructuredTokens(content) {
|
|
82
|
+
const breakdown = {};
|
|
83
|
+
let total = 0;
|
|
84
|
+
// System prompt
|
|
85
|
+
if (content.systemPrompt) {
|
|
86
|
+
breakdown.systemPrompt = estimateTokens(content.systemPrompt);
|
|
87
|
+
total += breakdown.systemPrompt;
|
|
88
|
+
}
|
|
89
|
+
// Context/preamble
|
|
90
|
+
if (content.context) {
|
|
91
|
+
breakdown.context = estimateTokens(content.context);
|
|
92
|
+
total += breakdown.context;
|
|
93
|
+
}
|
|
94
|
+
// Messages
|
|
95
|
+
if (content.messages && content.messages.length > 0) {
|
|
96
|
+
let messageTokens = 0;
|
|
97
|
+
for (const msg of content.messages) {
|
|
98
|
+
// Role tokens (~3-5 tokens per message for formatting)
|
|
99
|
+
messageTokens += 4;
|
|
100
|
+
messageTokens += estimateTokens(msg.content);
|
|
101
|
+
}
|
|
102
|
+
breakdown.messages = messageTokens;
|
|
103
|
+
total += messageTokens;
|
|
104
|
+
}
|
|
105
|
+
// Tool calls
|
|
106
|
+
if (content.toolCalls && content.toolCalls.length > 0) {
|
|
107
|
+
let toolTokens = 0;
|
|
108
|
+
for (const call of content.toolCalls) {
|
|
109
|
+
// Tool name and structure (~10 tokens overhead per call)
|
|
110
|
+
toolTokens += 10;
|
|
111
|
+
toolTokens += estimateTokens(call.name);
|
|
112
|
+
toolTokens += estimateTokens(JSON.stringify(call.input));
|
|
113
|
+
if (call.output) {
|
|
114
|
+
toolTokens += estimateTokens(JSON.stringify(call.output));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
breakdown.toolCalls = toolTokens;
|
|
118
|
+
total += toolTokens;
|
|
119
|
+
}
|
|
120
|
+
return { total, breakdown };
|
|
121
|
+
}
|
|
122
|
+
// ============================================================
|
|
123
|
+
// Pressure Calculation
|
|
124
|
+
// ============================================================
|
|
125
|
+
/**
|
|
126
|
+
* Get context limit for a model
|
|
127
|
+
*
|
|
128
|
+
* @param model - Model identifier
|
|
129
|
+
* @returns Context limit in tokens
|
|
130
|
+
*/
|
|
131
|
+
export function getContextLimit(model) {
|
|
132
|
+
// Check for exact match
|
|
133
|
+
if (MODEL_LIMITS[model]) {
|
|
134
|
+
return MODEL_LIMITS[model];
|
|
135
|
+
}
|
|
136
|
+
// Check for partial match (e.g., "claude-3" matches "claude-3-opus")
|
|
137
|
+
for (const [key, value] of Object.entries(MODEL_LIMITS)) {
|
|
138
|
+
if (model.includes(key) || key.includes(model)) {
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Default fallback
|
|
143
|
+
return MODEL_LIMITS.default;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Calculate pressure ratio
|
|
147
|
+
*
|
|
148
|
+
* @param estimatedTokens - Current estimated token usage
|
|
149
|
+
* @param contextLimit - Model's context limit
|
|
150
|
+
* @returns Pressure ratio (0.0 - 1.0, clamped)
|
|
151
|
+
*/
|
|
152
|
+
export function calculatePressure(estimatedTokens, contextLimit) {
|
|
153
|
+
if (contextLimit <= 0)
|
|
154
|
+
return 1.0;
|
|
155
|
+
const pressure = estimatedTokens / contextLimit;
|
|
156
|
+
return Math.min(1.0, Math.max(0.0, pressure));
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Classify pressure into zones
|
|
160
|
+
*
|
|
161
|
+
* @param pressure - Pressure ratio (0.0 - 1.0)
|
|
162
|
+
* @param thresholds - Optional custom thresholds
|
|
163
|
+
* @returns Pressure zone classification
|
|
164
|
+
*/
|
|
165
|
+
export function getPressureZone(pressure, thresholds = DEFAULT_THRESHOLDS) {
|
|
166
|
+
if (pressure < thresholds.optimal)
|
|
167
|
+
return 'optimal';
|
|
168
|
+
if (pressure < thresholds.elevated)
|
|
169
|
+
return 'elevated';
|
|
170
|
+
if (pressure < thresholds.warning)
|
|
171
|
+
return 'warning';
|
|
172
|
+
return 'critical';
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get pressure zone color for display
|
|
176
|
+
*
|
|
177
|
+
* @param zone - Pressure zone
|
|
178
|
+
* @returns Chalk color name
|
|
179
|
+
*/
|
|
180
|
+
export function getPressureColor(zone) {
|
|
181
|
+
switch (zone) {
|
|
182
|
+
case 'optimal':
|
|
183
|
+
return 'green';
|
|
184
|
+
case 'elevated':
|
|
185
|
+
return 'yellow';
|
|
186
|
+
case 'warning':
|
|
187
|
+
return 'red';
|
|
188
|
+
case 'critical':
|
|
189
|
+
return 'magenta';
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// ============================================================
|
|
193
|
+
// Context Monitor Class
|
|
194
|
+
// ============================================================
|
|
195
|
+
/**
|
|
196
|
+
* Context pressure monitor for orchestrator agents.
|
|
197
|
+
*
|
|
198
|
+
* Tracks metrics over time and provides pressure monitoring.
|
|
199
|
+
* Designed to be updated each orchestration cycle.
|
|
200
|
+
*/
|
|
201
|
+
export class ContextMonitor {
|
|
202
|
+
model;
|
|
203
|
+
contextLimit;
|
|
204
|
+
thresholds;
|
|
205
|
+
// Tracked metrics
|
|
206
|
+
messageCount = 0;
|
|
207
|
+
toolCallCount = 0;
|
|
208
|
+
eventsSinceStart = 0;
|
|
209
|
+
accumulatedTokens = 0;
|
|
210
|
+
// History for trend analysis
|
|
211
|
+
pressureHistory = [];
|
|
212
|
+
maxHistorySize = 100;
|
|
213
|
+
constructor(options = {}) {
|
|
214
|
+
this.model = options.model || 'claude-opus-4-5-20251101';
|
|
215
|
+
this.contextLimit = options.contextLimit || getContextLimit(this.model);
|
|
216
|
+
this.thresholds = { ...DEFAULT_THRESHOLDS, ...options.thresholds };
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Record a message (increments counter and estimates tokens)
|
|
220
|
+
*/
|
|
221
|
+
recordMessage(content) {
|
|
222
|
+
this.messageCount++;
|
|
223
|
+
this.accumulatedTokens += estimateTokens(content);
|
|
224
|
+
this.recordPressure();
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Record a tool call (increments counter and estimates tokens)
|
|
228
|
+
*/
|
|
229
|
+
recordToolCall(name, input, output) {
|
|
230
|
+
this.toolCallCount++;
|
|
231
|
+
this.accumulatedTokens += 10; // Overhead
|
|
232
|
+
this.accumulatedTokens += estimateTokens(name);
|
|
233
|
+
this.accumulatedTokens += estimateTokens(JSON.stringify(input));
|
|
234
|
+
if (output) {
|
|
235
|
+
this.accumulatedTokens += estimateTokens(JSON.stringify(output));
|
|
236
|
+
}
|
|
237
|
+
this.recordPressure();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Record an event (session activity)
|
|
241
|
+
*/
|
|
242
|
+
recordEvent() {
|
|
243
|
+
this.eventsSinceStart++;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Add tokens directly (for bulk updates)
|
|
247
|
+
*/
|
|
248
|
+
addTokens(count) {
|
|
249
|
+
this.accumulatedTokens += count;
|
|
250
|
+
this.recordPressure();
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get current metrics snapshot
|
|
254
|
+
*/
|
|
255
|
+
getMetrics() {
|
|
256
|
+
const pressure = calculatePressure(this.accumulatedTokens, this.contextLimit);
|
|
257
|
+
return {
|
|
258
|
+
estimatedTokens: this.accumulatedTokens,
|
|
259
|
+
contextLimit: this.contextLimit,
|
|
260
|
+
pressure,
|
|
261
|
+
messageCount: this.messageCount,
|
|
262
|
+
toolCallCount: this.toolCallCount,
|
|
263
|
+
eventsSinceStart: this.eventsSinceStart,
|
|
264
|
+
model: this.model,
|
|
265
|
+
measuredAt: new Date(),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Get current pressure ratio
|
|
270
|
+
*/
|
|
271
|
+
getPressure() {
|
|
272
|
+
return calculatePressure(this.accumulatedTokens, this.contextLimit);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Get current pressure zone
|
|
276
|
+
*/
|
|
277
|
+
getZone() {
|
|
278
|
+
return getPressureZone(this.getPressure(), this.thresholds);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Check if pressure exceeds a threshold
|
|
282
|
+
*/
|
|
283
|
+
isAboveThreshold(threshold) {
|
|
284
|
+
return this.getPressure() >= threshold;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Check if respawn is recommended (pressure > 80%)
|
|
288
|
+
*/
|
|
289
|
+
shouldRespawn() {
|
|
290
|
+
return this.isAboveThreshold(0.8);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Check if warning should be logged (pressure > 70%)
|
|
294
|
+
*/
|
|
295
|
+
shouldWarn() {
|
|
296
|
+
return this.isAboveThreshold(0.7);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Get pressure trend (increasing, stable, decreasing)
|
|
300
|
+
*/
|
|
301
|
+
getTrend() {
|
|
302
|
+
if (this.pressureHistory.length < 5)
|
|
303
|
+
return 'stable';
|
|
304
|
+
const recent = this.pressureHistory.slice(-5);
|
|
305
|
+
const first = recent[0].pressure;
|
|
306
|
+
const last = recent[recent.length - 1].pressure;
|
|
307
|
+
const delta = last - first;
|
|
308
|
+
if (delta > 0.05)
|
|
309
|
+
return 'increasing';
|
|
310
|
+
if (delta < -0.05)
|
|
311
|
+
return 'decreasing';
|
|
312
|
+
return 'stable';
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Reset metrics (for new session)
|
|
316
|
+
*/
|
|
317
|
+
reset() {
|
|
318
|
+
this.messageCount = 0;
|
|
319
|
+
this.toolCallCount = 0;
|
|
320
|
+
this.eventsSinceStart = 0;
|
|
321
|
+
this.accumulatedTokens = 0;
|
|
322
|
+
this.pressureHistory = [];
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Record pressure snapshot for history
|
|
326
|
+
*/
|
|
327
|
+
recordPressure() {
|
|
328
|
+
const pressure = this.getPressure();
|
|
329
|
+
this.pressureHistory.push({ timestamp: new Date(), pressure });
|
|
330
|
+
// Trim history if too large
|
|
331
|
+
if (this.pressureHistory.length > this.maxHistorySize) {
|
|
332
|
+
this.pressureHistory = this.pressureHistory.slice(-this.maxHistorySize);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Format metrics for display
|
|
337
|
+
*/
|
|
338
|
+
formatMetrics() {
|
|
339
|
+
const metrics = this.getMetrics();
|
|
340
|
+
const zone = this.getZone();
|
|
341
|
+
const trend = this.getTrend();
|
|
342
|
+
const pressurePercent = (metrics.pressure * 100).toFixed(1);
|
|
343
|
+
const tokenK = (metrics.estimatedTokens / 1000).toFixed(1);
|
|
344
|
+
const limitK = (metrics.contextLimit / 1000).toFixed(0);
|
|
345
|
+
const trendIcon = trend === 'increasing' ? '↑' : trend === 'decreasing' ? '↓' : '→';
|
|
346
|
+
return `${tokenK}K/${limitK}K tokens (${pressurePercent}% ${trendIcon}) [${zone}]`;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// ============================================================
|
|
350
|
+
// Singleton Instance
|
|
351
|
+
// ============================================================
|
|
352
|
+
let globalMonitor = null;
|
|
353
|
+
/**
|
|
354
|
+
* Get or create the global context monitor
|
|
355
|
+
*/
|
|
356
|
+
export function getContextMonitor(options) {
|
|
357
|
+
if (!globalMonitor) {
|
|
358
|
+
globalMonitor = new ContextMonitor(options);
|
|
359
|
+
}
|
|
360
|
+
return globalMonitor;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Reset the global context monitor (for testing or session restart)
|
|
364
|
+
*/
|
|
365
|
+
export function resetContextMonitor() {
|
|
366
|
+
if (globalMonitor) {
|
|
367
|
+
globalMonitor.reset();
|
|
368
|
+
}
|
|
369
|
+
globalMonitor = null;
|
|
370
|
+
}
|
|
371
|
+
export default ContextMonitor;
|
|
372
|
+
//# sourceMappingURL=context-metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-metrics.js","sourceRoot":"","sources":["../../src/lib/context-metrics.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA2DH;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAA2B;IAClD,gBAAgB;IAChB,0BAA0B,EAAE,MAAM;IAClC,0BAA0B,EAAE,MAAM;IAClC,4BAA4B,EAAE,MAAM;IACpC,wBAAwB,EAAE,MAAM;IAChC,0BAA0B,EAAE,MAAM;IAClC,yBAAyB,EAAE,MAAM;IAEjC,gBAAgB;IAChB,aAAa,EAAE,MAAM;IACrB,qBAAqB,EAAE,MAAM;IAC7B,QAAQ,EAAE,MAAM;IAChB,aAAa,EAAE,MAAM;IACrB,OAAO,EAAE,IAAI;IACb,eAAe,EAAE,KAAK;IAEtB,gBAAgB;IAChB,YAAY,EAAE,OAAO;IACrB,gBAAgB,EAAE,OAAO;IACzB,kBAAkB,EAAE,OAAO;IAE3B,mBAAmB;IACnB,OAAO,EAAE,MAAM;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAuB;IACpD,OAAO,EAAE,GAAG;IACZ,QAAQ,EAAE,GAAG;IACb,OAAO,EAAE,IAAI;CACd,CAAC;AAEF,+DAA+D;AAC/D,mBAAmB;AACnB,+DAA+D;AAE/D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,IAAI,CAAC,IAAI;QAAE,OAAO,C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AC,CAAC,CAAC;QAExD,MAAM,SAAS,GAAG,KAAK,KAAK,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,KAAK,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAEpF,OAAO,GAAG,MAAM,KAAK,MAAM,aAAa,eAAe,KAAK,SAAS,MAAM,IAAI,GAAG,CAAC;IACrF,CAAC;CACF;AAED,+DAA+D;AAC/D,qBAAqB;AACrB,+DAA+D;AAE/D,IAAI,aAAa,GAA0B,IAAI,CAAC;AAEhD;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAIjC;IACC,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,aAAa,GAAG,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;IAC9C,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB;IACjC,IAAI,aAAa,EAAE,CAAC;QAClB,aAAa,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;IACD,aAAa,GAAG,IAAI,CAAC;AACvB,CAAC;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileType: utility
|
|
3
|
+
* @status: current
|
|
4
|
+
* @updated: 2025-12-07
|
|
5
|
+
* @tags: [dead-letter-queue, dlq, resilience, event-retry, epic-004]
|
|
6
|
+
* @related: [event-queue.ts, event-logger.ts, api-client.ts]
|
|
7
|
+
* @priority: high
|
|
8
|
+
* @complexity: medium
|
|
9
|
+
* @dependencies: [fs-extra, uuid]
|
|
10
|
+
*/
|
|
11
|
+
import { Event } from './event-logger.js';
|
|
12
|
+
/**
|
|
13
|
+
* Dead Letter Queue Entry
|
|
14
|
+
*
|
|
15
|
+
* Captures failed sync attempts for later retry
|
|
16
|
+
*/
|
|
17
|
+
export interface DeadLetterEntry {
|
|
18
|
+
id: string;
|
|
19
|
+
originalEvent: Event;
|
|
20
|
+
failureReason: string;
|
|
21
|
+
failedAt: Date;
|
|
22
|
+
retryCount: number;
|
|
23
|
+
lastRetryAt?: Date;
|
|
24
|
+
status: 'pending' | 'retrying' | 'resolved' | 'abandoned';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* DLQ Configuration
|
|
28
|
+
*/
|
|
29
|
+
interface DLQConfig {
|
|
30
|
+
maxRetries: number;
|
|
31
|
+
retryDelays: number[];
|
|
32
|
+
abandonedThreshold: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Add event to Dead Letter Queue
|
|
36
|
+
*
|
|
37
|
+
* Called when event sync fails after all retries in event-queue.ts
|
|
38
|
+
*
|
|
39
|
+
* @param event - Original event that failed to sync
|
|
40
|
+
* @param reason - Failure reason description
|
|
41
|
+
* @returns DLQ entry ID
|
|
42
|
+
*/
|
|
43
|
+
export declare function addToDeadLetter(event: Event, reason: string): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Get all DLQ entries, optionally filtered by status
|
|
46
|
+
*
|
|
47
|
+
* @param status - Optional status filter
|
|
48
|
+
* @returns Array of DLQ entries
|
|
49
|
+
*/
|
|
50
|
+
export declare function getDeadLetterEntries(status?: 'pending' | 'retrying' | 'resolved' | 'abandoned'): Promise<DeadLetterEntry[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Get a specific DLQ entry by ID
|
|
53
|
+
*
|
|
54
|
+
* @param entryId - DLQ entry ID
|
|
55
|
+
* @returns DLQ entry or null if not found
|
|
56
|
+
*/
|
|
57
|
+
export declare function getDeadLetterEntry(entryId: string): Promise<DeadLetterEntry | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Retry a specific DLQ entry
|
|
60
|
+
*
|
|
61
|
+
* Attempts to sync the event again, respecting retry count and delays
|
|
62
|
+
*
|
|
63
|
+
* @param entryId - DLQ entry ID to retry
|
|
64
|
+
* @param config - Optional DLQ configuration
|
|
65
|
+
* @returns Success status and updated entry
|
|
66
|
+
*/
|
|
67
|
+
export declare function retryDeadLetter(entryId: string, config?: Partial<DLQConfig>): Promise<{
|
|
68
|
+
success: boolean;
|
|
69
|
+
entry: DeadLetterEntry;
|
|
70
|
+
error?: string;
|
|
71
|
+
}>;
|
|
72
|
+
/**
|
|
73
|
+
* Auto-retry eligible DLQ entries
|
|
74
|
+
*
|
|
75
|
+
* Called periodically to retry pending entries that meet delay requirements
|
|
76
|
+
*
|
|
77
|
+
* @param config - Optional DLQ configuration
|
|
78
|
+
* @returns Number of entries retried
|
|
79
|
+
*/
|
|
80
|
+
export declare function autoRetryDeadLetters(config?: Partial<DLQConfig>): Promise<number>;
|
|
81
|
+
/**
|
|
82
|
+
* Delete a resolved or abandoned DLQ entry
|
|
83
|
+
*
|
|
84
|
+
* @param entryId - DLQ entry ID to delete
|
|
85
|
+
*/
|
|
86
|
+
export declare function deleteDeadLetterEntry(entryId: string): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Clean up old resolved/abandoned entries
|
|
89
|
+
*
|
|
90
|
+
* @param olderThanDays - Delete entries older than N days (default 30)
|
|
91
|
+
* @returns Number of entries deleted
|
|
92
|
+
*/
|
|
93
|
+
export declare function cleanupDeadLetters(olderThanDays?: number): Promise<number>;
|
|
94
|
+
/**
|
|
95
|
+
* Get DLQ statistics
|
|
96
|
+
*
|
|
97
|
+
* @returns Summary of DLQ state
|
|
98
|
+
*/
|
|
99
|
+
export declare function getDeadLetterStats(): Promise<{
|
|
100
|
+
pending: number;
|
|
101
|
+
retrying: number;
|
|
102
|
+
resolved: number;
|
|
103
|
+
abandoned: number;
|
|
104
|
+
total: number;
|
|
105
|
+
oldestPending?: Date;
|
|
106
|
+
}>;
|
|
107
|
+
export {};
|
|
108
|
+
//# sourceMappingURL=dead-letter-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dead-letter-queue.d.ts","sourceRoot":"","sources":["../../src/lib/dead-letter-queue.ts"],"names":[],"mappings":"AAAA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