sentinelayer-cli 0.1.2 → 0.3.0
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/README.md +996 -996
- package/bin/create-sentinelayer.js +5 -5
- package/bin/sentinelayer-cli.js +4 -4
- package/bin/sl.js +5 -5
- package/package.json +62 -54
- package/src/agents/jules/config/definition.js +209 -209
- package/src/agents/jules/config/system-prompt.js +175 -175
- package/src/agents/jules/error-intake.js +51 -51
- package/src/agents/jules/fix-cycle.js +377 -377
- package/src/agents/jules/loop.js +367 -367
- package/src/agents/jules/pulse.js +319 -319
- package/src/agents/jules/stream.js +186 -186
- package/src/agents/jules/swarm/file-scanner.js +74 -74
- package/src/agents/jules/swarm/index.js +11 -11
- package/src/agents/jules/swarm/orchestrator.js +362 -362
- package/src/agents/jules/swarm/pattern-hunter.js +123 -123
- package/src/agents/jules/swarm/sub-agent.js +308 -308
- package/src/agents/jules/tools/auth-audit.js +226 -222
- package/src/agents/jules/tools/dispatch.js +327 -327
- package/src/agents/jules/tools/file-edit.js +180 -180
- package/src/agents/jules/tools/file-read.js +100 -100
- package/src/agents/jules/tools/frontend-analyze.js +570 -570
- package/src/agents/jules/tools/glob.js +168 -168
- package/src/agents/jules/tools/grep.js +228 -228
- package/src/agents/jules/tools/index.js +29 -29
- package/src/agents/jules/tools/path-guards.js +161 -161
- package/src/agents/jules/tools/runtime-audit.js +493 -493
- package/src/agents/jules/tools/shell.js +383 -383
- package/src/ai/aidenid.js +972 -945
- package/src/ai/client.js +508 -508
- package/src/ai/domain-target-store.js +268 -268
- package/src/ai/identity-store.js +270 -270
- package/src/ai/site-store.js +145 -145
- package/src/audit/agents/architecture.js +180 -180
- package/src/audit/agents/compliance.js +179 -179
- package/src/audit/agents/documentation.js +165 -165
- package/src/audit/agents/performance.js +145 -145
- package/src/audit/agents/security.js +215 -215
- package/src/audit/agents/testing.js +172 -172
- package/src/audit/orchestrator.js +557 -557
- package/src/audit/package.js +204 -204
- package/src/audit/registry.js +284 -284
- package/src/audit/replay.js +103 -103
- package/src/auth/http.js +113 -113
- package/src/auth/service.js +891 -848
- package/src/auth/session-store.js +359 -345
- package/src/cli.js +252 -252
- package/src/commands/ai/identity-lifecycle.js +1338 -1337
- package/src/commands/ai/provision-governance.js +1272 -1246
- package/src/commands/ai/shared.js +147 -147
- package/src/commands/ai.js +11 -11
- package/src/commands/apply.js +12 -12
- package/src/commands/audit.js +1166 -1166
- package/src/commands/auth.js +375 -366
- package/src/commands/chat.js +191 -191
- package/src/commands/config.js +184 -184
- package/src/commands/cost.js +311 -311
- package/src/commands/daemon/core.js +850 -850
- package/src/commands/daemon/extended.js +1048 -1048
- package/src/commands/daemon/shared.js +213 -213
- package/src/commands/daemon.js +11 -11
- package/src/commands/guide.js +174 -174
- package/src/commands/ingest.js +58 -58
- package/src/commands/init.js +55 -55
- package/src/commands/legacy-args.js +10 -10
- package/src/commands/mcp.js +461 -404
- package/src/commands/omargate.js +15 -15
- package/src/commands/persona.js +20 -20
- package/src/commands/plugin.js +260 -260
- package/src/commands/policy.js +132 -132
- package/src/commands/prompt.js +238 -238
- package/src/commands/review.js +704 -704
- package/src/commands/scan.js +866 -788
- package/src/commands/spec.js +716 -716
- package/src/commands/swarm.js +651 -651
- package/src/commands/telemetry.js +202 -202
- package/src/commands/watch.js +510 -510
- package/src/config/agent-dictionary.js +182 -182
- package/src/config/io.js +56 -56
- package/src/config/paths.js +18 -18
- package/src/config/schema.js +55 -55
- package/src/config/service.js +184 -184
- package/src/cost/budget.js +235 -235
- package/src/cost/history.js +188 -188
- package/src/cost/tracker.js +171 -171
- package/src/daemon/artifact-lineage.js +534 -534
- package/src/daemon/assignment-ledger.js +770 -770
- package/src/daemon/ast-parser-layer.js +258 -258
- package/src/daemon/budget-governor.js +633 -633
- package/src/daemon/callgraph-overlay.js +646 -646
- package/src/daemon/error-worker.js +626 -626
- package/src/daemon/hybrid-mapper.js +929 -929
- package/src/daemon/jira-lifecycle.js +632 -632
- package/src/daemon/operator-control.js +657 -657
- package/src/daemon/reliability-lane.js +471 -471
- package/src/daemon/watchdog.js +971 -971
- package/src/guide/generator.js +316 -316
- package/src/ingest/engine.js +918 -918
- package/src/legacy-cli.js +2548 -2435
- package/src/mcp/registry.js +695 -695
- package/src/memory/blackboard.js +301 -301
- package/src/memory/retrieval.js +581 -581
- package/src/plugin/manifest.js +553 -553
- package/src/policy/packs.js +144 -144
- package/src/prompt/generator.js +118 -106
- package/src/review/ai-review.js +669 -669
- package/src/review/local-review.js +1284 -1284
- package/src/review/replay.js +235 -235
- package/src/review/report.js +664 -664
- package/src/review/spec-binding.js +487 -487
- package/src/scaffold/generator.js +67 -0
- package/src/scaffold/templates.js +150 -0
- package/src/scan/generator.js +418 -351
- package/src/scan/gh-secrets.js +107 -0
- package/src/spec/generator.js +519 -519
- package/src/spec/regenerate.js +237 -237
- package/src/spec/templates.js +91 -91
- package/src/swarm/dashboard.js +247 -247
- package/src/swarm/factory.js +363 -363
- package/src/swarm/pentest.js +934 -934
- package/src/swarm/registry.js +419 -419
- package/src/swarm/report.js +158 -158
- package/src/swarm/runtime.js +576 -576
- package/src/swarm/scenario-dsl.js +272 -272
- package/src/telemetry/ledger.js +302 -302
- package/src/telemetry/sync.js +96 -59
- package/src/ui/markdown.js +220 -220
package/src/commands/cost.js
CHANGED
|
@@ -1,311 +1,311 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
|
|
3
|
-
import pc from "picocolors";
|
|
4
|
-
|
|
5
|
-
import { evaluateBudget } from "../cost/budget.js";
|
|
6
|
-
import {
|
|
7
|
-
appendCostEntry,
|
|
8
|
-
loadCostHistory,
|
|
9
|
-
summarizeCostHistory,
|
|
10
|
-
} from "../cost/history.js";
|
|
11
|
-
import { estimateModelCost } from "../cost/tracker.js";
|
|
12
|
-
import { appendRunEvent, deriveStopClassFromBudget } from "../telemetry/ledger.js";
|
|
13
|
-
|
|
14
|
-
function shouldEmitJson(options, command) {
|
|
15
|
-
const local = Boolean(options && options.json);
|
|
16
|
-
const globalFromCommand =
|
|
17
|
-
command && command.optsWithGlobals ? Boolean(command.optsWithGlobals().json) : false;
|
|
18
|
-
return local || globalFromCommand;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function parseNonNegativeNumber(rawValue, field) {
|
|
22
|
-
const normalized = Number(rawValue || 0);
|
|
23
|
-
if (!Number.isFinite(normalized) || normalized < 0) {
|
|
24
|
-
throw new Error(`${field} must be a non-negative number.`);
|
|
25
|
-
}
|
|
26
|
-
return normalized;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function parsePercent(rawValue, field) {
|
|
30
|
-
const normalized = Number(rawValue || 0);
|
|
31
|
-
if (!Number.isFinite(normalized) || normalized < 0 || normalized > 100) {
|
|
32
|
-
throw new Error(`${field} must be between 0 and 100.`);
|
|
33
|
-
}
|
|
34
|
-
return normalized;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function formatUsd(value) {
|
|
38
|
-
return `$${Number(value || 0).toFixed(6)}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function printSessionSummary(session) {
|
|
42
|
-
console.log(pc.bold(`Session: ${session.sessionId}`));
|
|
43
|
-
console.log(
|
|
44
|
-
pc.gray(
|
|
45
|
-
`Invocations=${session.invocationCount}, Input=${session.inputTokens}, Output=${session.outputTokens}, Cost=${formatUsd(
|
|
46
|
-
session.costUsd
|
|
47
|
-
)}`
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
console.log(
|
|
51
|
-
pc.gray(
|
|
52
|
-
`Cache(read/write)=${session.cacheReadTokens}/${session.cacheWriteTokens}, RuntimeMs=${session.durationMs}, ToolCalls=${session.toolCalls}, No-progress streak=${session.noProgressStreak}`
|
|
53
|
-
)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async function runShow({ targetPath, outputDir, emitJson }) {
|
|
58
|
-
const { filePath, history } = await loadCostHistory({
|
|
59
|
-
targetPath,
|
|
60
|
-
outputDirOverride: outputDir,
|
|
61
|
-
});
|
|
62
|
-
const summary = summarizeCostHistory(history);
|
|
63
|
-
|
|
64
|
-
const payload = {
|
|
65
|
-
command: "cost show",
|
|
66
|
-
targetPath,
|
|
67
|
-
filePath,
|
|
68
|
-
summary,
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
if (emitJson) {
|
|
72
|
-
console.log(JSON.stringify(payload, null, 2));
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
console.log(pc.bold("Cost history summary"));
|
|
77
|
-
console.log(pc.gray(`File: ${filePath}`));
|
|
78
|
-
console.log(
|
|
79
|
-
pc.gray(
|
|
80
|
-
`Sessions=${summary.sessionCount}, Invocations=${summary.invocationCount}, Total cost=${formatUsd(
|
|
81
|
-
summary.costUsd
|
|
82
|
-
)}`
|
|
83
|
-
)
|
|
84
|
-
);
|
|
85
|
-
for (const session of summary.sessions) {
|
|
86
|
-
printSessionSummary(session);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function registerCostCommand(program) {
|
|
91
|
-
const cost = program.command("cost").description("Track cost usage and enforce budget governors");
|
|
92
|
-
|
|
93
|
-
cost
|
|
94
|
-
.option("--path <path>", "Target workspace path", ".")
|
|
95
|
-
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
96
|
-
.option("--json", "Emit machine-readable output")
|
|
97
|
-
.action(async (options, command) => {
|
|
98
|
-
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
99
|
-
await runShow({
|
|
100
|
-
targetPath,
|
|
101
|
-
outputDir: options.outputDir,
|
|
102
|
-
emitJson: shouldEmitJson(options, command),
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
cost
|
|
107
|
-
.command("show")
|
|
108
|
-
.description("Show project/session cost usage summary")
|
|
109
|
-
.option("--path <path>", "Target workspace path", ".")
|
|
110
|
-
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
111
|
-
.option("--json", "Emit machine-readable output")
|
|
112
|
-
.action(async (options, command) => {
|
|
113
|
-
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
114
|
-
await runShow({
|
|
115
|
-
targetPath,
|
|
116
|
-
outputDir: options.outputDir,
|
|
117
|
-
emitJson: shouldEmitJson(options, command),
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
cost
|
|
122
|
-
.command("record")
|
|
123
|
-
.description("Record one invocation usage event and evaluate budget limits")
|
|
124
|
-
.option("--path <path>", "Target workspace path", ".")
|
|
125
|
-
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
126
|
-
.option("--session-id <id>", "Session identifier", "default")
|
|
127
|
-
.option("--provider <name>", "Provider name", "openai")
|
|
128
|
-
.option("--model <id>", "Model identifier", "gpt-5.3-codex")
|
|
129
|
-
.option("--input-tokens <n>", "Input token count", "0")
|
|
130
|
-
.option("--output-tokens <n>", "Output token count", "0")
|
|
131
|
-
.option("--cache-read-tokens <n>", "Cache read token count", "0")
|
|
132
|
-
.option("--cache-write-tokens <n>", "Cache write token count", "0")
|
|
133
|
-
.option("--duration-ms <n>", "Invocation runtime in milliseconds", "0")
|
|
134
|
-
.option("--tool-calls <n>", "Invocation tool-call count", "0")
|
|
135
|
-
.option("--cost-usd <amount>", "Optional explicit cost override in USD")
|
|
136
|
-
.option("--progress-score <n>", "Progress score (<=0 increments no-progress streak)", "1")
|
|
137
|
-
.option("--max-cost <usd>", "Max cost budget per session", "1")
|
|
138
|
-
.option("--max-tokens <n>", "Max output token budget per session (0 = disabled)", "0")
|
|
139
|
-
.option("--max-runtime-ms <n>", "Max runtime budget per session in milliseconds (0 = disabled)", "0")
|
|
140
|
-
.option("--max-tool-calls <n>", "Max tool-call budget per session (0 = disabled)", "0")
|
|
141
|
-
.option("--max-no-progress <n>", "Max consecutive no-progress events before stop", "3")
|
|
142
|
-
.option("--warn-at-percent <n>", "Warning threshold percentage for enabled budgets", "80")
|
|
143
|
-
.option("--json", "Emit machine-readable output")
|
|
144
|
-
.action(async (options, command) => {
|
|
145
|
-
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
146
|
-
const provider = String(options.provider || "openai").trim().toLowerCase();
|
|
147
|
-
const model = String(options.model || "").trim();
|
|
148
|
-
const inputTokens = parseNonNegativeNumber(options.inputTokens, "inputTokens");
|
|
149
|
-
const outputTokens = parseNonNegativeNumber(options.outputTokens, "outputTokens");
|
|
150
|
-
const cacheReadTokens = parseNonNegativeNumber(options.cacheReadTokens, "cacheReadTokens");
|
|
151
|
-
const cacheWriteTokens = parseNonNegativeNumber(options.cacheWriteTokens, "cacheWriteTokens");
|
|
152
|
-
const durationMs = parseNonNegativeNumber(options.durationMs, "durationMs");
|
|
153
|
-
const toolCalls = parseNonNegativeNumber(options.toolCalls, "toolCalls");
|
|
154
|
-
const progressScore = Number(options.progressScore || 0);
|
|
155
|
-
|
|
156
|
-
const costUsd =
|
|
157
|
-
options.costUsd !== undefined && options.costUsd !== null
|
|
158
|
-
? parseNonNegativeNumber(options.costUsd, "costUsd")
|
|
159
|
-
: estimateModelCost({
|
|
160
|
-
modelId: model,
|
|
161
|
-
inputTokens,
|
|
162
|
-
outputTokens,
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const appended = await appendCostEntry(
|
|
166
|
-
{
|
|
167
|
-
targetPath,
|
|
168
|
-
outputDirOverride: options.outputDir,
|
|
169
|
-
},
|
|
170
|
-
{
|
|
171
|
-
sessionId: options.sessionId,
|
|
172
|
-
provider,
|
|
173
|
-
model,
|
|
174
|
-
inputTokens,
|
|
175
|
-
outputTokens,
|
|
176
|
-
cacheReadTokens,
|
|
177
|
-
cacheWriteTokens,
|
|
178
|
-
durationMs,
|
|
179
|
-
toolCalls,
|
|
180
|
-
costUsd,
|
|
181
|
-
progressScore,
|
|
182
|
-
}
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
const summary = summarizeCostHistory(appended.history);
|
|
186
|
-
const sessionSummary = summary.sessions.find((item) => item.sessionId === options.sessionId) || {
|
|
187
|
-
sessionId: options.sessionId,
|
|
188
|
-
invocationCount: 0,
|
|
189
|
-
inputTokens: 0,
|
|
190
|
-
outputTokens: 0,
|
|
191
|
-
cacheReadTokens: 0,
|
|
192
|
-
cacheWriteTokens: 0,
|
|
193
|
-
durationMs: 0,
|
|
194
|
-
toolCalls: 0,
|
|
195
|
-
costUsd: 0,
|
|
196
|
-
noProgressStreak: 0,
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
const budget = evaluateBudget({
|
|
200
|
-
sessionSummary,
|
|
201
|
-
maxCostUsd: parseNonNegativeNumber(options.maxCost, "maxCost"),
|
|
202
|
-
maxOutputTokens: parseNonNegativeNumber(options.maxTokens, "maxTokens"),
|
|
203
|
-
maxNoProgress: parseNonNegativeNumber(options.maxNoProgress, "maxNoProgress"),
|
|
204
|
-
maxRuntimeMs: parseNonNegativeNumber(options.maxRuntimeMs, "maxRuntimeMs"),
|
|
205
|
-
maxToolCalls: parseNonNegativeNumber(options.maxToolCalls, "maxToolCalls"),
|
|
206
|
-
warningThresholdPercent: parsePercent(options.warnAtPercent, "warnAtPercent"),
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
const usageTelemetry = await appendRunEvent(
|
|
210
|
-
{
|
|
211
|
-
targetPath,
|
|
212
|
-
outputDirOverride: options.outputDir,
|
|
213
|
-
},
|
|
214
|
-
{
|
|
215
|
-
sessionId: options.sessionId,
|
|
216
|
-
runId: options.sessionId,
|
|
217
|
-
eventType: "usage",
|
|
218
|
-
usage: {
|
|
219
|
-
inputTokens,
|
|
220
|
-
outputTokens,
|
|
221
|
-
cacheReadTokens,
|
|
222
|
-
cacheWriteTokens,
|
|
223
|
-
durationMs,
|
|
224
|
-
toolCalls,
|
|
225
|
-
costUsd,
|
|
226
|
-
},
|
|
227
|
-
metadata: {
|
|
228
|
-
sourceCommand: "cost record",
|
|
229
|
-
provider,
|
|
230
|
-
model,
|
|
231
|
-
invocationId: appended.entry.invocationId,
|
|
232
|
-
},
|
|
233
|
-
}
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
let stopTelemetry = null;
|
|
237
|
-
if (budget.blocking) {
|
|
238
|
-
stopTelemetry = await appendRunEvent(
|
|
239
|
-
{
|
|
240
|
-
targetPath,
|
|
241
|
-
outputDirOverride: options.outputDir,
|
|
242
|
-
},
|
|
243
|
-
{
|
|
244
|
-
sessionId: options.sessionId,
|
|
245
|
-
runId: options.sessionId,
|
|
246
|
-
eventType: "run_stop",
|
|
247
|
-
usage: {
|
|
248
|
-
inputTokens: sessionSummary.inputTokens,
|
|
249
|
-
outputTokens: sessionSummary.outputTokens,
|
|
250
|
-
cacheReadTokens: sessionSummary.cacheReadTokens,
|
|
251
|
-
cacheWriteTokens: sessionSummary.cacheWriteTokens,
|
|
252
|
-
costUsd: sessionSummary.costUsd,
|
|
253
|
-
durationMs: sessionSummary.durationMs,
|
|
254
|
-
toolCalls: sessionSummary.toolCalls,
|
|
255
|
-
},
|
|
256
|
-
stop: {
|
|
257
|
-
stopClass: deriveStopClassFromBudget(budget),
|
|
258
|
-
blocking: true,
|
|
259
|
-
reasonCodes: budget.reasons.map((reason) => reason.code),
|
|
260
|
-
},
|
|
261
|
-
metadata: {
|
|
262
|
-
sourceCommand: "cost record",
|
|
263
|
-
provider,
|
|
264
|
-
model,
|
|
265
|
-
invocationId: appended.entry.invocationId,
|
|
266
|
-
},
|
|
267
|
-
}
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const payload = {
|
|
272
|
-
command: "cost record",
|
|
273
|
-
targetPath,
|
|
274
|
-
filePath: appended.filePath,
|
|
275
|
-
entry: appended.entry,
|
|
276
|
-
session: sessionSummary,
|
|
277
|
-
budget,
|
|
278
|
-
telemetry: {
|
|
279
|
-
filePath: usageTelemetry.filePath,
|
|
280
|
-
usageEventId: usageTelemetry.event.eventId,
|
|
281
|
-
stopEventId: stopTelemetry?.event?.eventId || null,
|
|
282
|
-
},
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
if (shouldEmitJson(options, command)) {
|
|
286
|
-
console.log(JSON.stringify(payload, null, 2));
|
|
287
|
-
} else {
|
|
288
|
-
console.log(pc.bold("Cost entry recorded"));
|
|
289
|
-
console.log(pc.gray(`File: ${appended.filePath}`));
|
|
290
|
-
console.log(pc.gray(`Telemetry file: ${usageTelemetry.filePath}`));
|
|
291
|
-
printSessionSummary(sessionSummary);
|
|
292
|
-
if (budget.blocking) {
|
|
293
|
-
console.log(pc.red("Budget guardrail triggered:"));
|
|
294
|
-
for (const reason of budget.reasons) {
|
|
295
|
-
console.log(`- ${reason.code}: ${reason.message}`);
|
|
296
|
-
}
|
|
297
|
-
} else if (budget.warnings.length > 0) {
|
|
298
|
-
console.log(pc.yellow("Budget warning threshold reached:"));
|
|
299
|
-
for (const warning of budget.warnings) {
|
|
300
|
-
console.log(`- ${warning.code}: ${warning.message}`);
|
|
301
|
-
}
|
|
302
|
-
} else {
|
|
303
|
-
console.log(pc.green("Budget status: within limits."));
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
if (budget.blocking) {
|
|
308
|
-
process.exitCode = 2;
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
}
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
|
|
5
|
+
import { evaluateBudget } from "../cost/budget.js";
|
|
6
|
+
import {
|
|
7
|
+
appendCostEntry,
|
|
8
|
+
loadCostHistory,
|
|
9
|
+
summarizeCostHistory,
|
|
10
|
+
} from "../cost/history.js";
|
|
11
|
+
import { estimateModelCost } from "../cost/tracker.js";
|
|
12
|
+
import { appendRunEvent, deriveStopClassFromBudget } from "../telemetry/ledger.js";
|
|
13
|
+
|
|
14
|
+
function shouldEmitJson(options, command) {
|
|
15
|
+
const local = Boolean(options && options.json);
|
|
16
|
+
const globalFromCommand =
|
|
17
|
+
command && command.optsWithGlobals ? Boolean(command.optsWithGlobals().json) : false;
|
|
18
|
+
return local || globalFromCommand;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function parseNonNegativeNumber(rawValue, field) {
|
|
22
|
+
const normalized = Number(rawValue || 0);
|
|
23
|
+
if (!Number.isFinite(normalized) || normalized < 0) {
|
|
24
|
+
throw new Error(`${field} must be a non-negative number.`);
|
|
25
|
+
}
|
|
26
|
+
return normalized;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parsePercent(rawValue, field) {
|
|
30
|
+
const normalized = Number(rawValue || 0);
|
|
31
|
+
if (!Number.isFinite(normalized) || normalized < 0 || normalized > 100) {
|
|
32
|
+
throw new Error(`${field} must be between 0 and 100.`);
|
|
33
|
+
}
|
|
34
|
+
return normalized;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function formatUsd(value) {
|
|
38
|
+
return `$${Number(value || 0).toFixed(6)}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function printSessionSummary(session) {
|
|
42
|
+
console.log(pc.bold(`Session: ${session.sessionId}`));
|
|
43
|
+
console.log(
|
|
44
|
+
pc.gray(
|
|
45
|
+
`Invocations=${session.invocationCount}, Input=${session.inputTokens}, Output=${session.outputTokens}, Cost=${formatUsd(
|
|
46
|
+
session.costUsd
|
|
47
|
+
)}`
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
console.log(
|
|
51
|
+
pc.gray(
|
|
52
|
+
`Cache(read/write)=${session.cacheReadTokens}/${session.cacheWriteTokens}, RuntimeMs=${session.durationMs}, ToolCalls=${session.toolCalls}, No-progress streak=${session.noProgressStreak}`
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function runShow({ targetPath, outputDir, emitJson }) {
|
|
58
|
+
const { filePath, history } = await loadCostHistory({
|
|
59
|
+
targetPath,
|
|
60
|
+
outputDirOverride: outputDir,
|
|
61
|
+
});
|
|
62
|
+
const summary = summarizeCostHistory(history);
|
|
63
|
+
|
|
64
|
+
const payload = {
|
|
65
|
+
command: "cost show",
|
|
66
|
+
targetPath,
|
|
67
|
+
filePath,
|
|
68
|
+
summary,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
if (emitJson) {
|
|
72
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
console.log(pc.bold("Cost history summary"));
|
|
77
|
+
console.log(pc.gray(`File: ${filePath}`));
|
|
78
|
+
console.log(
|
|
79
|
+
pc.gray(
|
|
80
|
+
`Sessions=${summary.sessionCount}, Invocations=${summary.invocationCount}, Total cost=${formatUsd(
|
|
81
|
+
summary.costUsd
|
|
82
|
+
)}`
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
for (const session of summary.sessions) {
|
|
86
|
+
printSessionSummary(session);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function registerCostCommand(program) {
|
|
91
|
+
const cost = program.command("cost").description("Track cost usage and enforce budget governors");
|
|
92
|
+
|
|
93
|
+
cost
|
|
94
|
+
.option("--path <path>", "Target workspace path", ".")
|
|
95
|
+
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
96
|
+
.option("--json", "Emit machine-readable output")
|
|
97
|
+
.action(async (options, command) => {
|
|
98
|
+
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
99
|
+
await runShow({
|
|
100
|
+
targetPath,
|
|
101
|
+
outputDir: options.outputDir,
|
|
102
|
+
emitJson: shouldEmitJson(options, command),
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
cost
|
|
107
|
+
.command("show")
|
|
108
|
+
.description("Show project/session cost usage summary")
|
|
109
|
+
.option("--path <path>", "Target workspace path", ".")
|
|
110
|
+
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
111
|
+
.option("--json", "Emit machine-readable output")
|
|
112
|
+
.action(async (options, command) => {
|
|
113
|
+
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
114
|
+
await runShow({
|
|
115
|
+
targetPath,
|
|
116
|
+
outputDir: options.outputDir,
|
|
117
|
+
emitJson: shouldEmitJson(options, command),
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
cost
|
|
122
|
+
.command("record")
|
|
123
|
+
.description("Record one invocation usage event and evaluate budget limits")
|
|
124
|
+
.option("--path <path>", "Target workspace path", ".")
|
|
125
|
+
.option("--output-dir <path>", "Optional output dir override for cost history")
|
|
126
|
+
.option("--session-id <id>", "Session identifier", "default")
|
|
127
|
+
.option("--provider <name>", "Provider name", "openai")
|
|
128
|
+
.option("--model <id>", "Model identifier", "gpt-5.3-codex")
|
|
129
|
+
.option("--input-tokens <n>", "Input token count", "0")
|
|
130
|
+
.option("--output-tokens <n>", "Output token count", "0")
|
|
131
|
+
.option("--cache-read-tokens <n>", "Cache read token count", "0")
|
|
132
|
+
.option("--cache-write-tokens <n>", "Cache write token count", "0")
|
|
133
|
+
.option("--duration-ms <n>", "Invocation runtime in milliseconds", "0")
|
|
134
|
+
.option("--tool-calls <n>", "Invocation tool-call count", "0")
|
|
135
|
+
.option("--cost-usd <amount>", "Optional explicit cost override in USD")
|
|
136
|
+
.option("--progress-score <n>", "Progress score (<=0 increments no-progress streak)", "1")
|
|
137
|
+
.option("--max-cost <usd>", "Max cost budget per session", "1")
|
|
138
|
+
.option("--max-tokens <n>", "Max output token budget per session (0 = disabled)", "0")
|
|
139
|
+
.option("--max-runtime-ms <n>", "Max runtime budget per session in milliseconds (0 = disabled)", "0")
|
|
140
|
+
.option("--max-tool-calls <n>", "Max tool-call budget per session (0 = disabled)", "0")
|
|
141
|
+
.option("--max-no-progress <n>", "Max consecutive no-progress events before stop", "3")
|
|
142
|
+
.option("--warn-at-percent <n>", "Warning threshold percentage for enabled budgets", "80")
|
|
143
|
+
.option("--json", "Emit machine-readable output")
|
|
144
|
+
.action(async (options, command) => {
|
|
145
|
+
const targetPath = path.resolve(process.cwd(), String(options.path || "."));
|
|
146
|
+
const provider = String(options.provider || "openai").trim().toLowerCase();
|
|
147
|
+
const model = String(options.model || "").trim();
|
|
148
|
+
const inputTokens = parseNonNegativeNumber(options.inputTokens, "inputTokens");
|
|
149
|
+
const outputTokens = parseNonNegativeNumber(options.outputTokens, "outputTokens");
|
|
150
|
+
const cacheReadTokens = parseNonNegativeNumber(options.cacheReadTokens, "cacheReadTokens");
|
|
151
|
+
const cacheWriteTokens = parseNonNegativeNumber(options.cacheWriteTokens, "cacheWriteTokens");
|
|
152
|
+
const durationMs = parseNonNegativeNumber(options.durationMs, "durationMs");
|
|
153
|
+
const toolCalls = parseNonNegativeNumber(options.toolCalls, "toolCalls");
|
|
154
|
+
const progressScore = Number(options.progressScore || 0);
|
|
155
|
+
|
|
156
|
+
const costUsd =
|
|
157
|
+
options.costUsd !== undefined && options.costUsd !== null
|
|
158
|
+
? parseNonNegativeNumber(options.costUsd, "costUsd")
|
|
159
|
+
: estimateModelCost({
|
|
160
|
+
modelId: model,
|
|
161
|
+
inputTokens,
|
|
162
|
+
outputTokens,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const appended = await appendCostEntry(
|
|
166
|
+
{
|
|
167
|
+
targetPath,
|
|
168
|
+
outputDirOverride: options.outputDir,
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
sessionId: options.sessionId,
|
|
172
|
+
provider,
|
|
173
|
+
model,
|
|
174
|
+
inputTokens,
|
|
175
|
+
outputTokens,
|
|
176
|
+
cacheReadTokens,
|
|
177
|
+
cacheWriteTokens,
|
|
178
|
+
durationMs,
|
|
179
|
+
toolCalls,
|
|
180
|
+
costUsd,
|
|
181
|
+
progressScore,
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const summary = summarizeCostHistory(appended.history);
|
|
186
|
+
const sessionSummary = summary.sessions.find((item) => item.sessionId === options.sessionId) || {
|
|
187
|
+
sessionId: options.sessionId,
|
|
188
|
+
invocationCount: 0,
|
|
189
|
+
inputTokens: 0,
|
|
190
|
+
outputTokens: 0,
|
|
191
|
+
cacheReadTokens: 0,
|
|
192
|
+
cacheWriteTokens: 0,
|
|
193
|
+
durationMs: 0,
|
|
194
|
+
toolCalls: 0,
|
|
195
|
+
costUsd: 0,
|
|
196
|
+
noProgressStreak: 0,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const budget = evaluateBudget({
|
|
200
|
+
sessionSummary,
|
|
201
|
+
maxCostUsd: parseNonNegativeNumber(options.maxCost, "maxCost"),
|
|
202
|
+
maxOutputTokens: parseNonNegativeNumber(options.maxTokens, "maxTokens"),
|
|
203
|
+
maxNoProgress: parseNonNegativeNumber(options.maxNoProgress, "maxNoProgress"),
|
|
204
|
+
maxRuntimeMs: parseNonNegativeNumber(options.maxRuntimeMs, "maxRuntimeMs"),
|
|
205
|
+
maxToolCalls: parseNonNegativeNumber(options.maxToolCalls, "maxToolCalls"),
|
|
206
|
+
warningThresholdPercent: parsePercent(options.warnAtPercent, "warnAtPercent"),
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const usageTelemetry = await appendRunEvent(
|
|
210
|
+
{
|
|
211
|
+
targetPath,
|
|
212
|
+
outputDirOverride: options.outputDir,
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
sessionId: options.sessionId,
|
|
216
|
+
runId: options.sessionId,
|
|
217
|
+
eventType: "usage",
|
|
218
|
+
usage: {
|
|
219
|
+
inputTokens,
|
|
220
|
+
outputTokens,
|
|
221
|
+
cacheReadTokens,
|
|
222
|
+
cacheWriteTokens,
|
|
223
|
+
durationMs,
|
|
224
|
+
toolCalls,
|
|
225
|
+
costUsd,
|
|
226
|
+
},
|
|
227
|
+
metadata: {
|
|
228
|
+
sourceCommand: "cost record",
|
|
229
|
+
provider,
|
|
230
|
+
model,
|
|
231
|
+
invocationId: appended.entry.invocationId,
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
let stopTelemetry = null;
|
|
237
|
+
if (budget.blocking) {
|
|
238
|
+
stopTelemetry = await appendRunEvent(
|
|
239
|
+
{
|
|
240
|
+
targetPath,
|
|
241
|
+
outputDirOverride: options.outputDir,
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
sessionId: options.sessionId,
|
|
245
|
+
runId: options.sessionId,
|
|
246
|
+
eventType: "run_stop",
|
|
247
|
+
usage: {
|
|
248
|
+
inputTokens: sessionSummary.inputTokens,
|
|
249
|
+
outputTokens: sessionSummary.outputTokens,
|
|
250
|
+
cacheReadTokens: sessionSummary.cacheReadTokens,
|
|
251
|
+
cacheWriteTokens: sessionSummary.cacheWriteTokens,
|
|
252
|
+
costUsd: sessionSummary.costUsd,
|
|
253
|
+
durationMs: sessionSummary.durationMs,
|
|
254
|
+
toolCalls: sessionSummary.toolCalls,
|
|
255
|
+
},
|
|
256
|
+
stop: {
|
|
257
|
+
stopClass: deriveStopClassFromBudget(budget),
|
|
258
|
+
blocking: true,
|
|
259
|
+
reasonCodes: budget.reasons.map((reason) => reason.code),
|
|
260
|
+
},
|
|
261
|
+
metadata: {
|
|
262
|
+
sourceCommand: "cost record",
|
|
263
|
+
provider,
|
|
264
|
+
model,
|
|
265
|
+
invocationId: appended.entry.invocationId,
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const payload = {
|
|
272
|
+
command: "cost record",
|
|
273
|
+
targetPath,
|
|
274
|
+
filePath: appended.filePath,
|
|
275
|
+
entry: appended.entry,
|
|
276
|
+
session: sessionSummary,
|
|
277
|
+
budget,
|
|
278
|
+
telemetry: {
|
|
279
|
+
filePath: usageTelemetry.filePath,
|
|
280
|
+
usageEventId: usageTelemetry.event.eventId,
|
|
281
|
+
stopEventId: stopTelemetry?.event?.eventId || null,
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
if (shouldEmitJson(options, command)) {
|
|
286
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
287
|
+
} else {
|
|
288
|
+
console.log(pc.bold("Cost entry recorded"));
|
|
289
|
+
console.log(pc.gray(`File: ${appended.filePath}`));
|
|
290
|
+
console.log(pc.gray(`Telemetry file: ${usageTelemetry.filePath}`));
|
|
291
|
+
printSessionSummary(sessionSummary);
|
|
292
|
+
if (budget.blocking) {
|
|
293
|
+
console.log(pc.red("Budget guardrail triggered:"));
|
|
294
|
+
for (const reason of budget.reasons) {
|
|
295
|
+
console.log(`- ${reason.code}: ${reason.message}`);
|
|
296
|
+
}
|
|
297
|
+
} else if (budget.warnings.length > 0) {
|
|
298
|
+
console.log(pc.yellow("Budget warning threshold reached:"));
|
|
299
|
+
for (const warning of budget.warnings) {
|
|
300
|
+
console.log(`- ${warning.code}: ${warning.message}`);
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
console.log(pc.green("Budget status: within limits."));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (budget.blocking) {
|
|
308
|
+
process.exitCode = 2;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|