sentinelayer-cli 0.1.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 -0
- package/bin/create-sentinelayer.js +5 -0
- package/bin/sentinelayer-cli.js +5 -0
- package/bin/sl.js +5 -0
- package/package.json +54 -0
- package/src/agents/jules/config/definition.js +209 -0
- package/src/agents/jules/config/system-prompt.js +175 -0
- package/src/agents/jules/error-intake.js +51 -0
- package/src/agents/jules/fix-cycle.js +377 -0
- package/src/agents/jules/loop.js +367 -0
- package/src/agents/jules/pulse.js +319 -0
- package/src/agents/jules/stream.js +186 -0
- package/src/agents/jules/swarm/file-scanner.js +74 -0
- package/src/agents/jules/swarm/index.js +11 -0
- package/src/agents/jules/swarm/orchestrator.js +362 -0
- package/src/agents/jules/swarm/pattern-hunter.js +123 -0
- package/src/agents/jules/swarm/sub-agent.js +308 -0
- package/src/agents/jules/tools/auth-audit.js +222 -0
- package/src/agents/jules/tools/dispatch.js +327 -0
- package/src/agents/jules/tools/file-edit.js +180 -0
- package/src/agents/jules/tools/file-read.js +100 -0
- package/src/agents/jules/tools/frontend-analyze.js +570 -0
- package/src/agents/jules/tools/glob.js +168 -0
- package/src/agents/jules/tools/grep.js +228 -0
- package/src/agents/jules/tools/index.js +29 -0
- package/src/agents/jules/tools/path-guards.js +161 -0
- package/src/agents/jules/tools/runtime-audit.js +409 -0
- package/src/agents/jules/tools/shell.js +383 -0
- package/src/ai/aidenid.js +945 -0
- package/src/ai/client.js +508 -0
- package/src/ai/domain-target-store.js +268 -0
- package/src/ai/identity-store.js +270 -0
- package/src/ai/site-store.js +145 -0
- package/src/audit/agents/architecture.js +180 -0
- package/src/audit/agents/compliance.js +179 -0
- package/src/audit/agents/documentation.js +165 -0
- package/src/audit/agents/performance.js +145 -0
- package/src/audit/agents/security.js +215 -0
- package/src/audit/agents/testing.js +172 -0
- package/src/audit/orchestrator.js +557 -0
- package/src/audit/package.js +204 -0
- package/src/audit/registry.js +284 -0
- package/src/audit/replay.js +103 -0
- package/src/auth/http.js +113 -0
- package/src/auth/service.js +848 -0
- package/src/auth/session-store.js +345 -0
- package/src/cli.js +244 -0
- package/src/commands/ai/identity-lifecycle.js +1337 -0
- package/src/commands/ai/provision-governance.js +1246 -0
- package/src/commands/ai/shared.js +147 -0
- package/src/commands/ai.js +11 -0
- package/src/commands/apply.js +19 -0
- package/src/commands/audit.js +1147 -0
- package/src/commands/auth.js +366 -0
- package/src/commands/chat.js +191 -0
- package/src/commands/config.js +184 -0
- package/src/commands/cost.js +311 -0
- package/src/commands/daemon/core.js +850 -0
- package/src/commands/daemon/extended.js +1048 -0
- package/src/commands/daemon/shared.js +213 -0
- package/src/commands/daemon.js +11 -0
- package/src/commands/guide.js +174 -0
- package/src/commands/ingest.js +58 -0
- package/src/commands/init.js +55 -0
- package/src/commands/legacy-args.js +30 -0
- package/src/commands/mcp.js +404 -0
- package/src/commands/omargate.js +21 -0
- package/src/commands/persona.js +27 -0
- package/src/commands/plugin.js +260 -0
- package/src/commands/policy.js +132 -0
- package/src/commands/prompt.js +238 -0
- package/src/commands/review.js +704 -0
- package/src/commands/scan.js +788 -0
- package/src/commands/spec.js +716 -0
- package/src/commands/swarm.js +651 -0
- package/src/commands/telemetry.js +202 -0
- package/src/commands/watch.js +510 -0
- package/src/config/agent-dictionary.js +182 -0
- package/src/config/io.js +56 -0
- package/src/config/paths.js +18 -0
- package/src/config/schema.js +55 -0
- package/src/config/service.js +184 -0
- package/src/cost/budget.js +235 -0
- package/src/cost/history.js +188 -0
- package/src/cost/tracker.js +171 -0
- package/src/daemon/artifact-lineage.js +534 -0
- package/src/daemon/assignment-ledger.js +770 -0
- package/src/daemon/ast-parser-layer.js +258 -0
- package/src/daemon/budget-governor.js +633 -0
- package/src/daemon/callgraph-overlay.js +646 -0
- package/src/daemon/error-worker.js +626 -0
- package/src/daemon/hybrid-mapper.js +929 -0
- package/src/daemon/jira-lifecycle.js +632 -0
- package/src/daemon/operator-control.js +657 -0
- package/src/daemon/reliability-lane.js +471 -0
- package/src/daemon/watchdog.js +971 -0
- package/src/guide/generator.js +316 -0
- package/src/ingest/engine.js +918 -0
- package/src/legacy-cli.js +2435 -0
- package/src/mcp/registry.js +695 -0
- package/src/memory/blackboard.js +301 -0
- package/src/memory/retrieval.js +581 -0
- package/src/plugin/manifest.js +553 -0
- package/src/policy/packs.js +144 -0
- package/src/prompt/generator.js +106 -0
- package/src/review/ai-review.js +669 -0
- package/src/review/local-review.js +1284 -0
- package/src/review/replay.js +235 -0
- package/src/review/report.js +664 -0
- package/src/review/spec-binding.js +487 -0
- package/src/scan/generator.js +351 -0
- package/src/spec/generator.js +519 -0
- package/src/spec/regenerate.js +237 -0
- package/src/spec/templates.js +91 -0
- package/src/swarm/dashboard.js +247 -0
- package/src/swarm/factory.js +363 -0
- package/src/swarm/pentest.js +934 -0
- package/src/swarm/registry.js +419 -0
- package/src/swarm/report.js +158 -0
- package/src/swarm/runtime.js +576 -0
- package/src/swarm/scenario-dsl.js +272 -0
- package/src/telemetry/ledger.js +302 -0
- package/src/ui/markdown.js +220 -0
- package/src/ui/progress.js +100 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ensureEditableConfigPath,
|
|
8
|
+
findConfigSource,
|
|
9
|
+
getLayer,
|
|
10
|
+
listConfigKeys,
|
|
11
|
+
loadConfig,
|
|
12
|
+
setConfigValue,
|
|
13
|
+
} from "../config/service.js";
|
|
14
|
+
|
|
15
|
+
const SCOPES = "global|project|env|resolved";
|
|
16
|
+
|
|
17
|
+
function shouldEmitJson(options, command) {
|
|
18
|
+
const local = Boolean(options && options.json);
|
|
19
|
+
const globalFromCommand =
|
|
20
|
+
command && command.optsWithGlobals ? Boolean(command.optsWithGlobals().json) : false;
|
|
21
|
+
return local || globalFromCommand;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function displayValue(value) {
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
return JSON.stringify(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function assertKnownKey(key) {
|
|
35
|
+
const normalized = String(key || "").trim();
|
|
36
|
+
if (!listConfigKeys().includes(normalized)) {
|
|
37
|
+
throw new Error(`Unknown key '${normalized}'. Allowed keys: ${listConfigKeys().join(", ")}`);
|
|
38
|
+
}
|
|
39
|
+
return normalized;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function registerConfigCommand(program) {
|
|
43
|
+
const config = program
|
|
44
|
+
.command("config")
|
|
45
|
+
.description("Manage layered Sentinelayer CLI configuration");
|
|
46
|
+
|
|
47
|
+
config
|
|
48
|
+
.command("list")
|
|
49
|
+
.description("List configuration values")
|
|
50
|
+
.option("--scope <scope>", `Config scope (${SCOPES})`, "resolved")
|
|
51
|
+
.option("--path <path>", "Project root for project scope resolution")
|
|
52
|
+
.option("--json", "Emit machine-readable output")
|
|
53
|
+
.action(async (options, command) => {
|
|
54
|
+
const payload = await loadConfig({ cwd: options.path });
|
|
55
|
+
const scope = String(options.scope || "resolved");
|
|
56
|
+
const layer = getLayer(payload, scope);
|
|
57
|
+
const emitJson = shouldEmitJson(options, command);
|
|
58
|
+
|
|
59
|
+
if (emitJson) {
|
|
60
|
+
console.log(
|
|
61
|
+
JSON.stringify(
|
|
62
|
+
{
|
|
63
|
+
scope,
|
|
64
|
+
config: layer,
|
|
65
|
+
paths: payload.paths,
|
|
66
|
+
},
|
|
67
|
+
null,
|
|
68
|
+
2
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const entries = Object.entries(layer).sort(([left], [right]) => left.localeCompare(right));
|
|
75
|
+
console.log(pc.bold(`Config scope: ${scope}`));
|
|
76
|
+
if (!entries.length) {
|
|
77
|
+
console.log(pc.gray("(empty)"));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
for (const [key, value] of entries) {
|
|
81
|
+
console.log(`${key}: ${displayValue(value)}`);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
config
|
|
86
|
+
.command("get <key>")
|
|
87
|
+
.description("Read a single configuration value")
|
|
88
|
+
.option("--scope <scope>", `Config scope (${SCOPES})`, "resolved")
|
|
89
|
+
.option("--path <path>", "Project root for project scope resolution")
|
|
90
|
+
.option("--json", "Emit machine-readable output")
|
|
91
|
+
.action(async (key, options, command) => {
|
|
92
|
+
const normalizedKey = assertKnownKey(key);
|
|
93
|
+
const payload = await loadConfig({ cwd: options.path });
|
|
94
|
+
const scope = String(options.scope || "resolved");
|
|
95
|
+
const layer = getLayer(payload, scope);
|
|
96
|
+
const value = layer[normalizedKey];
|
|
97
|
+
const source = findConfigSource(payload, normalizedKey);
|
|
98
|
+
const emitJson = shouldEmitJson(options, command);
|
|
99
|
+
|
|
100
|
+
if (emitJson) {
|
|
101
|
+
console.log(
|
|
102
|
+
JSON.stringify(
|
|
103
|
+
{
|
|
104
|
+
key: normalizedKey,
|
|
105
|
+
scope,
|
|
106
|
+
value,
|
|
107
|
+
source,
|
|
108
|
+
},
|
|
109
|
+
null,
|
|
110
|
+
2
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (value === undefined) {
|
|
117
|
+
console.log(pc.yellow(`No value set for '${normalizedKey}' in scope '${scope}'.`));
|
|
118
|
+
process.exitCode = 1;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(displayValue(value));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
config
|
|
126
|
+
.command("set <key> <value>")
|
|
127
|
+
.description("Set a configuration value in global or project scope")
|
|
128
|
+
.option("--scope <scope>", "Write scope (global|project)", "project")
|
|
129
|
+
.option("--path <path>", "Project root for project scope resolution")
|
|
130
|
+
.option("--json", "Emit machine-readable output")
|
|
131
|
+
.action(async (key, value, options, command) => {
|
|
132
|
+
const result = await setConfigValue({
|
|
133
|
+
key: assertKnownKey(key),
|
|
134
|
+
value,
|
|
135
|
+
scope: options.scope,
|
|
136
|
+
cwd: options.path,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (shouldEmitJson(options, command)) {
|
|
140
|
+
console.log(JSON.stringify(result, null, 2));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
console.log(pc.green(`Updated ${result.key} in ${result.scope} config.`));
|
|
145
|
+
console.log(pc.gray(`Path: ${result.path}`));
|
|
146
|
+
console.log(`${result.key}: ${displayValue(result.value)}`);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
config
|
|
150
|
+
.command("edit")
|
|
151
|
+
.description("Open config file for editing (uses $EDITOR or $VISUAL)")
|
|
152
|
+
.option("--scope <scope>", "Edit scope (global|project)", "project")
|
|
153
|
+
.option("--path <path>", "Project root for project scope resolution")
|
|
154
|
+
.option("--json", "Emit machine-readable output")
|
|
155
|
+
.action(async (options, command) => {
|
|
156
|
+
const target = await ensureEditableConfigPath({
|
|
157
|
+
scope: options.scope,
|
|
158
|
+
cwd: options.path,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
if (shouldEmitJson(options, command)) {
|
|
162
|
+
console.log(JSON.stringify(target, null, 2));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const editor = String(process.env.EDITOR || process.env.VISUAL || "").trim();
|
|
167
|
+
if (!editor) {
|
|
168
|
+
console.log(pc.yellow(`No editor configured. Set $EDITOR or open manually:`));
|
|
169
|
+
console.log(target.path);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const result = spawnSync(editor, [target.path], {
|
|
174
|
+
stdio: "inherit",
|
|
175
|
+
shell: true,
|
|
176
|
+
});
|
|
177
|
+
if (result.error) {
|
|
178
|
+
throw result.error;
|
|
179
|
+
}
|
|
180
|
+
if (typeof result.status === "number" && result.status !== 0) {
|
|
181
|
+
throw new Error(`Editor exited with status ${result.status}.`);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
@@ -0,0 +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
|
+
}
|