troxy-cli 1.29.38 → 1.29.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "troxy-cli",
3
- "version": "1.29.38",
3
+ "version": "1.29.40",
4
4
  "description": "A secure control layer for AI agents: policies across payments, messages, logins, destructive actions, model usage, and secrets, all enforceable from the CLI",
5
5
  "homepage": "https://troxy.io",
6
6
  "bugs": {
@@ -2,5 +2,8 @@
2
2
  "name": "troxy-guard",
3
3
  "version": "0.1.0",
4
4
  "description": "Troxy keeps your Claude usage bar going longer. Type /troxy off to pause it.",
5
- "author": { "name": "Troxy" }
5
+ "author": {
6
+ "name": "Troxy"
7
+ },
8
+ "types": "./types/index.d.ts"
6
9
  }
@@ -50,7 +50,7 @@ async function failClosed($, e, next, err) {
50
50
  //
51
51
  // Ported from the proxy's scorer (troxy-tf-live model-proxy/app.py; bands match
52
52
  // the production ROUTE_SCORER_BANDS=0.2:haiku,0.6:sonnet,0.8:opus,
53
- // never-upward rule, 50k context floor for Haiku). The mod scores each human
53
+ // never-upward rule; the 50k context floor here covers every downward route, not just Haiku). The mod scores each human
54
54
  // prompt once with the user's own session via $.model.complete, so there is no
55
55
  // Troxy round trip and no credential to carry. One decision per prompt, applied
56
56
  // to every step of its turn, so the cache prefix stays stable within a turn.
@@ -62,7 +62,7 @@ const MODELS = {
62
62
  };
63
63
  const RANK = { haiku: 0, sonnet: 1, opus: 2 };
64
64
  const BANDS = [[0.2, 'haiku'], [0.6, 'sonnet'], [0.8, 'opus']];
65
- const HAIKU_CONTEXT_FLOOR_TOKENS = 50000;
65
+ const ROUTE_CONTEXT_FLOOR_TOKENS = 50000;
66
66
  const SCORE_TIMEOUT_MS = 3000;
67
67
  const SCORER_PROMPT_CHARS = 4000;
68
68
  const SCORER_SYSTEM = 'You are a routing classifier for a coding-assistant proxy. Read the request below '
@@ -98,9 +98,9 @@ function pickModel(score, requested, contextTokens) {
98
98
  for (const [limit, name] of BANDS) {
99
99
  if (score <= limit) { if (RANK[name] < RANK[rq]) tier = name; break; }
100
100
  }
101
- if (tier === 'haiku' && rq !== 'haiku' && contextTokens > HAIKU_CONTEXT_FLOOR_TOKENS) {
102
- tier = RANK.sonnet < RANK[rq] ? 'sonnet' : rq;
103
- }
101
+ // The cache is per model: the first prompt on a new model re-writes the whole
102
+ // conversation, so past the floor a switch costs more than one cheap turn saves.
103
+ if (tier !== rq && contextTokens > ROUTE_CONTEXT_FLOOR_TOKENS) tier = rq;
104
104
  return tier === rq ? requested : MODELS[tier];
105
105
  }
106
106
 
@@ -119,7 +119,16 @@ function troxyCommand(args, paused) {
119
119
  // ponytail: turn-weighted and ignores cache writes after a model switch, so it
120
120
  // is an estimate; weight by r.usage tokens if the number must be exact.
121
121
  const MODEL_COST = { haiku: 0.3, sonnet: 0.5, opus: 1.0 };
122
- const stats = { prompts: 0, routed: 0, cost: 0 }; // cost: actual spend in "requested-model prompts"
122
+ // Kept in $.state (per session) so a `troxy update` hot reload keeps the counts.
123
+ // cost: actual spend in "requested-model prompts".
124
+ const STATS = { plugin: 'troxy-guard', key: 'stats' };
125
+
126
+ async function addStats($, prompts, routed, cost) {
127
+ const { value: s = { prompts: 0, routed: 0, cost: 0 } } = await $.state.get(STATS);
128
+ const next = { prompts: s.prompts + prompts, routed: s.routed + routed, cost: s.cost + cost };
129
+ await $.state.set(STATS, next);
130
+ $.ui.status(usageLine(next));
131
+ }
123
132
 
124
133
  function costRatio(fromModel, toModel) {
125
134
  const f = MODEL_COST[tierOf(fromModel)], t = MODEL_COST[tierOf(toModel)];
@@ -162,9 +171,7 @@ async function scorePrompt($, e, next) {
162
171
  );
163
172
  const score = r.isAnswered ? parseScore(r.text) : null;
164
173
  $.ui.log(`troxy scorer: ${r.isAnswered ? JSON.stringify(r.text) : r.reason} -> ${score}`, { to: 'debug' });
165
- stats.prompts += 1;
166
- stats.cost += 1; // until routeStep says otherwise
167
- $.ui.status(usageLine(stats));
174
+ await addStats($, 1, 0, 1); // full cost until routeStep says otherwise
168
175
  if (score !== null) {
169
176
  const u = await $.session.usage();
170
177
  decision = { score, tokens: estimateTokens(u.context && u.context.tokens, e.text) };
@@ -179,9 +186,7 @@ async function* routeStep($, e, next) {
179
186
  if (model !== e.model) {
180
187
  if (!decision.counted) {
181
188
  decision.counted = true;
182
- stats.routed += 1;
183
- stats.cost += costRatio(e.model, model) - 1;
184
- $.ui.status(usageLine(stats));
189
+ await addStats($, 0, 1, costRatio(e.model, model) - 1);
185
190
  }
186
191
  const sid = await $.session.id();
187
192
  const file = modRouteFile($.plugin.root, sid);
@@ -198,6 +203,9 @@ export function register(on) {
198
203
  on('tool.call', { tool: 'Bash' }, guardBash).catch(failClosed);
199
204
  on('session.start', async ($, e, next) => {
200
205
  await $.command.register({ name: 'troxy', description: 'Pause or resume Troxy model routing', argumentHint: '[on|off|status]' });
206
+ // session.start fires again on a hot reload: put the bar back from the kept counts
207
+ const { value } = await $.state.get(STATS);
208
+ if (value && value.prompts && !(await $.store.get('paused'))) $.ui.status(usageLine(value));
201
209
  return next(e);
202
210
  });
203
211
  on('command.run', { command: 'troxy' }, async ($, e) => {
@@ -0,0 +1,7 @@
1
+ export type UsageStats = { prompts: number; routed: number; cost: number }
2
+
3
+ declare module 'claude-code' {
4
+ interface PluginState {
5
+ 'troxy-guard': { stats: UsageStats }
6
+ }
7
+ }
@@ -31,10 +31,12 @@ test('pickModel: never routes upward', () => {
31
31
  assert.equal(pickModel(0.4, MODELS.haiku, 1000), MODELS.haiku);
32
32
  });
33
33
 
34
- test('pickModel: no haiku once the context passes the 50k floor, sonnet instead', () => {
35
- assert.equal(pickModel(0.05, MODELS.opus, 60000), MODELS.sonnet);
34
+ test('pickModel: past the 50k floor nothing routes down, a cold cache would cost more than it saves', () => {
35
+ assert.equal(pickModel(0.05, MODELS.opus, 60000), MODELS.opus);
36
+ assert.equal(pickModel(0.4, MODELS.opus, 60000), MODELS.opus);
36
37
  assert.equal(pickModel(0.05, MODELS.sonnet, 60000), MODELS.sonnet);
37
38
  assert.equal(pickModel(0.05, MODELS.haiku, 60000), MODELS.haiku);
39
+ assert.equal(pickModel(0.4, MODELS.opus, 50000), MODELS.sonnet);
38
40
  });
39
41
 
40
42
  test('pickModel: a model it does not recognise is left alone', () => {
@@ -71,6 +73,6 @@ test('estimateTokens: counts the prompt being submitted, not just the context so
71
73
  assert.equal(estimateTokens(30000, 'x'.repeat(40000)), 40000);
72
74
  assert.equal(estimateTokens(undefined, 'hi'), 1);
73
75
  assert.equal(estimateTokens(5, ''), 5);
74
- // a first-prompt of ~60k tokens must trip the Haiku floor
75
- assert.equal(pickModel(0.05, MODELS.opus, estimateTokens(0, 'x'.repeat(240000))), MODELS.sonnet);
76
+ // a first-prompt of ~60k tokens must trip the floor
77
+ assert.equal(pickModel(0.05, MODELS.opus, estimateTokens(0, 'x'.repeat(240000))), MODELS.opus);
76
78
  });