@plexor-dev/claude-code-plugin-staging 0.1.0-beta.27 → 0.1.0-beta.28

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.
@@ -17,7 +17,7 @@
17
17
  const path = require('path');
18
18
 
19
19
  // Use lib modules
20
- let ConfigManager, SessionManager, LocalCache, Logger, ServerSync;
20
+ let ConfigManager, SessionManager, LocalCache, Logger, ServerSync, SupervisorEmitter;
21
21
  try {
22
22
  ConfigManager = require('../lib/config');
23
23
  SessionManager = require('../lib/session');
@@ -26,6 +26,8 @@ try {
26
26
  // Issue #701: Phase 2 - Server sync for persistent session state
27
27
  const serverSyncModule = require('../lib/server-sync');
28
28
  ServerSync = serverSyncModule.getServerSync;
29
+ // Phase 1 supervisor UX
30
+ SupervisorEmitter = require('../lib/supervisor').SupervisorEmitter;
29
31
  } catch {
30
32
  // Fallback inline implementations if lib not found
31
33
  const fs = require('fs');
@@ -171,12 +173,18 @@ try {
171
173
  scheduleSync: () => {},
172
174
  needsSync: () => false
173
175
  });
176
+
177
+ // Fallback SupervisorEmitter (no-op)
178
+ SupervisorEmitter = class {
179
+ emit() {}
180
+ };
174
181
  }
175
182
 
176
183
  const logger = new Logger('track-response');
177
184
  const config = new ConfigManager();
178
185
  const cache = new LocalCache();
179
186
  const session = new SessionManager();
187
+ const supervisor = new SupervisorEmitter();
180
188
 
181
189
  // Issue #701: Phase 2 - Initialize server sync (lazy, initialized on first use)
182
190
  let serverSync = null;
@@ -189,7 +197,7 @@ async function getServerSync() {
189
197
  if (settings.apiKey && settings.enabled) {
190
198
  serverSync = ServerSync({
191
199
  apiKey: settings.apiKey,
192
- baseUrl: settings.apiUrl || 'https://api.plexor.dev',
200
+ baseUrl: settings.apiUrl || 'http://127.0.0.1:8000',
193
201
  enabled: settings.serverSyncEnabled !== false
194
202
  });
195
203
  } else {
@@ -221,6 +229,12 @@ async function main() {
221
229
  const plexorMeta = response._plexor;
222
230
  emitPlexorOutcomeSummary(response, plexorMeta, outputTokens);
223
231
 
232
+ // Phase 1 supervisor UX: concise single-line routing summary
233
+ supervisor.emit(response, plexorMeta);
234
+
235
+ // Proactive compact warning: check prompt token count against provider limits
236
+ emitCompactWarning(response, plexorMeta);
237
+
224
238
  // Issue #701: Track ALL responses, not just when enabled
225
239
  // This ensures session stats are always accurate
226
240
  if (plexorMeta) {
@@ -674,3 +688,29 @@ function emitPlexorOutcomeSummary(response, plexorMeta, outputTokens) {
674
688
  logger.ux(msg);
675
689
  }
676
690
  }
691
+
692
+ /**
693
+ * Proactive compact warning: emit context-size alerts at 70K and 80K prompt tokens.
694
+ * Uses the per-request prompt token count (usage.input_tokens / usage.prompt_tokens)
695
+ * which represents the current context window size for that call.
696
+ */
697
+ function emitCompactWarning(response, plexorMeta) {
698
+ if (!logger || typeof logger.ux !== 'function') return;
699
+
700
+ const promptTokens =
701
+ toNumber(response?.plexor_prompt_tokens) ??
702
+ toNumber(response?.usage?.input_tokens) ??
703
+ toNumber(response?.usage?.prompt_tokens) ??
704
+ toNumber(plexorMeta?.optimized_tokens) ??
705
+ null;
706
+
707
+ if (promptTokens === null || promptTokens < 70000) return;
708
+
709
+ const tokensK = Math.round(promptTokens / 1000);
710
+
711
+ if (promptTokens >= 80000) {
712
+ logger.ux(`\u26a0 Context at ${tokensK}K tokens \u2014 recommend /compact to prevent errors`);
713
+ } else {
714
+ logger.ux(`Context at ${tokensK}K tokens \u2014 approaching provider limits`);
715
+ }
716
+ }
package/lib/supervisor.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Plexor Supervisor Emitter — Phases 1-4
2
+ * Plexor Supervisor Emitter — Phases 1-5
3
3
  *
4
4
  * Phase 1: Basic routing summary
5
5
  * [PLEXOR: Routed to {provider}/{model}, {latency}ms, {routing_source}]
@@ -13,6 +13,13 @@
13
13
  * Phase 4: Scaffolding gate blocked detection
14
14
  * [PLEXOR: Scaffolding gate: {model} blocked, using {alternative}]
15
15
  *
16
+ * Phase 5: Session narration — cumulative tokens, cost, provider reliability,
17
+ * context warnings, auto-compact suggestions
18
+ * [PLEXOR: Session: 12 turns, $0.42 cost, 45K tokens]
19
+ * [PLEXOR: Provider reliability (last 5): DeepSeek 4/5 tool_calls, Gemini 1/5]
20
+ * [PLEXOR: Context at 72K tokens — approaching provider limits]
21
+ * [PLEXOR: Context at 82K — recommend /compact to prevent errors]
22
+ *
16
23
  * This module is consumed by track-response.js to surface routing
17
24
  * decisions to the developer without requiring them to parse verbose logs.
18
25
  */
@@ -20,8 +27,12 @@
20
27
  const CYAN = '\x1b[36m';
21
28
  const YELLOW = '\x1b[33m';
22
29
  const RED = '\x1b[31m';
30
+ const MAGENTA = '\x1b[35m';
23
31
  const RESET = '\x1b[0m';
24
32
 
33
+ const CONTEXT_WARNING_THRESHOLD = 70000;
34
+ const CONTEXT_COMPACT_THRESHOLD = 80000;
35
+
25
36
  class SupervisorEmitter {
26
37
  /**
27
38
  * @param {object} [opts]
@@ -34,6 +45,12 @@ class SupervisorEmitter {
34
45
  } else {
35
46
  this.enabled = opts.enabled !== false;
36
47
  }
48
+
49
+ // Phase 5: Session-level state
50
+ this._turnCount = 0;
51
+ this._cumulativeTokens = 0;
52
+ this._cumulativeCost = 0;
53
+ this._providerHistory = []; // last N entries: { provider, hadToolCalls }
37
54
  }
38
55
 
39
56
  /**
@@ -201,6 +218,9 @@ class SupervisorEmitter {
201
218
  return;
202
219
  }
203
220
 
221
+ // Phase 5: Accumulate session state before emitting
222
+ this._accumulateSessionState(response, plexorMeta);
223
+
204
224
  // Phase 4: Scaffolding gate (highest priority — emit first if present)
205
225
  const scaffoldingNotice = this.buildScaffoldingGateNotice(response, plexorMeta);
206
226
  if (scaffoldingNotice) {
@@ -218,8 +238,129 @@ class SupervisorEmitter {
218
238
  if (summary) {
219
239
  process.stderr.write(`${CYAN}${summary}${RESET}\n`);
220
240
  }
241
+
242
+ // Phase 5: Session narration (after per-turn messages)
243
+ this._emitSessionNarration();
221
244
  }
222
245
 
246
+ // ---- Phase 5: session narration ----
247
+
248
+ /**
249
+ * Accumulate per-turn data into session state.
250
+ * Called at the start of emit() so all session fields are current
251
+ * before any Phase 5 messages are built.
252
+ */
253
+ _accumulateSessionState(response, plexorMeta) {
254
+ this._turnCount++;
255
+
256
+ // Cumulative token counter — read prompt_tokens from usage block
257
+ const usage = response?.usage || response?.plexor?.usage || {};
258
+ const promptTokens = Number(usage.prompt_tokens) || 0;
259
+ const completionTokens = Number(usage.completion_tokens) || 0;
260
+ this._cumulativeTokens += promptTokens + completionTokens;
261
+
262
+ // Cumulative cost
263
+ const costUsd = Number(
264
+ response?.plexor_cost_usd ??
265
+ response?.plexor?.cost_usd ??
266
+ plexorMeta?.cost_usd ??
267
+ 0
268
+ );
269
+ if (Number.isFinite(costUsd)) {
270
+ this._cumulativeCost += costUsd;
271
+ }
272
+
273
+ // Provider reliability tracking
274
+ const provider = this._resolveProvider(response, plexorMeta);
275
+ if (provider) {
276
+ const stopReason =
277
+ response?.stop_reason ||
278
+ response?.choices?.[0]?.finish_reason ||
279
+ response?.plexor?.stop_reason ||
280
+ null;
281
+ this._providerHistory.push({
282
+ provider,
283
+ hadToolCalls: stopReason === 'tool_use' || stopReason === 'tool_calls',
284
+ });
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Emit session-level narration lines based on accumulated state.
290
+ */
291
+ _emitSessionNarration() {
292
+ // Session summary line — every turn
293
+ const tokenStr = this._formatTokenCount(this._cumulativeTokens);
294
+ const costStr = this._cumulativeCost < 0.01
295
+ ? `$${this._cumulativeCost.toFixed(4)}`
296
+ : `$${this._cumulativeCost.toFixed(2)}`;
297
+ process.stderr.write(
298
+ `${MAGENTA}[PLEXOR: Session: ${this._turnCount} turns, ${costStr} cost, ${tokenStr} tokens]${RESET}\n`
299
+ );
300
+
301
+ // Provider reliability digest — every 5th turn
302
+ if (this._turnCount % 5 === 0 && this._providerHistory.length > 0) {
303
+ const digest = this._buildProviderReliabilityDigest();
304
+ if (digest) {
305
+ process.stderr.write(`${MAGENTA}${digest}${RESET}\n`);
306
+ }
307
+ }
308
+
309
+ // Context warning at 70K tokens
310
+ if (this._cumulativeTokens >= CONTEXT_COMPACT_THRESHOLD) {
311
+ const kTokens = Math.round(this._cumulativeTokens / 1000);
312
+ process.stderr.write(
313
+ `${YELLOW}[PLEXOR: Context at ${kTokens}K \u2014 recommend /compact to prevent errors]${RESET}\n`
314
+ );
315
+ } else if (this._cumulativeTokens >= CONTEXT_WARNING_THRESHOLD) {
316
+ const kTokens = Math.round(this._cumulativeTokens / 1000);
317
+ process.stderr.write(
318
+ `${YELLOW}[PLEXOR: Context at ${kTokens}K tokens \u2014 approaching provider limits]${RESET}\n`
319
+ );
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Build provider reliability digest from the last 5 entries in history.
325
+ * Format: [PLEXOR: Provider reliability (last 5): DeepSeek 4/5 tool_calls, Gemini 1/5]
326
+ */
327
+ _buildProviderReliabilityDigest() {
328
+ const recent = this._providerHistory.slice(-5);
329
+ const totals = {};
330
+ const toolHits = {};
331
+
332
+ for (const entry of recent) {
333
+ const p = entry.provider;
334
+ totals[p] = (totals[p] || 0) + 1;
335
+ if (entry.hadToolCalls) {
336
+ toolHits[p] = (toolHits[p] || 0) + 1;
337
+ }
338
+ }
339
+
340
+ const parts = Object.keys(totals).map(p => {
341
+ const hits = toolHits[p] || 0;
342
+ return `${p}: ${hits}/${totals[p]} tool_calls`;
343
+ });
344
+
345
+ if (parts.length === 0) return null;
346
+ return `[PLEXOR: Provider reliability (last 5): ${parts.join(', ')}]`;
347
+ }
348
+
349
+ /**
350
+ * Format token count: 1234 -> "1.2K", 123456 -> "123K"
351
+ */
352
+ _formatTokenCount(tokens) {
353
+ if (tokens < 1000) return String(tokens);
354
+ if (tokens < 10000) return `${(tokens / 1000).toFixed(1)}K`;
355
+ return `${Math.round(tokens / 1000)}K`;
356
+ }
357
+
358
+ // ---- Phase 5 accessors (for testing) ----
359
+
360
+ get turnCount() { return this._turnCount; }
361
+ get cumulativeTokens() { return this._cumulativeTokens; }
362
+ get cumulativeCost() { return this._cumulativeCost; }
363
+
223
364
  // ---- private helpers ----
224
365
 
225
366
  _resolveProvider(response, meta) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plexor-dev/claude-code-plugin-staging",
3
- "version": "0.1.0-beta.27",
3
+ "version": "0.1.0-beta.28",
4
4
  "description": "STAGING - LLM cost optimization plugin for Claude Code (internal testing)",
5
5
  "main": "lib/constants.js",
6
6
  "bin": {