aiopt 0.2.6 → 0.2.7

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 CHANGED
@@ -15,7 +15,14 @@ No server. No upload. No account. No payments inside the CLI.
15
15
  ```bash
16
16
  npx aiopt install --force
17
17
  # baseline: your existing usage log
18
- npx aiopt guard --context-mult 1.2
18
+ npx aiopt guard --context-mult 1.2 --call-mult 3 --budget-monthly 200
19
+ ```
20
+
21
+ ## 1-minute demo
22
+ ```bash
23
+ npx aiopt quickstart --demo
24
+ npx aiopt dashboard --port 3010
25
+ # open http://127.0.0.1:3010/
19
26
  ```
20
27
 
21
28
  Exit codes:
@@ -23,12 +30,22 @@ Exit codes:
23
30
  - `2` WARN (cost accident possible)
24
31
  - `3` FAIL (merge-blocking)
25
32
 
26
- Common knobs:
33
+ Common knobs (transform mode):
27
34
  - `--context-mult <n>` (prompt/context grows)
28
35
  - `--output-mult <n>` (output grows)
29
36
  - `--retries-delta <n>` (more retries/attempts)
30
37
  - `--call-mult <n>` (traffic spike / call volume)
31
38
 
39
+ Budget gate:
40
+ - `--budget-monthly <usd>` (FAIL if candidate monthly cost exceeds your budget)
41
+
42
+ Diff mode (real before/after logs):
43
+ ```bash
44
+ npx aiopt guard --baseline ./usage-baseline.jsonl --candidate ./usage-candidate.jsonl
45
+ ```
46
+
47
+ Output includes a short **Top causes** summary (1–3) to explain the biggest drivers.
48
+
32
49
  ## CI integration (GitHub Actions)
33
50
  You can run `aiopt guard` in CI to catch accidental cost blow-ups before merge.
34
51
 
package/dist/cli.js CHANGED
@@ -32,6 +32,72 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
32
32
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
33
33
  mod
34
34
  ));
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+
37
+ // src/io.ts
38
+ var io_exports = {};
39
+ __export(io_exports, {
40
+ ensureDir: () => ensureDir,
41
+ isCsvPath: () => isCsvPath,
42
+ readCsv: () => readCsv,
43
+ readJsonl: () => readJsonl
44
+ });
45
+ function ensureDir(p) {
46
+ import_fs.default.mkdirSync(p, { recursive: true });
47
+ }
48
+ function readJsonl(filePath) {
49
+ const raw = import_fs.default.readFileSync(filePath, "utf8");
50
+ const lines = raw.split(/\r?\n/).filter((l) => l.trim().length > 0);
51
+ const out = [];
52
+ for (const line of lines) {
53
+ const obj = JSON.parse(line);
54
+ out.push(normalizeEvent(obj));
55
+ }
56
+ return out;
57
+ }
58
+ function readCsv(filePath) {
59
+ const raw = import_fs.default.readFileSync(filePath, "utf8");
60
+ const records = (0, import_sync.parse)(raw, { columns: true, skip_empty_lines: true, trim: true });
61
+ return records.map((r) => normalizeEvent(r));
62
+ }
63
+ function toNum(x, def = 0) {
64
+ const n = Number(x);
65
+ return Number.isFinite(n) ? n : def;
66
+ }
67
+ function normalizeEvent(x) {
68
+ const inputTokens = x.input_tokens ?? x.prompt_tokens;
69
+ const outputTokens = x.output_tokens ?? x.completion_tokens;
70
+ const featureTag = x.feature_tag ?? x?.meta?.feature_tag ?? x.endpoint ?? "";
71
+ const retries = x.retries ?? (x.attempt !== void 0 ? Math.max(0, toNum(x.attempt) - 1) : 0);
72
+ const billed = x.billed_cost ?? x.cost_usd;
73
+ return {
74
+ ts: String(x.ts ?? ""),
75
+ provider: String(x.provider ?? "").toLowerCase(),
76
+ model: String(x.model ?? ""),
77
+ input_tokens: toNum(inputTokens),
78
+ output_tokens: toNum(outputTokens),
79
+ feature_tag: String(featureTag ?? ""),
80
+ retries: toNum(retries),
81
+ status: String(x.status ?? ""),
82
+ billed_cost: billed === void 0 || billed === "" ? void 0 : toNum(billed),
83
+ trace_id: x.trace_id ? String(x.trace_id) : void 0,
84
+ request_id: x.request_id ? String(x.request_id) : void 0,
85
+ attempt: x.attempt === void 0 ? void 0 : toNum(x.attempt),
86
+ endpoint: x.endpoint ? String(x.endpoint) : void 0
87
+ };
88
+ }
89
+ function isCsvPath(p) {
90
+ return import_path.default.extname(p).toLowerCase() === ".csv";
91
+ }
92
+ var import_fs, import_path, import_sync;
93
+ var init_io = __esm({
94
+ "src/io.ts"() {
95
+ "use strict";
96
+ import_fs = __toESM(require("fs"));
97
+ import_path = __toESM(require("path"));
98
+ import_sync = require("csv-parse/sync");
99
+ }
100
+ });
35
101
 
36
102
  // src/cost.ts
37
103
  function getRates(rt, provider, model) {
@@ -415,7 +481,7 @@ var require_package = __commonJS({
415
481
  "package.json"(exports2, module2) {
416
482
  module2.exports = {
417
483
  name: "aiopt",
418
- version: "0.2.6",
484
+ version: "0.2.7",
419
485
  description: "Pre-deploy LLM cost accident guardrail (serverless local CLI)",
420
486
  bin: {
421
487
  aiopt: "dist/cli.js"
@@ -945,7 +1011,7 @@ function accidentRiskFromMonthly(monthly) {
945
1011
  function round23(n) {
946
1012
  return Math.round(n * 100) / 100;
947
1013
  }
948
- function monthEstimate(delta, events) {
1014
+ function windowDays(events) {
949
1015
  let days = 1;
950
1016
  try {
951
1017
  const times = events.map((e) => Date.parse(e.ts)).filter((t) => Number.isFinite(t)).sort((a, b) => a - b);
@@ -957,6 +1023,10 @@ function monthEstimate(delta, events) {
957
1023
  } catch {
958
1024
  days = 1;
959
1025
  }
1026
+ return days;
1027
+ }
1028
+ function monthEstimate(delta, events) {
1029
+ const days = windowDays(events);
960
1030
  return delta * 30 / days;
961
1031
  }
962
1032
  function applyCandidate(events, cand) {
@@ -1024,7 +1094,7 @@ function runGuard(rt, input) {
1024
1094
  }
1025
1095
  const baselineEvents = input.baselineEvents.map((e) => ({ ...e, billed_cost: void 0 }));
1026
1096
  const base = analyze(rt, baselineEvents);
1027
- const candidateEvents = applyCandidate(baselineEvents, input.candidate);
1097
+ const candidateEvents = input.candidateEvents && input.candidateEvents.length > 0 ? input.candidateEvents.map((e) => ({ ...e, billed_cost: void 0 })) : applyCandidate(baselineEvents, input.candidate);
1028
1098
  const cand = analyze(rt, candidateEvents);
1029
1099
  const baseCost = base.analysis.total_cost;
1030
1100
  let candCost = cand.analysis.total_cost;
@@ -1051,7 +1121,10 @@ function runGuard(rt, input) {
1051
1121
  candCost += retryUnit * input.candidate.retriesDelta;
1052
1122
  }
1053
1123
  const delta = candCost - baseCost;
1054
- const changeConf = confidenceFromChange(input.candidate);
1124
+ const days = windowDays(baselineEvents);
1125
+ const baselineMonthly = baseCost * 30 / days;
1126
+ const candidateMonthly = candCost * 30 / days;
1127
+ const changeConf = input.candidateEvents ? { level: "High", reasons: ["actual logs diff (--baseline/--candidate)"] } : confidenceFromChange(input.candidate);
1055
1128
  const dq = assessDataQuality(baselineEvents, base);
1056
1129
  const conf = { level: degrade(changeConf.level, dq.penalty), reasons: [...changeConf.reasons, ...dq.reasons.map((r) => `data: ${r}`)] };
1057
1130
  const monthly = monthEstimate(Math.max(0, delta), baselineEvents);
@@ -1065,11 +1138,37 @@ function runGuard(rt, input) {
1065
1138
  exitCode = 2;
1066
1139
  headline = "WARN: possible LLM cost accident";
1067
1140
  }
1141
+ const budget = input.candidate.budgetMonthlyUsd;
1142
+ if (budget && Number.isFinite(budget) && budget > 0 && candidateMonthly > budget) {
1143
+ exitCode = 3;
1144
+ headline = "FAIL: candidate exceeds monthly budget";
1145
+ }
1068
1146
  const reasons = conf.reasons.length ? conf.reasons.join(", ") : "n/a";
1147
+ function topCauses() {
1148
+ const out = [];
1149
+ if (!input.candidateEvents) {
1150
+ if (callMult !== 1) out.push("traffic spike (call-mult)");
1151
+ if (input.candidate.retriesDelta && input.candidate.retriesDelta !== 0) out.push("retry spike (retries-delta)");
1152
+ if (input.candidate.model || input.candidate.provider) out.push("model/provider change");
1153
+ if (input.candidate.contextMultiplier && input.candidate.contextMultiplier !== 1) out.push("context length change");
1154
+ if (input.candidate.outputMultiplier && input.candidate.outputMultiplier !== 1) out.push("output length change");
1155
+ } else {
1156
+ if (candidateEvents.length !== baselineEvents.length) out.push("traffic/call volume changed");
1157
+ const bTop = base.analysis.by_model_top?.[0]?.key;
1158
+ const cTop = cand.analysis.by_model_top?.[0]?.key;
1159
+ if (bTop && cTop && bTop !== cTop) out.push("model mix changed");
1160
+ if ((cand.savings?.retry_waste_usd || 0) > (base.savings?.retry_waste_usd || 0)) out.push("retry waste increased");
1161
+ }
1162
+ if (budget && Number.isFinite(budget) && budget > 0) out.push(`budget gate: $${round23(budget)}/mo`);
1163
+ return out.slice(0, 3);
1164
+ }
1165
+ const causes = topCauses();
1069
1166
  const msg = [
1070
1167
  headline,
1071
1168
  `Summary: baseline=$${round23(baseCost)} \u2192 candidate=$${round23(candCost)} (\u0394=$${round23(delta)})`,
1169
+ `Monthly est: baseline=$${round23(baselineMonthly)} \u2192 candidate=$${round23(candidateMonthly)}${budget ? ` (budget=$${round23(budget)})` : ""}`,
1072
1170
  callMult !== 1 ? `Call volume multiplier: x${callMult}` : null,
1171
+ causes.length ? `Top causes: ${causes.join(" | ")}` : null,
1073
1172
  `Impact (monthly est): +$${monthlyRounded}`,
1074
1173
  `Accident risk: ${accidentRiskFromMonthly(monthly)}`,
1075
1174
  `Confidence: ${conf.level} (${reasons})`,
@@ -1186,7 +1285,13 @@ async function startDashboard(cwd, opts) {
1186
1285
  <a href="/api/guard-last.txt" target="_blank">raw txt</a>
1187
1286
  <span class="muted">\xB7</span>
1188
1287
  <a href="/api/guard-last.json" target="_blank">raw json</a>
1288
+ <span class="muted">\xB7</span>
1289
+ <a href="/api/guard-history.jsonl" target="_blank">history</a>
1189
1290
  </div>
1291
+
1292
+ <div style="height:12px"></div>
1293
+ <div style="font-weight:900">Recent guard runs</div>
1294
+ <pre id="guardHist" style="max-height:220px; overflow:auto">loading\u2026</pre>
1190
1295
  </div>
1191
1296
 
1192
1297
  <div class="card c6">
@@ -1200,6 +1305,11 @@ async function startDashboard(cwd, opts) {
1200
1305
  <div class="badge" id="savingsBadge"><span class="b" style="background:#a78bfa"></span><span id="savings">savings: \u2014</span></div>
1201
1306
  </div>
1202
1307
 
1308
+ <div style="height:12px"></div>
1309
+ <div style="font-weight:900">Cost trend (last 7d)</div>
1310
+ <div class="mini"><a href="/api/usage.jsonl" target="_blank">usage.jsonl</a></div>
1311
+ <pre id="trend">loading\u2026</pre>
1312
+
1203
1313
  <div style="height:12px"></div>
1204
1314
  <div style="font-weight:900">Cost by model</div>
1205
1315
  <div id="byModel" class="bars"></div>
@@ -1262,6 +1372,16 @@ async function load(){
1262
1372
  else {badge.classList.add('fail'); t.textContent='FAIL (3)';}
1263
1373
  }
1264
1374
 
1375
+ const histTxt = await fetch('/api/guard-history.jsonl').then(r=>r.ok?r.text():null);
1376
+ if(histTxt){
1377
+ const lines = histTxt.trim().split('
1378
+ ').filter(Boolean).slice(-15).reverse();
1379
+ document.getElementById('guardHist').textContent = lines.join('
1380
+ ');
1381
+ } else {
1382
+ document.getElementById('guardHist').textContent = '(no guard-history.jsonl yet \u2014 run: aiopt guard)';
1383
+ }
1384
+
1265
1385
  const reportJson = await fetch('/api/report.json').then(r=>r.ok?r.json():null);
1266
1386
  if(reportJson){
1267
1387
  const total = reportJson.summary && reportJson.summary.total_cost_usd;
@@ -1275,6 +1395,40 @@ async function load(){
1275
1395
  document.getElementById('scanMeta').textContent = '(no report.json yet \u2014 run: aiopt scan)';
1276
1396
  }
1277
1397
 
1398
+ const usageTxt = await fetch('/api/usage.jsonl').then(r=>r.ok?r.text():null);
1399
+ if(usageTxt){
1400
+ // 7d cost trend: sum(cost_usd) per day from usage.jsonl (ev.ts).
1401
+ const now = Date.now();
1402
+ const bins = Array.from({length:7}, (_,i)=>({ day:i, cost:0, calls:0 }));
1403
+ for(const line of usageTxt.trim().split('
1404
+ ')){
1405
+ if(!line) continue;
1406
+ try{
1407
+ const ev = JSON.parse(line);
1408
+ const t = Date.parse(ev.ts);
1409
+ if(!Number.isFinite(t)) continue;
1410
+ const d = Math.floor((now - t) / 86400000);
1411
+ if(d>=0 && d<7){
1412
+ bins[d].calls++;
1413
+ const c = Number(ev.cost_usd);
1414
+ if(Number.isFinite(c)) bins[d].cost += c;
1415
+ }
1416
+ }catch{}
1417
+ }
1418
+ const max = Math.max(...bins.map(b=>b.cost), 0.000001);
1419
+ const rows = bins.reverse().map((b,idx)=>{
1420
+ const w = Math.round((b.cost/max)*20);
1421
+ const bar = '\u2588'.repeat(w) + '\u2591'.repeat(20-w);
1422
+ const label = (idx===0 ? 'today' : ('d-'+idx));
1423
+ const dollars = ('$' + (Math.round(b.cost*100)/100).toFixed(2));
1424
+ return String(label).padEnd(7) + ' ' + bar + ' ' + String(dollars).padStart(9) + ' (' + b.calls + ' calls)';
1425
+ });
1426
+ document.getElementById('trend').textContent = rows.join('
1427
+ ');
1428
+ } else {
1429
+ document.getElementById('trend').textContent = '(no usage.jsonl yet)';
1430
+ }
1431
+
1278
1432
  const reportMd = await fetch('/api/report.md').then(r=>r.ok?r.text():null);
1279
1433
  document.getElementById('scan').textContent = reportMd || '(no report.md yet \u2014 run: aiopt scan)';
1280
1434
  }
@@ -1291,7 +1445,7 @@ load();
1291
1445
  }
1292
1446
  if (url.startsWith("/api/")) {
1293
1447
  const name = url.replace("/api/", "");
1294
- const allow = /* @__PURE__ */ new Set(["guard-last.txt", "guard-last.json", "report.md", "report.json"]);
1448
+ const allow = /* @__PURE__ */ new Set(["guard-last.txt", "guard-last.json", "guard-history.jsonl", "report.md", "report.json", "usage.jsonl"]);
1295
1449
  if (!allow.has(name)) {
1296
1450
  res.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
1297
1451
  res.end("not found");
@@ -1331,80 +1485,121 @@ var init_dashboard = __esm({
1331
1485
  }
1332
1486
  });
1333
1487
 
1334
- // src/cli.ts
1335
- var import_fs8 = __toESM(require("fs"));
1336
- var import_path8 = __toESM(require("path"));
1337
- var import_commander = require("commander");
1338
-
1339
- // src/io.ts
1340
- var import_fs = __toESM(require("fs"));
1341
- var import_path = __toESM(require("path"));
1342
- var import_sync = require("csv-parse/sync");
1343
- function ensureDir(p) {
1344
- import_fs.default.mkdirSync(p, { recursive: true });
1488
+ // src/rates-util.ts
1489
+ function loadRateTableFromDistPath() {
1490
+ const p = import_path8.default.join(__dirname, "..", "rates", "rate_table.json");
1491
+ return JSON.parse(import_fs8.default.readFileSync(p, "utf8"));
1345
1492
  }
1346
- function readJsonl(filePath) {
1347
- const raw = import_fs.default.readFileSync(filePath, "utf8");
1348
- const lines = raw.split(/\r?\n/).filter((l) => l.trim().length > 0);
1349
- const out = [];
1350
- for (const line of lines) {
1351
- const obj = JSON.parse(line);
1352
- out.push(normalizeEvent(obj));
1493
+ var import_fs8, import_path8;
1494
+ var init_rates_util = __esm({
1495
+ "src/rates-util.ts"() {
1496
+ "use strict";
1497
+ import_fs8 = __toESM(require("fs"));
1498
+ import_path8 = __toESM(require("path"));
1353
1499
  }
1354
- return out;
1355
- }
1356
- function readCsv(filePath) {
1357
- const raw = import_fs.default.readFileSync(filePath, "utf8");
1358
- const records = (0, import_sync.parse)(raw, { columns: true, skip_empty_lines: true, trim: true });
1359
- return records.map((r) => normalizeEvent(r));
1360
- }
1361
- function toNum(x, def = 0) {
1362
- const n = Number(x);
1363
- return Number.isFinite(n) ? n : def;
1364
- }
1365
- function normalizeEvent(x) {
1366
- const inputTokens = x.input_tokens ?? x.prompt_tokens;
1367
- const outputTokens = x.output_tokens ?? x.completion_tokens;
1368
- const featureTag = x.feature_tag ?? x?.meta?.feature_tag ?? x.endpoint ?? "";
1369
- const retries = x.retries ?? (x.attempt !== void 0 ? Math.max(0, toNum(x.attempt) - 1) : 0);
1370
- const billed = x.billed_cost ?? x.cost_usd;
1371
- return {
1372
- ts: String(x.ts ?? ""),
1373
- provider: String(x.provider ?? "").toLowerCase(),
1374
- model: String(x.model ?? ""),
1375
- input_tokens: toNum(inputTokens),
1376
- output_tokens: toNum(outputTokens),
1377
- feature_tag: String(featureTag ?? ""),
1378
- retries: toNum(retries),
1379
- status: String(x.status ?? ""),
1380
- billed_cost: billed === void 0 || billed === "" ? void 0 : toNum(billed),
1381
- trace_id: x.trace_id ? String(x.trace_id) : void 0,
1382
- request_id: x.request_id ? String(x.request_id) : void 0,
1383
- attempt: x.attempt === void 0 ? void 0 : toNum(x.attempt),
1384
- endpoint: x.endpoint ? String(x.endpoint) : void 0
1385
- };
1500
+ });
1501
+
1502
+ // src/quickstart.ts
1503
+ var quickstart_exports = {};
1504
+ __export(quickstart_exports, {
1505
+ runQuickstart: () => runQuickstart,
1506
+ seedDemoUsage: () => seedDemoUsage
1507
+ });
1508
+ function seedDemoUsage(outDir) {
1509
+ import_fs9.default.mkdirSync(outDir, { recursive: true });
1510
+ const p = import_path9.default.join(outDir, "usage.jsonl");
1511
+ const now = Date.now();
1512
+ const lines = [];
1513
+ for (let i = 0; i < 60; i++) {
1514
+ lines.push({
1515
+ ts: new Date(now - i * 6e4).toISOString(),
1516
+ provider: "openai",
1517
+ model: i % 4 === 0 ? "gpt-5.2" : "gpt-5-mini",
1518
+ endpoint: "responses",
1519
+ attempt: 1,
1520
+ trace_id: "demo-" + i,
1521
+ status: "ok",
1522
+ prompt_tokens: 1e4 + i % 10 * 1e3,
1523
+ completion_tokens: 1200 + i % 5 * 200,
1524
+ meta: { feature_tag: i % 2 ? "summarize" : "coding" }
1525
+ });
1526
+ }
1527
+ import_fs9.default.writeFileSync(p, lines.map((x) => JSON.stringify(x)).join("\n") + "\n");
1528
+ return p;
1386
1529
  }
1387
- function isCsvPath(p) {
1388
- return import_path.default.extname(p).toLowerCase() === ".csv";
1530
+ function runQuickstart(cwd, opts) {
1531
+ const outDir = import_path9.default.join(cwd, "aiopt-output");
1532
+ const usagePath = seedDemoUsage(outDir);
1533
+ const rt = loadRateTableFromDistPath();
1534
+ const { readJsonl: readJsonl2 } = (init_io(), __toCommonJS(io_exports));
1535
+ const events = readJsonl2(usagePath);
1536
+ const { analysis, savings, policy, meta } = analyze(rt, events);
1537
+ import_fs9.default.writeFileSync(import_path9.default.join(outDir, "analysis.json"), JSON.stringify(analysis, null, 2));
1538
+ import_fs9.default.writeFileSync(import_path9.default.join(outDir, "report.json"), JSON.stringify({
1539
+ version: 3,
1540
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
1541
+ confidence: analysis.unknown_models?.length ? "MEDIUM" : "HIGH",
1542
+ warnings: [],
1543
+ assumptions: { quickstart: true },
1544
+ summary: {
1545
+ total_cost_usd: analysis.total_cost,
1546
+ estimated_savings_usd: savings.estimated_savings_total,
1547
+ routing_savings_usd: savings.estimated_savings_routing,
1548
+ context_savings_usd: savings.estimated_savings_context,
1549
+ retry_waste_usd: savings.retry_waste
1550
+ },
1551
+ top: {
1552
+ by_model: analysis.by_model_top,
1553
+ by_feature: analysis.by_feature_top
1554
+ },
1555
+ unknown_models: analysis.unknown_models || [],
1556
+ notes: []
1557
+ }, null, 2));
1558
+ import_fs9.default.writeFileSync(import_path9.default.join(outDir, "cost-policy.json"), JSON.stringify(policy, null, 2));
1559
+ import_fs9.default.writeFileSync(import_path9.default.join(outDir, "report.md"), "# AIOpt quickstart demo\n\nThis is a demo report generated by `aiopt quickstart --demo`.\n");
1560
+ const r = runGuard(rt, {
1561
+ baselineEvents: events,
1562
+ candidate: {
1563
+ contextMultiplier: 1.2,
1564
+ callMultiplier: 5,
1565
+ budgetMonthlyUsd: opts.budgetMonthlyUsd
1566
+ }
1567
+ });
1568
+ return { usagePath, outDir, guard: r, port: opts.port };
1389
1569
  }
1570
+ var import_fs9, import_path9;
1571
+ var init_quickstart = __esm({
1572
+ "src/quickstart.ts"() {
1573
+ "use strict";
1574
+ import_fs9 = __toESM(require("fs"));
1575
+ import_path9 = __toESM(require("path"));
1576
+ init_scan();
1577
+ init_rates_util();
1578
+ init_guard();
1579
+ }
1580
+ });
1390
1581
 
1391
1582
  // src/cli.ts
1583
+ var import_fs10 = __toESM(require("fs"));
1584
+ var import_path10 = __toESM(require("path"));
1585
+ var import_commander = require("commander");
1586
+ init_io();
1392
1587
  init_scan();
1393
1588
  var program = new import_commander.Command();
1394
1589
  var DEFAULT_INPUT = "./aiopt-output/usage.jsonl";
1395
1590
  var DEFAULT_OUTPUT_DIR = "./aiopt-output";
1396
1591
  function loadRateTable() {
1397
- const p = import_path8.default.join(__dirname, "..", "rates", "rate_table.json");
1398
- return JSON.parse(import_fs8.default.readFileSync(p, "utf8"));
1592
+ const p = import_path10.default.join(__dirname, "..", "rates", "rate_table.json");
1593
+ return JSON.parse(import_fs10.default.readFileSync(p, "utf8"));
1399
1594
  }
1400
1595
  program.name("aiopt").description("AI \uBE44\uC6A9 \uC790\uB3D9 \uC808\uAC10 \uC778\uD504\uB77C \u2014 \uC11C\uBC84 \uC5C6\uB294 \uB85C\uCEEC CLI MVP").version(require_package().version);
1401
1596
  program.command("init").description("aiopt-input/ \uBC0F \uC0D8\uD50C usage.jsonl, aiopt-output/ \uC0DD\uC131").action(() => {
1402
1597
  ensureDir("./aiopt-input");
1403
1598
  ensureDir("./aiopt-output");
1404
- const sampleSrc = import_path8.default.join(__dirname, "..", "samples", "sample_usage.jsonl");
1405
- const dst = import_path8.default.join("./aiopt-input", "usage.jsonl");
1406
- if (!import_fs8.default.existsSync(dst)) {
1407
- import_fs8.default.copyFileSync(sampleSrc, dst);
1599
+ const sampleSrc = import_path10.default.join(__dirname, "..", "samples", "sample_usage.jsonl");
1600
+ const dst = import_path10.default.join("./aiopt-input", "usage.jsonl");
1601
+ if (!import_fs10.default.existsSync(dst)) {
1602
+ import_fs10.default.copyFileSync(sampleSrc, dst);
1408
1603
  console.log("Created ./aiopt-input/usage.jsonl (sample)");
1409
1604
  } else {
1410
1605
  console.log("Exists ./aiopt-input/usage.jsonl (skip)");
@@ -1414,7 +1609,7 @@ program.command("init").description("aiopt-input/ \uBC0F \uC0D8\uD50C usage.json
1414
1609
  program.command("scan").description("\uC785\uB825 \uB85C\uADF8(JSONL/CSV)\uB97C \uBD84\uC11D\uD558\uACE0 report.md/report.json + patches\uAE4C\uC9C0 \uC0DD\uC131").option("--input <path>", "input file path (default: ./aiopt-output/usage.jsonl)", DEFAULT_INPUT).option("--out <dir>", "output dir (default: ./aiopt-output)", DEFAULT_OUTPUT_DIR).action(async (opts) => {
1415
1610
  const inputPath = String(opts.input);
1416
1611
  const outDir = String(opts.out);
1417
- if (!import_fs8.default.existsSync(inputPath)) {
1612
+ if (!import_fs10.default.existsSync(inputPath)) {
1418
1613
  console.error(`Input not found: ${inputPath}`);
1419
1614
  process.exit(1);
1420
1615
  }
@@ -1430,7 +1625,7 @@ program.command("scan").description("\uC785\uB825 \uB85C\uADF8(JSONL/CSV)\uB97C
1430
1625
  const tag = f.status === "no-issue" ? "(no issue detected)" : `($${Math.round(f.impact_usd * 100) / 100})`;
1431
1626
  console.log(`${i + 1}) ${f.title} ${tag}`);
1432
1627
  });
1433
- console.log(`Report: ${import_path8.default.join(outDir, "report.md")}`);
1628
+ console.log(`Report: ${import_path10.default.join(outDir, "report.md")}`);
1434
1629
  });
1435
1630
  program.command("policy").description("\uB9C8\uC9C0\uB9C9 scan \uACB0\uACFC \uAE30\uBC18\uC73C\uB85C cost-policy.json\uB9CC \uC7AC\uC0DD\uC131 (MVP: scan\uACFC \uB3D9\uC77C \uB85C\uC9C1)").option("--input <path>", "input file path (default: ./aiopt-input/usage.jsonl)", DEFAULT_INPUT).option("--out <dir>", "output dir (default: ./aiopt-output)", DEFAULT_OUTPUT_DIR).action((opts) => {
1436
1631
  const inputPath = String(opts.input);
@@ -1440,7 +1635,7 @@ program.command("policy").description("\uB9C8\uC9C0\uB9C9 scan \uACB0\uACFC \uAE
1440
1635
  const { policy } = analyze(rt, events);
1441
1636
  policy.generated_from.input = inputPath;
1442
1637
  ensureDir(outDir);
1443
- import_fs8.default.writeFileSync(import_path8.default.join(outDir, "cost-policy.json"), JSON.stringify(policy, null, 2));
1638
+ import_fs10.default.writeFileSync(import_path10.default.join(outDir, "cost-policy.json"), JSON.stringify(policy, null, 2));
1444
1639
  console.log(`OK: ${outDir}/cost-policy.json`);
1445
1640
  });
1446
1641
  program.command("install").description("Install AIOpt guardrails: create aiopt/ + policies + usage.jsonl").option("--force", "overwrite existing files").option("--seed-sample", "seed 1 sample line into aiopt-output/usage.jsonl").action(async (opts) => {
@@ -1480,7 +1675,7 @@ licenseCmd.command("verify").option("--path <path>", "license.json path (default
1480
1675
  const { DEFAULT_PUBLIC_KEY_PEM: DEFAULT_PUBLIC_KEY_PEM2, defaultLicensePath: defaultLicensePath2, readLicenseFile: readLicenseFile2, verifyLicenseKey: verifyLicenseKey2 } = await Promise.resolve().then(() => (init_license(), license_exports));
1481
1676
  const p = opts.path ? String(opts.path) : defaultLicensePath2(process.cwd());
1482
1677
  const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM2;
1483
- if (!import_fs8.default.existsSync(p)) {
1678
+ if (!import_fs10.default.existsSync(p)) {
1484
1679
  console.error(`FAIL: license file not found: ${p}`);
1485
1680
  process.exit(3);
1486
1681
  }
@@ -1497,7 +1692,7 @@ licenseCmd.command("status").option("--path <path>", "license.json path (default
1497
1692
  const { DEFAULT_PUBLIC_KEY_PEM: DEFAULT_PUBLIC_KEY_PEM2, defaultLicensePath: defaultLicensePath2, readLicenseFile: readLicenseFile2, verifyLicenseKey: verifyLicenseKey2 } = await Promise.resolve().then(() => (init_license(), license_exports));
1498
1693
  const p = opts.path ? String(opts.path) : defaultLicensePath2(process.cwd());
1499
1694
  const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM2;
1500
- if (!import_fs8.default.existsSync(p)) {
1695
+ if (!import_fs10.default.existsSync(p)) {
1501
1696
  console.log("NO_LICENSE");
1502
1697
  process.exit(2);
1503
1698
  }
@@ -1510,32 +1705,48 @@ licenseCmd.command("status").option("--path <path>", "license.json path (default
1510
1705
  console.log(`INVALID: ${v.reason || "unknown"}`);
1511
1706
  process.exit(3);
1512
1707
  });
1513
- program.command("guard").description("Pre-deploy guardrail: compare baseline usage vs candidate change and print warnings (exit codes 0/2/3)").option("--input <path>", "baseline usage jsonl/csv (default: ./aiopt-output/usage.jsonl)", DEFAULT_INPUT).option("--provider <provider>", "candidate provider override").option("--model <model>", "candidate model override").option("--context-mult <n>", "multiply input_tokens by n", (v) => Number(v)).option("--output-mult <n>", "multiply output_tokens by n", (v) => Number(v)).option("--retries-delta <n>", "add n to retries", (v) => Number(v)).option("--call-mult <n>", "multiply call volume by n (traffic spike)", (v) => Number(v)).action(async (opts) => {
1708
+ program.command("guard").description("Pre-deploy guardrail: compare baseline usage vs candidate change (or diff two log sets) and print warnings (exit codes 0/2/3)").option("--input <path>", "baseline usage jsonl/csv (legacy alias for --baseline; default: ./aiopt-output/usage.jsonl)", DEFAULT_INPUT).option("--baseline <path>", "baseline usage jsonl/csv (diff mode when used with --candidate)").option("--candidate <path>", "candidate usage jsonl/csv (diff mode: compare two real log sets)").option("--provider <provider>", "candidate provider override (transform mode only)").option("--model <model>", "candidate model override (transform mode only)").option("--context-mult <n>", "multiply input_tokens by n (transform mode only)", (v) => Number(v)).option("--output-mult <n>", "multiply output_tokens by n (transform mode only)", (v) => Number(v)).option("--retries-delta <n>", "add n to retries (transform mode only)", (v) => Number(v)).option("--call-mult <n>", "multiply call volume by n (traffic spike)", (v) => Number(v)).option("--budget-monthly <usd>", "fail if estimated candidate monthly cost exceeds this budget", (v) => Number(v)).action(async (opts) => {
1514
1709
  const rt = loadRateTable();
1515
- const inputPath = String(opts.input);
1516
- if (!import_fs8.default.existsSync(inputPath)) {
1517
- console.error(`FAIL: baseline not found: ${inputPath}`);
1710
+ const baselinePath = String(opts.baseline || opts.input);
1711
+ const candidatePath = opts.candidate ? String(opts.candidate) : null;
1712
+ const diffMode = Boolean(opts.baseline || opts.candidate);
1713
+ if (diffMode && (!opts.baseline || !opts.candidate)) {
1714
+ console.error("FAIL: diff mode requires both --baseline and --candidate");
1518
1715
  process.exit(3);
1519
1716
  }
1520
- const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);
1717
+ if (!import_fs10.default.existsSync(baselinePath)) {
1718
+ console.error(`FAIL: baseline not found: ${baselinePath}`);
1719
+ process.exit(3);
1720
+ }
1721
+ if (candidatePath && !import_fs10.default.existsSync(candidatePath)) {
1722
+ console.error(`FAIL: candidate not found: ${candidatePath}`);
1723
+ process.exit(3);
1724
+ }
1725
+ const baselineEvents = isCsvPath(baselinePath) ? readCsv(baselinePath) : readJsonl(baselinePath);
1726
+ const candidateEvents = candidatePath ? isCsvPath(candidatePath) ? readCsv(candidatePath) : readJsonl(candidatePath) : void 0;
1521
1727
  const { runGuard: runGuard2 } = await Promise.resolve().then(() => (init_guard(), guard_exports));
1522
1728
  const r = runGuard2(rt, {
1523
- baselineEvents: events,
1729
+ baselineEvents,
1730
+ candidateEvents,
1524
1731
  candidate: {
1525
1732
  provider: opts.provider,
1526
1733
  model: opts.model,
1527
1734
  contextMultiplier: opts.contextMult,
1528
1735
  outputMultiplier: opts.outputMult,
1529
1736
  retriesDelta: opts.retriesDelta,
1530
- callMultiplier: opts.callMult
1737
+ callMultiplier: opts.callMult,
1738
+ budgetMonthlyUsd: opts.budgetMonthly
1531
1739
  }
1532
1740
  });
1533
1741
  console.log(r.message);
1534
1742
  try {
1535
- const outDir = import_path8.default.resolve(DEFAULT_OUTPUT_DIR);
1536
- import_fs8.default.mkdirSync(outDir, { recursive: true });
1537
- import_fs8.default.writeFileSync(import_path8.default.join(outDir, "guard-last.txt"), r.message);
1538
- import_fs8.default.writeFileSync(import_path8.default.join(outDir, "guard-last.json"), JSON.stringify({ ts: (/* @__PURE__ */ new Date()).toISOString(), exitCode: r.exitCode }, null, 2));
1743
+ const outDir = import_path10.default.resolve(DEFAULT_OUTPUT_DIR);
1744
+ import_fs10.default.mkdirSync(outDir, { recursive: true });
1745
+ const ts = (/* @__PURE__ */ new Date()).toISOString();
1746
+ import_fs10.default.writeFileSync(import_path10.default.join(outDir, "guard-last.txt"), r.message);
1747
+ import_fs10.default.writeFileSync(import_path10.default.join(outDir, "guard-last.json"), JSON.stringify({ ts, exitCode: r.exitCode }, null, 2));
1748
+ const histLine = JSON.stringify({ ts, exitCode: r.exitCode, mode: candidateEvents ? "diff" : "transform", baseline: baselinePath, candidate: candidatePath }) + "\n";
1749
+ import_fs10.default.appendFileSync(import_path10.default.join(outDir, "guard-history.jsonl"), histLine);
1539
1750
  } catch {
1540
1751
  }
1541
1752
  process.exit(r.exitCode);
@@ -1544,5 +1755,31 @@ program.command("dashboard").description("Local dashboard (localhost only): view
1544
1755
  const { startDashboard: startDashboard2 } = await Promise.resolve().then(() => (init_dashboard(), dashboard_exports));
1545
1756
  await startDashboard2(process.cwd(), { port: Number(opts.port || 3010) });
1546
1757
  });
1758
+ program.command("quickstart").description("1-minute demo: generate sample usage, run scan+guard, and print dashboard URL").option("--demo", "run demo workflow (writes to ./aiopt-output)").option("--port <n>", "dashboard port (default: 3010)", (v) => Number(v), 3010).option("--budget-monthly <usd>", "optional budget gate for the demo guard", (v) => Number(v)).action(async (opts) => {
1759
+ if (!opts.demo) {
1760
+ console.error("FAIL: quickstart requires --demo");
1761
+ process.exit(3);
1762
+ }
1763
+ const { runQuickstart: runQuickstart2 } = await Promise.resolve().then(() => (init_quickstart(), quickstart_exports));
1764
+ const r = runQuickstart2(process.cwd(), { port: Number(opts.port || 3010), budgetMonthlyUsd: opts.budgetMonthly });
1765
+ console.log("OK: demo usage written:", r.usagePath);
1766
+ console.log("--- guard ---");
1767
+ console.log(r.guard.message);
1768
+ try {
1769
+ const fs11 = await import("fs");
1770
+ const path11 = await import("path");
1771
+ fs11.mkdirSync(r.outDir, { recursive: true });
1772
+ const ts = (/* @__PURE__ */ new Date()).toISOString();
1773
+ fs11.writeFileSync(path11.join(r.outDir, "guard-last.txt"), r.guard.message);
1774
+ fs11.writeFileSync(path11.join(r.outDir, "guard-last.json"), JSON.stringify({ ts, exitCode: r.guard.exitCode }, null, 2));
1775
+ const histLine = JSON.stringify({ ts, exitCode: r.guard.exitCode, mode: "quickstart", baseline: r.usagePath, candidate: null }) + "\n";
1776
+ fs11.appendFileSync(path11.join(r.outDir, "guard-history.jsonl"), histLine);
1777
+ } catch {
1778
+ }
1779
+ console.log("--- next ---");
1780
+ console.log(`Run: npx aiopt dashboard --port ${r.port}`);
1781
+ console.log(`Open: http://127.0.0.1:${r.port}/`);
1782
+ process.exit(r.guard.exitCode);
1783
+ });
1547
1784
  program.parse(process.argv);
1548
1785
  //# sourceMappingURL=cli.js.map
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cost.ts","../src/solutions.ts","../src/scan.ts","../package.json","../src/install.ts","../src/doctor.ts","../src/license.ts","../src/guard.ts","../src/dashboard.ts","../src/cli.ts","../src/io.ts"],"sourcesContent":["import { RateTable, UsageEvent } from './types';\n\nexport type CostResult = {\n cost: number;\n used_rate: {\n kind: 'billed_cost' | 'official' | 'estimated';\n provider: string;\n model: string;\n input_per_m: number;\n output_per_m: number;\n };\n};\n\nexport function getRates(rt: RateTable, provider: string, model: string) {\n const prov = String(provider || '').toLowerCase();\n\n // local/offline LLM: treat cost as 0 (or user can add a provider rate later)\n if (prov === 'local' || prov === 'ollama' || prov === 'vllm') {\n return { kind: 'official' as const, input: 0, output: 0 };\n }\n\n const p = rt.providers[prov];\n if (!p) return null;\n const m = p.models[model];\n if (m) return { kind: 'official' as const, input: m.input, output: m.output };\n return { kind: 'estimated' as const, input: p.default_estimated.input, output: p.default_estimated.output };\n}\n\nexport function costOfEvent(rt: RateTable, ev: UsageEvent): CostResult {\n if (typeof ev.billed_cost === 'number' && Number.isFinite(ev.billed_cost)) {\n return {\n cost: ev.billed_cost,\n used_rate: {\n kind: 'billed_cost',\n provider: ev.provider,\n model: ev.model,\n input_per_m: 0,\n output_per_m: 0\n }\n };\n }\n\n const r = getRates(rt, ev.provider, ev.model);\n if (!r) {\n // Unknown provider: deterministic fallback estimate (kept stable for reproducibility)\n const input_per_m = 1.0;\n const output_per_m = 4.0;\n const cost = (ev.input_tokens / 1e6) * input_per_m + (ev.output_tokens / 1e6) * output_per_m;\n return {\n cost,\n used_rate: { kind: 'estimated', provider: String(ev.provider || '').toLowerCase(), model: ev.model, input_per_m, output_per_m }\n };\n }\n\n const cost = (ev.input_tokens / 1e6) * r.input + (ev.output_tokens / 1e6) * r.output;\n return {\n cost,\n used_rate: {\n kind: r.kind,\n provider: ev.provider,\n model: ev.model,\n input_per_m: r.input,\n output_per_m: r.output\n }\n };\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { AnalysisJson, Savings } from './scan';\n\nexport type Fix = {\n id: string;\n title: string;\n impact_usd: number;\n why: string;\n what_to_change: string[];\n status: 'action' | 'no-issue';\n};\n\nconst EPS = 0.0001;\n\nexport function buildTopFixes(analysis: AnalysisJson, savings: Savings): Fix[] {\n const fixes: Fix[] = [];\n\n // Retry tuning\n fixes.push({\n id: 'fix-retry-tuning',\n title: 'Retry tuning',\n impact_usd: Number(savings.retry_waste || 0),\n status: Number(savings.retry_waste || 0) > EPS ? 'action' : 'no-issue',\n why: `Retry waste is estimated at $${round2(Number(savings.retry_waste || 0))}.`,\n what_to_change: [\n 'aiopt/policies/retry.json: lower max_attempts or adjust backoff_ms',\n 'Ensure idempotency keys are stable per trace_id',\n 'Log error_code to identify retryable classes'\n ]\n });\n\n // Output cap (use context_savings as proxy impact)\n fixes.push({\n id: 'fix-output-cap',\n title: 'Output cap',\n impact_usd: Number(savings.context_savings || 0),\n status: Number(savings.context_savings || 0) > EPS ? 'action' : 'no-issue',\n why: `Context savings estimate: $${round2(Number(savings.context_savings || 0))}. Output caps prevent runaway completions.`,\n what_to_change: [\n 'aiopt/policies/output.json: set max_output_tokens_default',\n 'aiopt/policies/output.json: set per_feature caps (summarize/classify/translate)'\n ]\n });\n\n // Routing\n const topFeature = analysis.by_feature_top?.[0]?.key;\n fixes.push({\n id: 'fix-routing',\n title: 'Routing rule',\n impact_usd: Number(savings.routing_savings || 0),\n status: Number(savings.routing_savings || 0) > EPS ? 'action' : 'no-issue',\n why: `Routing savings estimate: $${round2(Number(savings.routing_savings || 0))}.`,\n what_to_change: [\n 'aiopt/policies/routing.json: route summarize/classify/translate to cheap tier',\n `Consider adding feature_tag_in for top feature: ${topFeature || '(unknown)'}`\n ]\n });\n\n // sort by impact desc, deterministic tie-break\n fixes.sort((a, b) => (b.impact_usd - a.impact_usd) || a.id.localeCompare(b.id));\n return fixes;\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nexport function writePatches(outDir: string, fixes: Fix[]) {\n const patchesDir = path.join(outDir, 'patches');\n fs.mkdirSync(patchesDir, { recursive: true });\n\n const readme = [\n '# AIOpt patches (MVP)',\n '',\n 'This folder contains suggested changes you can apply locally.',\n '',\n '## Top fixes',\n ...fixes.map((f, i) => `${i + 1}. ${f.title} — ${f.why}`),\n '',\n 'Files are stubs (human review required).',\n ''\n ].join('\\n');\n\n fs.writeFileSync(path.join(patchesDir, 'README.md'), readme);\n\n // Minimal stub files to satisfy DoD naming.\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.routing.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/routing.json', fixes: fixes.filter(f => f.id.includes('routing')) }, null, 2));\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.retry.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/retry.json', fixes: fixes.filter(f => f.id.includes('retry')) }, null, 2));\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.output.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/output.json', fixes: fixes.filter(f => f.id.includes('output')) }, null, 2));\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { RateTable, UsageEvent } from './types';\nimport { costOfEvent, getRates } from './cost';\nimport { buildTopFixes, writePatches } from './solutions';\n\nexport type AnalysisJson = {\n total_cost: number;\n by_model_top: Array<{ key: string; cost: number; events: number }>;\n by_feature_top: Array<{ key: string; cost: number; events: number }>;\n unknown_models: Array<{ provider: string; model: string; reason: string }>;\n rate_table_version: string;\n rate_table_date: string;\n};\n\nexport type Savings = {\n estimated_savings_total: number;\n routing_savings: number;\n context_savings: number;\n retry_waste: number;\n notes: [string, string, string];\n};\n\nexport type PolicyJson = {\n version: number;\n default_provider: string;\n rules: Array<any>;\n budgets: { currency: string; notes?: string };\n generated_from: { rate_table_version: string; input: string };\n};\n\nconst ROUTE_TO_CHEAP_FEATURES = new Set(['summarize', 'classify', 'translate']);\n\nfunction topN(map: Map<string, { cost: number; events: number }>, n: number) {\n return [...map.entries()]\n .map(([key, v]) => ({ key, cost: v.cost, events: v.events }))\n .sort((a, b) => b.cost - a.cost)\n .slice(0, n);\n}\n\nexport function analyze(rt: RateTable, events: UsageEvent[]): { analysis: AnalysisJson; savings: Savings; policy: PolicyJson; meta: { mode: 'attempt-log'|'legacy' } } {\n const byModel = new Map<string, { cost: number; events: number }>();\n const byFeature = new Map<string, { cost: number; events: number }>();\n const unknownModels: AnalysisJson['unknown_models'] = [];\n\n const perEventCosts: Array<{ ev: UsageEvent; cost: number }> = [];\n\n // Detect wrapper attempt-log mode: each JSONL line is an attempt (trace_id/attempt present)\n const isAttemptLog = events.some(e => (e.trace_id && String(e.trace_id).length > 0) || (e.attempt !== undefined && Number(e.attempt) > 0));\n\n let baseTotal = 0;\n let total = 0;\n for (const ev of events) {\n const cr = costOfEvent(rt, ev);\n\n baseTotal += cr.cost;\n\n if (isAttemptLog) {\n // Each event line is already one attempt; do NOT multiply by retries.\n total += cr.cost;\n } else {\n // Legacy aggregate mode: one line represents a request with retries count.\n const retries = Math.max(0, Number(ev.retries || 0));\n total += cr.cost * (1 + retries);\n }\n\n perEventCosts.push({ ev, cost: cr.cost });\n\n const mk = `${ev.provider}:${ev.model}`;\n const fk = ev.feature_tag || '(none)';\n\n const mv = byModel.get(mk) || { cost: 0, events: 0 };\n mv.cost += cr.cost; mv.events += 1;\n byModel.set(mk, mv);\n\n const fv = byFeature.get(fk) || { cost: 0, events: 0 };\n fv.cost += cr.cost; fv.events += 1;\n byFeature.set(fk, fv);\n\n const rr = getRates(rt, ev.provider, ev.model);\n if (!rr) {\n unknownModels.push({ provider: ev.provider, model: ev.model, reason: 'unknown provider (estimated)' });\n } else if (rr.kind === 'estimated') {\n unknownModels.push({ provider: ev.provider, model: ev.model, reason: 'unknown model (estimated)' });\n }\n }\n\n // --- Savings simulator (refactor): per-event before/after with caps + no double counting\n type Pot = { routing: number; context: number; retry: number; total: number; waste: number };\n const potByIdx: Pot[] = [];\n\n // Potential routing/context computed per event\n for (const { ev, cost } of perEventCosts) {\n const retries = Math.max(0, Number(ev.retries || 0));\n const attempt = Number(ev.attempt || 1);\n\n const total_i = isAttemptLog ? cost : cost * (1 + retries);\n // Retry waste:\n // - attempt-log mode: attempts >= 2 are retry waste\n // - legacy mode: base_cost * retries\n const waste_i = isAttemptLog ? (attempt >= 2 ? cost : 0) : cost * retries;\n\n let routing_i = 0;\n if (ROUTE_TO_CHEAP_FEATURES.has(String(ev.feature_tag || '').toLowerCase())) {\n const provider = ev.provider;\n const p = rt.providers[provider];\n if (p) {\n const entries = Object.entries(p.models);\n if (entries.length > 0) {\n const cheapest = entries\n .map(([name, r]) => ({ name, score: (r.input + r.output) / 2, r }))\n .sort((a, b) => a.score - b.score)[0];\n const currentRate = getRates(rt, provider, ev.model);\n if (currentRate && currentRate.kind !== 'estimated') {\n const currentCost = (ev.input_tokens / 1e6) * currentRate.input + (ev.output_tokens / 1e6) * currentRate.output;\n const cheapCost = (ev.input_tokens / 1e6) * cheapest.r.input + (ev.output_tokens / 1e6) * cheapest.r.output;\n const diff = (currentCost - cheapCost) * (1 + retries);\n routing_i = Math.max(0, diff);\n }\n }\n }\n }\n\n // context: top 20% rule is applied later by index set\n potByIdx.push({ routing: routing_i, context: 0, retry: waste_i, total: total_i, waste: waste_i });\n }\n\n // context potential assignment (deterministic): top 20% by input_tokens => 25% reduction\n // In attempt-log mode, only apply to attempt==1 to avoid overcounting retries.\n const sortedIdx = [...events.map((e, i) => ({ i, input: Number(e.input_tokens || 0), ok: !isAttemptLog || Number(e.attempt || 1) === 1 }))]\n .filter(x => x.ok)\n .sort((a, b) => b.input - a.input);\n const k = Math.max(1, Math.floor(sortedIdx.length * 0.2));\n const topIdx = new Set(sortedIdx.slice(0, k).map(x => x.i));\n for (let i = 0; i < events.length; i++) {\n if (!topIdx.has(i)) continue;\n const ev = events[i];\n const retries = Math.max(0, Number(ev.retries || 0));\n const r = getRates(rt, ev.provider, ev.model);\n if (!r) continue;\n const saveTokens = (Number(ev.input_tokens || 0)) * 0.25;\n const multiplier = isAttemptLog ? 1 : (1 + retries);\n const diff = (saveTokens / 1e6) * r.input * multiplier;\n potByIdx[i].context = Math.max(0, diff);\n }\n\n // Allocate savings without overlap (routing -> context -> retry), each capped by remaining cost.\n let routingSavings = 0;\n let contextSavings = 0;\n let retryWaste = 0;\n\n for (const p of potByIdx) {\n let remaining = p.total;\n\n const rSave = Math.min(p.routing, remaining);\n remaining -= rSave;\n routingSavings += rSave;\n\n const cSave = Math.min(p.context, remaining);\n remaining -= cSave;\n contextSavings += cSave;\n\n // Retry tuning can only save the waste portion, and cannot exceed remaining.\n const retrySave = Math.min(p.retry, remaining);\n retryWaste += retrySave;\n }\n\n const estimatedSavingsTotal = routingSavings + contextSavings + retryWaste;\n // Global guard\n const guardedSavingsTotal = Math.min(estimatedSavingsTotal, total);\n\n const analysis: AnalysisJson = {\n total_cost: round2(total),\n by_model_top: topN(byModel, 10).map(x => ({ ...x, cost: round2(x.cost) })),\n by_feature_top: topN(byFeature, 10).map(x => ({ ...x, cost: round2(x.cost) })),\n unknown_models: uniqUnknown(unknownModels),\n rate_table_version: rt.version,\n rate_table_date: rt.date\n };\n\n const savings: Savings = {\n estimated_savings_total: round2(guardedSavingsTotal),\n routing_savings: round2(routingSavings),\n context_savings: round2(contextSavings),\n retry_waste: round2(retryWaste),\n notes: [\n `a) 모델 라우팅 절감(추정): $${round2(routingSavings)}`,\n `b) 컨텍스트 감축(추정): $${round2(contextSavings)} (상위 20% input에 25% 감축 가정)` ,\n `c) 재시도/오류 낭비(상한 적용): $${round2(retryWaste)} (retries 기반)`\n ]\n };\n\n const policy: PolicyJson = buildPolicy(rt, events);\n\n return { analysis, savings, policy, meta: { mode: isAttemptLog ? 'attempt-log' : 'legacy' } };\n}\n\nfunction buildPolicy(rt: RateTable, events: UsageEvent[]): PolicyJson {\n // Default provider: most frequent\n const freq = new Map<string, number>();\n for (const ev of events) freq.set(ev.provider, (freq.get(ev.provider) || 0) + 1);\n const defaultProvider = [...freq.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] || 'openai';\n\n // For cheap features: recommend cheapest known model per provider.\n const rules: any[] = [];\n for (const provider of Object.keys(rt.providers)) {\n const p = rt.providers[provider];\n const entries = Object.entries(p.models);\n if (entries.length === 0) continue;\n const cheapest = entries\n .map(([name, r]) => ({ name, score: (r.input + r.output) / 2, r }))\n .sort((a, b) => a.score - b.score)[0];\n\n rules.push({\n match: { provider, feature_tag_in: ['summarize', 'classify', 'translate'] },\n action: { recommend_model: cheapest.name, reason: 'cheap-feature routing' }\n });\n }\n\n // Unknown models: keep (no policy)\n rules.push({ match: { model_unknown: true }, action: { keep: true, reason: 'unknown model -> no policy applied' } });\n\n return {\n version: 1,\n default_provider: defaultProvider,\n rules,\n budgets: { currency: rt.currency, notes: 'MVP: budgets not enforced' },\n generated_from: { rate_table_version: rt.version, input: './aiopt-input/usage.jsonl' }\n };\n}\n\nfunction uniqUnknown(list: AnalysisJson['unknown_models']) {\n const seen = new Set<string>();\n const out: AnalysisJson['unknown_models'] = [];\n for (const x of list) {\n const k = `${x.provider}:${x.model}:${x.reason}`;\n if (seen.has(k)) continue;\n seen.add(k);\n out.push(x);\n }\n return out;\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nexport function writeOutputs(outDir: string, analysis: AnalysisJson, savings: Savings, policy: PolicyJson, meta?: { mode?: 'attempt-log'|'legacy' }) {\n const mode = meta?.mode || 'legacy';\n\n fs.mkdirSync(outDir, { recursive: true });\n\n fs.writeFileSync(path.join(outDir, 'analysis.json'), JSON.stringify(analysis, null, 2));\n\n // report.json is the “one file to parse” summary for downstream tooling.\n const unknownCount = analysis.unknown_models?.length || 0;\n // confidence: downgrade if many unknowns; keep deterministic\n const confidence = unknownCount === 0 ? 'HIGH' : (unknownCount <= 3 ? 'MED' : 'LOW');\n const ratio = analysis.total_cost > 0 ? (savings.estimated_savings_total / analysis.total_cost) : 0;\n const warnings: string[] = [];\n if (ratio >= 0.9) warnings.push('estimated savings >= 90%');\n if (unknownCount > 0) warnings.push('unknown models/providers detected (estimated pricing used)');\n\n const reportJson = {\n version: 3,\n generated_at: new Date().toISOString(),\n confidence,\n warnings,\n assumptions: {\n no_double_counting: 'routing -> context -> retry allocation per-event with remaining-cost caps',\n retry_cost_model: mode === 'attempt-log'\n ? 'attempt-log mode: total_cost is sum of attempt lines; retry_waste is sum of attempts>=2'\n : 'legacy mode: total_cost includes retries as extra attempts (base_cost*(1+retries))',\n context_model: 'top 20% by input_tokens assume 25% input reduction',\n estimated_pricing_note: unknownCount > 0 ? 'some items use estimated rates; treat savings as a band' : 'all items used known rates'\n },\n summary: {\n total_cost_usd: analysis.total_cost,\n estimated_savings_usd: savings.estimated_savings_total,\n routing_savings_usd: savings.routing_savings,\n context_savings_usd: savings.context_savings,\n retry_waste_usd: savings.retry_waste\n },\n top: {\n by_model: analysis.by_model_top,\n by_feature: analysis.by_feature_top\n },\n unknown_models: analysis.unknown_models,\n notes: savings.notes\n };\n fs.writeFileSync(path.join(outDir, 'report.json'), JSON.stringify(reportJson, null, 2));\n\n // report.md: \"what to change\" guidance + confidence/assumptions (T4 DoD)\n const ratioMd = analysis.total_cost > 0 ? (savings.estimated_savings_total / analysis.total_cost) : 0;\n const warningsMd: string[] = [];\n if (ratioMd >= 0.9) warningsMd.push('WARNING: estimated savings >= 90% — check overlap/missing rate table');\n\n const reportMd = [\n '# AIOpt Report',\n '',\n `- Total cost: $${analysis.total_cost}`,\n `- Estimated savings: $${savings.estimated_savings_total} (guarded <= total_cost)`,\n `- Confidence: ${confidence}`,\n unknownCount > 0 ? `- Unknown models: ${unknownCount} (estimated pricing used)` : '- Unknown models: 0',\n ...warningsMd.map(w => `- ${w}`),\n '',\n '## ASSUMPTIONS',\n '- No double-counting: routing → context → retry savings allocated per-event with remaining-cost caps.',\n mode === 'attempt-log'\n ? '- Retry cost model: attempt-log mode (total_cost=sum attempts, retry_waste=sum attempt>=2).'\n : '- Retry cost model: legacy mode (total_cost=base_cost*(1+retries)).',\n '- Context savings: top 20% input_tokens events assume 25% input reduction.',\n '',\n '## WHAT TO CHANGE',\n '1) Retry tuning → edit `aiopt/policies/retry.json`',\n '2) Output cap → edit `aiopt/policies/output.json`',\n '3) Routing rule → edit `aiopt/policies/routing.json`',\n '',\n '## OUTPUTS',\n '- `aiopt-output/analysis.json`',\n '- `aiopt-output/report.json`',\n '- `aiopt-output/patches/*`',\n ''\n ].join('\\n');\n fs.writeFileSync(path.join(outDir, 'report.md'), reportMd);\n\n const reportTxt = [\n `총비용: $${analysis.total_cost}`,\n `절감 가능 금액(Estimated): $${savings.estimated_savings_total}`,\n `절감 근거 3줄:`,\n savings.notes[0],\n savings.notes[1],\n savings.notes[2],\n ''\n ].join('\\n');\n fs.writeFileSync(path.join(outDir, 'report.txt'), reportTxt);\n\n fs.writeFileSync(path.join(outDir, 'cost-policy.json'), JSON.stringify(policy, null, 2));\n\n // patches/*\n const fixes = buildTopFixes(analysis, savings);\n writePatches(outDir, fixes);\n}\n","{\n \"name\": \"aiopt\",\n \"version\": \"0.2.6\",\n \"description\": \"Pre-deploy LLM cost accident guardrail (serverless local CLI)\",\n \"bin\": {\n \"aiopt\": \"dist/cli.js\"\n },\n \"type\": \"commonjs\",\n \"main\": \"dist/cli.js\",\n \"files\": [\n \"dist\",\n \"rates\",\n \"samples\",\n \"README.md\"\n ],\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"node --enable-source-maps dist/cli.js\",\n \"prepack\": \"npm run build\",\n \"test:npx\": \"npm pack --silent && node -e \\\"const fs=require('fs');const p=fs.readdirSync('.').find(f=>/^aiopt-.*\\\\.tgz$/.test(f)); if(!p) throw new Error('tgz not found'); console.log('tgz',p);\\\" && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) install --force && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) doctor && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) scan && test -f ./aiopt-output/report.md && echo OK\",\n \"test:guard\": \"npm run build --silent && node scripts/test-guard.js\",\n \"test:license\": \"npm run build --silent && node scripts/test-license.js\",\n \"test:landing\": \"node scripts/test-landing.js\"\n },\n \"dependencies\": {\n \"commander\": \"^14.0.0\",\n \"csv-parse\": \"^6.1.0\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^24.0.0\",\n \"tsup\": \"^8.5.0\",\n \"typescript\": \"^5.9.2\"\n }\n}\n","import fs from 'fs';\nimport path from 'path';\n\nexport type InstallOptions = {\n force?: boolean;\n seedSample?: boolean;\n};\n\nfunction ensureDir(p: string) {\n fs.mkdirSync(p, { recursive: true });\n}\n\nfunction writeFile(filePath: string, content: string, force?: boolean) {\n if (!force && fs.existsSync(filePath)) return { wrote: false, reason: 'exists' as const };\n ensureDir(path.dirname(filePath));\n fs.writeFileSync(filePath, content);\n return { wrote: true as const };\n}\n\nexport function runInstall(cwd: string, opts: InstallOptions) {\n const force = Boolean(opts.force);\n\n const aioptDir = path.join(cwd, 'aiopt');\n const policiesDir = path.join(aioptDir, 'policies');\n const outDir = path.join(cwd, 'aiopt-output');\n\n ensureDir(aioptDir);\n ensureDir(policiesDir);\n ensureDir(outDir);\n\n const created: Array<{ path: string; status: 'created' | 'skipped' }> = [];\n\n // 1) aiopt/README.md\n const readme = `# AIOpt\n\nAIOpt는 **scan툴이 아니라 설치형 비용 가드레일이다.**\n\n## Quick start\n\\`\\`\\`bash\nnpx aiopt install --force\nnpx aiopt doctor\n# (your app runs, wrapper logs usage)\nnpx aiopt scan\n\\`\\`\\`\n\n- 서버/대시보드/계정/업로드/결제/프록시 없음\n- 로컬 파일 기반(정책 + usage.jsonl)\n- LLM 호출 금지(수학/룰 기반)\n\n## Wrapper usage (Node.js)\n\nAIOpt 설치 후, 앱 코드에서 wrapper를 불러서 사용량 JSONL을 자동 기록할 수 있습니다.\n\n\na) 최소 형태(토큰을 직접 넘김)\n\n\\`\\`\\`js\nconst { guardedCall } = require('./aiopt/aiopt-wrapper.js');\n\nawait guardedCall(process.cwd(), {\n provider: 'openai',\n model: 'gpt-5-mini',\n endpoint: 'responses.create',\n feature_tag: 'summarize',\n prompt_tokens: 1200,\n trace_id: 'my-trace'\n}, async (req) => {\n // req: { provider, model, endpoint, max_output_tokens, prompt_tokens, idempotency_key }\n // 여기서 실제 SDK 호출 후 결과를 반환\n return { status: 'ok', completion_tokens: 200 };\n});\n\\`\\`\\`\n\nb) OpenAI-style 응답(usage 자동 추출)\n\n\\`\\`\\`js\nreturn {\n status: 'ok',\n response: {\n usage: { prompt_tokens: 1200, completion_tokens: 200, total_tokens: 1400 }\n }\n};\n\\`\\`\\`\n`;\n\n const r1 = writeFile(path.join(aioptDir, 'README.md'), readme, force);\n created.push({ path: 'aiopt/README.md', status: r1.wrote ? 'created' : 'skipped' });\n\n // 2) aiopt/aiopt.config.json\n const config = {\n version: 1,\n installed_at: new Date().toISOString(),\n output_dir: './aiopt-output',\n usage_path: './aiopt-output/usage.jsonl',\n policies_dir: './aiopt/policies',\n rate_table: { path: './rates/rate_table.json' }\n };\n const r2 = writeFile(path.join(aioptDir, 'aiopt.config.json'), JSON.stringify(config, null, 2) + '\\n', force);\n created.push({ path: 'aiopt/aiopt.config.json', status: r2.wrote ? 'created' : 'skipped' });\n\n // 3) policies\n const routing = {\n version: 1,\n rules: [\n { match: { feature_tag_in: ['summarize', 'classify', 'translate'] }, action: { tier: 'cheap', reason: 'cheap feature routing' } },\n { match: { feature_tag_in: ['coding', 'reasoning'] }, action: { tier: 'default', reason: 'keep for quality' } }\n ]\n };\n const retry = {\n version: 1,\n max_attempts: 2,\n backoff_ms: [200, 500],\n retry_on_status: ['error', 'timeout'],\n notes: 'MVP deterministic retry tuning'\n };\n const output = {\n version: 1,\n max_output_tokens_default: 1024,\n per_feature: {\n summarize: 512,\n classify: 256,\n translate: 512\n }\n };\n const context = {\n version: 1,\n input_token_soft_cap: 12000,\n reduce_top_percentile: 0.2,\n assumed_reduction_ratio: 0.25\n };\n\n const p1 = writeFile(path.join(policiesDir, 'routing.json'), JSON.stringify(routing, null, 2) + '\\n', force);\n const p2 = writeFile(path.join(policiesDir, 'retry.json'), JSON.stringify(retry, null, 2) + '\\n', force);\n const p3 = writeFile(path.join(policiesDir, 'output.json'), JSON.stringify(output, null, 2) + '\\n', force);\n const p4 = writeFile(path.join(policiesDir, 'context.json'), JSON.stringify(context, null, 2) + '\\n', force);\n\n created.push({ path: 'aiopt/policies/routing.json', status: p1.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/retry.json', status: p2.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/output.json', status: p3.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/context.json', status: p4.wrote ? 'created' : 'skipped' });\n\n // 4) wrapper template (T2: real guardrails wrapper)\n const wrapperPath = path.join(aioptDir, 'aiopt-wrapper.js');\n const wrapper = \n`// AIOpt Wrapper (guardrails) — local-only (CommonJS)\n\nconst fs = require('fs');\n\nconst path = require('path');\n\nconst crypto = require('crypto');\n\nfunction readJson(p){ return JSON.parse(fs.readFileSync(p,'utf8')); }\nfunction ensureDir(p){ fs.mkdirSync(p,{recursive:true}); }\nfunction appendJsonl(filePath,obj){ ensureDir(path.dirname(filePath)); fs.appendFileSync(filePath, JSON.stringify(obj)+'\\\\n'); }\nfunction sleep(ms){ return new Promise(r=>setTimeout(r,ms)); }\n\nfunction loadConfig(cwd){ return readJson(path.join(cwd,'aiopt','aiopt.config.json')); }\nfunction loadPolicies(cwd,cfg){ const dir=path.isAbsolute(cfg.policies_dir)?cfg.policies_dir:path.join(cwd,cfg.policies_dir);\n return {\n retry: readJson(path.join(dir,'retry.json')) ,\n output: readJson(path.join(dir,'output.json'))\n };\n}\nfunction loadRates(cwd,cfg){\n const rp=path.isAbsolute(cfg.rate_table.path)?cfg.rate_table.path:path.join(cwd,cfg.rate_table.path);\n try{ return readJson(rp); }catch(e){\n // Fresh projects may not have a rates/ table yet. Fall back to a safe default.\n return { providers: {} };\n }\n}\n\nfunction costUsd(rt, provider, model, promptTokens, completionTokens){\n const p=rt.providers && rt.providers[provider];\n const r=(p && p.models && p.models[model]) || (p && p.default_estimated) || {input:1.0, output:4.0};\n return (promptTokens/1e6)*r.input + (completionTokens/1e6)*r.output;\n}\n\nfunction pickRoutedModel(rt, provider, featureTag, currentModel){\n const cheap=['summarize','classify','translate'];\n if(!cheap.includes(String(featureTag||'').toLowerCase())) return { model: currentModel, routed_from: null, hit: null };\n const p=rt.providers && rt.providers[provider];\n const entries=p && p.models ? Object.entries(p.models) : [];\n if(!entries.length) return { model: currentModel, routed_from: null, hit: null };\n const cheapest=entries.map(([name,r])=>({name,score:(r.input+r.output)/2})).sort((a,b)=>a.score-b.score)[0];\n if(!cheapest || cheapest.name===currentModel) return { model: currentModel, routed_from: null, hit: null };\n return { model: cheapest.name, routed_from: currentModel, hit: 'routing:cheap-feature' };\n}\n\nfunction outputCap(outputPolicy, featureTag, requested){\n const per=(outputPolicy && outputPolicy.per_feature) || {};\n const cap=per[String(featureTag||'').toLowerCase()] ?? (outputPolicy.max_output_tokens_default || 1024);\n const req=requested ?? cap;\n return Math.min(req, cap);\n}\n\nconst IDEMPOTENCY=new Map();\n\nfunction normalizeResult(out, input){\n // Accept either normalized return or provider raw.\n const o = (out && out.response) ? out.response : out;\n const status = (out && out.status) || o.status || (o.error ? 'error' : 'ok');\n const usage = o.usage || (o.data && o.data.usage) || null;\n\n const prompt_tokens = Number(\n (out && out.prompt_tokens) ??\n (o && o.prompt_tokens) ??\n (usage && usage.prompt_tokens) ??\n input.prompt_tokens ??\n 0\n );\n const completion_tokens = Number(\n (out && out.completion_tokens) ??\n (o && o.completion_tokens) ??\n (usage && usage.completion_tokens) ??\n 0\n );\n const total_tokens = Number(\n (out && out.total_tokens) ??\n (o && o.total_tokens) ??\n (usage && usage.total_tokens) ??\n (prompt_tokens + completion_tokens)\n );\n\n const error_code = status === 'ok' ? null : String((out && out.error_code) || (o && o.error_code) || (o && o.error && (o.error.code || o.error.type)) || status);\n const cost_usd = (out && typeof out.cost_usd === 'number') ? out.cost_usd : null;\n\n return { status, prompt_tokens, completion_tokens, total_tokens, error_code, cost_usd };\n}\n\n/**\n * guardedCall(cwd, input, fn)\n *\n * fn(req) can return either:\n * 1) Normalized shape:\n * { status: 'ok'|'error'|'timeout', prompt_tokens?, completion_tokens?, total_tokens?, cost_usd?, error_code? }\n * 2) Provider raw response (OpenAI-style), e.g.:\n * { status:'ok', response:{ usage:{prompt_tokens, completion_tokens, total_tokens} } }\n * { usage:{prompt_tokens, completion_tokens, total_tokens} }\n *\n * If token fields are missing, AIOpt will fall back to input.prompt_tokens and/or 0.\n */\nasync function guardedCall(cwd, input, fn){\n const cfg=loadConfig(cwd);\n const pol=loadPolicies(cwd,cfg);\n const rt=loadRates(cwd,cfg);\n\n const request_id=crypto.randomUUID();\n const trace_id=input.trace_id || request_id;\n const idem=input.idempotency_key || trace_id;\n if(IDEMPOTENCY.has(idem)) return IDEMPOTENCY.get(idem);\n\n const routed=pickRoutedModel(rt, input.provider, input.feature_tag, input.model);\n const maxOut=outputCap(pol.output, input.feature_tag, input.max_output_tokens);\n const usagePath=path.isAbsolute(cfg.usage_path)?cfg.usage_path:path.join(cwd,cfg.usage_path);\n\n const maxAttempts=Math.max(1, Number(pol.retry.max_attempts||1));\n const backoffs=pol.retry.backoff_ms || [200];\n const retryOn=new Set(pol.retry.retry_on_status || ['error','timeout']);\n\n let last={status:'error', completion_tokens:0, error_code:'unknown'};\n\n for(let attempt=1; attempt<=maxAttempts; attempt++){\n const t0=Date.now();\n const policy_hits=[];\n if(routed.hit) policy_hits.push(routed.hit);\n policy_hits.push('outputcap:'+maxOut);\n try{\n const out=await fn({ provider: input.provider, model: routed.model, endpoint: input.endpoint, max_output_tokens: maxOut, prompt_tokens: input.prompt_tokens, idempotency_key: idem });\n const latency_ms=Date.now()-t0;\n const norm=normalizeResult(out, input);\n const cost_usd=(typeof norm.cost_usd==='number') ? norm.cost_usd : costUsd(rt, input.provider, routed.model, norm.prompt_tokens, norm.completion_tokens);\n const feature_tag = input.feature_tag || input.endpoint || 'unknown';\n appendJsonl(usagePath, { ts:new Date().toISOString(), request_id, trace_id, attempt, status: norm.status, error_code: norm.error_code, provider: input.provider, model: routed.model, endpoint: input.endpoint, prompt_tokens: norm.prompt_tokens, completion_tokens: norm.completion_tokens, total_tokens: norm.total_tokens, cost_usd, latency_ms, meta:{ feature_tag, routed_from: routed.routed_from, policy_hits } });\n last={ status: norm.status, completion_tokens: norm.completion_tokens, error_code: norm.error_code };\n if(norm.status==='ok'){ IDEMPOTENCY.set(idem,out); return out; }\n if(retryOn.has(norm.status) && attempt<maxAttempts){ await sleep(Number(backoffs[Math.min(attempt-1, backoffs.length-1)]||200)); continue; }\n IDEMPOTENCY.set(idem,out); return out;\n }catch(e){\n const latency_ms=Date.now()-t0;\n const out={ status:'error', completion_tokens:0, error_code:String(e && (e.code||e.name) || 'exception') };\n const feature_tag = input.feature_tag || input.endpoint || 'unknown';\n appendJsonl(usagePath, { ts:new Date().toISOString(), request_id, trace_id, attempt, status: out.status, error_code: out.error_code, provider: input.provider, model: routed.model, endpoint: input.endpoint, prompt_tokens:Number(input.prompt_tokens||0), completion_tokens:0, total_tokens:Number(input.prompt_tokens||0), cost_usd:costUsd(rt, input.provider, routed.model, Number(input.prompt_tokens||0), 0), latency_ms, meta:{ feature_tag, routed_from: routed.routed_from, policy_hits:[routed.hit||'routing:none','outputcap:'+maxOut,'error:exception'] } });\n last=out;\n if(attempt<maxAttempts){ await sleep(Number(backoffs[Math.min(attempt-1, backoffs.length-1)]||200)); continue; }\n IDEMPOTENCY.set(idem,out); return out;\n }\n }\n IDEMPOTENCY.set(idem,last);\n return last;\n}\n\nmodule.exports = { guardedCall };\n`;\n;\n const w = writeFile(wrapperPath, wrapper, force);\n created.push({ path: 'aiopt/aiopt-wrapper.js', status: w.wrote ? 'created' : 'skipped' });\n\n // 5) usage.jsonl\n const usagePath = path.join(outDir, 'usage.jsonl');\n if (force || !fs.existsSync(usagePath)) {\n // default: empty file (avoid mixing demo data into real user logs)\n fs.writeFileSync(usagePath, '');\n created.push({ path: 'aiopt-output/usage.jsonl', status: 'created' });\n\n if (opts.seedSample) {\n const sample = {\n ts: new Date().toISOString(),\n request_id: 'sample',\n trace_id: 'sample',\n attempt: 1,\n status: 'ok',\n error_code: null,\n provider: 'openai',\n model: 'gpt-5-mini',\n endpoint: 'demo',\n prompt_tokens: 12,\n completion_tokens: 3,\n total_tokens: 15,\n cost_usd: 0.0,\n latency_ms: 1,\n meta: { feature_tag: 'demo', routed_from: null, policy_hits: ['install-sample'] }\n };\n fs.appendFileSync(usagePath, JSON.stringify(sample) + '\\n');\n }\n } else {\n created.push({ path: 'aiopt-output/usage.jsonl', status: 'skipped' });\n }\n\n return { created };\n}\n","import fs from 'fs';\nimport path from 'path';\n\nfunction canWrite(dir: string) {\n try {\n fs.mkdirSync(dir, { recursive: true });\n const p = path.join(dir, `.aiopt-write-test-${Date.now()}`);\n fs.writeFileSync(p, 'ok');\n fs.unlinkSync(p);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction tailLines(filePath: string, n: number): string[] {\n try {\n const raw = fs.readFileSync(filePath, 'utf8');\n const lines = raw.split(/\\r?\\n/).filter(l => l.trim().length > 0);\n return lines.slice(Math.max(0, lines.length - n));\n } catch {\n return [];\n }\n}\n\nexport type DoctorResult = {\n ok: boolean;\n checks: Array<{ name: string; ok: boolean; detail?: string }>;\n last5: Array<{ status?: string; provider?: string; model?: string; endpoint?: string; attempt?: number }>;\n};\n\nexport function runDoctor(cwd: string): DoctorResult {\n const aioptDir = path.join(cwd, 'aiopt');\n const policiesDir = path.join(aioptDir, 'policies');\n const outDir = path.join(cwd, 'aiopt-output');\n const usagePath = path.join(outDir, 'usage.jsonl');\n\n const checks: DoctorResult['checks'] = [];\n\n checks.push({ name: 'aiopt/ exists', ok: fs.existsSync(aioptDir) });\n checks.push({ name: 'aiopt/policies exists', ok: fs.existsSync(policiesDir) });\n checks.push({ name: 'aiopt-output/ writable', ok: canWrite(outDir) });\n checks.push({ name: 'usage.jsonl exists', ok: fs.existsSync(usagePath), detail: usagePath });\n\n const last5raw = tailLines(usagePath, 5);\n const last5 = last5raw.length === 0 ? [{ status: '(empty usage.jsonl)' }] as any : last5raw.map(l => {\n try {\n const j = JSON.parse(l);\n return {\n status: j.status,\n provider: j.provider,\n model: j.model,\n endpoint: j.endpoint,\n attempt: j.attempt,\n feature_tag: j?.meta?.feature_tag\n };\n } catch {\n return {};\n }\n });\n\n // Feature-tag quality check (sample last 50 lines)\n const last50 = tailLines(usagePath, 50);\n let missing = 0;\n let total50 = 0;\n for (const l of last50) {\n total50++;\n try {\n const j = JSON.parse(l);\n const ft = j?.meta?.feature_tag;\n if (!ft || String(ft).trim() === '') missing++;\n } catch {\n missing++;\n }\n }\n if (total50 > 0 && missing > 0) {\n checks.push({ name: 'feature_tag quality (last50)', ok: false, detail: `${missing}/${total50} missing meta.feature_tag` });\n } else {\n checks.push({ name: 'feature_tag quality (last50)', ok: true, detail: 'meta.feature_tag present' });\n }\n\n const ok = checks.every(c => c.ok);\n return { ok, checks, last5 };\n}\n\n","import crypto from 'crypto';\nimport fs from 'fs';\nimport path from 'path';\n\nexport type LicensePayload = {\n sub: string; // customer id/email/etc\n plan: 'pro' | 'team' | 'trial' | string;\n iat: number; // issued at (unix seconds)\n exp: number; // expires at (unix seconds)\n features?: Record<string, boolean>;\n};\n\nexport type LicenseFile = {\n key: string;\n payload: LicensePayload;\n verified: boolean;\n verified_at: string;\n};\n\n// NOTE: Public key only. No servers.\n// Replace this with your own RSA public key in PEM (SPKI) format.\nexport const DEFAULT_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz1LLE/pXIx5TloDa0LAf\njg9NSIW6STWhsAFP2ZzXgpWoQ3cCmW6xcB/4QNEmPpGlfMWhyRfkxsdKuhnjUMTg\n8MpMAcbjjF8JrGS9iLnW4yrLm7jzsOcndjkGO7pH+32GopZk98dVzmIRPok2Je76\n3MQRaxLi0jWytaCmacEB4R7HyuquOQlHPg0vD9NOEwrC/+br2GdQbD1lKPyLeLv3\nRidwAs8Iw2xx5g8G+BsVSM/HRC3jQT5GynfnuDsvMHCvGLRct/76ajiR71/NFZEP\nZ7liILNnZzCTlKGGZfZmG70t+zkg8HKdpRuWy8rZ0DPWyQg5MKm6TZOMV6dC0Rpg\nDwIDAQAB\n-----END PUBLIC KEY-----`;\n\nfunction b64urlDecodeToBuffer(s: string): Buffer {\n const padLen = (4 - (s.length % 4)) % 4;\n const padded = s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat(padLen);\n return Buffer.from(padded, 'base64');\n}\n\nfunction safeJsonParse(buf: Buffer): any {\n const txt = buf.toString('utf8');\n return JSON.parse(txt);\n}\n\nexport function parseLicenseKey(key: string): { payload: LicensePayload; signature: Buffer; payloadB64Url: string } {\n const parts = String(key).trim().split('.');\n if (parts.length !== 2) throw new Error('invalid license key format: expected <payloadB64Url>.<sigB64Url>');\n const [payloadB64Url, sigB64Url] = parts;\n const payloadBuf = b64urlDecodeToBuffer(payloadB64Url);\n const sigBuf = b64urlDecodeToBuffer(sigB64Url);\n const payload = safeJsonParse(payloadBuf) as LicensePayload;\n return { payload, signature: sigBuf, payloadB64Url };\n}\n\nexport function verifyLicenseKey(key: string, publicKeyPem: string): { ok: boolean; reason?: string; payload?: LicensePayload } {\n let parsed: { payload: LicensePayload; signature: Buffer; payloadB64Url: string };\n try {\n parsed = parseLicenseKey(key);\n } catch (e: any) {\n return { ok: false, reason: e?.message || 'parse error' };\n }\n\n const now = Math.floor(Date.now() / 1000);\n if (typeof parsed.payload?.exp !== 'number' || parsed.payload.exp < now) {\n return { ok: false, reason: 'expired', payload: parsed.payload };\n }\n\n try {\n const verifier = crypto.createVerify('RSA-SHA256');\n verifier.update(parsed.payloadB64Url);\n verifier.end();\n const ok = verifier.verify(publicKeyPem, parsed.signature);\n if (!ok) return { ok: false, reason: 'bad signature', payload: parsed.payload };\n return { ok: true, payload: parsed.payload };\n } catch (e: any) {\n return { ok: false, reason: e?.message || 'verify error', payload: parsed.payload };\n }\n}\n\nexport function defaultLicensePath(cwd: string): string {\n return path.join(cwd, 'aiopt', 'license.json');\n}\n\nexport function writeLicenseFile(p: string, key: string, payload: LicensePayload, verified: boolean) {\n const out: LicenseFile = {\n key,\n payload,\n verified,\n verified_at: new Date().toISOString()\n };\n fs.mkdirSync(path.dirname(p), { recursive: true });\n fs.writeFileSync(p, JSON.stringify(out, null, 2));\n}\n\nexport function readLicenseFile(p: string): LicenseFile {\n return JSON.parse(fs.readFileSync(p, 'utf8')) as LicenseFile;\n}\n","import { RateTable, UsageEvent } from './types';\nimport { analyze } from './scan';\nimport { costOfEvent } from './cost';\n\nexport type GuardInput = {\n baselineEvents: UsageEvent[];\n candidate: {\n provider?: string;\n model?: string;\n contextMultiplier?: number; // multiplies input_tokens\n outputMultiplier?: number; // multiplies output_tokens\n retriesDelta?: number; // adds to retries\n callMultiplier?: number; // multiplies call volume\n };\n};\n\nexport type GuardResult = {\n exitCode: 0 | 2 | 3;\n message: string;\n};\n\nfunction accidentRiskFromMonthly(monthly: number): 'Low' | 'Medium' | 'High' {\n if (!Number.isFinite(monthly)) return 'Medium';\n if (monthly >= 100) return 'High';\n if (monthly >= 10) return 'Medium';\n return 'Low';\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nfunction monthEstimate(delta: number, events: UsageEvent[]) {\n // Convert observed delta (over the sample window) into a monthly estimate.\n // Prefer timestamp-derived window length for determinism; fall back to 1 day.\n let days = 1;\n try {\n const times = events\n .map(e => Date.parse(e.ts))\n .filter(t => Number.isFinite(t))\n .sort((a, b) => a - b);\n if (times.length >= 2) {\n const spanMs = Math.max(0, times[times.length - 1] - times[0]);\n const spanDays = spanMs / (1000 * 60 * 60 * 24);\n // If logs cover <1 day (or identical timestamps), treat as 1 day.\n days = Math.max(1, spanDays);\n }\n } catch {\n days = 1;\n }\n return (delta * 30) / days;\n}\n\nfunction applyCandidate(events: UsageEvent[], cand: GuardInput['candidate']): UsageEvent[] {\n const ctxM = cand.contextMultiplier ?? 1;\n const outM = cand.outputMultiplier ?? 1;\n const rDelta = cand.retriesDelta ?? 0;\n\n return events.map(ev => ({\n ...ev,\n provider: cand.provider ? String(cand.provider).toLowerCase() : ev.provider,\n model: cand.model ? String(cand.model) : ev.model,\n input_tokens: Math.max(0, Math.round((ev.input_tokens || 0) * ctxM)),\n output_tokens: Math.max(0, Math.round((ev.output_tokens || 0) * outM)),\n retries: Math.max(0, Math.round((ev.retries || 0) + rDelta)),\n // clear billed_cost so pricing recalculates for new model/provider\n billed_cost: undefined\n }));\n}\n\nfunction confidenceFromChange(cand: GuardInput['candidate']): { level: 'High'|'Medium'|'Low'; reasons: string[] } {\n const reasons: string[] = [];\n\n if (cand.retriesDelta && cand.retriesDelta !== 0) reasons.push('retries change');\n if (cand.model) reasons.push('model change');\n if (cand.provider) reasons.push('provider change');\n if (cand.contextMultiplier && cand.contextMultiplier !== 1) reasons.push('context length change');\n if (cand.outputMultiplier && cand.outputMultiplier !== 1) reasons.push('output length change');\n\n // Spec rule:\n // High: retries/failed/dup calls (we only model retries here)\n // Medium: model/provider change\n // Low: context trimming / prompt structure\n if (cand.retriesDelta && cand.retriesDelta !== 0) return { level: 'High', reasons };\n if (cand.model || cand.provider) return { level: 'Medium', reasons };\n if ((cand.contextMultiplier && cand.contextMultiplier !== 1) || (cand.outputMultiplier && cand.outputMultiplier !== 1)) {\n return { level: 'Low', reasons };\n }\n return { level: 'Medium', reasons: reasons.length ? reasons : ['unknown change'] };\n}\n\nfunction assessDataQuality(baselineEvents: UsageEvent[], base: ReturnType<typeof analyze>) {\n const reasons: string[] = [];\n\n // ts span\n const times = baselineEvents\n .map(e => Date.parse(e.ts))\n .filter(t => Number.isFinite(t))\n .sort((a, b) => a - b);\n if (times.length < 2) reasons.push('ts span unknown');\n else {\n const spanDays = Math.max(0, (times[times.length - 1] - times[0]) / (1000 * 60 * 60 * 24));\n if (spanDays < 0.25) reasons.push('ts span too short');\n }\n\n // feature tag coverage\n const missingFt = baselineEvents.filter(e => !e.feature_tag && !(e.meta && (e.meta as any).feature_tag)).length;\n if (missingFt / Math.max(1, baselineEvents.length) > 0.2) reasons.push('feature_tag missing >20%');\n\n // unknown model/provider\n const unknown = (base.analysis.unknown_models || []).length;\n if (unknown / Math.max(1, baselineEvents.length) > 0.2) reasons.push('unknown model/provider >20%');\n\n // Map to a confidence penalty.\n let penalty: 'none'|'low'|'medium' = 'none';\n if (reasons.length >= 2) penalty = 'medium';\n else if (reasons.length === 1) penalty = 'low';\n\n return { reasons, penalty };\n}\n\nfunction degrade(level: 'High'|'Medium'|'Low', penalty: 'none'|'low'|'medium'): 'High'|'Medium'|'Low' {\n if (penalty === 'none') return level;\n if (penalty === 'low') return level === 'High' ? 'Medium' : 'Low';\n // medium\n return 'Low';\n}\n\nexport function runGuard(rt: RateTable, input: GuardInput): GuardResult {\n if (!input.baselineEvents || input.baselineEvents.length === 0) {\n const conf = { level: 'Low' as const, reasons: ['baseline empty'] };\n const msg = [\n 'FAIL: baseline usage is empty (need aiopt-output/usage.jsonl)',\n 'Impact (monthly est): +$0 (insufficient baseline)',\n `Accident risk: ${accidentRiskFromMonthly(100)}`,\n `Confidence: ${conf.level} (${(conf.reasons.length ? conf.reasons.join(', ') : 'baseline empty')})`,\n 'Recommendation: run the wrapper to collect baseline usage before using guard.'\n ].join('\\n');\n return { exitCode: 3, message: msg };\n }\n\n // For fair comparison, ignore billed_cost/cost_usd and recompute both sides from rate table.\n const baselineEvents = input.baselineEvents.map(e => ({ ...e, billed_cost: undefined }));\n const base = analyze(rt, baselineEvents);\n const candidateEvents = applyCandidate(baselineEvents, input.candidate);\n const cand = analyze(rt, candidateEvents);\n\n const baseCost = base.analysis.total_cost;\n let candCost = cand.analysis.total_cost;\n\n // call volume multiplier (traffic spike)\n const callMult = input.candidate.callMultiplier && input.candidate.callMultiplier > 0 ? input.candidate.callMultiplier : 1;\n if (callMult !== 1) {\n candCost = candCost * callMult;\n }\n\n // attempt-log baseline: retriesDelta should be interpreted as \"extra attempts\".\n const attemptLog = baselineEvents.some(e => (e.trace_id && String(e.trace_id).length > 0) || (e.attempt !== undefined && Number(e.attempt) > 0));\n if (attemptLog && input.candidate.retriesDelta && input.candidate.retriesDelta > 0) {\n // More accurate deterministic approximation:\n // - If baseline already has retry attempts (attempt>=2), use their average cost as retry unit.\n // - Else fall back to avg baseline attempt cost.\n let retryUnit = 0;\n let retryCount = 0;\n for (const ev of baselineEvents) {\n const attempt = Number(ev.attempt || 1);\n if (attempt >= 2) {\n retryUnit += costOfEvent(rt, ev).cost;\n retryCount += 1;\n }\n }\n if (retryCount > 0) {\n retryUnit = retryUnit / retryCount;\n } else {\n retryUnit = baseCost / Math.max(1, baselineEvents.length);\n }\n candCost += retryUnit * input.candidate.retriesDelta;\n }\n\n const delta = candCost - baseCost;\n\n const changeConf = confidenceFromChange(input.candidate);\n const dq = assessDataQuality(baselineEvents, base);\n const conf = { level: degrade(changeConf.level, dq.penalty), reasons: [...changeConf.reasons, ...dq.reasons.map(r => `data: ${r}`)] };\n\n // Guard logic:\n // - Warning if delta > 0 and monthEstimate(delta) >= $10\n // - Fail if delta > 0 and monthEstimate(delta) >= $100 (merge-blocking)\n const monthly = monthEstimate(Math.max(0, delta), baselineEvents);\n const monthlyRounded = round2(monthly);\n\n let exitCode: 0 | 2 | 3 = 0;\n let headline = 'OK: no cost accident risk detected';\n\n if (monthly >= 100) { exitCode = 3; headline = 'FAIL: high risk of LLM cost accident'; }\n else if (monthly >= 10) { exitCode = 2; headline = 'WARN: possible LLM cost accident'; }\n\n const reasons = conf.reasons.length ? conf.reasons.join(', ') : 'n/a';\n\n const msg = [\n headline,\n `Summary: baseline=$${round2(baseCost)} → candidate=$${round2(candCost)} (Δ=$${round2(delta)})`,\n callMult !== 1 ? `Call volume multiplier: x${callMult}` : null,\n `Impact (monthly est): +$${monthlyRounded}`,\n `Accident risk: ${accidentRiskFromMonthly(monthly)}`,\n `Confidence: ${conf.level} (${reasons})`,\n 'Recommendation: review model/provider/retry/context changes before deploy.'\n ].filter(Boolean).join('\\n');\n\n return { exitCode, message: msg };\n}\n","import http from 'http';\nimport fs from 'fs';\nimport path from 'path';\n\nexport async function startDashboard(cwd: string, opts: { port: number }) {\n const host = '127.0.0.1';\n const port = opts.port || 3010;\n\n const outDir = path.join(cwd, 'aiopt-output');\n const file = (name: string) => path.join(outDir, name);\n\n function readOrNull(p: string) {\n try {\n if (!fs.existsSync(p)) return null;\n return fs.readFileSync(p, 'utf8');\n } catch {\n return null;\n }\n }\n\n const indexHtml = `<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n <title>AIOpt Local Dashboard</title>\n <style>\n :root{\n --bg:#070b16; --card:#0b1222; --card2:#0e1730; --txt:#e7eaf2; --muted:#a3acc2;\n --border:rgba(255,255,255,.10);\n --ok:#34d399; --warn:#fbbf24; --fail:#fb7185;\n --shadow:0 18px 70px rgba(0,0,0,.35);\n }\n *{box-sizing:border-box}\n body{margin:0; font-family: ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto; background:var(--bg); color:var(--txt)}\n a{color:#bfffe3; text-decoration:none} a:hover{text-decoration:underline}\n .wrap{max-width:1100px; margin:0 auto; padding:18px}\n .top{display:flex; align-items:center; justify-content:space-between; gap:10px; flex-wrap:wrap}\n .h1{font-size:22px; font-weight:950; letter-spacing:-.02em}\n .pill{display:inline-flex; align-items:center; gap:8px; padding:7px 10px; border-radius:999px; border:1px solid var(--border);\n background:rgba(255,255,255,.03); color:var(--muted); font-size:12px; line-height:1}\n .dot{width:8px;height:8px;border-radius:99px;background:linear-gradient(90deg,#60a5fa,#34d399)}\n\n .grid{display:grid; grid-template-columns:repeat(12,1fr); gap:12px; margin-top:12px}\n .card{grid-column: span 12; background:linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,0)), var(--card);\n border:1px solid var(--border); border-radius:18px; padding:14px; box-shadow: var(--shadow)}\n @media(min-width: 920px){\n .c6{grid-column: span 6}\n .c4{grid-column: span 4}\n .c8{grid-column: span 8}\n }\n\n .k{padding:2px 6px; border-radius:8px; border:1px solid var(--border); font-family: ui-monospace; font-size:12px; color:#d7def2; background:rgba(255,255,255,.03)}\n .muted{color:var(--muted)}\n .row{display:flex; gap:10px; flex-wrap:wrap; align-items:center}\n\n .badge{display:inline-flex; align-items:center; gap:8px; padding:8px 10px; border-radius:14px; border:1px solid var(--border);\n background:rgba(255,255,255,.03); font-weight:900}\n .badge.ok{border-color:rgba(52,211,153,.35)}\n .badge.warn{border-color:rgba(251,191,36,.35)}\n .badge.fail{border-color:rgba(251,113,133,.35)}\n .badge .b{width:10px;height:10px;border-radius:99px}\n .badge.ok .b{background:var(--ok)}\n .badge.warn .b{background:var(--warn)}\n .badge.fail .b{background:var(--fail)}\n\n pre{margin:0; white-space:pre-wrap; word-break:break-word; background: rgba(3,6,14,.65);\n border:1px solid rgba(255,255,255,.12);\n padding:12px; border-radius:14px;\n font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\"; font-size:13px; line-height:1.55}\n\n .bars{display:flex; flex-direction:column; gap:8px; margin-top:8px}\n .bar{display:grid; grid-template-columns: 1fr 64px; gap:10px; align-items:center}\n .track{height:10px; border-radius:999px; background:rgba(255,255,255,.08); overflow:hidden; border:1px solid rgba(255,255,255,.10)}\n .fill{height:100%; border-radius:999px; background:linear-gradient(90deg,#60a5fa,#34d399)}\n .label{font-size:13px}\n .val{font-family: ui-monospace; font-size:12px; color:#d7def2; text-align:right}\n\n .mini{font-size:12px; color:var(--muted)}\n </style>\n</head>\n<body>\n <div class=\"wrap\">\n <div class=\"top\">\n <div class=\"h1\">AIOpt Local Dashboard</div>\n <div class=\"pill\"><span class=\"dot\"></span> local-only · reads <span class=\"k\">./aiopt-output</span></div>\n </div>\n\n <div class=\"grid\">\n <div class=\"card c6\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Guard verdict</div>\n <div class=\"mini\" id=\"guardMeta\">—</div>\n </div>\n <div style=\"height:8px\"></div>\n <div id=\"guardBadge\" class=\"badge\"><span class=\"b\"></span><span id=\"guardBadgeText\">loading…</span></div>\n <div style=\"height:10px\"></div>\n <pre id=\"guard\">loading…</pre>\n <div style=\"height:10px\"></div>\n <div class=\"row\">\n <a href=\"/api/guard-last.txt\" target=\"_blank\">raw txt</a>\n <span class=\"muted\">·</span>\n <a href=\"/api/guard-last.json\" target=\"_blank\">raw json</a>\n </div>\n </div>\n\n <div class=\"card c6\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Scan summary</div>\n <div class=\"mini\"><a href=\"/api/report.json\" target=\"_blank\">report.json</a> · <a href=\"/api/report.md\" target=\"_blank\">report.md</a></div>\n </div>\n <div style=\"height:8px\"></div>\n <div class=\"row\">\n <div class=\"badge\" id=\"totalCostBadge\"><span class=\"b\" style=\"background:#60a5fa\"></span><span id=\"totalCost\">total: —</span></div>\n <div class=\"badge\" id=\"savingsBadge\"><span class=\"b\" style=\"background:#a78bfa\"></span><span id=\"savings\">savings: —</span></div>\n </div>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Cost by model</div>\n <div id=\"byModel\" class=\"bars\"></div>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Cost by feature</div>\n <div id=\"byFeature\" class=\"bars\"></div>\n\n <div style=\"height:10px\"></div>\n <div class=\"mini\" id=\"scanMeta\">—</div>\n </div>\n\n <div class=\"card\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Latest report.md</div>\n <div class=\"mini\">Tip: run <span class=\"k\">aiopt scan</span> after collecting baseline</div>\n </div>\n <div style=\"height:8px\"></div>\n <pre id=\"scan\">loading…</pre>\n </div>\n </div>\n </div>\n\n<script>\nfunction money(x){\n if (x === null || x === undefined || Number.isNaN(Number(x))) return '—';\n return '$' + (Math.round(Number(x)*100)/100);\n}\nfunction renderBars(el, items){\n el.innerHTML='';\n if(!items || items.length===0){ el.innerHTML='<div class=\"mini\">(no data)</div>'; return; }\n const max = Math.max(...items.map(i=>Number(i.cost)||0), 0.000001);\n for(const it of items.slice(0,8)){\n const w = Math.max(2, Math.round(((Number(it.cost)||0)/max)*100));\n const row = document.createElement('div');\n row.className='bar';\n row.innerHTML =\n '<div>'+\n '<div class=\"label\">'+ String(it.key) +'</div>'+\n '<div class=\"track\"><div class=\"fill\" style=\"width:'+ w +'%\"></div></div>'+\n '</div>'+\n '<div class=\"val\">'+ money(it.cost) +'</div>';\n el.appendChild(row);\n }\n}\n\nasync function load(){\n const guardTxt = await fetch('/api/guard-last.txt').then(r=>r.ok?r.text():null);\n const guardMeta = await fetch('/api/guard-last.json').then(r=>r.ok?r.json():null);\n\n document.getElementById('guard').textContent = guardTxt || '(no guard-last.txt yet — run: aiopt guard)';\n if(guardMeta){\n document.getElementById('guardMeta').textContent = 'exit=' + guardMeta.exitCode + ' · ' + guardMeta.ts;\n const badge = document.getElementById('guardBadge');\n const t = document.getElementById('guardBadgeText');\n const code = Number(guardMeta.exitCode);\n badge.classList.remove('ok','warn','fail');\n if(code===0){badge.classList.add('ok'); t.textContent='OK (0)';}\n else if(code===2){badge.classList.add('warn'); t.textContent='WARN (2)';}\n else {badge.classList.add('fail'); t.textContent='FAIL (3)';}\n }\n\n const reportJson = await fetch('/api/report.json').then(r=>r.ok?r.json():null);\n if(reportJson){\n const total = reportJson.summary && reportJson.summary.total_cost_usd;\n const sav = reportJson.summary && reportJson.summary.estimated_savings_usd;\n document.getElementById('totalCost').textContent = 'total: ' + money(total);\n document.getElementById('savings').textContent = 'savings: ' + money(sav);\n renderBars(document.getElementById('byModel'), reportJson.top && reportJson.top.by_model);\n renderBars(document.getElementById('byFeature'), reportJson.top && reportJson.top.by_feature);\n document.getElementById('scanMeta').textContent = 'confidence=' + (reportJson.confidence || '—') + ' · generated_at=' + (reportJson.generated_at || '—');\n } else {\n document.getElementById('scanMeta').textContent = '(no report.json yet — run: aiopt scan)';\n }\n\n const reportMd = await fetch('/api/report.md').then(r=>r.ok?r.text():null);\n document.getElementById('scan').textContent = reportMd || '(no report.md yet — run: aiopt scan)';\n}\nload();\n</script>\n</body>\n</html>`;\n\n const server = http.createServer((req, res) => {\n const url = req.url || '/';\n if (url === '/' || url === '/index.html') {\n res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });\n res.end(indexHtml);\n return;\n }\n\n if (url.startsWith('/api/')) {\n const name = url.replace('/api/', '');\n const allow = new Set(['guard-last.txt', 'guard-last.json', 'report.md', 'report.json']);\n if (!allow.has(name)) {\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('not found');\n return;\n }\n\n const p = file(name);\n const txt = readOrNull(p);\n if (txt === null) {\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('missing');\n return;\n }\n\n const ct = name.endsWith('.json') ? 'application/json; charset=utf-8' : 'text/plain; charset=utf-8';\n res.writeHead(200, { 'content-type': ct });\n res.end(txt);\n return;\n }\n\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('not found');\n });\n\n await new Promise<void>((resolve, reject) => {\n server.once('error', reject);\n server.listen(port, host, () => resolve());\n });\n\n console.log(`OK: dashboard http://${host}:${port}/`);\n console.log('Tip: run `aiopt guard ...` and `aiopt scan` to populate aiopt-output files.');\n\n // keep alive\n await new Promise(() => {});\n}\n","#!/usr/bin/env node\n\nimport fs from 'fs';\nimport path from 'path';\nimport { Command } from 'commander';\nimport { ensureDir, isCsvPath, readCsv, readJsonl } from './io';\nimport { RateTable } from './types';\nimport { analyze, writeOutputs } from './scan';\n\nconst program = new Command();\n\nconst DEFAULT_INPUT = './aiopt-output/usage.jsonl';\nconst DEFAULT_OUTPUT_DIR = './aiopt-output';\n\nfunction loadRateTable(): RateTable {\n const p = path.join(__dirname, '..', 'rates', 'rate_table.json');\n return JSON.parse(fs.readFileSync(p, 'utf8'));\n}\n\nprogram\n .name('aiopt')\n .description('AI 비용 자동 절감 인프라 — 서버 없는 로컬 CLI MVP')\n // keep CLI version in sync with package.json\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n .version(require('../package.json').version);\n\nprogram\n .command('init')\n .description('aiopt-input/ 및 샘플 usage.jsonl, aiopt-output/ 생성')\n .action(() => {\n ensureDir('./aiopt-input');\n ensureDir('./aiopt-output');\n\n const sampleSrc = path.join(__dirname, '..', 'samples', 'sample_usage.jsonl');\n const dst = path.join('./aiopt-input', 'usage.jsonl');\n\n if (!fs.existsSync(dst)) {\n fs.copyFileSync(sampleSrc, dst);\n console.log('Created ./aiopt-input/usage.jsonl (sample)');\n } else {\n console.log('Exists ./aiopt-input/usage.jsonl (skip)');\n }\n\n console.log('Ready: ./aiopt-output/');\n });\n\nprogram\n .command('scan')\n .description('입력 로그(JSONL/CSV)를 분석하고 report.md/report.json + patches까지 생성')\n .option('--input <path>', 'input file path (default: ./aiopt-output/usage.jsonl)', DEFAULT_INPUT)\n .option('--out <dir>', 'output dir (default: ./aiopt-output)', DEFAULT_OUTPUT_DIR)\n .action(async (opts) => {\n const inputPath = String(opts.input);\n const outDir = String(opts.out);\n\n if (!fs.existsSync(inputPath)) {\n console.error(`Input not found: ${inputPath}`);\n process.exit(1);\n }\n\n const rt = loadRateTable();\n const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);\n\n const { analysis, savings, policy, meta } = analyze(rt, events);\n // For reproducibility: embed input path & rate table meta\n policy.generated_from.input = inputPath;\n\n writeOutputs(outDir, analysis, savings, policy, meta);\n\n // Console: Top Fix 3 (data-driven)\n const { buildTopFixes } = await import('./solutions');\n const fixes = buildTopFixes(analysis, savings).slice(0, 3);\n\n console.log('Top Fix 3:');\n fixes.forEach((f, i) => {\n const tag = f.status === 'no-issue' ? '(no issue detected)' : `($${Math.round(f.impact_usd * 100) / 100})`;\n console.log(`${i + 1}) ${f.title} ${tag}`);\n });\n console.log(`Report: ${path.join(outDir, 'report.md')}`);\n });\n\nprogram\n .command('policy')\n .description('마지막 scan 결과 기반으로 cost-policy.json만 재생성 (MVP: scan과 동일 로직)')\n .option('--input <path>', 'input file path (default: ./aiopt-input/usage.jsonl)', DEFAULT_INPUT)\n .option('--out <dir>', 'output dir (default: ./aiopt-output)', DEFAULT_OUTPUT_DIR)\n .action((opts) => {\n const inputPath = String(opts.input);\n const outDir = String(opts.out);\n const rt = loadRateTable();\n const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);\n const { policy } = analyze(rt, events);\n policy.generated_from.input = inputPath;\n\n ensureDir(outDir);\n fs.writeFileSync(path.join(outDir, 'cost-policy.json'), JSON.stringify(policy, null, 2));\n console.log(`OK: ${outDir}/cost-policy.json`);\n });\n\n// v0.2: install/doctor (no servers)\nprogram\n .command('install')\n .description('Install AIOpt guardrails: create aiopt/ + policies + usage.jsonl')\n .option('--force', 'overwrite existing files')\n .option('--seed-sample', 'seed 1 sample line into aiopt-output/usage.jsonl')\n .action(async (opts) => {\n const { runInstall } = await import('./install');\n const result = runInstall(process.cwd(), { force: Boolean(opts.force), seedSample: Boolean(opts.seedSample) });\n for (const c of result.created) {\n console.log(`${c.status === 'created' ? 'CREATED' : 'SKIP'}: ${c.path}`);\n }\n });\n\nprogram\n .command('doctor')\n .description('Check installation + print last 5 usage events')\n .action(async () => {\n const { runDoctor } = await import('./doctor');\n const r = runDoctor(process.cwd());\n console.log(r.ok ? 'OK: doctor' : 'WARN: doctor');\n for (const c of r.checks) {\n console.log(`${c.ok ? 'OK' : 'FAIL'}: ${c.name}${c.detail ? ` (${c.detail})` : ''}`);\n }\n console.log('--- last5 usage');\n for (const x of r.last5) {\n console.log(JSON.stringify(x));\n }\n });\n\n// Offline license (no servers)\nconst licenseCmd = program\n .command('license')\n .description('Offline license activate/verify (public key only; no server calls)');\n\nlicenseCmd\n .command('activate')\n .argument('<KEY>', 'license key (<payloadB64Url>.<sigB64Url>)')\n .option('--out <path>', 'output license.json path (default: ./aiopt/license.json)')\n .action(async (key, opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, verifyLicenseKey, writeLicenseFile } = await import('./license');\n const outPath = opts.out ? String(opts.out) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n const v = verifyLicenseKey(String(key), pub);\n if (!v.payload) {\n console.error(`FAIL: ${v.reason || 'invalid license'}`);\n process.exit(3);\n }\n writeLicenseFile(outPath, String(key), v.payload, v.ok);\n console.log(v.ok ? `OK: activated (${outPath})` : `WARN: saved but not verified (${v.reason}) (${outPath})`);\n process.exit(v.ok ? 0 : 2);\n });\n\nlicenseCmd\n .command('verify')\n .option('--path <path>', 'license.json path (default: ./aiopt/license.json)')\n .action(async (opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, readLicenseFile, verifyLicenseKey } = await import('./license');\n const p = opts.path ? String(opts.path) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n if (!fs.existsSync(p)) {\n console.error(`FAIL: license file not found: ${p}`);\n process.exit(3);\n }\n const f = readLicenseFile(p);\n const v = verifyLicenseKey(f.key, pub);\n if (v.ok) {\n console.log('OK: license verified');\n process.exit(0);\n }\n console.error(`FAIL: license invalid (${v.reason || 'unknown'})`);\n process.exit(3);\n });\n\nlicenseCmd\n .command('status')\n .option('--path <path>', 'license.json path (default: ./aiopt/license.json)')\n .action(async (opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, readLicenseFile, verifyLicenseKey } = await import('./license');\n const p = opts.path ? String(opts.path) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n if (!fs.existsSync(p)) {\n console.log('NO_LICENSE');\n process.exit(2);\n }\n const f = readLicenseFile(p);\n const v = verifyLicenseKey(f.key, pub);\n if (v.ok) {\n console.log(`OK: ${f.payload.plan} exp=${f.payload.exp}`);\n process.exit(0);\n }\n console.log(`INVALID: ${v.reason || 'unknown'}`);\n process.exit(3);\n });\n\n// vNext: guardrail mode (pre-deploy warning)\nprogram\n .command('guard')\n .description('Pre-deploy guardrail: compare baseline usage vs candidate change and print warnings (exit codes 0/2/3)')\n .option('--input <path>', 'baseline usage jsonl/csv (default: ./aiopt-output/usage.jsonl)', DEFAULT_INPUT)\n .option('--provider <provider>', 'candidate provider override')\n .option('--model <model>', 'candidate model override')\n .option('--context-mult <n>', 'multiply input_tokens by n', (v) => Number(v))\n .option('--output-mult <n>', 'multiply output_tokens by n', (v) => Number(v))\n .option('--retries-delta <n>', 'add n to retries', (v) => Number(v))\n .option('--call-mult <n>', 'multiply call volume by n (traffic spike)', (v) => Number(v))\n .action(async (opts) => {\n const rt = loadRateTable();\n const inputPath = String(opts.input);\n if (!fs.existsSync(inputPath)) {\n console.error(`FAIL: baseline not found: ${inputPath}`);\n process.exit(3);\n }\n const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);\n\n const { runGuard } = await import('./guard');\n const r = runGuard(rt, {\n baselineEvents: events,\n candidate: {\n provider: opts.provider,\n model: opts.model,\n contextMultiplier: opts.contextMult,\n outputMultiplier: opts.outputMult,\n retriesDelta: opts.retriesDelta,\n callMultiplier: opts.callMult\n }\n });\n\n console.log(r.message);\n\n // Persist last guard output for local dashboard / CI attachments\n try {\n const outDir = path.resolve(DEFAULT_OUTPUT_DIR);\n fs.mkdirSync(outDir, { recursive: true });\n fs.writeFileSync(path.join(outDir, 'guard-last.txt'), r.message);\n fs.writeFileSync(path.join(outDir, 'guard-last.json'), JSON.stringify({ ts: new Date().toISOString(), exitCode: r.exitCode }, null, 2));\n } catch {\n // ignore\n }\n\n process.exit(r.exitCode);\n });\n\n// Local-only dashboard (no auth; binds to 127.0.0.1)\nprogram\n .command('dashboard')\n .description('Local dashboard (localhost only): view last guard + last scan outputs')\n .option('--port <n>', 'port (default: 3010)', (v) => Number(v), 3010)\n .action(async (opts) => {\n const { startDashboard } = await import('./dashboard');\n await startDashboard(process.cwd(), { port: Number(opts.port || 3010) });\n });\n\nprogram.parse(process.argv);\n","import fs from 'fs';\nimport path from 'path';\nimport { parse as parseCsv } from 'csv-parse/sync';\nimport { UsageEvent } from './types';\n\nexport function ensureDir(p: string) {\n fs.mkdirSync(p, { recursive: true });\n}\n\nexport function readJsonl(filePath: string): UsageEvent[] {\n const raw = fs.readFileSync(filePath, 'utf8');\n const lines = raw.split(/\\r?\\n/).filter(l => l.trim().length > 0);\n const out: UsageEvent[] = [];\n for (const line of lines) {\n const obj = JSON.parse(line);\n out.push(normalizeEvent(obj));\n }\n return out;\n}\n\nexport function readCsv(filePath: string): UsageEvent[] {\n const raw = fs.readFileSync(filePath, 'utf8');\n const records = parseCsv(raw, { columns: true, skip_empty_lines: true, trim: true });\n return records.map((r: any) => normalizeEvent(r));\n}\n\nfunction toNum(x: any, def = 0): number {\n const n = Number(x);\n return Number.isFinite(n) ? n : def;\n}\n\nfunction normalizeEvent(x: any): UsageEvent {\n // Supports two schemas:\n // 1) scan input schema: input_tokens/output_tokens/feature_tag/retries\n // 2) wrapper usage schema: prompt_tokens/completion_tokens/endpoint/attempt/trace_id/cost_usd\n\n const inputTokens = x.input_tokens ?? x.prompt_tokens;\n const outputTokens = x.output_tokens ?? x.completion_tokens;\n\n // feature_tag fallback: feature_tag -> meta.feature_tag -> endpoint\n const featureTag = x.feature_tag ?? x?.meta?.feature_tag ?? x.endpoint ?? '';\n\n // retries fallback: retries -> max(attempt-1,0)\n const retries = x.retries ?? (x.attempt !== undefined ? Math.max(0, toNum(x.attempt) - 1) : 0);\n\n // billed_cost fallback: billed_cost -> cost_usd\n const billed = x.billed_cost ?? x.cost_usd;\n\n return {\n ts: String(x.ts ?? ''),\n provider: String(x.provider ?? '').toLowerCase(),\n model: String(x.model ?? ''),\n input_tokens: toNum(inputTokens),\n output_tokens: toNum(outputTokens),\n feature_tag: String(featureTag ?? ''),\n retries: toNum(retries),\n status: String(x.status ?? ''),\n billed_cost: billed === undefined || billed === '' ? undefined : toNum(billed),\n\n trace_id: x.trace_id ? String(x.trace_id) : undefined,\n request_id: x.request_id ? String(x.request_id) : undefined,\n attempt: x.attempt === undefined ? undefined : toNum(x.attempt),\n endpoint: x.endpoint ? String(x.endpoint) : undefined\n };\n}\n\nexport function isCsvPath(p: string) {\n return path.extname(p).toLowerCase() === '.csv';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaO,SAAS,SAAS,IAAe,UAAkB,OAAe;AACvE,QAAM,OAAO,OAAO,YAAY,EAAE,EAAE,YAAY;AAGhD,MAAI,SAAS,WAAW,SAAS,YAAY,SAAS,QAAQ;AAC5D,WAAO,EAAE,MAAM,YAAqB,OAAO,GAAG,QAAQ,EAAE;AAAA,EAC1D;AAEA,QAAM,IAAI,GAAG,UAAU,IAAI;AAC3B,MAAI,CAAC,EAAG,QAAO;AACf,QAAM,IAAI,EAAE,OAAO,KAAK;AACxB,MAAI,EAAG,QAAO,EAAE,MAAM,YAAqB,OAAO,EAAE,OAAO,QAAQ,EAAE,OAAO;AAC5E,SAAO,EAAE,MAAM,aAAsB,OAAO,EAAE,kBAAkB,OAAO,QAAQ,EAAE,kBAAkB,OAAO;AAC5G;AAEO,SAAS,YAAY,IAAe,IAA4B;AACrE,MAAI,OAAO,GAAG,gBAAgB,YAAY,OAAO,SAAS,GAAG,WAAW,GAAG;AACzE,WAAO;AAAA,MACL,MAAM,GAAG;AAAA,MACT,WAAW;AAAA,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,IAAI;","names":["cost","path","fs","import_fs","import_path","round2","fs","path","import_fs","import_path","exports","module","ensureDir","fs","path","import_fs","import_path","fs","path","import_fs","import_path","crypto","path","fs","import_fs","import_path","round2","conf","msg","path","fs","http","import_fs","import_path","import_fs","import_path","fs","parseCsv","path","path","fs","buildTopFixes","runInstall","runDoctor","DEFAULT_PUBLIC_KEY_PEM","defaultLicensePath","verifyLicenseKey","writeLicenseFile","readLicenseFile","runGuard","startDashboard"]}
1
+ {"version":3,"sources":["../src/io.ts","../src/cost.ts","../src/solutions.ts","../src/scan.ts","../package.json","../src/install.ts","../src/doctor.ts","../src/license.ts","../src/guard.ts","../src/dashboard.ts","../src/rates-util.ts","../src/quickstart.ts","../src/cli.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport { parse as parseCsv } from 'csv-parse/sync';\nimport { UsageEvent } from './types';\n\nexport function ensureDir(p: string) {\n fs.mkdirSync(p, { recursive: true });\n}\n\nexport function readJsonl(filePath: string): UsageEvent[] {\n const raw = fs.readFileSync(filePath, 'utf8');\n const lines = raw.split(/\\r?\\n/).filter(l => l.trim().length > 0);\n const out: UsageEvent[] = [];\n for (const line of lines) {\n const obj = JSON.parse(line);\n out.push(normalizeEvent(obj));\n }\n return out;\n}\n\nexport function readCsv(filePath: string): UsageEvent[] {\n const raw = fs.readFileSync(filePath, 'utf8');\n const records = parseCsv(raw, { columns: true, skip_empty_lines: true, trim: true });\n return records.map((r: any) => normalizeEvent(r));\n}\n\nfunction toNum(x: any, def = 0): number {\n const n = Number(x);\n return Number.isFinite(n) ? n : def;\n}\n\nfunction normalizeEvent(x: any): UsageEvent {\n // Supports two schemas:\n // 1) scan input schema: input_tokens/output_tokens/feature_tag/retries\n // 2) wrapper usage schema: prompt_tokens/completion_tokens/endpoint/attempt/trace_id/cost_usd\n\n const inputTokens = x.input_tokens ?? x.prompt_tokens;\n const outputTokens = x.output_tokens ?? x.completion_tokens;\n\n // feature_tag fallback: feature_tag -> meta.feature_tag -> endpoint\n const featureTag = x.feature_tag ?? x?.meta?.feature_tag ?? x.endpoint ?? '';\n\n // retries fallback: retries -> max(attempt-1,0)\n const retries = x.retries ?? (x.attempt !== undefined ? Math.max(0, toNum(x.attempt) - 1) : 0);\n\n // billed_cost fallback: billed_cost -> cost_usd\n const billed = x.billed_cost ?? x.cost_usd;\n\n return {\n ts: String(x.ts ?? ''),\n provider: String(x.provider ?? '').toLowerCase(),\n model: String(x.model ?? ''),\n input_tokens: toNum(inputTokens),\n output_tokens: toNum(outputTokens),\n feature_tag: String(featureTag ?? ''),\n retries: toNum(retries),\n status: String(x.status ?? ''),\n billed_cost: billed === undefined || billed === '' ? undefined : toNum(billed),\n\n trace_id: x.trace_id ? String(x.trace_id) : undefined,\n request_id: x.request_id ? String(x.request_id) : undefined,\n attempt: x.attempt === undefined ? undefined : toNum(x.attempt),\n endpoint: x.endpoint ? String(x.endpoint) : undefined\n };\n}\n\nexport function isCsvPath(p: string) {\n return path.extname(p).toLowerCase() === '.csv';\n}\n","import { RateTable, UsageEvent } from './types';\n\nexport type CostResult = {\n cost: number;\n used_rate: {\n kind: 'billed_cost' | 'official' | 'estimated';\n provider: string;\n model: string;\n input_per_m: number;\n output_per_m: number;\n };\n};\n\nexport function getRates(rt: RateTable, provider: string, model: string) {\n const prov = String(provider || '').toLowerCase();\n\n // local/offline LLM: treat cost as 0 (or user can add a provider rate later)\n if (prov === 'local' || prov === 'ollama' || prov === 'vllm') {\n return { kind: 'official' as const, input: 0, output: 0 };\n }\n\n const p = rt.providers[prov];\n if (!p) return null;\n const m = p.models[model];\n if (m) return { kind: 'official' as const, input: m.input, output: m.output };\n return { kind: 'estimated' as const, input: p.default_estimated.input, output: p.default_estimated.output };\n}\n\nexport function costOfEvent(rt: RateTable, ev: UsageEvent): CostResult {\n if (typeof ev.billed_cost === 'number' && Number.isFinite(ev.billed_cost)) {\n return {\n cost: ev.billed_cost,\n used_rate: {\n kind: 'billed_cost',\n provider: ev.provider,\n model: ev.model,\n input_per_m: 0,\n output_per_m: 0\n }\n };\n }\n\n const r = getRates(rt, ev.provider, ev.model);\n if (!r) {\n // Unknown provider: deterministic fallback estimate (kept stable for reproducibility)\n const input_per_m = 1.0;\n const output_per_m = 4.0;\n const cost = (ev.input_tokens / 1e6) * input_per_m + (ev.output_tokens / 1e6) * output_per_m;\n return {\n cost,\n used_rate: { kind: 'estimated', provider: String(ev.provider || '').toLowerCase(), model: ev.model, input_per_m, output_per_m }\n };\n }\n\n const cost = (ev.input_tokens / 1e6) * r.input + (ev.output_tokens / 1e6) * r.output;\n return {\n cost,\n used_rate: {\n kind: r.kind,\n provider: ev.provider,\n model: ev.model,\n input_per_m: r.input,\n output_per_m: r.output\n }\n };\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { AnalysisJson, Savings } from './scan';\n\nexport type Fix = {\n id: string;\n title: string;\n impact_usd: number;\n why: string;\n what_to_change: string[];\n status: 'action' | 'no-issue';\n};\n\nconst EPS = 0.0001;\n\nexport function buildTopFixes(analysis: AnalysisJson, savings: Savings): Fix[] {\n const fixes: Fix[] = [];\n\n // Retry tuning\n fixes.push({\n id: 'fix-retry-tuning',\n title: 'Retry tuning',\n impact_usd: Number(savings.retry_waste || 0),\n status: Number(savings.retry_waste || 0) > EPS ? 'action' : 'no-issue',\n why: `Retry waste is estimated at $${round2(Number(savings.retry_waste || 0))}.`,\n what_to_change: [\n 'aiopt/policies/retry.json: lower max_attempts or adjust backoff_ms',\n 'Ensure idempotency keys are stable per trace_id',\n 'Log error_code to identify retryable classes'\n ]\n });\n\n // Output cap (use context_savings as proxy impact)\n fixes.push({\n id: 'fix-output-cap',\n title: 'Output cap',\n impact_usd: Number(savings.context_savings || 0),\n status: Number(savings.context_savings || 0) > EPS ? 'action' : 'no-issue',\n why: `Context savings estimate: $${round2(Number(savings.context_savings || 0))}. Output caps prevent runaway completions.`,\n what_to_change: [\n 'aiopt/policies/output.json: set max_output_tokens_default',\n 'aiopt/policies/output.json: set per_feature caps (summarize/classify/translate)'\n ]\n });\n\n // Routing\n const topFeature = analysis.by_feature_top?.[0]?.key;\n fixes.push({\n id: 'fix-routing',\n title: 'Routing rule',\n impact_usd: Number(savings.routing_savings || 0),\n status: Number(savings.routing_savings || 0) > EPS ? 'action' : 'no-issue',\n why: `Routing savings estimate: $${round2(Number(savings.routing_savings || 0))}.`,\n what_to_change: [\n 'aiopt/policies/routing.json: route summarize/classify/translate to cheap tier',\n `Consider adding feature_tag_in for top feature: ${topFeature || '(unknown)'}`\n ]\n });\n\n // sort by impact desc, deterministic tie-break\n fixes.sort((a, b) => (b.impact_usd - a.impact_usd) || a.id.localeCompare(b.id));\n return fixes;\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nexport function writePatches(outDir: string, fixes: Fix[]) {\n const patchesDir = path.join(outDir, 'patches');\n fs.mkdirSync(patchesDir, { recursive: true });\n\n const readme = [\n '# AIOpt patches (MVP)',\n '',\n 'This folder contains suggested changes you can apply locally.',\n '',\n '## Top fixes',\n ...fixes.map((f, i) => `${i + 1}. ${f.title} — ${f.why}`),\n '',\n 'Files are stubs (human review required).',\n ''\n ].join('\\n');\n\n fs.writeFileSync(path.join(patchesDir, 'README.md'), readme);\n\n // Minimal stub files to satisfy DoD naming.\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.routing.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/routing.json', fixes: fixes.filter(f => f.id.includes('routing')) }, null, 2));\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.retry.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/retry.json', fixes: fixes.filter(f => f.id.includes('retry')) }, null, 2));\n fs.writeFileSync(path.join(patchesDir, 'policies.updated.output.json'), JSON.stringify({ note: 'apply changes to aiopt/policies/output.json', fixes: fixes.filter(f => f.id.includes('output')) }, null, 2));\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { RateTable, UsageEvent } from './types';\nimport { costOfEvent, getRates } from './cost';\nimport { buildTopFixes, writePatches } from './solutions';\n\nexport type AnalysisJson = {\n total_cost: number;\n by_model_top: Array<{ key: string; cost: number; events: number }>;\n by_feature_top: Array<{ key: string; cost: number; events: number }>;\n unknown_models: Array<{ provider: string; model: string; reason: string }>;\n rate_table_version: string;\n rate_table_date: string;\n};\n\nexport type Savings = {\n estimated_savings_total: number;\n routing_savings: number;\n context_savings: number;\n retry_waste: number;\n notes: [string, string, string];\n};\n\nexport type PolicyJson = {\n version: number;\n default_provider: string;\n rules: Array<any>;\n budgets: { currency: string; notes?: string };\n generated_from: { rate_table_version: string; input: string };\n};\n\nconst ROUTE_TO_CHEAP_FEATURES = new Set(['summarize', 'classify', 'translate']);\n\nfunction topN(map: Map<string, { cost: number; events: number }>, n: number) {\n return [...map.entries()]\n .map(([key, v]) => ({ key, cost: v.cost, events: v.events }))\n .sort((a, b) => b.cost - a.cost)\n .slice(0, n);\n}\n\nexport function analyze(rt: RateTable, events: UsageEvent[]): { analysis: AnalysisJson; savings: Savings; policy: PolicyJson; meta: { mode: 'attempt-log'|'legacy' } } {\n const byModel = new Map<string, { cost: number; events: number }>();\n const byFeature = new Map<string, { cost: number; events: number }>();\n const unknownModels: AnalysisJson['unknown_models'] = [];\n\n const perEventCosts: Array<{ ev: UsageEvent; cost: number }> = [];\n\n // Detect wrapper attempt-log mode: each JSONL line is an attempt (trace_id/attempt present)\n const isAttemptLog = events.some(e => (e.trace_id && String(e.trace_id).length > 0) || (e.attempt !== undefined && Number(e.attempt) > 0));\n\n let baseTotal = 0;\n let total = 0;\n for (const ev of events) {\n const cr = costOfEvent(rt, ev);\n\n baseTotal += cr.cost;\n\n if (isAttemptLog) {\n // Each event line is already one attempt; do NOT multiply by retries.\n total += cr.cost;\n } else {\n // Legacy aggregate mode: one line represents a request with retries count.\n const retries = Math.max(0, Number(ev.retries || 0));\n total += cr.cost * (1 + retries);\n }\n\n perEventCosts.push({ ev, cost: cr.cost });\n\n const mk = `${ev.provider}:${ev.model}`;\n const fk = ev.feature_tag || '(none)';\n\n const mv = byModel.get(mk) || { cost: 0, events: 0 };\n mv.cost += cr.cost; mv.events += 1;\n byModel.set(mk, mv);\n\n const fv = byFeature.get(fk) || { cost: 0, events: 0 };\n fv.cost += cr.cost; fv.events += 1;\n byFeature.set(fk, fv);\n\n const rr = getRates(rt, ev.provider, ev.model);\n if (!rr) {\n unknownModels.push({ provider: ev.provider, model: ev.model, reason: 'unknown provider (estimated)' });\n } else if (rr.kind === 'estimated') {\n unknownModels.push({ provider: ev.provider, model: ev.model, reason: 'unknown model (estimated)' });\n }\n }\n\n // --- Savings simulator (refactor): per-event before/after with caps + no double counting\n type Pot = { routing: number; context: number; retry: number; total: number; waste: number };\n const potByIdx: Pot[] = [];\n\n // Potential routing/context computed per event\n for (const { ev, cost } of perEventCosts) {\n const retries = Math.max(0, Number(ev.retries || 0));\n const attempt = Number(ev.attempt || 1);\n\n const total_i = isAttemptLog ? cost : cost * (1 + retries);\n // Retry waste:\n // - attempt-log mode: attempts >= 2 are retry waste\n // - legacy mode: base_cost * retries\n const waste_i = isAttemptLog ? (attempt >= 2 ? cost : 0) : cost * retries;\n\n let routing_i = 0;\n if (ROUTE_TO_CHEAP_FEATURES.has(String(ev.feature_tag || '').toLowerCase())) {\n const provider = ev.provider;\n const p = rt.providers[provider];\n if (p) {\n const entries = Object.entries(p.models);\n if (entries.length > 0) {\n const cheapest = entries\n .map(([name, r]) => ({ name, score: (r.input + r.output) / 2, r }))\n .sort((a, b) => a.score - b.score)[0];\n const currentRate = getRates(rt, provider, ev.model);\n if (currentRate && currentRate.kind !== 'estimated') {\n const currentCost = (ev.input_tokens / 1e6) * currentRate.input + (ev.output_tokens / 1e6) * currentRate.output;\n const cheapCost = (ev.input_tokens / 1e6) * cheapest.r.input + (ev.output_tokens / 1e6) * cheapest.r.output;\n const diff = (currentCost - cheapCost) * (1 + retries);\n routing_i = Math.max(0, diff);\n }\n }\n }\n }\n\n // context: top 20% rule is applied later by index set\n potByIdx.push({ routing: routing_i, context: 0, retry: waste_i, total: total_i, waste: waste_i });\n }\n\n // context potential assignment (deterministic): top 20% by input_tokens => 25% reduction\n // In attempt-log mode, only apply to attempt==1 to avoid overcounting retries.\n const sortedIdx = [...events.map((e, i) => ({ i, input: Number(e.input_tokens || 0), ok: !isAttemptLog || Number(e.attempt || 1) === 1 }))]\n .filter(x => x.ok)\n .sort((a, b) => b.input - a.input);\n const k = Math.max(1, Math.floor(sortedIdx.length * 0.2));\n const topIdx = new Set(sortedIdx.slice(0, k).map(x => x.i));\n for (let i = 0; i < events.length; i++) {\n if (!topIdx.has(i)) continue;\n const ev = events[i];\n const retries = Math.max(0, Number(ev.retries || 0));\n const r = getRates(rt, ev.provider, ev.model);\n if (!r) continue;\n const saveTokens = (Number(ev.input_tokens || 0)) * 0.25;\n const multiplier = isAttemptLog ? 1 : (1 + retries);\n const diff = (saveTokens / 1e6) * r.input * multiplier;\n potByIdx[i].context = Math.max(0, diff);\n }\n\n // Allocate savings without overlap (routing -> context -> retry), each capped by remaining cost.\n let routingSavings = 0;\n let contextSavings = 0;\n let retryWaste = 0;\n\n for (const p of potByIdx) {\n let remaining = p.total;\n\n const rSave = Math.min(p.routing, remaining);\n remaining -= rSave;\n routingSavings += rSave;\n\n const cSave = Math.min(p.context, remaining);\n remaining -= cSave;\n contextSavings += cSave;\n\n // Retry tuning can only save the waste portion, and cannot exceed remaining.\n const retrySave = Math.min(p.retry, remaining);\n retryWaste += retrySave;\n }\n\n const estimatedSavingsTotal = routingSavings + contextSavings + retryWaste;\n // Global guard\n const guardedSavingsTotal = Math.min(estimatedSavingsTotal, total);\n\n const analysis: AnalysisJson = {\n total_cost: round2(total),\n by_model_top: topN(byModel, 10).map(x => ({ ...x, cost: round2(x.cost) })),\n by_feature_top: topN(byFeature, 10).map(x => ({ ...x, cost: round2(x.cost) })),\n unknown_models: uniqUnknown(unknownModels),\n rate_table_version: rt.version,\n rate_table_date: rt.date\n };\n\n const savings: Savings = {\n estimated_savings_total: round2(guardedSavingsTotal),\n routing_savings: round2(routingSavings),\n context_savings: round2(contextSavings),\n retry_waste: round2(retryWaste),\n notes: [\n `a) 모델 라우팅 절감(추정): $${round2(routingSavings)}`,\n `b) 컨텍스트 감축(추정): $${round2(contextSavings)} (상위 20% input에 25% 감축 가정)` ,\n `c) 재시도/오류 낭비(상한 적용): $${round2(retryWaste)} (retries 기반)`\n ]\n };\n\n const policy: PolicyJson = buildPolicy(rt, events);\n\n return { analysis, savings, policy, meta: { mode: isAttemptLog ? 'attempt-log' : 'legacy' } };\n}\n\nfunction buildPolicy(rt: RateTable, events: UsageEvent[]): PolicyJson {\n // Default provider: most frequent\n const freq = new Map<string, number>();\n for (const ev of events) freq.set(ev.provider, (freq.get(ev.provider) || 0) + 1);\n const defaultProvider = [...freq.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] || 'openai';\n\n // For cheap features: recommend cheapest known model per provider.\n const rules: any[] = [];\n for (const provider of Object.keys(rt.providers)) {\n const p = rt.providers[provider];\n const entries = Object.entries(p.models);\n if (entries.length === 0) continue;\n const cheapest = entries\n .map(([name, r]) => ({ name, score: (r.input + r.output) / 2, r }))\n .sort((a, b) => a.score - b.score)[0];\n\n rules.push({\n match: { provider, feature_tag_in: ['summarize', 'classify', 'translate'] },\n action: { recommend_model: cheapest.name, reason: 'cheap-feature routing' }\n });\n }\n\n // Unknown models: keep (no policy)\n rules.push({ match: { model_unknown: true }, action: { keep: true, reason: 'unknown model -> no policy applied' } });\n\n return {\n version: 1,\n default_provider: defaultProvider,\n rules,\n budgets: { currency: rt.currency, notes: 'MVP: budgets not enforced' },\n generated_from: { rate_table_version: rt.version, input: './aiopt-input/usage.jsonl' }\n };\n}\n\nfunction uniqUnknown(list: AnalysisJson['unknown_models']) {\n const seen = new Set<string>();\n const out: AnalysisJson['unknown_models'] = [];\n for (const x of list) {\n const k = `${x.provider}:${x.model}:${x.reason}`;\n if (seen.has(k)) continue;\n seen.add(k);\n out.push(x);\n }\n return out;\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nexport function writeOutputs(outDir: string, analysis: AnalysisJson, savings: Savings, policy: PolicyJson, meta?: { mode?: 'attempt-log'|'legacy' }) {\n const mode = meta?.mode || 'legacy';\n\n fs.mkdirSync(outDir, { recursive: true });\n\n fs.writeFileSync(path.join(outDir, 'analysis.json'), JSON.stringify(analysis, null, 2));\n\n // report.json is the “one file to parse” summary for downstream tooling.\n const unknownCount = analysis.unknown_models?.length || 0;\n // confidence: downgrade if many unknowns; keep deterministic\n const confidence = unknownCount === 0 ? 'HIGH' : (unknownCount <= 3 ? 'MED' : 'LOW');\n const ratio = analysis.total_cost > 0 ? (savings.estimated_savings_total / analysis.total_cost) : 0;\n const warnings: string[] = [];\n if (ratio >= 0.9) warnings.push('estimated savings >= 90%');\n if (unknownCount > 0) warnings.push('unknown models/providers detected (estimated pricing used)');\n\n const reportJson = {\n version: 3,\n generated_at: new Date().toISOString(),\n confidence,\n warnings,\n assumptions: {\n no_double_counting: 'routing -> context -> retry allocation per-event with remaining-cost caps',\n retry_cost_model: mode === 'attempt-log'\n ? 'attempt-log mode: total_cost is sum of attempt lines; retry_waste is sum of attempts>=2'\n : 'legacy mode: total_cost includes retries as extra attempts (base_cost*(1+retries))',\n context_model: 'top 20% by input_tokens assume 25% input reduction',\n estimated_pricing_note: unknownCount > 0 ? 'some items use estimated rates; treat savings as a band' : 'all items used known rates'\n },\n summary: {\n total_cost_usd: analysis.total_cost,\n estimated_savings_usd: savings.estimated_savings_total,\n routing_savings_usd: savings.routing_savings,\n context_savings_usd: savings.context_savings,\n retry_waste_usd: savings.retry_waste\n },\n top: {\n by_model: analysis.by_model_top,\n by_feature: analysis.by_feature_top\n },\n unknown_models: analysis.unknown_models,\n notes: savings.notes\n };\n fs.writeFileSync(path.join(outDir, 'report.json'), JSON.stringify(reportJson, null, 2));\n\n // report.md: \"what to change\" guidance + confidence/assumptions (T4 DoD)\n const ratioMd = analysis.total_cost > 0 ? (savings.estimated_savings_total / analysis.total_cost) : 0;\n const warningsMd: string[] = [];\n if (ratioMd >= 0.9) warningsMd.push('WARNING: estimated savings >= 90% — check overlap/missing rate table');\n\n const reportMd = [\n '# AIOpt Report',\n '',\n `- Total cost: $${analysis.total_cost}`,\n `- Estimated savings: $${savings.estimated_savings_total} (guarded <= total_cost)`,\n `- Confidence: ${confidence}`,\n unknownCount > 0 ? `- Unknown models: ${unknownCount} (estimated pricing used)` : '- Unknown models: 0',\n ...warningsMd.map(w => `- ${w}`),\n '',\n '## ASSUMPTIONS',\n '- No double-counting: routing → context → retry savings allocated per-event with remaining-cost caps.',\n mode === 'attempt-log'\n ? '- Retry cost model: attempt-log mode (total_cost=sum attempts, retry_waste=sum attempt>=2).'\n : '- Retry cost model: legacy mode (total_cost=base_cost*(1+retries)).',\n '- Context savings: top 20% input_tokens events assume 25% input reduction.',\n '',\n '## WHAT TO CHANGE',\n '1) Retry tuning → edit `aiopt/policies/retry.json`',\n '2) Output cap → edit `aiopt/policies/output.json`',\n '3) Routing rule → edit `aiopt/policies/routing.json`',\n '',\n '## OUTPUTS',\n '- `aiopt-output/analysis.json`',\n '- `aiopt-output/report.json`',\n '- `aiopt-output/patches/*`',\n ''\n ].join('\\n');\n fs.writeFileSync(path.join(outDir, 'report.md'), reportMd);\n\n const reportTxt = [\n `총비용: $${analysis.total_cost}`,\n `절감 가능 금액(Estimated): $${savings.estimated_savings_total}`,\n `절감 근거 3줄:`,\n savings.notes[0],\n savings.notes[1],\n savings.notes[2],\n ''\n ].join('\\n');\n fs.writeFileSync(path.join(outDir, 'report.txt'), reportTxt);\n\n fs.writeFileSync(path.join(outDir, 'cost-policy.json'), JSON.stringify(policy, null, 2));\n\n // patches/*\n const fixes = buildTopFixes(analysis, savings);\n writePatches(outDir, fixes);\n}\n","{\n \"name\": \"aiopt\",\n \"version\": \"0.2.7\",\n \"description\": \"Pre-deploy LLM cost accident guardrail (serverless local CLI)\",\n \"bin\": {\n \"aiopt\": \"dist/cli.js\"\n },\n \"type\": \"commonjs\",\n \"main\": \"dist/cli.js\",\n \"files\": [\n \"dist\",\n \"rates\",\n \"samples\",\n \"README.md\"\n ],\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"node --enable-source-maps dist/cli.js\",\n \"prepack\": \"npm run build\",\n \"test:npx\": \"npm pack --silent && node -e \\\"const fs=require('fs');const p=fs.readdirSync('.').find(f=>/^aiopt-.*\\\\.tgz$/.test(f)); if(!p) throw new Error('tgz not found'); console.log('tgz',p);\\\" && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) install --force && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) doctor && npx --yes ./$(ls -1 aiopt-*.tgz | tail -n 1) scan && test -f ./aiopt-output/report.md && echo OK\",\n \"test:guard\": \"npm run build --silent && node scripts/test-guard.js\",\n \"test:license\": \"npm run build --silent && node scripts/test-license.js\",\n \"test:landing\": \"node scripts/test-landing.js\"\n },\n \"dependencies\": {\n \"commander\": \"^14.0.0\",\n \"csv-parse\": \"^6.1.0\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^24.0.0\",\n \"tsup\": \"^8.5.0\",\n \"typescript\": \"^5.9.2\"\n }\n}\n","import fs from 'fs';\nimport path from 'path';\n\nexport type InstallOptions = {\n force?: boolean;\n seedSample?: boolean;\n};\n\nfunction ensureDir(p: string) {\n fs.mkdirSync(p, { recursive: true });\n}\n\nfunction writeFile(filePath: string, content: string, force?: boolean) {\n if (!force && fs.existsSync(filePath)) return { wrote: false, reason: 'exists' as const };\n ensureDir(path.dirname(filePath));\n fs.writeFileSync(filePath, content);\n return { wrote: true as const };\n}\n\nexport function runInstall(cwd: string, opts: InstallOptions) {\n const force = Boolean(opts.force);\n\n const aioptDir = path.join(cwd, 'aiopt');\n const policiesDir = path.join(aioptDir, 'policies');\n const outDir = path.join(cwd, 'aiopt-output');\n\n ensureDir(aioptDir);\n ensureDir(policiesDir);\n ensureDir(outDir);\n\n const created: Array<{ path: string; status: 'created' | 'skipped' }> = [];\n\n // 1) aiopt/README.md\n const readme = `# AIOpt\n\nAIOpt는 **scan툴이 아니라 설치형 비용 가드레일이다.**\n\n## Quick start\n\\`\\`\\`bash\nnpx aiopt install --force\nnpx aiopt doctor\n# (your app runs, wrapper logs usage)\nnpx aiopt scan\n\\`\\`\\`\n\n- 서버/대시보드/계정/업로드/결제/프록시 없음\n- 로컬 파일 기반(정책 + usage.jsonl)\n- LLM 호출 금지(수학/룰 기반)\n\n## Wrapper usage (Node.js)\n\nAIOpt 설치 후, 앱 코드에서 wrapper를 불러서 사용량 JSONL을 자동 기록할 수 있습니다.\n\n\na) 최소 형태(토큰을 직접 넘김)\n\n\\`\\`\\`js\nconst { guardedCall } = require('./aiopt/aiopt-wrapper.js');\n\nawait guardedCall(process.cwd(), {\n provider: 'openai',\n model: 'gpt-5-mini',\n endpoint: 'responses.create',\n feature_tag: 'summarize',\n prompt_tokens: 1200,\n trace_id: 'my-trace'\n}, async (req) => {\n // req: { provider, model, endpoint, max_output_tokens, prompt_tokens, idempotency_key }\n // 여기서 실제 SDK 호출 후 결과를 반환\n return { status: 'ok', completion_tokens: 200 };\n});\n\\`\\`\\`\n\nb) OpenAI-style 응답(usage 자동 추출)\n\n\\`\\`\\`js\nreturn {\n status: 'ok',\n response: {\n usage: { prompt_tokens: 1200, completion_tokens: 200, total_tokens: 1400 }\n }\n};\n\\`\\`\\`\n`;\n\n const r1 = writeFile(path.join(aioptDir, 'README.md'), readme, force);\n created.push({ path: 'aiopt/README.md', status: r1.wrote ? 'created' : 'skipped' });\n\n // 2) aiopt/aiopt.config.json\n const config = {\n version: 1,\n installed_at: new Date().toISOString(),\n output_dir: './aiopt-output',\n usage_path: './aiopt-output/usage.jsonl',\n policies_dir: './aiopt/policies',\n rate_table: { path: './rates/rate_table.json' }\n };\n const r2 = writeFile(path.join(aioptDir, 'aiopt.config.json'), JSON.stringify(config, null, 2) + '\\n', force);\n created.push({ path: 'aiopt/aiopt.config.json', status: r2.wrote ? 'created' : 'skipped' });\n\n // 3) policies\n const routing = {\n version: 1,\n rules: [\n { match: { feature_tag_in: ['summarize', 'classify', 'translate'] }, action: { tier: 'cheap', reason: 'cheap feature routing' } },\n { match: { feature_tag_in: ['coding', 'reasoning'] }, action: { tier: 'default', reason: 'keep for quality' } }\n ]\n };\n const retry = {\n version: 1,\n max_attempts: 2,\n backoff_ms: [200, 500],\n retry_on_status: ['error', 'timeout'],\n notes: 'MVP deterministic retry tuning'\n };\n const output = {\n version: 1,\n max_output_tokens_default: 1024,\n per_feature: {\n summarize: 512,\n classify: 256,\n translate: 512\n }\n };\n const context = {\n version: 1,\n input_token_soft_cap: 12000,\n reduce_top_percentile: 0.2,\n assumed_reduction_ratio: 0.25\n };\n\n const p1 = writeFile(path.join(policiesDir, 'routing.json'), JSON.stringify(routing, null, 2) + '\\n', force);\n const p2 = writeFile(path.join(policiesDir, 'retry.json'), JSON.stringify(retry, null, 2) + '\\n', force);\n const p3 = writeFile(path.join(policiesDir, 'output.json'), JSON.stringify(output, null, 2) + '\\n', force);\n const p4 = writeFile(path.join(policiesDir, 'context.json'), JSON.stringify(context, null, 2) + '\\n', force);\n\n created.push({ path: 'aiopt/policies/routing.json', status: p1.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/retry.json', status: p2.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/output.json', status: p3.wrote ? 'created' : 'skipped' });\n created.push({ path: 'aiopt/policies/context.json', status: p4.wrote ? 'created' : 'skipped' });\n\n // 4) wrapper template (T2: real guardrails wrapper)\n const wrapperPath = path.join(aioptDir, 'aiopt-wrapper.js');\n const wrapper = \n`// AIOpt Wrapper (guardrails) — local-only (CommonJS)\n\nconst fs = require('fs');\n\nconst path = require('path');\n\nconst crypto = require('crypto');\n\nfunction readJson(p){ return JSON.parse(fs.readFileSync(p,'utf8')); }\nfunction ensureDir(p){ fs.mkdirSync(p,{recursive:true}); }\nfunction appendJsonl(filePath,obj){ ensureDir(path.dirname(filePath)); fs.appendFileSync(filePath, JSON.stringify(obj)+'\\\\n'); }\nfunction sleep(ms){ return new Promise(r=>setTimeout(r,ms)); }\n\nfunction loadConfig(cwd){ return readJson(path.join(cwd,'aiopt','aiopt.config.json')); }\nfunction loadPolicies(cwd,cfg){ const dir=path.isAbsolute(cfg.policies_dir)?cfg.policies_dir:path.join(cwd,cfg.policies_dir);\n return {\n retry: readJson(path.join(dir,'retry.json')) ,\n output: readJson(path.join(dir,'output.json'))\n };\n}\nfunction loadRates(cwd,cfg){\n const rp=path.isAbsolute(cfg.rate_table.path)?cfg.rate_table.path:path.join(cwd,cfg.rate_table.path);\n try{ return readJson(rp); }catch(e){\n // Fresh projects may not have a rates/ table yet. Fall back to a safe default.\n return { providers: {} };\n }\n}\n\nfunction costUsd(rt, provider, model, promptTokens, completionTokens){\n const p=rt.providers && rt.providers[provider];\n const r=(p && p.models && p.models[model]) || (p && p.default_estimated) || {input:1.0, output:4.0};\n return (promptTokens/1e6)*r.input + (completionTokens/1e6)*r.output;\n}\n\nfunction pickRoutedModel(rt, provider, featureTag, currentModel){\n const cheap=['summarize','classify','translate'];\n if(!cheap.includes(String(featureTag||'').toLowerCase())) return { model: currentModel, routed_from: null, hit: null };\n const p=rt.providers && rt.providers[provider];\n const entries=p && p.models ? Object.entries(p.models) : [];\n if(!entries.length) return { model: currentModel, routed_from: null, hit: null };\n const cheapest=entries.map(([name,r])=>({name,score:(r.input+r.output)/2})).sort((a,b)=>a.score-b.score)[0];\n if(!cheapest || cheapest.name===currentModel) return { model: currentModel, routed_from: null, hit: null };\n return { model: cheapest.name, routed_from: currentModel, hit: 'routing:cheap-feature' };\n}\n\nfunction outputCap(outputPolicy, featureTag, requested){\n const per=(outputPolicy && outputPolicy.per_feature) || {};\n const cap=per[String(featureTag||'').toLowerCase()] ?? (outputPolicy.max_output_tokens_default || 1024);\n const req=requested ?? cap;\n return Math.min(req, cap);\n}\n\nconst IDEMPOTENCY=new Map();\n\nfunction normalizeResult(out, input){\n // Accept either normalized return or provider raw.\n const o = (out && out.response) ? out.response : out;\n const status = (out && out.status) || o.status || (o.error ? 'error' : 'ok');\n const usage = o.usage || (o.data && o.data.usage) || null;\n\n const prompt_tokens = Number(\n (out && out.prompt_tokens) ??\n (o && o.prompt_tokens) ??\n (usage && usage.prompt_tokens) ??\n input.prompt_tokens ??\n 0\n );\n const completion_tokens = Number(\n (out && out.completion_tokens) ??\n (o && o.completion_tokens) ??\n (usage && usage.completion_tokens) ??\n 0\n );\n const total_tokens = Number(\n (out && out.total_tokens) ??\n (o && o.total_tokens) ??\n (usage && usage.total_tokens) ??\n (prompt_tokens + completion_tokens)\n );\n\n const error_code = status === 'ok' ? null : String((out && out.error_code) || (o && o.error_code) || (o && o.error && (o.error.code || o.error.type)) || status);\n const cost_usd = (out && typeof out.cost_usd === 'number') ? out.cost_usd : null;\n\n return { status, prompt_tokens, completion_tokens, total_tokens, error_code, cost_usd };\n}\n\n/**\n * guardedCall(cwd, input, fn)\n *\n * fn(req) can return either:\n * 1) Normalized shape:\n * { status: 'ok'|'error'|'timeout', prompt_tokens?, completion_tokens?, total_tokens?, cost_usd?, error_code? }\n * 2) Provider raw response (OpenAI-style), e.g.:\n * { status:'ok', response:{ usage:{prompt_tokens, completion_tokens, total_tokens} } }\n * { usage:{prompt_tokens, completion_tokens, total_tokens} }\n *\n * If token fields are missing, AIOpt will fall back to input.prompt_tokens and/or 0.\n */\nasync function guardedCall(cwd, input, fn){\n const cfg=loadConfig(cwd);\n const pol=loadPolicies(cwd,cfg);\n const rt=loadRates(cwd,cfg);\n\n const request_id=crypto.randomUUID();\n const trace_id=input.trace_id || request_id;\n const idem=input.idempotency_key || trace_id;\n if(IDEMPOTENCY.has(idem)) return IDEMPOTENCY.get(idem);\n\n const routed=pickRoutedModel(rt, input.provider, input.feature_tag, input.model);\n const maxOut=outputCap(pol.output, input.feature_tag, input.max_output_tokens);\n const usagePath=path.isAbsolute(cfg.usage_path)?cfg.usage_path:path.join(cwd,cfg.usage_path);\n\n const maxAttempts=Math.max(1, Number(pol.retry.max_attempts||1));\n const backoffs=pol.retry.backoff_ms || [200];\n const retryOn=new Set(pol.retry.retry_on_status || ['error','timeout']);\n\n let last={status:'error', completion_tokens:0, error_code:'unknown'};\n\n for(let attempt=1; attempt<=maxAttempts; attempt++){\n const t0=Date.now();\n const policy_hits=[];\n if(routed.hit) policy_hits.push(routed.hit);\n policy_hits.push('outputcap:'+maxOut);\n try{\n const out=await fn({ provider: input.provider, model: routed.model, endpoint: input.endpoint, max_output_tokens: maxOut, prompt_tokens: input.prompt_tokens, idempotency_key: idem });\n const latency_ms=Date.now()-t0;\n const norm=normalizeResult(out, input);\n const cost_usd=(typeof norm.cost_usd==='number') ? norm.cost_usd : costUsd(rt, input.provider, routed.model, norm.prompt_tokens, norm.completion_tokens);\n const feature_tag = input.feature_tag || input.endpoint || 'unknown';\n appendJsonl(usagePath, { ts:new Date().toISOString(), request_id, trace_id, attempt, status: norm.status, error_code: norm.error_code, provider: input.provider, model: routed.model, endpoint: input.endpoint, prompt_tokens: norm.prompt_tokens, completion_tokens: norm.completion_tokens, total_tokens: norm.total_tokens, cost_usd, latency_ms, meta:{ feature_tag, routed_from: routed.routed_from, policy_hits } });\n last={ status: norm.status, completion_tokens: norm.completion_tokens, error_code: norm.error_code };\n if(norm.status==='ok'){ IDEMPOTENCY.set(idem,out); return out; }\n if(retryOn.has(norm.status) && attempt<maxAttempts){ await sleep(Number(backoffs[Math.min(attempt-1, backoffs.length-1)]||200)); continue; }\n IDEMPOTENCY.set(idem,out); return out;\n }catch(e){\n const latency_ms=Date.now()-t0;\n const out={ status:'error', completion_tokens:0, error_code:String(e && (e.code||e.name) || 'exception') };\n const feature_tag = input.feature_tag || input.endpoint || 'unknown';\n appendJsonl(usagePath, { ts:new Date().toISOString(), request_id, trace_id, attempt, status: out.status, error_code: out.error_code, provider: input.provider, model: routed.model, endpoint: input.endpoint, prompt_tokens:Number(input.prompt_tokens||0), completion_tokens:0, total_tokens:Number(input.prompt_tokens||0), cost_usd:costUsd(rt, input.provider, routed.model, Number(input.prompt_tokens||0), 0), latency_ms, meta:{ feature_tag, routed_from: routed.routed_from, policy_hits:[routed.hit||'routing:none','outputcap:'+maxOut,'error:exception'] } });\n last=out;\n if(attempt<maxAttempts){ await sleep(Number(backoffs[Math.min(attempt-1, backoffs.length-1)]||200)); continue; }\n IDEMPOTENCY.set(idem,out); return out;\n }\n }\n IDEMPOTENCY.set(idem,last);\n return last;\n}\n\nmodule.exports = { guardedCall };\n`;\n;\n const w = writeFile(wrapperPath, wrapper, force);\n created.push({ path: 'aiopt/aiopt-wrapper.js', status: w.wrote ? 'created' : 'skipped' });\n\n // 5) usage.jsonl\n const usagePath = path.join(outDir, 'usage.jsonl');\n if (force || !fs.existsSync(usagePath)) {\n // default: empty file (avoid mixing demo data into real user logs)\n fs.writeFileSync(usagePath, '');\n created.push({ path: 'aiopt-output/usage.jsonl', status: 'created' });\n\n if (opts.seedSample) {\n const sample = {\n ts: new Date().toISOString(),\n request_id: 'sample',\n trace_id: 'sample',\n attempt: 1,\n status: 'ok',\n error_code: null,\n provider: 'openai',\n model: 'gpt-5-mini',\n endpoint: 'demo',\n prompt_tokens: 12,\n completion_tokens: 3,\n total_tokens: 15,\n cost_usd: 0.0,\n latency_ms: 1,\n meta: { feature_tag: 'demo', routed_from: null, policy_hits: ['install-sample'] }\n };\n fs.appendFileSync(usagePath, JSON.stringify(sample) + '\\n');\n }\n } else {\n created.push({ path: 'aiopt-output/usage.jsonl', status: 'skipped' });\n }\n\n return { created };\n}\n","import fs from 'fs';\nimport path from 'path';\n\nfunction canWrite(dir: string) {\n try {\n fs.mkdirSync(dir, { recursive: true });\n const p = path.join(dir, `.aiopt-write-test-${Date.now()}`);\n fs.writeFileSync(p, 'ok');\n fs.unlinkSync(p);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction tailLines(filePath: string, n: number): string[] {\n try {\n const raw = fs.readFileSync(filePath, 'utf8');\n const lines = raw.split(/\\r?\\n/).filter(l => l.trim().length > 0);\n return lines.slice(Math.max(0, lines.length - n));\n } catch {\n return [];\n }\n}\n\nexport type DoctorResult = {\n ok: boolean;\n checks: Array<{ name: string; ok: boolean; detail?: string }>;\n last5: Array<{ status?: string; provider?: string; model?: string; endpoint?: string; attempt?: number }>;\n};\n\nexport function runDoctor(cwd: string): DoctorResult {\n const aioptDir = path.join(cwd, 'aiopt');\n const policiesDir = path.join(aioptDir, 'policies');\n const outDir = path.join(cwd, 'aiopt-output');\n const usagePath = path.join(outDir, 'usage.jsonl');\n\n const checks: DoctorResult['checks'] = [];\n\n checks.push({ name: 'aiopt/ exists', ok: fs.existsSync(aioptDir) });\n checks.push({ name: 'aiopt/policies exists', ok: fs.existsSync(policiesDir) });\n checks.push({ name: 'aiopt-output/ writable', ok: canWrite(outDir) });\n checks.push({ name: 'usage.jsonl exists', ok: fs.existsSync(usagePath), detail: usagePath });\n\n const last5raw = tailLines(usagePath, 5);\n const last5 = last5raw.length === 0 ? [{ status: '(empty usage.jsonl)' }] as any : last5raw.map(l => {\n try {\n const j = JSON.parse(l);\n return {\n status: j.status,\n provider: j.provider,\n model: j.model,\n endpoint: j.endpoint,\n attempt: j.attempt,\n feature_tag: j?.meta?.feature_tag\n };\n } catch {\n return {};\n }\n });\n\n // Feature-tag quality check (sample last 50 lines)\n const last50 = tailLines(usagePath, 50);\n let missing = 0;\n let total50 = 0;\n for (const l of last50) {\n total50++;\n try {\n const j = JSON.parse(l);\n const ft = j?.meta?.feature_tag;\n if (!ft || String(ft).trim() === '') missing++;\n } catch {\n missing++;\n }\n }\n if (total50 > 0 && missing > 0) {\n checks.push({ name: 'feature_tag quality (last50)', ok: false, detail: `${missing}/${total50} missing meta.feature_tag` });\n } else {\n checks.push({ name: 'feature_tag quality (last50)', ok: true, detail: 'meta.feature_tag present' });\n }\n\n const ok = checks.every(c => c.ok);\n return { ok, checks, last5 };\n}\n\n","import crypto from 'crypto';\nimport fs from 'fs';\nimport path from 'path';\n\nexport type LicensePayload = {\n sub: string; // customer id/email/etc\n plan: 'pro' | 'team' | 'trial' | string;\n iat: number; // issued at (unix seconds)\n exp: number; // expires at (unix seconds)\n features?: Record<string, boolean>;\n};\n\nexport type LicenseFile = {\n key: string;\n payload: LicensePayload;\n verified: boolean;\n verified_at: string;\n};\n\n// NOTE: Public key only. No servers.\n// Replace this with your own RSA public key in PEM (SPKI) format.\nexport const DEFAULT_PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz1LLE/pXIx5TloDa0LAf\njg9NSIW6STWhsAFP2ZzXgpWoQ3cCmW6xcB/4QNEmPpGlfMWhyRfkxsdKuhnjUMTg\n8MpMAcbjjF8JrGS9iLnW4yrLm7jzsOcndjkGO7pH+32GopZk98dVzmIRPok2Je76\n3MQRaxLi0jWytaCmacEB4R7HyuquOQlHPg0vD9NOEwrC/+br2GdQbD1lKPyLeLv3\nRidwAs8Iw2xx5g8G+BsVSM/HRC3jQT5GynfnuDsvMHCvGLRct/76ajiR71/NFZEP\nZ7liILNnZzCTlKGGZfZmG70t+zkg8HKdpRuWy8rZ0DPWyQg5MKm6TZOMV6dC0Rpg\nDwIDAQAB\n-----END PUBLIC KEY-----`;\n\nfunction b64urlDecodeToBuffer(s: string): Buffer {\n const padLen = (4 - (s.length % 4)) % 4;\n const padded = s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat(padLen);\n return Buffer.from(padded, 'base64');\n}\n\nfunction safeJsonParse(buf: Buffer): any {\n const txt = buf.toString('utf8');\n return JSON.parse(txt);\n}\n\nexport function parseLicenseKey(key: string): { payload: LicensePayload; signature: Buffer; payloadB64Url: string } {\n const parts = String(key).trim().split('.');\n if (parts.length !== 2) throw new Error('invalid license key format: expected <payloadB64Url>.<sigB64Url>');\n const [payloadB64Url, sigB64Url] = parts;\n const payloadBuf = b64urlDecodeToBuffer(payloadB64Url);\n const sigBuf = b64urlDecodeToBuffer(sigB64Url);\n const payload = safeJsonParse(payloadBuf) as LicensePayload;\n return { payload, signature: sigBuf, payloadB64Url };\n}\n\nexport function verifyLicenseKey(key: string, publicKeyPem: string): { ok: boolean; reason?: string; payload?: LicensePayload } {\n let parsed: { payload: LicensePayload; signature: Buffer; payloadB64Url: string };\n try {\n parsed = parseLicenseKey(key);\n } catch (e: any) {\n return { ok: false, reason: e?.message || 'parse error' };\n }\n\n const now = Math.floor(Date.now() / 1000);\n if (typeof parsed.payload?.exp !== 'number' || parsed.payload.exp < now) {\n return { ok: false, reason: 'expired', payload: parsed.payload };\n }\n\n try {\n const verifier = crypto.createVerify('RSA-SHA256');\n verifier.update(parsed.payloadB64Url);\n verifier.end();\n const ok = verifier.verify(publicKeyPem, parsed.signature);\n if (!ok) return { ok: false, reason: 'bad signature', payload: parsed.payload };\n return { ok: true, payload: parsed.payload };\n } catch (e: any) {\n return { ok: false, reason: e?.message || 'verify error', payload: parsed.payload };\n }\n}\n\nexport function defaultLicensePath(cwd: string): string {\n return path.join(cwd, 'aiopt', 'license.json');\n}\n\nexport function writeLicenseFile(p: string, key: string, payload: LicensePayload, verified: boolean) {\n const out: LicenseFile = {\n key,\n payload,\n verified,\n verified_at: new Date().toISOString()\n };\n fs.mkdirSync(path.dirname(p), { recursive: true });\n fs.writeFileSync(p, JSON.stringify(out, null, 2));\n}\n\nexport function readLicenseFile(p: string): LicenseFile {\n return JSON.parse(fs.readFileSync(p, 'utf8')) as LicenseFile;\n}\n","import { RateTable, UsageEvent } from './types';\nimport { analyze } from './scan';\nimport { costOfEvent } from './cost';\n\nexport type GuardInput = {\n // Required. Historical usage for comparison.\n baselineEvents: UsageEvent[];\n\n // Optional. If provided, guard compares two *actual* log sets.\n // When set, candidate transform knobs (model/context/retries multipliers) are ignored.\n candidateEvents?: UsageEvent[];\n\n candidate: {\n provider?: string;\n model?: string;\n contextMultiplier?: number; // multiplies input_tokens\n outputMultiplier?: number; // multiplies output_tokens\n retriesDelta?: number; // adds to retries\n callMultiplier?: number; // multiplies call volume\n budgetMonthlyUsd?: number; // absolute budget gate\n };\n};\n\nexport type GuardResult = {\n exitCode: 0 | 2 | 3;\n message: string;\n};\n\nfunction accidentRiskFromMonthly(monthly: number): 'Low' | 'Medium' | 'High' {\n if (!Number.isFinite(monthly)) return 'Medium';\n if (monthly >= 100) return 'High';\n if (monthly >= 10) return 'Medium';\n return 'Low';\n}\n\nfunction round2(n: number) {\n return Math.round(n * 100) / 100;\n}\n\nfunction windowDays(events: UsageEvent[]): number {\n let days = 1;\n try {\n const times = events\n .map(e => Date.parse(e.ts))\n .filter(t => Number.isFinite(t))\n .sort((a, b) => a - b);\n if (times.length >= 2) {\n const spanMs = Math.max(0, times[times.length - 1] - times[0]);\n const spanDays = spanMs / (1000 * 60 * 60 * 24);\n days = Math.max(1, spanDays);\n }\n } catch {\n days = 1;\n }\n return days;\n}\n\nfunction monthEstimate(delta: number, events: UsageEvent[]) {\n // Convert observed delta (over the sample window) into a monthly estimate.\n const days = windowDays(events);\n return (delta * 30) / days;\n}\n\nfunction applyCandidate(events: UsageEvent[], cand: GuardInput['candidate']): UsageEvent[] {\n const ctxM = cand.contextMultiplier ?? 1;\n const outM = cand.outputMultiplier ?? 1;\n const rDelta = cand.retriesDelta ?? 0;\n\n return events.map(ev => ({\n ...ev,\n provider: cand.provider ? String(cand.provider).toLowerCase() : ev.provider,\n model: cand.model ? String(cand.model) : ev.model,\n input_tokens: Math.max(0, Math.round((ev.input_tokens || 0) * ctxM)),\n output_tokens: Math.max(0, Math.round((ev.output_tokens || 0) * outM)),\n retries: Math.max(0, Math.round((ev.retries || 0) + rDelta)),\n // clear billed_cost so pricing recalculates for new model/provider\n billed_cost: undefined\n }));\n}\n\nfunction confidenceFromChange(cand: GuardInput['candidate']): { level: 'High'|'Medium'|'Low'; reasons: string[] } {\n const reasons: string[] = [];\n\n if (cand.retriesDelta && cand.retriesDelta !== 0) reasons.push('retries change');\n if (cand.model) reasons.push('model change');\n if (cand.provider) reasons.push('provider change');\n if (cand.contextMultiplier && cand.contextMultiplier !== 1) reasons.push('context length change');\n if (cand.outputMultiplier && cand.outputMultiplier !== 1) reasons.push('output length change');\n\n // Spec rule:\n // High: retries/failed/dup calls (we only model retries here)\n // Medium: model/provider change\n // Low: context trimming / prompt structure\n if (cand.retriesDelta && cand.retriesDelta !== 0) return { level: 'High', reasons };\n if (cand.model || cand.provider) return { level: 'Medium', reasons };\n if ((cand.contextMultiplier && cand.contextMultiplier !== 1) || (cand.outputMultiplier && cand.outputMultiplier !== 1)) {\n return { level: 'Low', reasons };\n }\n return { level: 'Medium', reasons: reasons.length ? reasons : ['unknown change'] };\n}\n\nfunction assessDataQuality(baselineEvents: UsageEvent[], base: ReturnType<typeof analyze>) {\n const reasons: string[] = [];\n\n // ts span\n const times = baselineEvents\n .map(e => Date.parse(e.ts))\n .filter(t => Number.isFinite(t))\n .sort((a, b) => a - b);\n if (times.length < 2) reasons.push('ts span unknown');\n else {\n const spanDays = Math.max(0, (times[times.length - 1] - times[0]) / (1000 * 60 * 60 * 24));\n if (spanDays < 0.25) reasons.push('ts span too short');\n }\n\n // feature tag coverage\n const missingFt = baselineEvents.filter(e => !e.feature_tag && !(e.meta && (e.meta as any).feature_tag)).length;\n if (missingFt / Math.max(1, baselineEvents.length) > 0.2) reasons.push('feature_tag missing >20%');\n\n // unknown model/provider\n const unknown = (base.analysis.unknown_models || []).length;\n if (unknown / Math.max(1, baselineEvents.length) > 0.2) reasons.push('unknown model/provider >20%');\n\n // Map to a confidence penalty.\n let penalty: 'none'|'low'|'medium' = 'none';\n if (reasons.length >= 2) penalty = 'medium';\n else if (reasons.length === 1) penalty = 'low';\n\n return { reasons, penalty };\n}\n\nfunction degrade(level: 'High'|'Medium'|'Low', penalty: 'none'|'low'|'medium'): 'High'|'Medium'|'Low' {\n if (penalty === 'none') return level;\n if (penalty === 'low') return level === 'High' ? 'Medium' : 'Low';\n // medium\n return 'Low';\n}\n\nexport function runGuard(rt: RateTable, input: GuardInput): GuardResult {\n if (!input.baselineEvents || input.baselineEvents.length === 0) {\n const conf = { level: 'Low' as const, reasons: ['baseline empty'] };\n const msg = [\n 'FAIL: baseline usage is empty (need aiopt-output/usage.jsonl)',\n 'Impact (monthly est): +$0 (insufficient baseline)',\n `Accident risk: ${accidentRiskFromMonthly(100)}`,\n `Confidence: ${conf.level} (${(conf.reasons.length ? conf.reasons.join(', ') : 'baseline empty')})`,\n 'Recommendation: run the wrapper to collect baseline usage before using guard.'\n ].join('\\n');\n return { exitCode: 3, message: msg };\n }\n\n // For fair comparison, ignore billed_cost/cost_usd and recompute both sides from rate table.\n const baselineEvents = input.baselineEvents.map(e => ({ ...e, billed_cost: undefined }));\n const base = analyze(rt, baselineEvents);\n\n const candidateEvents = (input.candidateEvents && input.candidateEvents.length > 0)\n ? input.candidateEvents.map(e => ({ ...e, billed_cost: undefined }))\n : applyCandidate(baselineEvents, input.candidate);\n\n const cand = analyze(rt, candidateEvents);\n\n const baseCost = base.analysis.total_cost;\n let candCost = cand.analysis.total_cost;\n\n // call volume multiplier (traffic spike)\n const callMult = input.candidate.callMultiplier && input.candidate.callMultiplier > 0 ? input.candidate.callMultiplier : 1;\n if (callMult !== 1) {\n candCost = candCost * callMult;\n }\n\n // attempt-log baseline: retriesDelta should be interpreted as \"extra attempts\".\n const attemptLog = baselineEvents.some(e => (e.trace_id && String(e.trace_id).length > 0) || (e.attempt !== undefined && Number(e.attempt) > 0));\n if (attemptLog && input.candidate.retriesDelta && input.candidate.retriesDelta > 0) {\n // More accurate deterministic approximation:\n // - If baseline already has retry attempts (attempt>=2), use their average cost as retry unit.\n // - Else fall back to avg baseline attempt cost.\n let retryUnit = 0;\n let retryCount = 0;\n for (const ev of baselineEvents) {\n const attempt = Number(ev.attempt || 1);\n if (attempt >= 2) {\n retryUnit += costOfEvent(rt, ev).cost;\n retryCount += 1;\n }\n }\n if (retryCount > 0) {\n retryUnit = retryUnit / retryCount;\n } else {\n retryUnit = baseCost / Math.max(1, baselineEvents.length);\n }\n candCost += retryUnit * input.candidate.retriesDelta;\n }\n\n const delta = candCost - baseCost;\n\n const days = windowDays(baselineEvents);\n const baselineMonthly = (baseCost * 30) / days;\n const candidateMonthly = (candCost * 30) / days;\n\n const changeConf = input.candidateEvents\n ? ({ level: 'High' as const, reasons: ['actual logs diff (--baseline/--candidate)'] })\n : confidenceFromChange(input.candidate);\n\n const dq = assessDataQuality(baselineEvents, base);\n const conf = { level: degrade(changeConf.level, dq.penalty), reasons: [...changeConf.reasons, ...dq.reasons.map(r => `data: ${r}`)] };\n\n // Guard logic:\n // - Warning if delta > 0 and monthEstimate(delta) >= $10\n // - Fail if delta > 0 and monthEstimate(delta) >= $100 (merge-blocking)\n const monthly = monthEstimate(Math.max(0, delta), baselineEvents);\n const monthlyRounded = round2(monthly);\n\n let exitCode: 0 | 2 | 3 = 0;\n let headline = 'OK: no cost accident risk detected';\n\n if (monthly >= 100) { exitCode = 3; headline = 'FAIL: high risk of LLM cost accident'; }\n else if (monthly >= 10) { exitCode = 2; headline = 'WARN: possible LLM cost accident'; }\n\n // Budget gate: absolute monthly cost ceiling for candidate.\n const budget = input.candidate.budgetMonthlyUsd;\n if (budget && Number.isFinite(budget) && budget > 0 && candidateMonthly > budget) {\n exitCode = 3;\n headline = 'FAIL: candidate exceeds monthly budget';\n }\n\n const reasons = conf.reasons.length ? conf.reasons.join(', ') : 'n/a';\n\n function topCauses(): string[] {\n const out: string[] = [];\n\n // transform mode causes\n if (!input.candidateEvents) {\n if (callMult !== 1) out.push('traffic spike (call-mult)');\n if (input.candidate.retriesDelta && input.candidate.retriesDelta !== 0) out.push('retry spike (retries-delta)');\n if (input.candidate.model || input.candidate.provider) out.push('model/provider change');\n if (input.candidate.contextMultiplier && input.candidate.contextMultiplier !== 1) out.push('context length change');\n if (input.candidate.outputMultiplier && input.candidate.outputMultiplier !== 1) out.push('output length change');\n } else {\n // diff mode heuristic causes\n if (candidateEvents.length !== baselineEvents.length) out.push('traffic/call volume changed');\n const bTop = base.analysis.by_model_top?.[0]?.key;\n const cTop = cand.analysis.by_model_top?.[0]?.key;\n if (bTop && cTop && bTop !== cTop) out.push('model mix changed');\n if ((cand.savings?.retry_waste_usd || 0) > (base.savings?.retry_waste_usd || 0)) out.push('retry waste increased');\n }\n\n // budget\n if (budget && Number.isFinite(budget) && budget > 0) out.push(`budget gate: $${round2(budget)}/mo`);\n\n return out.slice(0, 3);\n }\n\n const causes = topCauses();\n\n const msg = [\n headline,\n `Summary: baseline=$${round2(baseCost)} → candidate=$${round2(candCost)} (Δ=$${round2(delta)})`,\n `Monthly est: baseline=$${round2(baselineMonthly)} → candidate=$${round2(candidateMonthly)}${budget ? ` (budget=$${round2(budget)})` : ''}`,\n callMult !== 1 ? `Call volume multiplier: x${callMult}` : null,\n causes.length ? `Top causes: ${causes.join(' | ')}` : null,\n `Impact (monthly est): +$${monthlyRounded}`,\n `Accident risk: ${accidentRiskFromMonthly(monthly)}`,\n `Confidence: ${conf.level} (${reasons})`,\n 'Recommendation: review model/provider/retry/context changes before deploy.'\n ].filter(Boolean).join('\\n');\n\n return { exitCode, message: msg };\n}\n","import http from 'http';\nimport fs from 'fs';\nimport path from 'path';\n\nexport async function startDashboard(cwd: string, opts: { port: number }) {\n const host = '127.0.0.1';\n const port = opts.port || 3010;\n\n const outDir = path.join(cwd, 'aiopt-output');\n const file = (name: string) => path.join(outDir, name);\n\n function readOrNull(p: string) {\n try {\n if (!fs.existsSync(p)) return null;\n return fs.readFileSync(p, 'utf8');\n } catch {\n return null;\n }\n }\n\n const indexHtml = `<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n <title>AIOpt Local Dashboard</title>\n <style>\n :root{\n --bg:#070b16; --card:#0b1222; --card2:#0e1730; --txt:#e7eaf2; --muted:#a3acc2;\n --border:rgba(255,255,255,.10);\n --ok:#34d399; --warn:#fbbf24; --fail:#fb7185;\n --shadow:0 18px 70px rgba(0,0,0,.35);\n }\n *{box-sizing:border-box}\n body{margin:0; font-family: ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto; background:var(--bg); color:var(--txt)}\n a{color:#bfffe3; text-decoration:none} a:hover{text-decoration:underline}\n .wrap{max-width:1100px; margin:0 auto; padding:18px}\n .top{display:flex; align-items:center; justify-content:space-between; gap:10px; flex-wrap:wrap}\n .h1{font-size:22px; font-weight:950; letter-spacing:-.02em}\n .pill{display:inline-flex; align-items:center; gap:8px; padding:7px 10px; border-radius:999px; border:1px solid var(--border);\n background:rgba(255,255,255,.03); color:var(--muted); font-size:12px; line-height:1}\n .dot{width:8px;height:8px;border-radius:99px;background:linear-gradient(90deg,#60a5fa,#34d399)}\n\n .grid{display:grid; grid-template-columns:repeat(12,1fr); gap:12px; margin-top:12px}\n .card{grid-column: span 12; background:linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,0)), var(--card);\n border:1px solid var(--border); border-radius:18px; padding:14px; box-shadow: var(--shadow)}\n @media(min-width: 920px){\n .c6{grid-column: span 6}\n .c4{grid-column: span 4}\n .c8{grid-column: span 8}\n }\n\n .k{padding:2px 6px; border-radius:8px; border:1px solid var(--border); font-family: ui-monospace; font-size:12px; color:#d7def2; background:rgba(255,255,255,.03)}\n .muted{color:var(--muted)}\n .row{display:flex; gap:10px; flex-wrap:wrap; align-items:center}\n\n .badge{display:inline-flex; align-items:center; gap:8px; padding:8px 10px; border-radius:14px; border:1px solid var(--border);\n background:rgba(255,255,255,.03); font-weight:900}\n .badge.ok{border-color:rgba(52,211,153,.35)}\n .badge.warn{border-color:rgba(251,191,36,.35)}\n .badge.fail{border-color:rgba(251,113,133,.35)}\n .badge .b{width:10px;height:10px;border-radius:99px}\n .badge.ok .b{background:var(--ok)}\n .badge.warn .b{background:var(--warn)}\n .badge.fail .b{background:var(--fail)}\n\n pre{margin:0; white-space:pre-wrap; word-break:break-word; background: rgba(3,6,14,.65);\n border:1px solid rgba(255,255,255,.12);\n padding:12px; border-radius:14px;\n font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\"; font-size:13px; line-height:1.55}\n\n .bars{display:flex; flex-direction:column; gap:8px; margin-top:8px}\n .bar{display:grid; grid-template-columns: 1fr 64px; gap:10px; align-items:center}\n .track{height:10px; border-radius:999px; background:rgba(255,255,255,.08); overflow:hidden; border:1px solid rgba(255,255,255,.10)}\n .fill{height:100%; border-radius:999px; background:linear-gradient(90deg,#60a5fa,#34d399)}\n .label{font-size:13px}\n .val{font-family: ui-monospace; font-size:12px; color:#d7def2; text-align:right}\n\n .mini{font-size:12px; color:var(--muted)}\n </style>\n</head>\n<body>\n <div class=\"wrap\">\n <div class=\"top\">\n <div class=\"h1\">AIOpt Local Dashboard</div>\n <div class=\"pill\"><span class=\"dot\"></span> local-only · reads <span class=\"k\">./aiopt-output</span></div>\n </div>\n\n <div class=\"grid\">\n <div class=\"card c6\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Guard verdict</div>\n <div class=\"mini\" id=\"guardMeta\">—</div>\n </div>\n <div style=\"height:8px\"></div>\n <div id=\"guardBadge\" class=\"badge\"><span class=\"b\"></span><span id=\"guardBadgeText\">loading…</span></div>\n <div style=\"height:10px\"></div>\n <pre id=\"guard\">loading…</pre>\n <div style=\"height:10px\"></div>\n <div class=\"row\">\n <a href=\"/api/guard-last.txt\" target=\"_blank\">raw txt</a>\n <span class=\"muted\">·</span>\n <a href=\"/api/guard-last.json\" target=\"_blank\">raw json</a>\n <span class=\"muted\">·</span>\n <a href=\"/api/guard-history.jsonl\" target=\"_blank\">history</a>\n </div>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Recent guard runs</div>\n <pre id=\"guardHist\" style=\"max-height:220px; overflow:auto\">loading…</pre>\n </div>\n\n <div class=\"card c6\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Scan summary</div>\n <div class=\"mini\"><a href=\"/api/report.json\" target=\"_blank\">report.json</a> · <a href=\"/api/report.md\" target=\"_blank\">report.md</a></div>\n </div>\n <div style=\"height:8px\"></div>\n <div class=\"row\">\n <div class=\"badge\" id=\"totalCostBadge\"><span class=\"b\" style=\"background:#60a5fa\"></span><span id=\"totalCost\">total: —</span></div>\n <div class=\"badge\" id=\"savingsBadge\"><span class=\"b\" style=\"background:#a78bfa\"></span><span id=\"savings\">savings: —</span></div>\n </div>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Cost trend (last 7d)</div>\n <div class=\"mini\"><a href=\"/api/usage.jsonl\" target=\"_blank\">usage.jsonl</a></div>\n <pre id=\"trend\">loading…</pre>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Cost by model</div>\n <div id=\"byModel\" class=\"bars\"></div>\n\n <div style=\"height:12px\"></div>\n <div style=\"font-weight:900\">Cost by feature</div>\n <div id=\"byFeature\" class=\"bars\"></div>\n\n <div style=\"height:10px\"></div>\n <div class=\"mini\" id=\"scanMeta\">—</div>\n </div>\n\n <div class=\"card\">\n <div class=\"row\" style=\"justify-content:space-between\">\n <div style=\"font-weight:950\">Latest report.md</div>\n <div class=\"mini\">Tip: run <span class=\"k\">aiopt scan</span> after collecting baseline</div>\n </div>\n <div style=\"height:8px\"></div>\n <pre id=\"scan\">loading…</pre>\n </div>\n </div>\n </div>\n\n<script>\nfunction money(x){\n if (x === null || x === undefined || Number.isNaN(Number(x))) return '—';\n return '$' + (Math.round(Number(x)*100)/100);\n}\nfunction renderBars(el, items){\n el.innerHTML='';\n if(!items || items.length===0){ el.innerHTML='<div class=\"mini\">(no data)</div>'; return; }\n const max = Math.max(...items.map(i=>Number(i.cost)||0), 0.000001);\n for(const it of items.slice(0,8)){\n const w = Math.max(2, Math.round(((Number(it.cost)||0)/max)*100));\n const row = document.createElement('div');\n row.className='bar';\n row.innerHTML =\n '<div>'+\n '<div class=\"label\">'+ String(it.key) +'</div>'+\n '<div class=\"track\"><div class=\"fill\" style=\"width:'+ w +'%\"></div></div>'+\n '</div>'+\n '<div class=\"val\">'+ money(it.cost) +'</div>';\n el.appendChild(row);\n }\n}\n\nasync function load(){\n const guardTxt = await fetch('/api/guard-last.txt').then(r=>r.ok?r.text():null);\n const guardMeta = await fetch('/api/guard-last.json').then(r=>r.ok?r.json():null);\n\n document.getElementById('guard').textContent = guardTxt || '(no guard-last.txt yet — run: aiopt guard)';\n if(guardMeta){\n document.getElementById('guardMeta').textContent = 'exit=' + guardMeta.exitCode + ' · ' + guardMeta.ts;\n const badge = document.getElementById('guardBadge');\n const t = document.getElementById('guardBadgeText');\n const code = Number(guardMeta.exitCode);\n badge.classList.remove('ok','warn','fail');\n if(code===0){badge.classList.add('ok'); t.textContent='OK (0)';}\n else if(code===2){badge.classList.add('warn'); t.textContent='WARN (2)';}\n else {badge.classList.add('fail'); t.textContent='FAIL (3)';}\n }\n\n const histTxt = await fetch('/api/guard-history.jsonl').then(r=>r.ok?r.text():null);\n if(histTxt){\n const lines = histTxt.trim().split('\\n').filter(Boolean).slice(-15).reverse();\n document.getElementById('guardHist').textContent = lines.join('\\n');\n } else {\n document.getElementById('guardHist').textContent = '(no guard-history.jsonl yet — run: aiopt guard)';\n }\n\n const reportJson = await fetch('/api/report.json').then(r=>r.ok?r.json():null);\n if(reportJson){\n const total = reportJson.summary && reportJson.summary.total_cost_usd;\n const sav = reportJson.summary && reportJson.summary.estimated_savings_usd;\n document.getElementById('totalCost').textContent = 'total: ' + money(total);\n document.getElementById('savings').textContent = 'savings: ' + money(sav);\n renderBars(document.getElementById('byModel'), reportJson.top && reportJson.top.by_model);\n renderBars(document.getElementById('byFeature'), reportJson.top && reportJson.top.by_feature);\n document.getElementById('scanMeta').textContent = 'confidence=' + (reportJson.confidence || '—') + ' · generated_at=' + (reportJson.generated_at || '—');\n } else {\n document.getElementById('scanMeta').textContent = '(no report.json yet — run: aiopt scan)';\n }\n\n const usageTxt = await fetch('/api/usage.jsonl').then(r=>r.ok?r.text():null);\n if(usageTxt){\n // 7d cost trend: sum(cost_usd) per day from usage.jsonl (ev.ts).\n const now = Date.now();\n const bins = Array.from({length:7}, (_,i)=>({ day:i, cost:0, calls:0 }));\n for(const line of usageTxt.trim().split('\\n')){\n if(!line) continue;\n try{\n const ev = JSON.parse(line);\n const t = Date.parse(ev.ts);\n if(!Number.isFinite(t)) continue;\n const d = Math.floor((now - t) / 86400000);\n if(d>=0 && d<7){\n bins[d].calls++;\n const c = Number(ev.cost_usd);\n if(Number.isFinite(c)) bins[d].cost += c;\n }\n }catch{}\n }\n const max = Math.max(...bins.map(b=>b.cost), 0.000001);\n const rows = bins.reverse().map((b,idx)=>{\n const w = Math.round((b.cost/max)*20);\n const bar = '█'.repeat(w) + '░'.repeat(20-w);\n const label = (idx===0 ? 'today' : ('d-'+idx));\n const dollars = ('$' + (Math.round(b.cost*100)/100).toFixed(2));\n return String(label).padEnd(7) + ' ' + bar + ' ' + String(dollars).padStart(9) + ' (' + b.calls + ' calls)';\n });\n document.getElementById('trend').textContent = rows.join('\\n');\n } else {\n document.getElementById('trend').textContent = '(no usage.jsonl yet)';\n }\n\n const reportMd = await fetch('/api/report.md').then(r=>r.ok?r.text():null);\n document.getElementById('scan').textContent = reportMd || '(no report.md yet — run: aiopt scan)';\n}\nload();\n</script>\n</body>\n</html>`;\n\n const server = http.createServer((req, res) => {\n const url = req.url || '/';\n if (url === '/' || url === '/index.html') {\n res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });\n res.end(indexHtml);\n return;\n }\n\n if (url.startsWith('/api/')) {\n const name = url.replace('/api/', '');\n const allow = new Set(['guard-last.txt', 'guard-last.json', 'guard-history.jsonl', 'report.md', 'report.json', 'usage.jsonl']);\n if (!allow.has(name)) {\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('not found');\n return;\n }\n\n const p = file(name);\n const txt = readOrNull(p);\n if (txt === null) {\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('missing');\n return;\n }\n\n const ct = name.endsWith('.json') ? 'application/json; charset=utf-8' : 'text/plain; charset=utf-8';\n res.writeHead(200, { 'content-type': ct });\n res.end(txt);\n return;\n }\n\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('not found');\n });\n\n await new Promise<void>((resolve, reject) => {\n server.once('error', reject);\n server.listen(port, host, () => resolve());\n });\n\n console.log(`OK: dashboard http://${host}:${port}/`);\n console.log('Tip: run `aiopt guard ...` and `aiopt scan` to populate aiopt-output files.');\n\n // keep alive\n await new Promise(() => {});\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { RateTable } from './types';\n\nexport function loadRateTableFromDistPath(): RateTable {\n // Works both in src/tsup and dist runtime.\n const p = path.join(__dirname, '..', 'rates', 'rate_table.json');\n return JSON.parse(fs.readFileSync(p, 'utf8')) as RateTable;\n}\n","import fs from 'fs';\nimport path from 'path';\nimport { analyze } from './scan';\nimport { loadRateTableFromDistPath } from './rates-util';\nimport { runGuard } from './guard';\n\nexport function seedDemoUsage(outDir: string) {\n fs.mkdirSync(outDir, { recursive: true });\n const p = path.join(outDir, 'usage.jsonl');\n const now = Date.now();\n const lines: any[] = [];\n for (let i = 0; i < 60; i++) {\n lines.push({\n ts: new Date(now - i * 60_000).toISOString(),\n provider: 'openai',\n model: i % 4 === 0 ? 'gpt-5.2' : 'gpt-5-mini',\n endpoint: 'responses',\n attempt: 1,\n trace_id: 'demo-' + i,\n status: 'ok',\n prompt_tokens: 10_000 + (i % 10) * 1000,\n completion_tokens: 1200 + (i % 5) * 200,\n meta: { feature_tag: i % 2 ? 'summarize' : 'coding' }\n });\n }\n fs.writeFileSync(p, lines.map(x => JSON.stringify(x)).join('\\n') + '\\n');\n return p;\n}\n\nexport function runQuickstart(cwd: string, opts: { port: number; budgetMonthlyUsd?: number }) {\n const outDir = path.join(cwd, 'aiopt-output');\n const usagePath = seedDemoUsage(outDir);\n\n const rt = loadRateTableFromDistPath();\n // Use the same normalizer as CLI scan/guard.\n const { readJsonl } = require('./io');\n const events = readJsonl(usagePath);\n const { analysis, savings, policy, meta } = analyze(rt, events);\n\n // write outputs (re-use scan writer by requiring dist is heavy; keep minimal here)\n fs.writeFileSync(path.join(outDir, 'analysis.json'), JSON.stringify(analysis, null, 2));\n fs.writeFileSync(path.join(outDir, 'report.json'), JSON.stringify({\n version: 3,\n generated_at: new Date().toISOString(),\n confidence: analysis.unknown_models?.length ? 'MEDIUM' : 'HIGH',\n warnings: [],\n assumptions: { quickstart: true },\n summary: {\n total_cost_usd: analysis.total_cost,\n estimated_savings_usd: savings.estimated_savings_total,\n routing_savings_usd: savings.estimated_savings_routing,\n context_savings_usd: savings.estimated_savings_context,\n retry_waste_usd: savings.retry_waste\n },\n top: {\n by_model: analysis.by_model_top,\n by_feature: analysis.by_feature_top\n },\n unknown_models: analysis.unknown_models || [],\n notes: []\n }, null, 2));\n fs.writeFileSync(path.join(outDir, 'cost-policy.json'), JSON.stringify(policy, null, 2));\n fs.writeFileSync(path.join(outDir, 'report.md'), '# AIOpt quickstart demo\\n\\nThis is a demo report generated by `aiopt quickstart --demo`.\\n');\n\n // Run guard on the same log with traffic spike to show WARN\n const r = runGuard(rt, {\n baselineEvents: events,\n candidate: {\n contextMultiplier: 1.2,\n callMultiplier: 5,\n budgetMonthlyUsd: opts.budgetMonthlyUsd\n }\n });\n\n return { usagePath, outDir, guard: r, port: opts.port };\n}\n","#!/usr/bin/env node\n\nimport fs from 'fs';\nimport path from 'path';\nimport { Command } from 'commander';\nimport { ensureDir, isCsvPath, readCsv, readJsonl } from './io';\nimport { RateTable } from './types';\nimport { analyze, writeOutputs } from './scan';\n\nconst program = new Command();\n\nconst DEFAULT_INPUT = './aiopt-output/usage.jsonl';\nconst DEFAULT_OUTPUT_DIR = './aiopt-output';\n\nfunction loadRateTable(): RateTable {\n const p = path.join(__dirname, '..', 'rates', 'rate_table.json');\n return JSON.parse(fs.readFileSync(p, 'utf8'));\n}\n\nprogram\n .name('aiopt')\n .description('AI 비용 자동 절감 인프라 — 서버 없는 로컬 CLI MVP')\n // keep CLI version in sync with package.json\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n .version(require('../package.json').version);\n\nprogram\n .command('init')\n .description('aiopt-input/ 및 샘플 usage.jsonl, aiopt-output/ 생성')\n .action(() => {\n ensureDir('./aiopt-input');\n ensureDir('./aiopt-output');\n\n const sampleSrc = path.join(__dirname, '..', 'samples', 'sample_usage.jsonl');\n const dst = path.join('./aiopt-input', 'usage.jsonl');\n\n if (!fs.existsSync(dst)) {\n fs.copyFileSync(sampleSrc, dst);\n console.log('Created ./aiopt-input/usage.jsonl (sample)');\n } else {\n console.log('Exists ./aiopt-input/usage.jsonl (skip)');\n }\n\n console.log('Ready: ./aiopt-output/');\n });\n\nprogram\n .command('scan')\n .description('입력 로그(JSONL/CSV)를 분석하고 report.md/report.json + patches까지 생성')\n .option('--input <path>', 'input file path (default: ./aiopt-output/usage.jsonl)', DEFAULT_INPUT)\n .option('--out <dir>', 'output dir (default: ./aiopt-output)', DEFAULT_OUTPUT_DIR)\n .action(async (opts) => {\n const inputPath = String(opts.input);\n const outDir = String(opts.out);\n\n if (!fs.existsSync(inputPath)) {\n console.error(`Input not found: ${inputPath}`);\n process.exit(1);\n }\n\n const rt = loadRateTable();\n const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);\n\n const { analysis, savings, policy, meta } = analyze(rt, events);\n // For reproducibility: embed input path & rate table meta\n policy.generated_from.input = inputPath;\n\n writeOutputs(outDir, analysis, savings, policy, meta);\n\n // Console: Top Fix 3 (data-driven)\n const { buildTopFixes } = await import('./solutions');\n const fixes = buildTopFixes(analysis, savings).slice(0, 3);\n\n console.log('Top Fix 3:');\n fixes.forEach((f, i) => {\n const tag = f.status === 'no-issue' ? '(no issue detected)' : `($${Math.round(f.impact_usd * 100) / 100})`;\n console.log(`${i + 1}) ${f.title} ${tag}`);\n });\n console.log(`Report: ${path.join(outDir, 'report.md')}`);\n });\n\nprogram\n .command('policy')\n .description('마지막 scan 결과 기반으로 cost-policy.json만 재생성 (MVP: scan과 동일 로직)')\n .option('--input <path>', 'input file path (default: ./aiopt-input/usage.jsonl)', DEFAULT_INPUT)\n .option('--out <dir>', 'output dir (default: ./aiopt-output)', DEFAULT_OUTPUT_DIR)\n .action((opts) => {\n const inputPath = String(opts.input);\n const outDir = String(opts.out);\n const rt = loadRateTable();\n const events = isCsvPath(inputPath) ? readCsv(inputPath) : readJsonl(inputPath);\n const { policy } = analyze(rt, events);\n policy.generated_from.input = inputPath;\n\n ensureDir(outDir);\n fs.writeFileSync(path.join(outDir, 'cost-policy.json'), JSON.stringify(policy, null, 2));\n console.log(`OK: ${outDir}/cost-policy.json`);\n });\n\n// v0.2: install/doctor (no servers)\nprogram\n .command('install')\n .description('Install AIOpt guardrails: create aiopt/ + policies + usage.jsonl')\n .option('--force', 'overwrite existing files')\n .option('--seed-sample', 'seed 1 sample line into aiopt-output/usage.jsonl')\n .action(async (opts) => {\n const { runInstall } = await import('./install');\n const result = runInstall(process.cwd(), { force: Boolean(opts.force), seedSample: Boolean(opts.seedSample) });\n for (const c of result.created) {\n console.log(`${c.status === 'created' ? 'CREATED' : 'SKIP'}: ${c.path}`);\n }\n });\n\nprogram\n .command('doctor')\n .description('Check installation + print last 5 usage events')\n .action(async () => {\n const { runDoctor } = await import('./doctor');\n const r = runDoctor(process.cwd());\n console.log(r.ok ? 'OK: doctor' : 'WARN: doctor');\n for (const c of r.checks) {\n console.log(`${c.ok ? 'OK' : 'FAIL'}: ${c.name}${c.detail ? ` (${c.detail})` : ''}`);\n }\n console.log('--- last5 usage');\n for (const x of r.last5) {\n console.log(JSON.stringify(x));\n }\n });\n\n// Offline license (no servers)\nconst licenseCmd = program\n .command('license')\n .description('Offline license activate/verify (public key only; no server calls)');\n\nlicenseCmd\n .command('activate')\n .argument('<KEY>', 'license key (<payloadB64Url>.<sigB64Url>)')\n .option('--out <path>', 'output license.json path (default: ./aiopt/license.json)')\n .action(async (key, opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, verifyLicenseKey, writeLicenseFile } = await import('./license');\n const outPath = opts.out ? String(opts.out) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n const v = verifyLicenseKey(String(key), pub);\n if (!v.payload) {\n console.error(`FAIL: ${v.reason || 'invalid license'}`);\n process.exit(3);\n }\n writeLicenseFile(outPath, String(key), v.payload, v.ok);\n console.log(v.ok ? `OK: activated (${outPath})` : `WARN: saved but not verified (${v.reason}) (${outPath})`);\n process.exit(v.ok ? 0 : 2);\n });\n\nlicenseCmd\n .command('verify')\n .option('--path <path>', 'license.json path (default: ./aiopt/license.json)')\n .action(async (opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, readLicenseFile, verifyLicenseKey } = await import('./license');\n const p = opts.path ? String(opts.path) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n if (!fs.existsSync(p)) {\n console.error(`FAIL: license file not found: ${p}`);\n process.exit(3);\n }\n const f = readLicenseFile(p);\n const v = verifyLicenseKey(f.key, pub);\n if (v.ok) {\n console.log('OK: license verified');\n process.exit(0);\n }\n console.error(`FAIL: license invalid (${v.reason || 'unknown'})`);\n process.exit(3);\n });\n\nlicenseCmd\n .command('status')\n .option('--path <path>', 'license.json path (default: ./aiopt/license.json)')\n .action(async (opts) => {\n const { DEFAULT_PUBLIC_KEY_PEM, defaultLicensePath, readLicenseFile, verifyLicenseKey } = await import('./license');\n const p = opts.path ? String(opts.path) : defaultLicensePath(process.cwd());\n const pub = process.env.AIOPT_LICENSE_PUBKEY || DEFAULT_PUBLIC_KEY_PEM;\n if (!fs.existsSync(p)) {\n console.log('NO_LICENSE');\n process.exit(2);\n }\n const f = readLicenseFile(p);\n const v = verifyLicenseKey(f.key, pub);\n if (v.ok) {\n console.log(`OK: ${f.payload.plan} exp=${f.payload.exp}`);\n process.exit(0);\n }\n console.log(`INVALID: ${v.reason || 'unknown'}`);\n process.exit(3);\n });\n\n// vNext: guardrail mode (pre-deploy warning)\nprogram\n .command('guard')\n .description('Pre-deploy guardrail: compare baseline usage vs candidate change (or diff two log sets) and print warnings (exit codes 0/2/3)')\n .option('--input <path>', 'baseline usage jsonl/csv (legacy alias for --baseline; default: ./aiopt-output/usage.jsonl)', DEFAULT_INPUT)\n .option('--baseline <path>', 'baseline usage jsonl/csv (diff mode when used with --candidate)')\n .option('--candidate <path>', 'candidate usage jsonl/csv (diff mode: compare two real log sets)')\n .option('--provider <provider>', 'candidate provider override (transform mode only)')\n .option('--model <model>', 'candidate model override (transform mode only)')\n .option('--context-mult <n>', 'multiply input_tokens by n (transform mode only)', (v) => Number(v))\n .option('--output-mult <n>', 'multiply output_tokens by n (transform mode only)', (v) => Number(v))\n .option('--retries-delta <n>', 'add n to retries (transform mode only)', (v) => Number(v))\n .option('--call-mult <n>', 'multiply call volume by n (traffic spike)', (v) => Number(v))\n .option('--budget-monthly <usd>', 'fail if estimated candidate monthly cost exceeds this budget', (v) => Number(v))\n .action(async (opts) => {\n const rt = loadRateTable();\n\n const baselinePath = String(opts.baseline || opts.input);\n const candidatePath = opts.candidate ? String(opts.candidate) : null;\n\n const diffMode = Boolean(opts.baseline || opts.candidate);\n if (diffMode && (!opts.baseline || !opts.candidate)) {\n console.error('FAIL: diff mode requires both --baseline and --candidate');\n process.exit(3);\n }\n\n if (!fs.existsSync(baselinePath)) {\n console.error(`FAIL: baseline not found: ${baselinePath}`);\n process.exit(3);\n }\n if (candidatePath && !fs.existsSync(candidatePath)) {\n console.error(`FAIL: candidate not found: ${candidatePath}`);\n process.exit(3);\n }\n\n const baselineEvents = isCsvPath(baselinePath) ? readCsv(baselinePath) : readJsonl(baselinePath);\n const candidateEvents = candidatePath\n ? (isCsvPath(candidatePath) ? readCsv(candidatePath) : readJsonl(candidatePath))\n : undefined;\n\n const { runGuard } = await import('./guard');\n const r = runGuard(rt, {\n baselineEvents,\n candidateEvents,\n candidate: {\n provider: opts.provider,\n model: opts.model,\n contextMultiplier: opts.contextMult,\n outputMultiplier: opts.outputMult,\n retriesDelta: opts.retriesDelta,\n callMultiplier: opts.callMult,\n budgetMonthlyUsd: opts.budgetMonthly\n }\n });\n\n console.log(r.message);\n\n // Persist last guard output + history for local dashboard / CI attachments\n try {\n const outDir = path.resolve(DEFAULT_OUTPUT_DIR);\n fs.mkdirSync(outDir, { recursive: true });\n const ts = new Date().toISOString();\n fs.writeFileSync(path.join(outDir, 'guard-last.txt'), r.message);\n fs.writeFileSync(path.join(outDir, 'guard-last.json'), JSON.stringify({ ts, exitCode: r.exitCode }, null, 2));\n const histLine = JSON.stringify({ ts, exitCode: r.exitCode, mode: candidateEvents ? 'diff' : 'transform', baseline: baselinePath, candidate: candidatePath }) + '\\n';\n fs.appendFileSync(path.join(outDir, 'guard-history.jsonl'), histLine);\n } catch {\n // ignore\n }\n\n process.exit(r.exitCode);\n });\n\n// Local-only dashboard (no auth; binds to 127.0.0.1)\nprogram\n .command('dashboard')\n .description('Local dashboard (localhost only): view last guard + last scan outputs')\n .option('--port <n>', 'port (default: 3010)', (v) => Number(v), 3010)\n .action(async (opts) => {\n const { startDashboard } = await import('./dashboard');\n await startDashboard(process.cwd(), { port: Number(opts.port || 3010) });\n });\n\nprogram\n .command('quickstart')\n .description('1-minute demo: generate sample usage, run scan+guard, and print dashboard URL')\n .option('--demo', 'run demo workflow (writes to ./aiopt-output)')\n .option('--port <n>', 'dashboard port (default: 3010)', (v) => Number(v), 3010)\n .option('--budget-monthly <usd>', 'optional budget gate for the demo guard', (v) => Number(v))\n .action(async (opts) => {\n if (!opts.demo) {\n console.error('FAIL: quickstart requires --demo');\n process.exit(3);\n }\n const { runQuickstart } = await import('./quickstart');\n const r = runQuickstart(process.cwd(), { port: Number(opts.port || 3010), budgetMonthlyUsd: opts.budgetMonthly });\n console.log('OK: demo usage written:', r.usagePath);\n console.log('--- guard ---');\n console.log(r.guard.message);\n // persist guard-last + append guard history for dashboard\n try {\n const fs = await import('fs');\n const path = await import('path');\n fs.mkdirSync(r.outDir, { recursive: true });\n const ts = new Date().toISOString();\n fs.writeFileSync(path.join(r.outDir, 'guard-last.txt'), r.guard.message);\n fs.writeFileSync(path.join(r.outDir, 'guard-last.json'), JSON.stringify({ ts, exitCode: r.guard.exitCode }, null, 2));\n const histLine = JSON.stringify({ ts, exitCode: r.guard.exitCode, mode: 'quickstart', baseline: r.usagePath, candidate: null }) + '\\n';\n fs.appendFileSync(path.join(r.outDir, 'guard-history.jsonl'), histLine);\n } catch {}\n console.log('--- next ---');\n console.log(`Run: npx aiopt dashboard --port ${r.port}`);\n console.log(`Open: http://127.0.0.1:${r.port}/`);\n process.exit(r.guard.exitCode);\n });\n\nprogram.parse(process.argv);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,SAAS,UAAU,GAAW;AACnC,YAAAA,QAAG,UAAU,GAAG,EAAE,WAAW,KAAK,CAAC;AACrC;AAEO,SAAS,UAAU,UAAgC;AACxD,QAAM,MAAM,UAAAA,QAAG,aAAa,UAAU,MAAM;AAC5C,QAAM,QAAQ,IAAI,MAAM,OAAO,EAAE,OAAO,OAAK,EAAE,KAAK,EAAE,SAAS,CAAC;AAChE,QAAM,MAAoB,CAAC;AAC3B,aAAW,QAAQ,OAAO;AACxB,UAAM,MAAM,KAAK,MAAM,IAAI;AAC3B,QAAI,KAAK,eAAe,GAAG,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAEO,SAAS,QAAQ,UAAgC;AACtD,QAAM,MAAM,UAAAA,QAAG,aAAa,UAAU,MAAM;AAC5C,QAAM,cAAU,YAAAC,OAAS,KAAK,EAAE,SAAS,MAAM,kBAAkB,MAAM,MAAM,KAAK,CAAC;AACnF,SAAO,QAAQ,IAAI,CAAC,MAAW,eAAe,CAAC,CAAC;AAClD;AAEA,SAAS,MAAM,GAAQ,MAAM,GAAW;AACtC,QAAM,IAAI,OAAO,CAAC;AAClB,SAAO,OAAO,SAAS,CAA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,MAAM,CAAC,CAAC;AACpH,UAAM,WAAW,KAAK,UAAU,EAAE,IAAI,UAAU,EAAE,MAAM,UAAU,MAAM,cAAc,UAAU,EAAE,WAAW,WAAW,KAAK,CAAC,IAAI;AAClI,IAAAC,KAAG,eAAeD,OAAK,KAAK,EAAE,QAAQ,qBAAqB,GAAG,QAAQ;AAAA,EACxE,QAAQ;AAAA,EAAC;AACT,UAAQ,IAAI,cAAc;AAC1B,UAAQ,IAAI,mCAAmC,EAAE,IAAI,EAAE;AACvD,UAAQ,IAAI,0BAA0B,EAAE,IAAI,GAAG;AAC/C,UAAQ,KAAK,EAAE,MAAM,QAAQ;AAC/B,CAAC;AAEH,QAAQ,MAAM,QAAQ,IAAI;","names":["fs","parseCsv","path","cost","path","fs","import_fs","import_path","round2","fs","path","import_fs","import_path","exports","module","ensureDir","fs","path","import_fs","import_path","fs","path","import_fs","import_path","crypto","path","fs","import_fs","import_path","round2","conf","msg","path","fs","http","import_fs","import_path","path","fs","import_fs","import_path","fs","path","readJsonl","import_fs","import_path","import_fs","import_path","path","fs","buildTopFixes","runInstall","runDoctor","DEFAULT_PUBLIC_KEY_PEM","defaultLicensePath","verifyLicenseKey","writeLicenseFile","readLicenseFile","runGuard","startDashboard","runQuickstart"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiopt",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Pre-deploy LLM cost accident guardrail (serverless local CLI)",
5
5
  "bin": {
6
6
  "aiopt": "dist/cli.js"