troxy-cli 1.29.29 → 1.29.30

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.29",
3
+ "version": "1.29.30",
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": {
@@ -77,6 +77,13 @@ function tierOf(model) {
77
77
  return null;
78
78
  }
79
79
 
80
+ // Context so far plus the prompt being submitted (about 4 characters a token). The
81
+ // context figure alone is ~0 on a session's first prompt, which let a huge first
82
+ // prompt through to Haiku.
83
+ function estimateTokens(contextTokens, text) {
84
+ return (Number(contextTokens) || 0) + Math.ceil(String(text || '').length / 4);
85
+ }
86
+
80
87
  function parseScore(text) {
81
88
  const m = /\d*\.?\d+/.exec(String(text || ''));
82
89
  if (!m) return null;
@@ -138,7 +145,7 @@ async function scorePrompt($, e, next) {
138
145
  const score = r.isAnswered ? parseScore(r.text) : null;
139
146
  if (score !== null) {
140
147
  const u = await $.session.usage();
141
- decision = { score, tokens: u.context ? u.context.tokens : 0 };
148
+ decision = { score, tokens: estimateTokens(u.context && u.context.tokens, e.text) };
142
149
  }
143
150
  return next(e);
144
151
  }
@@ -181,4 +188,4 @@ export function register(on) {
181
188
  }
182
189
 
183
190
  // Exported for tests only - not part of the mod's runtime contract.
184
- export const _internal = { toDomainAndFields, guardBash, failClosed, parseScore, pickModel, troxyCommand, modRouteFile, MODELS };
191
+ export const _internal = { toDomainAndFields, guardBash, failClosed, parseScore, pickModel, troxyCommand, modRouteFile, estimateTokens, MODELS };
@@ -2,7 +2,7 @@ import { test } from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
3
  import { _internal } from '../mods/troxy-guard/hooks/register.js';
4
4
 
5
- const { parseScore, pickModel, troxyCommand, modRouteFile, MODELS } = _internal;
5
+ const { parseScore, pickModel, troxyCommand, modRouteFile, estimateTokens, MODELS } = _internal;
6
6
 
7
7
  test('parseScore: reads a bare number and clamps to 0..1', () => {
8
8
  assert.equal(parseScore('0.35'), 0.35);
@@ -65,3 +65,12 @@ test('modRouteFile: null when not installed in ~/.claude/skills (e.g. --plugin-d
65
65
  assert.equal(modRouteFile('/work/troxy/src/mods/troxy-guard', sid), null);
66
66
  assert.equal(modRouteFile('/Users/a/.claude/skills/troxy-guard', '../x'), null);
67
67
  });
68
+
69
+ test('estimateTokens: counts the prompt being submitted, not just the context so far', () => {
70
+ assert.equal(estimateTokens(0, 'x'.repeat(240000)), 60000);
71
+ assert.equal(estimateTokens(30000, 'x'.repeat(40000)), 40000);
72
+ assert.equal(estimateTokens(undefined, 'hi'), 1);
73
+ 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
+ });