context-mode 1.0.65 → 1.0.67
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.openclaw-plugin/openclaw.plugin.json +1 -1
- package/.openclaw-plugin/package.json +1 -1
- package/README.md +3 -1
- package/build/cli.js +28 -0
- package/build/server.js +205 -205
- package/build/session/analytics.d.ts +381 -0
- package/build/session/analytics.js +708 -0
- package/cli.bundle.mjs +208 -148
- package/configs/antigravity/GEMINI.md +3 -0
- package/configs/claude-code/CLAUDE.md +3 -0
- package/configs/codex/AGENTS.md +3 -0
- package/configs/gemini-cli/GEMINI.md +3 -0
- package/configs/kilo/AGENTS.md +3 -0
- package/configs/kiro/KIRO.md +3 -0
- package/configs/openclaw/AGENTS.md +3 -0
- package/configs/opencode/AGENTS.md +3 -0
- package/configs/pi/AGENTS.md +3 -0
- package/configs/vscode-copilot/copilot-instructions.md +3 -0
- package/configs/zed/AGENTS.md +3 -0
- package/hooks/routing-block.mjs +4 -1
- package/hooks/session-helpers.mjs +0 -12
- package/hooks/sessionstart.mjs +2 -5
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/server.bundle.mjs +134 -74
- package/skills/context-mode/SKILL.md +2 -0
- package/skills/context-mode-ops/SKILL.md +18 -0
- package/skills/context-mode-ops/release.md +15 -0
- package/skills/ctx-purge/SKILL.md +35 -0
- package/skills/ctx-stats/SKILL.md +6 -0
- package/start.mjs +7 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AnalyticsEngine — All 27 metrics from SessionDB.
|
|
3
|
+
*
|
|
4
|
+
* Computes session-level and cross-session analytics using SQL queries
|
|
5
|
+
* and JavaScript post-processing. Groups:
|
|
6
|
+
*
|
|
7
|
+
* Group 1 (SQL Direct): 17 metrics — direct SQL against session tables
|
|
8
|
+
* Group 2 (JS Computed): 3 metrics — SQL + JS post-processing
|
|
9
|
+
* Group 3 (Runtime): 4 metrics — stubs for server.ts tracking
|
|
10
|
+
* Group 4 (New Extractor): 3 metrics — stubs for future extractors
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* const engine = new AnalyticsEngine(sessionDb);
|
|
14
|
+
* const report = engine.queryAll(runtimeStats);
|
|
15
|
+
*/
|
|
16
|
+
/** Database adapter — anything with a prepare() method (better-sqlite3, bun:sqlite, etc.) */
|
|
17
|
+
export interface DatabaseAdapter {
|
|
18
|
+
prepare(sql: string): {
|
|
19
|
+
run(...params: unknown[]): unknown;
|
|
20
|
+
get(...params: unknown[]): unknown;
|
|
21
|
+
all(...params: unknown[]): unknown[];
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Weekly trend data point */
|
|
25
|
+
export interface WeeklyTrendRow {
|
|
26
|
+
day: string;
|
|
27
|
+
sessions: number;
|
|
28
|
+
}
|
|
29
|
+
/** Category distribution row */
|
|
30
|
+
export interface ContinuityRow {
|
|
31
|
+
category: string;
|
|
32
|
+
count: number;
|
|
33
|
+
}
|
|
34
|
+
/** Hourly productivity row */
|
|
35
|
+
export interface HourlyRow {
|
|
36
|
+
hour: string;
|
|
37
|
+
count: number;
|
|
38
|
+
}
|
|
39
|
+
/** Project distribution row */
|
|
40
|
+
export interface ProjectRow {
|
|
41
|
+
project_dir: string;
|
|
42
|
+
sessions: number;
|
|
43
|
+
}
|
|
44
|
+
/** CLAUDE.md freshness row */
|
|
45
|
+
export interface FreshnessRow {
|
|
46
|
+
data: string;
|
|
47
|
+
last_updated: string;
|
|
48
|
+
}
|
|
49
|
+
/** Rework rate row */
|
|
50
|
+
export interface ReworkRow {
|
|
51
|
+
data: string;
|
|
52
|
+
edits: number;
|
|
53
|
+
}
|
|
54
|
+
/** Subagent usage row */
|
|
55
|
+
export interface SubagentRow {
|
|
56
|
+
data: string;
|
|
57
|
+
total: number;
|
|
58
|
+
}
|
|
59
|
+
/** Skill usage row */
|
|
60
|
+
export interface SkillRow {
|
|
61
|
+
data: string;
|
|
62
|
+
invocations: number;
|
|
63
|
+
}
|
|
64
|
+
/** Context savings result (#1) */
|
|
65
|
+
export interface ContextSavings {
|
|
66
|
+
rawBytes: number;
|
|
67
|
+
contextBytes: number;
|
|
68
|
+
savedBytes: number;
|
|
69
|
+
savedPercent: number;
|
|
70
|
+
}
|
|
71
|
+
/** Think in code comparison result (#2) */
|
|
72
|
+
export interface ThinkInCodeComparison {
|
|
73
|
+
fileBytes: number;
|
|
74
|
+
outputBytes: number;
|
|
75
|
+
ratio: number;
|
|
76
|
+
}
|
|
77
|
+
/** Tool-level savings result (#3) */
|
|
78
|
+
export interface ToolSavingsRow {
|
|
79
|
+
tool: string;
|
|
80
|
+
rawBytes: number;
|
|
81
|
+
contextBytes: number;
|
|
82
|
+
savedBytes: number;
|
|
83
|
+
}
|
|
84
|
+
/** Sandbox I/O result (#19) */
|
|
85
|
+
export interface SandboxIO {
|
|
86
|
+
inputBytes: number;
|
|
87
|
+
outputBytes: number;
|
|
88
|
+
}
|
|
89
|
+
/** Pattern insight result (#6) */
|
|
90
|
+
export interface PatternInsight {
|
|
91
|
+
pattern: string;
|
|
92
|
+
confidence: number;
|
|
93
|
+
}
|
|
94
|
+
/** Runtime stats tracked by server.ts during a live session. */
|
|
95
|
+
export interface RuntimeStats {
|
|
96
|
+
bytesReturned: Record<string, number>;
|
|
97
|
+
bytesIndexed: number;
|
|
98
|
+
bytesSandboxed: number;
|
|
99
|
+
calls: Record<string, number>;
|
|
100
|
+
sessionStart: number;
|
|
101
|
+
cacheHits: number;
|
|
102
|
+
cacheBytesSaved: number;
|
|
103
|
+
}
|
|
104
|
+
/** Unified report combining runtime stats, DB analytics, and continuity data. */
|
|
105
|
+
export interface FullReport {
|
|
106
|
+
/** Runtime context savings (passed in, not from DB) */
|
|
107
|
+
savings: {
|
|
108
|
+
processed_kb: number;
|
|
109
|
+
entered_kb: number;
|
|
110
|
+
saved_kb: number;
|
|
111
|
+
pct: number;
|
|
112
|
+
savings_ratio: number;
|
|
113
|
+
by_tool: Array<{
|
|
114
|
+
tool: string;
|
|
115
|
+
calls: number;
|
|
116
|
+
context_kb: number;
|
|
117
|
+
tokens: number;
|
|
118
|
+
}>;
|
|
119
|
+
total_calls: number;
|
|
120
|
+
total_bytes_returned: number;
|
|
121
|
+
kept_out: number;
|
|
122
|
+
total_processed: number;
|
|
123
|
+
};
|
|
124
|
+
cache?: {
|
|
125
|
+
hits: number;
|
|
126
|
+
bytes_saved: number;
|
|
127
|
+
ttl_hours_left: number;
|
|
128
|
+
total_with_cache: number;
|
|
129
|
+
total_savings_ratio: number;
|
|
130
|
+
};
|
|
131
|
+
/** Session metadata from SessionDB */
|
|
132
|
+
session: {
|
|
133
|
+
id: string;
|
|
134
|
+
duration_min: number | null;
|
|
135
|
+
tool_calls: number;
|
|
136
|
+
uptime_min: string;
|
|
137
|
+
};
|
|
138
|
+
/** Activity metrics */
|
|
139
|
+
activity: {
|
|
140
|
+
commits: number;
|
|
141
|
+
errors: number;
|
|
142
|
+
error_rate_pct: number;
|
|
143
|
+
tool_diversity: number;
|
|
144
|
+
efficiency_score: number;
|
|
145
|
+
commits_per_session_avg: number;
|
|
146
|
+
session_outcome: string;
|
|
147
|
+
productive_pct: number;
|
|
148
|
+
exploratory_pct: number;
|
|
149
|
+
};
|
|
150
|
+
/** Pattern metrics */
|
|
151
|
+
patterns: {
|
|
152
|
+
hourly_commits: number[];
|
|
153
|
+
weekly_trend: Array<{
|
|
154
|
+
day: string;
|
|
155
|
+
sessions: number;
|
|
156
|
+
}>;
|
|
157
|
+
iteration_cycles: number;
|
|
158
|
+
rework: Array<{
|
|
159
|
+
file: string;
|
|
160
|
+
edits: number;
|
|
161
|
+
}>;
|
|
162
|
+
};
|
|
163
|
+
/** Health metrics */
|
|
164
|
+
health: {
|
|
165
|
+
claude_md_freshness: Array<{
|
|
166
|
+
project: string;
|
|
167
|
+
days_ago: number | null;
|
|
168
|
+
}>;
|
|
169
|
+
compactions_this_week: number;
|
|
170
|
+
weekly_sessions: number;
|
|
171
|
+
permission_denials: number;
|
|
172
|
+
};
|
|
173
|
+
/** Agent metrics */
|
|
174
|
+
agents: {
|
|
175
|
+
subagents: Array<{
|
|
176
|
+
type: string;
|
|
177
|
+
count: number;
|
|
178
|
+
}>;
|
|
179
|
+
skills: Array<{
|
|
180
|
+
name: string;
|
|
181
|
+
count: number;
|
|
182
|
+
}>;
|
|
183
|
+
};
|
|
184
|
+
/** Session continuity data */
|
|
185
|
+
continuity: {
|
|
186
|
+
total_events: number;
|
|
187
|
+
by_category: Array<{
|
|
188
|
+
category: string;
|
|
189
|
+
count: number;
|
|
190
|
+
label: string;
|
|
191
|
+
preview: string;
|
|
192
|
+
why: string;
|
|
193
|
+
}>;
|
|
194
|
+
compact_count: number;
|
|
195
|
+
resume_ready: boolean;
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** Human-readable labels for event categories. */
|
|
199
|
+
export declare const categoryLabels: Record<string, string>;
|
|
200
|
+
/** Explains why each category matters for continuity. */
|
|
201
|
+
export declare const categoryHints: Record<string, string>;
|
|
202
|
+
export declare class AnalyticsEngine {
|
|
203
|
+
private readonly db;
|
|
204
|
+
/**
|
|
205
|
+
* Create an AnalyticsEngine.
|
|
206
|
+
*
|
|
207
|
+
* Accepts either a SessionDB instance (extracts internal db via
|
|
208
|
+
* the protected getter — use the static fromDB helper for raw adapters)
|
|
209
|
+
* or any object with a prepare() method for direct usage.
|
|
210
|
+
*/
|
|
211
|
+
constructor(db: DatabaseAdapter);
|
|
212
|
+
/**
|
|
213
|
+
* #5 Weekly Trend — sessions started per day over the last 7 days.
|
|
214
|
+
* Returns an array of { day, sessions } sorted by day.
|
|
215
|
+
*/
|
|
216
|
+
weeklyTrend(): WeeklyTrendRow[];
|
|
217
|
+
/**
|
|
218
|
+
* #7 Session Continuity — event category distribution for a session.
|
|
219
|
+
* Shows what the session focused on (file ops, git, errors, etc.).
|
|
220
|
+
*/
|
|
221
|
+
sessionContinuity(sessionId: string): ContinuityRow[];
|
|
222
|
+
/**
|
|
223
|
+
* #8 Commit Count — number of git commits made during a session.
|
|
224
|
+
* Matches events where category='git' and data contains 'commit'.
|
|
225
|
+
*/
|
|
226
|
+
commitCount(sessionId: string): number;
|
|
227
|
+
/**
|
|
228
|
+
* #9 Error Count — total error events in a session.
|
|
229
|
+
*/
|
|
230
|
+
errorCount(sessionId: string): number;
|
|
231
|
+
/**
|
|
232
|
+
* #10 Session Duration — elapsed minutes from session start to last event.
|
|
233
|
+
* Returns null if last_event_at is not set (session still initializing).
|
|
234
|
+
*/
|
|
235
|
+
sessionDuration(sessionId: string): number | null;
|
|
236
|
+
/**
|
|
237
|
+
* #11 Error Rate — percentage of events that are errors in a session.
|
|
238
|
+
* Returns 0 for sessions with no events (division by zero protection).
|
|
239
|
+
*/
|
|
240
|
+
errorRate(sessionId: string): number;
|
|
241
|
+
/**
|
|
242
|
+
* #12 Tool Diversity — number of distinct MCP tools used in a session.
|
|
243
|
+
* Higher diversity suggests more sophisticated tool usage.
|
|
244
|
+
*/
|
|
245
|
+
toolDiversity(sessionId: string): number;
|
|
246
|
+
/**
|
|
247
|
+
* #14 Hourly Productivity — event distribution by hour of day.
|
|
248
|
+
* Optionally scoped to a session; omit sessionId for all sessions.
|
|
249
|
+
*/
|
|
250
|
+
hourlyProductivity(sessionId?: string): HourlyRow[];
|
|
251
|
+
/**
|
|
252
|
+
* #15 Project Distribution — session count per project directory.
|
|
253
|
+
* Sorted descending by session count.
|
|
254
|
+
*/
|
|
255
|
+
projectDistribution(): ProjectRow[];
|
|
256
|
+
/**
|
|
257
|
+
* #16 Compaction Count — number of snapshot compactions for a session.
|
|
258
|
+
* Higher counts indicate longer/more active sessions.
|
|
259
|
+
*/
|
|
260
|
+
compactionCount(sessionId: string): number;
|
|
261
|
+
/**
|
|
262
|
+
* #17 Weekly Session Count — total sessions started in the last 7 days.
|
|
263
|
+
*/
|
|
264
|
+
weeklySessionCount(): number;
|
|
265
|
+
/**
|
|
266
|
+
* #18 Commits Per Session — average commits across all sessions.
|
|
267
|
+
* Returns 0 when no sessions exist (NULLIF prevents division by zero).
|
|
268
|
+
*/
|
|
269
|
+
commitsPerSession(): number;
|
|
270
|
+
/**
|
|
271
|
+
* #22 CLAUDE.md Freshness — last update timestamp for each rule file.
|
|
272
|
+
* Helps identify stale configuration files.
|
|
273
|
+
*/
|
|
274
|
+
claudeMdFreshness(): FreshnessRow[];
|
|
275
|
+
/**
|
|
276
|
+
* #24 Rework Rate — files edited more than once (indicates iteration/rework).
|
|
277
|
+
* Sorted descending by edit count.
|
|
278
|
+
*/
|
|
279
|
+
reworkRate(sessionId?: string): ReworkRow[];
|
|
280
|
+
/**
|
|
281
|
+
* #25 Session Outcome — classify a session as 'productive' or 'exploratory'.
|
|
282
|
+
* Productive: has at least one commit AND last event is not an error.
|
|
283
|
+
*/
|
|
284
|
+
sessionOutcome(sessionId: string): "productive" | "exploratory";
|
|
285
|
+
/**
|
|
286
|
+
* #26 Subagent Usage — subagent spawn counts grouped by type/purpose.
|
|
287
|
+
*/
|
|
288
|
+
subagentUsage(sessionId: string): SubagentRow[];
|
|
289
|
+
/**
|
|
290
|
+
* #27 Skill Usage — skill/slash-command invocation frequency.
|
|
291
|
+
* Sorted descending by invocation count.
|
|
292
|
+
*/
|
|
293
|
+
skillUsage(sessionId: string): SkillRow[];
|
|
294
|
+
/**
|
|
295
|
+
* #4 Session Mix — percentage of sessions classified as productive.
|
|
296
|
+
* Iterates all sessions and uses #25 (sessionOutcome) to classify each.
|
|
297
|
+
*/
|
|
298
|
+
sessionMix(): {
|
|
299
|
+
productive: number;
|
|
300
|
+
exploratory: number;
|
|
301
|
+
};
|
|
302
|
+
/**
|
|
303
|
+
* #13 / #20 Efficiency Score — composite score (0-100) measuring session productivity.
|
|
304
|
+
*
|
|
305
|
+
* Components:
|
|
306
|
+
* - Error rate (lower = better): weight 30%
|
|
307
|
+
* - Tool diversity (higher = better): weight 20%
|
|
308
|
+
* - Commit presence (boolean bonus): weight 25%
|
|
309
|
+
* - Rework rate (lower = better): weight 15%
|
|
310
|
+
* - Session duration efficiency (moderate = better): weight 10%
|
|
311
|
+
*
|
|
312
|
+
* Formula: score = 100 - errorPenalty + diversityBonus + commitBonus - reworkPenalty + durationBonus - 40
|
|
313
|
+
* The -40 baseline prevents empty sessions from scoring 100.
|
|
314
|
+
*/
|
|
315
|
+
efficiencyScore(sessionId: string): number;
|
|
316
|
+
/**
|
|
317
|
+
* #23 Iteration Cycles — counts edit-error-fix sequences in a session.
|
|
318
|
+
*
|
|
319
|
+
* Walks events chronologically and detects patterns where a file event
|
|
320
|
+
* is followed by an error event, then another file event.
|
|
321
|
+
*/
|
|
322
|
+
iterationCycles(sessionId: string): number;
|
|
323
|
+
/**
|
|
324
|
+
* #1 Context Savings Total — bytes kept out of context window.
|
|
325
|
+
*
|
|
326
|
+
* Stub: requires server.ts to accumulate rawBytes and contextBytes
|
|
327
|
+
* during a live session. Call with tracked values.
|
|
328
|
+
*/
|
|
329
|
+
static contextSavingsTotal(rawBytes: number, contextBytes: number): ContextSavings;
|
|
330
|
+
/**
|
|
331
|
+
* #2 Think in Code Comparison — ratio of file size to sandbox output size.
|
|
332
|
+
*
|
|
333
|
+
* Stub: requires server.ts tracking of execute/execute_file calls.
|
|
334
|
+
*/
|
|
335
|
+
static thinkInCodeComparison(fileBytes: number, outputBytes: number): ThinkInCodeComparison;
|
|
336
|
+
/**
|
|
337
|
+
* #3 Tool Savings — per-tool breakdown of context savings.
|
|
338
|
+
*
|
|
339
|
+
* Stub: requires per-tool accumulators in server.ts.
|
|
340
|
+
*/
|
|
341
|
+
static toolSavings(tools: Array<{
|
|
342
|
+
tool: string;
|
|
343
|
+
rawBytes: number;
|
|
344
|
+
contextBytes: number;
|
|
345
|
+
}>): ToolSavingsRow[];
|
|
346
|
+
/**
|
|
347
|
+
* #19 Sandbox I/O — total input/output bytes processed by the sandbox.
|
|
348
|
+
*
|
|
349
|
+
* Stub: requires PolyglotExecutor byte counters.
|
|
350
|
+
*/
|
|
351
|
+
static sandboxIO(inputBytes: number, outputBytes: number): SandboxIO;
|
|
352
|
+
/**
|
|
353
|
+
* #6 Pattern Detected — identifies recurring patterns in a session.
|
|
354
|
+
*
|
|
355
|
+
* Analyzes category distribution and detects dominant patterns
|
|
356
|
+
* (>60% threshold). Falls back to combination detection and
|
|
357
|
+
* "balanced" for evenly distributed sessions.
|
|
358
|
+
*/
|
|
359
|
+
patternDetected(sessionId: string): string;
|
|
360
|
+
/**
|
|
361
|
+
* #21 Permission Denials — count of tool calls blocked by security rules.
|
|
362
|
+
*
|
|
363
|
+
* Filters error events containing "denied", "blocked", or "permission".
|
|
364
|
+
* Stub: ideally requires a dedicated extractor in extract.ts.
|
|
365
|
+
*/
|
|
366
|
+
permissionDenials(sessionId: string): number;
|
|
367
|
+
/**
|
|
368
|
+
* Build a complete FullReport by merging runtime stats (passed in)
|
|
369
|
+
* with all 27 DB-backed metrics and continuity data.
|
|
370
|
+
*
|
|
371
|
+
* This is the ONE call that ctx_stats should use.
|
|
372
|
+
*/
|
|
373
|
+
queryAll(runtimeStats: RuntimeStats): FullReport;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Render a FullReport as the same markdown output ctx_stats has always produced.
|
|
377
|
+
*
|
|
378
|
+
* Preserves the exact output format: Context Window Protection table,
|
|
379
|
+
* TTL Cache section, Session Continuity table, and Analytics JSON block.
|
|
380
|
+
*/
|
|
381
|
+
export declare function formatReport(report: FullReport): string;
|