MCPcopy Create free account
hub / github.com/SethGammon/Citadel / run

Function run

hooks_src/cost-tracker.js:180–350  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

178}
179
180function run() {
181 const policy = readPolicy();
182 if (!policy.enabled) {
183 process.exit(0);
184 }
185
186 const now = Date.now();
187 const state = readState();
188
189 // Time gate: skip if we checked recently
190 if (state && state.lastCheckMs && (now - state.lastCheckMs) < policy.checkIntervalMs) {
191 process.exit(0);
192 }
193
194 // Load the Claude runtime token adapter
195 let sessionTokens;
196 try {
197 sessionTokens = require(path.join(PLUGIN_ROOT, 'runtimes', 'claude-code', 'adapters', 'session-tokens'));
198 } catch {
199 process.exit(0); // module not available
200 }
201
202 // Find current session
203 const sessionId = sessionTokens.getCurrentSessionId();
204 if (!sessionId) {
205 process.exit(0);
206 }
207
208 // Read real token data
209 const result = sessionTokens.readSessionTokens(sessionId);
210 if (!result || result.combined.messages === 0) {
211 writeState({ lastCheckMs: now, sessionId, lastThresholdIndex: -1 });
212 process.exit(0);
213 }
214
215 const cost = sessionTokens.computeCost(result.combined);
216 const tokens = result.combined;
217
218 // Calculate duration and burn rate
219 let durationMin = 0;
220 let burnRate = 0;
221 if (tokens.first_timestamp && tokens.last_timestamp) {
222 durationMin = Math.max(1, Math.round(
223 (new Date(tokens.last_timestamp) - new Date(tokens.first_timestamp)) / 60000
224 ));
225 burnRate = cost / durationMin;
226 }
227
228 // Find which threshold we've crossed
229 const thresholds = policy.thresholds.sort((a, b) => a - b);
230 let currentThresholdIndex = -1;
231 for (let i = thresholds.length - 1; i >= 0; i--) {
232 if (cost >= thresholds[i]) {
233 currentThresholdIndex = i;
234 break;
235 }
236 }
237

Callers 1

mainFunction · 0.70

Calls 5

readPolicyFunction · 0.85
checkCampaignBudgetFunction · 0.85
readStateFunction · 0.70
writeStateFunction · 0.70
formatTokensFunction · 0.70

Tested by

no test coverage detected