@aisdlc/core 4.3.0 → 4.3.1

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/dist/index.cjs CHANGED
@@ -13855,6 +13855,554 @@ function verifyPrivacy(options = {}) {
13855
13855
  writePrivacyOutputsIfNeeded(result, options, policy, rootDir);
13856
13856
  return result;
13857
13857
  }
13858
+ var TRAILER_REGEXES = {
13859
+ taskId: /^\s*Task-ID:\s*(.+)$/im,
13860
+ parentRef: /^\s*Parent-Ref:\s*(.+)$/im,
13861
+ authorType: /^\s*Author-Type:\s*(human|agent)/im,
13862
+ aiModel: /^\s*AI-Model:\s*(.+)$/im,
13863
+ promptTokens: /^\s*Prompt-Tokens:\s*([0-9]+)/im,
13864
+ completionTokens: /^\s*Completion-Tokens:\s*([0-9]+)/im,
13865
+ activeTimeSeconds: /^\s*Active-Time-Seconds:\s*([0-9]+)/im
13866
+ };
13867
+ function parseTrailerInt(match) {
13868
+ if (!match) return 0;
13869
+ const val = parseInt(match[1], 10);
13870
+ return isNaN(val) ? 0 : val;
13871
+ }
13872
+ function resolveAuthorType(authorTypeMatch, aiModelMatch) {
13873
+ if (authorTypeMatch) {
13874
+ return authorTypeMatch[1].toLowerCase();
13875
+ }
13876
+ if (aiModelMatch && aiModelMatch[1].trim() !== "n/a") {
13877
+ return "agent";
13878
+ }
13879
+ return "human";
13880
+ }
13881
+ function resolveTrailerOrScope(match, text, fallbackRegex) {
13882
+ if (match) return match[1].trim();
13883
+ const subMatch = text.match(fallbackRegex);
13884
+ return subMatch ? subMatch[1].toUpperCase() : void 0;
13885
+ }
13886
+ function parseCommitTrailers(rawMessage) {
13887
+ const text = rawMessage || "";
13888
+ const authorTypeMatch = text.match(TRAILER_REGEXES.authorType);
13889
+ const aiModelMatch = text.match(TRAILER_REGEXES.aiModel);
13890
+ const authorType = resolveAuthorType(authorTypeMatch, aiModelMatch);
13891
+ const rawModel = aiModelMatch ? aiModelMatch[1].trim() : void 0;
13892
+ const aiModel = rawModel && rawModel !== "n/a" ? rawModel : void 0;
13893
+ return {
13894
+ taskId: resolveTrailerOrScope(text.match(TRAILER_REGEXES.taskId), text, /\b(TSK-[0-9A-Za-z_-]+)\b/i),
13895
+ parentRef: resolveTrailerOrScope(text.match(TRAILER_REGEXES.parentRef), text, /\b((?:CHG|FEAT|BUG|FIX|PATCH)-[0-9A-Za-z_-]+)\b/i),
13896
+ authorType,
13897
+ aiModel,
13898
+ promptTokens: parseTrailerInt(text.match(TRAILER_REGEXES.promptTokens)),
13899
+ completionTokens: parseTrailerInt(text.match(TRAILER_REGEXES.completionTokens)),
13900
+ activeTimeSeconds: parseTrailerInt(text.match(TRAILER_REGEXES.activeTimeSeconds))
13901
+ };
13902
+ }
13903
+ function formatCommitTrailers(trailers) {
13904
+ const lines = [];
13905
+ if (trailers.taskId) lines.push(`Task-ID: ${trailers.taskId}`);
13906
+ if (trailers.parentRef) lines.push(`Parent-Ref: ${trailers.parentRef}`);
13907
+ lines.push(`Author-Type: ${trailers.authorType || "human"}`);
13908
+ lines.push(`AI-Model: ${trailers.aiModel || "n/a"}`);
13909
+ lines.push(`Prompt-Tokens: ${trailers.promptTokens ?? 0}`);
13910
+ lines.push(`Completion-Tokens: ${trailers.completionTokens ?? 0}`);
13911
+ lines.push(`Active-Time-Seconds: ${trailers.activeTimeSeconds ?? 0}`);
13912
+ return lines.join("\n");
13913
+ }
13914
+ function parseRawLogChunks(rawLog) {
13915
+ const rawCommits = rawLog.split("---AI_SDLC_COMMIT---").filter((c) => c.trim().length > 0);
13916
+ const chunks = [];
13917
+ for (const raw of rawCommits) {
13918
+ const endBodyIdx = raw.indexOf("---AI_SDLC_BODY_END---");
13919
+ if (endBodyIdx === -1) continue;
13920
+ const beforeEnd = raw.slice(0, endBodyIdx).trimStart();
13921
+ const afterEnd = raw.slice(endBodyIdx + "---AI_SDLC_BODY_END---".length);
13922
+ const firstNewline = beforeEnd.indexOf("\n");
13923
+ const header = firstNewline === -1 ? beforeEnd : beforeEnd.slice(0, firstNewline);
13924
+ const body = firstNewline === -1 ? "" : beforeEnd.slice(firstNewline + 1);
13925
+ const numstatLines = afterEnd.split("\n").filter((l) => /^\d+\s+\d+\s+/.test(l.trim()));
13926
+ chunks.push({ header, body, numstatLines });
13927
+ }
13928
+ return chunks;
13929
+ }
13930
+ function sumNumstatLines(lines) {
13931
+ let added = 0;
13932
+ let deleted = 0;
13933
+ const files = [];
13934
+ for (const line of lines) {
13935
+ const parts = line.trim().split(/\s+/);
13936
+ if (parts.length >= 2) {
13937
+ const a = parseInt(parts[0], 10);
13938
+ const d = parseInt(parts[1], 10);
13939
+ if (!isNaN(a)) added += a;
13940
+ if (!isNaN(d)) deleted += d;
13941
+ if (parts.length >= 3) files.push(parts.slice(2).join(" "));
13942
+ }
13943
+ }
13944
+ return { added, deleted, files };
13945
+ }
13946
+ function evaluateIsBugFix(subject, parentRef) {
13947
+ if (parentRef && /^(BUG|FIX)-/i.test(parentRef)) return true;
13948
+ return /^(fix|bug)(\([^)]+\))?:/i.test(subject.trim());
13949
+ }
13950
+ function inferAgentTrailers(trailers, linesAdded, linesDeleted) {
13951
+ const res = { ...trailers };
13952
+ const linesChanged = linesAdded + linesDeleted;
13953
+ let estimated = false;
13954
+ if (res.promptTokens === 0 && res.completionTokens === 0) {
13955
+ res.completionTokens = linesChanged > 0 ? Math.max(linesAdded * 10, 50) : 0;
13956
+ res.promptTokens = linesChanged > 0 ? Math.max(2500 + linesChanged * 25, 3e3) : 1e3;
13957
+ estimated = true;
13958
+ }
13959
+ if (res.activeTimeSeconds === 0) {
13960
+ res.activeTimeSeconds = linesChanged > 0 ? Math.round(30 + linesChanged * 0.5) : 30;
13961
+ estimated = true;
13962
+ }
13963
+ res.isTelemetryEstimated = estimated;
13964
+ return res;
13965
+ }
13966
+ function inferHumanTrailers(trailers, linesAdded, linesDeleted) {
13967
+ const res = { ...trailers };
13968
+ const linesChanged = linesAdded + linesDeleted;
13969
+ let estimated = false;
13970
+ if (res.activeTimeSeconds === 0) {
13971
+ res.activeTimeSeconds = linesChanged > 0 ? Math.round(180 + linesChanged * 2) : 120;
13972
+ estimated = true;
13973
+ }
13974
+ res.isTelemetryEstimated = estimated;
13975
+ return res;
13976
+ }
13977
+ function inferMissingTrailers(trailers, linesAdded, linesDeleted) {
13978
+ return trailers.authorType === "agent" ? inferAgentTrailers(trailers, linesAdded, linesDeleted) : inferHumanTrailers(trailers, linesAdded, linesDeleted);
13979
+ }
13980
+ function resolveGitRef(cwd, ref) {
13981
+ if (!ref || ref === "HEAD") return ref;
13982
+ const candidates = [
13983
+ ref,
13984
+ `feat/${ref}`,
13985
+ `release/${ref}`,
13986
+ `bug/${ref}`,
13987
+ `task/${ref}`,
13988
+ `origin/${ref}`,
13989
+ `origin/feat/${ref}`,
13990
+ `origin/release/${ref}`,
13991
+ `origin/bug/${ref}`
13992
+ ];
13993
+ for (const cand of candidates) {
13994
+ try {
13995
+ child_process3.execFileSync("git", ["rev-parse", "--verify", "--quiet", cand], {
13996
+ cwd,
13997
+ stdio: ["pipe", "pipe", "pipe"]
13998
+ });
13999
+ return cand;
14000
+ } catch {
14001
+ }
14002
+ }
14003
+ return ref;
14004
+ }
14005
+ function fetchGitLogOutput(cwd, baseRef, headRef, format) {
14006
+ const rangeArg = baseRef ? `${baseRef}..${headRef}` : headRef;
14007
+ try {
14008
+ return child_process3.execFileSync("git", ["log", rangeArg, `--format=${format}`, "--numstat"], {
14009
+ cwd,
14010
+ encoding: "utf-8",
14011
+ stdio: ["pipe", "pipe", "pipe"]
14012
+ });
14013
+ } catch {
14014
+ if (baseRef && !baseRef.includes("/")) {
14015
+ try {
14016
+ return child_process3.execFileSync("git", ["log", `origin/${baseRef}..${headRef}`, `--format=${format}`, `--numstat`], {
14017
+ cwd,
14018
+ encoding: "utf-8",
14019
+ stdio: ["pipe", "pipe", "pipe"]
14020
+ });
14021
+ } catch {
14022
+ return "";
14023
+ }
14024
+ }
14025
+ return "";
14026
+ }
14027
+ }
14028
+ function buildCommitKpiRecord(chunk) {
14029
+ const fields = chunk.header.split("");
14030
+ if (fields.length < 5) return null;
14031
+ const rawTrailers = parseCommitTrailers(chunk.body);
14032
+ const { added, deleted, files } = sumNumstatLines(chunk.numstatLines);
14033
+ const trailers = inferMissingTrailers(rawTrailers, added, deleted);
14034
+ return {
14035
+ sha: fields[0],
14036
+ authorName: fields[1],
14037
+ authorEmail: fields[2],
14038
+ date: fields[3],
14039
+ subject: fields[4],
14040
+ trailers,
14041
+ linesAdded: added,
14042
+ linesDeleted: deleted,
14043
+ isBugFix: evaluateIsBugFix(fields[4], trailers.parentRef),
14044
+ touchedFiles: files
14045
+ };
14046
+ }
14047
+ function extractCommitKpisFromRange(baseRef, headRef = "HEAD", options = {}) {
14048
+ const cwd = options.cwd || process.cwd();
14049
+ const resolvedBase = resolveGitRef(cwd, baseRef);
14050
+ const resolvedHead = resolveGitRef(cwd, headRef);
14051
+ const format = "---AI_SDLC_COMMIT---%n%H%x1f%an%x1f%ae%x1f%aI%x1f%s%n%B%n---AI_SDLC_BODY_END---";
14052
+ const rawOutput = fetchGitLogOutput(cwd, resolvedBase, resolvedHead, format);
14053
+ if (!rawOutput) return [];
14054
+ const chunks = parseRawLogChunks(rawOutput);
14055
+ const records = [];
14056
+ for (const chunk of chunks) {
14057
+ const rec = buildCommitKpiRecord(chunk);
14058
+ if (rec) records.push(rec);
14059
+ }
14060
+ return records;
14061
+ }
14062
+ function extractAllRepoCommitKpis(options = {}) {
14063
+ const cwd = options.cwd || process.cwd();
14064
+ const format = "---AI_SDLC_COMMIT---%n%H%x1f%an%x1f%ae%x1f%aI%x1f%s%n%B%n---AI_SDLC_BODY_END---";
14065
+ let rawOutput = "";
14066
+ try {
14067
+ rawOutput = child_process3.execFileSync("git", ["log", "--all", `--format=${format}`, "--numstat"], {
14068
+ cwd,
14069
+ encoding: "utf-8",
14070
+ stdio: ["pipe", "pipe", "pipe"]
14071
+ });
14072
+ } catch {
14073
+ return [];
14074
+ }
14075
+ if (!rawOutput) return [];
14076
+ const chunks = parseRawLogChunks(rawOutput);
14077
+ const records = [];
14078
+ for (const chunk of chunks) {
14079
+ const rec = buildCommitKpiRecord(chunk);
14080
+ if (rec) records.push(rec);
14081
+ }
14082
+ return records;
14083
+ }
14084
+
14085
+ // src/reporters/pr-kpis.ts
14086
+ var KPI_SUMMARY_START_TAG = "<!-- AI-SDLC-KPI-SUMMARY-START -->";
14087
+ var KPI_SUMMARY_END_TAG = "<!-- AI-SDLC-KPI-SUMMARY-END -->";
14088
+ function calculateEstimatedCost(promptTokens, completionTokens) {
14089
+ const promptCost = promptTokens / 1e6 * 3;
14090
+ const completionCost = completionTokens / 1e6 * 15;
14091
+ return Math.round((promptCost + completionCost) * 1e4) / 1e4;
14092
+ }
14093
+ function createEmptySummary() {
14094
+ return {
14095
+ commits: 0,
14096
+ linesAdded: 0,
14097
+ linesDeleted: 0,
14098
+ activeTimeSeconds: 0,
14099
+ promptTokens: 0,
14100
+ completionTokens: 0,
14101
+ estimatedCostUsd: 0
14102
+ };
14103
+ }
14104
+ function accumulateRecord(target, record) {
14105
+ target.commits += 1;
14106
+ target.linesAdded += record.linesAdded;
14107
+ target.linesDeleted += record.linesDeleted;
14108
+ target.activeTimeSeconds += record.trailers.activeTimeSeconds;
14109
+ target.promptTokens += record.trailers.promptTokens;
14110
+ target.completionTokens += record.trailers.completionTokens;
14111
+ target.estimatedCostUsd = calculateEstimatedCost(target.promptTokens, target.completionTokens);
14112
+ }
14113
+ function accumulateAuthorSummaries(records, humanSummary, byModel) {
14114
+ let linesAdded = 0;
14115
+ let linesDeleted = 0;
14116
+ let activeTime = 0;
14117
+ let prompt = 0;
14118
+ let completion = 0;
14119
+ let isBugPr = false;
14120
+ for (const record of records) {
14121
+ linesAdded += record.linesAdded;
14122
+ linesDeleted += record.linesDeleted;
14123
+ activeTime += record.trailers.activeTimeSeconds;
14124
+ prompt += record.trailers.promptTokens;
14125
+ completion += record.trailers.completionTokens;
14126
+ if (record.isBugFix) isBugPr = true;
14127
+ if (record.trailers.authorType === "agent") {
14128
+ const modelName = record.trailers.aiModel || "agent-generic";
14129
+ if (!byModel[modelName]) byModel[modelName] = createEmptySummary();
14130
+ accumulateRecord(byModel[modelName], record);
14131
+ } else {
14132
+ accumulateRecord(humanSummary, record);
14133
+ }
14134
+ }
14135
+ return { linesAdded, linesDeleted, activeTime, prompt, completion, isBugPr };
14136
+ }
14137
+ function initTaskKpi(taskId) {
14138
+ return {
14139
+ taskId,
14140
+ commits: 0,
14141
+ fixCommits: 0,
14142
+ linesAdded: 0,
14143
+ linesDeleted: 0,
14144
+ codeChurnPercent: 0,
14145
+ totalTokens: 0,
14146
+ activeTimeSeconds: 0,
14147
+ reworkTokens: 0,
14148
+ reworkTokensPercent: 0,
14149
+ reworkTimeSeconds: 0,
14150
+ reworkTimePercent: 0,
14151
+ isFirstTimeRight: true
14152
+ };
14153
+ }
14154
+ function processTaskCommit(tStat, record) {
14155
+ const recTokens = record.trailers.promptTokens + record.trailers.completionTokens;
14156
+ tStat.commits += 1;
14157
+ tStat.linesAdded += record.linesAdded;
14158
+ tStat.linesDeleted += record.linesDeleted;
14159
+ tStat.totalTokens += recTokens;
14160
+ tStat.activeTimeSeconds += record.trailers.activeTimeSeconds;
14161
+ const isRework = record.isBugFix || /^(fix|bug|remediate|refactor)(([^)]+))?:/i.test((record.subject || "").trim());
14162
+ if (isRework) {
14163
+ tStat.fixCommits += 1;
14164
+ tStat.reworkTokens += recTokens;
14165
+ tStat.reworkTimeSeconds += record.trailers.activeTimeSeconds;
14166
+ }
14167
+ }
14168
+ function accumulateTaskSummaries(records) {
14169
+ const byTask = {};
14170
+ for (const record of records) {
14171
+ const tid = record.trailers.taskId || record.subject.match(/\b(TSK-[0-9A-Za-z_-]+)\b/i)?.[1]?.toUpperCase();
14172
+ if (!tid) continue;
14173
+ if (!byTask[tid]) byTask[tid] = initTaskKpi(tid);
14174
+ processTaskCommit(byTask[tid], record);
14175
+ }
14176
+ for (const t of Object.values(byTask)) {
14177
+ const churn = t.linesAdded > 0 ? t.linesDeleted / t.linesAdded * 100 : 0;
14178
+ t.codeChurnPercent = Math.round(Math.min(churn, 100) * 10) / 10;
14179
+ t.reworkTokensPercent = t.totalTokens > 0 ? Math.round(t.reworkTokens / t.totalTokens * 1e3) / 10 : 0;
14180
+ t.reworkTimePercent = t.activeTimeSeconds > 0 ? Math.round(t.reworkTimeSeconds / t.activeTimeSeconds * 1e3) / 10 : 0;
14181
+ t.isFirstTimeRight = t.fixCommits === 0 && t.codeChurnPercent < 25;
14182
+ }
14183
+ return byTask;
14184
+ }
14185
+ function assemblePrKpiAggregation(baseRef, headRef, records, totals, humanSummary, byModel, byTask) {
14186
+ const totalTokens = totals.prompt + totals.completion;
14187
+ return {
14188
+ baseRef,
14189
+ headRef,
14190
+ totalCommits: records.length,
14191
+ linesAdded: totals.linesAdded,
14192
+ linesDeleted: totals.linesDeleted,
14193
+ totalActiveTimeSeconds: totals.activeTime,
14194
+ totalPromptTokens: totals.prompt,
14195
+ totalCompletionTokens: totals.completion,
14196
+ totalTokens,
14197
+ estimatedCostUsd: calculateEstimatedCost(totals.prompt, totals.completion),
14198
+ humanSummary,
14199
+ byModel,
14200
+ isBugPr: totals.isBugPr,
14201
+ commitRecords: records,
14202
+ byTask
14203
+ };
14204
+ }
14205
+ function aggregateCommitKpiRecords(records, baseRef = "main", headRef = "HEAD") {
14206
+ const humanSummary = createEmptySummary();
14207
+ const byModel = {};
14208
+ const totals = accumulateAuthorSummaries(records, humanSummary, byModel);
14209
+ const byTask = accumulateTaskSummaries(records);
14210
+ return assemblePrKpiAggregation(baseRef, headRef, records, totals, humanSummary, byModel, byTask);
14211
+ }
14212
+ function aggregatePrKpis(baseRef, headRef = "HEAD", options = {}) {
14213
+ const records = extractCommitKpisFromRange(baseRef, headRef, options);
14214
+ return aggregateCommitKpiRecords(records, baseRef, headRef);
14215
+ }
14216
+ function formatMinutes(seconds) {
14217
+ const mins = Math.round(seconds / 60);
14218
+ return mins > 0 ? `${mins} min` : `${seconds}s`;
14219
+ }
14220
+ function buildPrKpiRows(kpi, models) {
14221
+ const human = kpi.humanSummary;
14222
+ const commitCols = models.map((m) => kpi.byModel[m].commits).join(" | ");
14223
+ const linesCols = models.map((m) => `+${kpi.byModel[m].linesAdded} / -${kpi.byModel[m].linesDeleted}`).join(" | ");
14224
+ const timeCols = models.map((m) => formatMinutes(kpi.byModel[m].activeTimeSeconds)).join(" | ");
14225
+ const tokenCols = models.map((m) => `${(kpi.byModel[m].promptTokens + kpi.byModel[m].completionTokens).toLocaleString("en-US")}`).join(" | ");
14226
+ const costCols = models.map((m) => `$${kpi.byModel[m].estimatedCostUsd.toFixed(2)}`).join(" | ");
14227
+ const totalLines = `+${kpi.linesAdded} / -${kpi.linesDeleted}`;
14228
+ const totalTime = formatMinutes(kpi.totalActiveTimeSeconds);
14229
+ const totalTokensStr = `${kpi.totalTokens.toLocaleString("en-US")} tokens`;
14230
+ const totalCostStr = `$${kpi.estimatedCostUsd.toFixed(2)} USD`;
14231
+ return [
14232
+ `| **Commits** | ${human.commits} |${models.length > 0 ? ` ${commitCols} |` : ""} **${kpi.totalCommits}** |`,
14233
+ `| **Lines Added / Deleted** | +${human.linesAdded} / -${human.linesDeleted} |${models.length > 0 ? ` ${linesCols} |` : ""} **${totalLines}** |`,
14234
+ `| **Active Development Time** | ${formatMinutes(human.activeTimeSeconds)} |${models.length > 0 ? ` ${timeCols} |` : ""} **${totalTime}** |`,
14235
+ `| **Tokens Consumed (In+Out)** | 0 |${models.length > 0 ? ` ${tokenCols} |` : ""} **${totalTokensStr}** |`,
14236
+ `| **Estimated Cost (USD)** | \u2014 (Human labor) |${models.length > 0 ? ` ${costCols} |` : ""} **${totalCostStr}** |`
14237
+ ];
14238
+ }
14239
+ function formatTaskKpiRows(byTask) {
14240
+ const taskIds = Object.keys(byTask || {});
14241
+ if (taskIds.length === 0) return [];
14242
+ const rows = [
14243
+ "",
14244
+ "#### \u{1F4CB} Task-Level Rework & Code Churn Breakdown",
14245
+ "",
14246
+ "| Task ID | Commits (Total / Fix) | Lines (+ / -) | Code Churn % | Rework Tokens % | Rework Time % | Status |",
14247
+ "| :--- | :---: | :---: | :---: | :---: | :---: | :---: |"
14248
+ ];
14249
+ for (const tid of taskIds) {
14250
+ const t = byTask[tid];
14251
+ const statusLabel = t.isFirstTimeRight ? "\u2714 First-Time-Right" : `\u26A0 ${t.fixCommits} rework commit${t.fixCommits === 1 ? "" : "s"}`;
14252
+ rows.push(
14253
+ `| \`${t.taskId}\` | ${t.commits} (${t.fixCommits} fix) | +${t.linesAdded} / -${t.linesDeleted} | ${t.codeChurnPercent}% | ${t.reworkTokensPercent}% | ${t.reworkTimePercent}% | ${statusLabel} |`
14254
+ );
14255
+ }
14256
+ return rows;
14257
+ }
14258
+ function formatPrKpiMarkdown(kpi) {
14259
+ const models = Object.keys(kpi.byModel);
14260
+ const modelHeaders = models.map((m) => `Agent (${m})`).join(" | ");
14261
+ const modelSeparator = models.map(() => ":---:").join(" | ");
14262
+ const headers = `| Metric | Human |${models.length > 0 ? ` ${modelHeaders} |` : ""} Total PR |`;
14263
+ const sep5 = `| :--- | :---: |${models.length > 0 ? ` ${modelSeparator} |` : ""} :---: |`;
14264
+ const rows = buildPrKpiRows(kpi, models);
14265
+ const taskRowsList = formatTaskKpiRows(kpi.byTask);
14266
+ return [
14267
+ KPI_SUMMARY_START_TAG,
14268
+ "### \u{1F4CA} AI-SDLC: Aggregated PR KPI Summary",
14269
+ "",
14270
+ headers,
14271
+ sep5,
14272
+ ...rows,
14273
+ ...taskRowsList,
14274
+ "",
14275
+ `> **Branch Attribution:** ${kpi.isBugPr ? "\u{1F6A8} *Defect Fix / Bugfix*" : "\u2728 *New Feature*"} (Range: \`${kpi.baseRef}..${kpi.headRef}\`)`,
14276
+ KPI_SUMMARY_END_TAG
14277
+ ].join("\n");
14278
+ }
14279
+ function injectKpisIntoPrBody(originalBody, kpiMarkdown) {
14280
+ const body = originalBody || "";
14281
+ const startIndex = body.indexOf(KPI_SUMMARY_START_TAG);
14282
+ const endIndex = body.indexOf(KPI_SUMMARY_END_TAG);
14283
+ if (startIndex !== -1 && endIndex !== -1) {
14284
+ const before = body.slice(0, startIndex);
14285
+ const after = body.slice(endIndex + KPI_SUMMARY_END_TAG.length);
14286
+ return `${before.trimEnd()}
14287
+
14288
+ ${kpiMarkdown}
14289
+
14290
+ ${after.trimStart()}`.trim();
14291
+ }
14292
+ return `${body.trimEnd()}
14293
+
14294
+ ${kpiMarkdown}
14295
+ `;
14296
+ }
14297
+
14298
+ // src/reporters/agent-kpis.ts
14299
+ function roundOneDecimal(value) {
14300
+ return Math.round(value * 10) / 10;
14301
+ }
14302
+ function computeAuditedCommitsPercent(records) {
14303
+ if (records.length === 0) return 0;
14304
+ const audited = records.filter((r) => r.trailers.isTelemetryEstimated === false).length;
14305
+ return roundOneDecimal(audited / records.length * 100);
14306
+ }
14307
+ function computeTaskYieldAndRework(records) {
14308
+ const tasks = Object.values(accumulateTaskSummaries(records));
14309
+ if (tasks.length === 0) {
14310
+ return { totalTasks: 0, firstPassYieldPercent: 100, reworkLoopsPerTask: 0 };
14311
+ }
14312
+ const firstPassCount = tasks.filter((t) => t.isFirstTimeRight).length;
14313
+ const totalReworkLoops = tasks.reduce((sum, t) => sum + t.fixCommits, 0);
14314
+ return {
14315
+ totalTasks: tasks.length,
14316
+ firstPassYieldPercent: roundOneDecimal(firstPassCount / tasks.length * 100),
14317
+ reworkLoopsPerTask: Math.round(totalReworkLoops / tasks.length * 100) / 100
14318
+ };
14319
+ }
14320
+ function resolveTaskKey(record) {
14321
+ return record.trailers.taskId || record.subject.match(/\b(TSK-[0-9A-Za-z_-]+)\b/i)?.[1]?.toUpperCase();
14322
+ }
14323
+ function computeHumanTakeoverRate(records) {
14324
+ const byTask = /* @__PURE__ */ new Map();
14325
+ for (const r of records) {
14326
+ const tid = resolveTaskKey(r);
14327
+ if (!tid) continue;
14328
+ const state = byTask.get(tid) || { hasAgent: false, hasHumanTakeover: false };
14329
+ if (r.trailers.authorType === "agent") {
14330
+ state.hasAgent = true;
14331
+ } else if (state.hasAgent || r.isBugFix) {
14332
+ state.hasHumanTakeover = true;
14333
+ }
14334
+ byTask.set(tid, state);
14335
+ }
14336
+ const agentTasks = Array.from(byTask.values()).filter((s) => s.hasAgent);
14337
+ if (agentTasks.length === 0) return 0;
14338
+ const takeovers = agentTasks.filter((s) => s.hasHumanTakeover).length;
14339
+ return roundOneDecimal(takeovers / agentTasks.length * 100);
14340
+ }
14341
+ function computeSpecificationAdherence(input) {
14342
+ const cov = input.coverageResult;
14343
+ const trace = input.traceabilityResult;
14344
+ const gate = input.gateResult;
14345
+ const bddRatio = cov && cov.totalRequirements > 0 ? cov.passedRequirements / cov.totalRequirements : 1;
14346
+ const traceRatio = trace && trace.totalRequirements > 0 ? (trace.totalRequirements - trace.orphanCount) / trace.totalRequirements : 1;
14347
+ const gateRatio = gate && gate.totalFunctions > 0 ? (gate.totalFunctions - gate.failCount) / gate.totalFunctions : 1;
14348
+ return roundOneDecimal((bddRatio + traceRatio + gateRatio) / 3 * 100);
14349
+ }
14350
+ function computeTokenCostEfficiency(records, gateResult) {
14351
+ if (!gateResult || gateResult.results.length === 0) return 0;
14352
+ const totalCc = gateResult.results.reduce((sum, fn) => sum + fn.cyclomatic, 0);
14353
+ if (totalCc <= 0) return 0;
14354
+ const totalTokens = records.reduce(
14355
+ (sum, r) => sum + r.trailers.promptTokens + r.trailers.completionTokens,
14356
+ 0
14357
+ );
14358
+ return Math.round(totalTokens / totalCc);
14359
+ }
14360
+ function isComponentContractPath(filePath) {
14361
+ const normalized = filePath.replace(/\\/g, "/").toLowerCase();
14362
+ return normalized.includes("specs/architecture/components/") || /\/?cmp-[^/]+\.md$/i.test(normalized);
14363
+ }
14364
+ function computeArchitectureDriftRate(records) {
14365
+ const cmpCommits = records.filter((r) => (r.touchedFiles || []).some(isComponentContractPath));
14366
+ if (cmpCommits.length === 0) return 0;
14367
+ const unauthorized = cmpCommits.filter((r) => {
14368
+ const ref = r.trailers.parentRef || "";
14369
+ const hasApprovedRef = /^(CHG|ADR)-/i.test(ref) || Boolean(r.trailers.taskId);
14370
+ return !hasApprovedRef;
14371
+ });
14372
+ return roundOneDecimal(unauthorized.length / cmpCommits.length * 100);
14373
+ }
14374
+ function computeAgentPerformanceKpis(input) {
14375
+ const { totalTasks, firstPassYieldPercent, reworkLoopsPerTask } = computeTaskYieldAndRework(input.records);
14376
+ return {
14377
+ totalCommits: input.records.length,
14378
+ totalTasks,
14379
+ auditedCommitsPercent: computeAuditedCommitsPercent(input.records),
14380
+ firstPassYieldPercent,
14381
+ reworkLoopsPerTask,
14382
+ specificationAdherenceIndex: computeSpecificationAdherence(input),
14383
+ humanTakeoverRatePercent: computeHumanTakeoverRate(input.records),
14384
+ tokenCostEfficiency: computeTokenCostEfficiency(input.records, input.gateResult),
14385
+ architectureDriftRatePercent: computeArchitectureDriftRate(input.records)
14386
+ };
14387
+ }
14388
+ function formatAgentKpisMarkdownSection(kpis) {
14389
+ return [
14390
+ "## 2. AI Agent Performance KPIs (SWE-bench & DORA AI)",
14391
+ "",
14392
+ `> **Evaluated Tasks:** \`${kpis.totalTasks}\` | **Commits:** \`${kpis.totalCommits}\` | **Audited Trailer Coverage:** \`${kpis.auditedCommitsPercent}%\``,
14393
+ "",
14394
+ "| KPI | Measured Value | Benchmark / Target |",
14395
+ "| :--- | :---: | :---: |",
14396
+ `| **First-Pass Yield (FPY)** | \`${kpis.firstPassYieldPercent}%\` | $\\ge 80\\%$ |`,
14397
+ `| **Rework Loops per Task** | \`${kpis.reworkLoopsPerTask}\` | $\\le 0.5$ |`,
14398
+ `| **Specification Adherence Index** | \`${kpis.specificationAdherenceIndex}%\` | $\\ge 95\\%$ |`,
14399
+ `| **Human Takeover / Intervention Rate** | \`${kpis.humanTakeoverRatePercent}%\` | $\\le 15\\%$ |`,
14400
+ `| **Token Cost Efficiency** | \`${kpis.tokenCostEfficiency.toLocaleString("en-US")} tokens/CC\` | Lower is better |`,
14401
+ `| **Architecture Drift Rate** | \`${kpis.architectureDriftRatePercent}%\` | $0\\%$ |`
14402
+ ];
14403
+ }
14404
+
14405
+ // src/reporters/quality-report.ts
13858
14406
  function calculateRating(mi, cc) {
13859
14407
  if (mi >= 75 && cc <= 5) return "A";
13860
14408
  if (mi >= 60 && cc <= 10) return "B";
@@ -14003,19 +14551,39 @@ function buildExecutiveTable(gateResult, totalLoc, averages, coverage) {
14003
14551
  `| **Functions in Violation** | \`${failCount}\` | $0$ (Mode ${policy.enforce_mode}) | ${status.violStatus} |`
14004
14552
  ];
14005
14553
  }
14006
- function renderQualityReportMarkdown(gateResult, coverage, accumulated, averages) {
14007
- const verdict = gateResult.success ? "PASS" : "FAIL";
14008
- const header = buildReportHeader3(verdict, averages.globalRating, averages.avgMaintainability, averages.avgCyclomatic);
14009
- const table = buildExecutiveTable(gateResult, accumulated.totalLoc, averages, coverage);
14010
- const polyglot = [
14011
- "## 2. Polyglot Breakdown by Language Ecosystem",
14554
+ function buildPolyglotSection(langMap) {
14555
+ return [
14556
+ "## 3. Polyglot Breakdown by Language Ecosystem",
14012
14557
  "",
14013
14558
  "| Language | Functions | Total LOC | Average MI | Average CC | Rating |",
14014
14559
  "| :--- | :---: | :---: | :---: | :---: | :---: |",
14015
- ...renderPolyglotRows(accumulated.langMap)
14560
+ ...renderPolyglotRows(langMap)
14561
+ ];
14562
+ }
14563
+ function renderQualityReportMarkdown(gateResult, coverage, accumulated, averages, agentKpis) {
14564
+ const verdict = gateResult.success ? "PASS" : "FAIL";
14565
+ const sections = [
14566
+ buildReportHeader3(verdict, averages.globalRating, averages.avgMaintainability, averages.avgCyclomatic),
14567
+ buildExecutiveTable(gateResult, accumulated.totalLoc, averages, coverage),
14568
+ formatAgentKpisMarkdownSection(agentKpis),
14569
+ buildPolyglotSection(accumulated.langMap)
14016
14570
  ];
14017
- const violationsSection = accumulated.violationsList.length > 0 ? ["", "---", "", "## 3. Quality Gate Violations Log", "", ...accumulated.violationsList] : [];
14018
- return [...header, "", "---", "", ...table, "", "---", "", ...polyglot, ...violationsSection, "", EVALUATION_STANDARDS_MD].join("\n");
14571
+ if (accumulated.violationsList.length > 0) {
14572
+ sections.push(["## 4. Quality Gate Violations Log", "", ...accumulated.violationsList]);
14573
+ }
14574
+ return [...sections.map((s) => s.join("\n")), EVALUATION_STANDARDS_MD].join("\n\n---\n\n");
14575
+ }
14576
+ function collectAgentKpisForReport(rootDir, gateResult) {
14577
+ const records = extractAllRepoCommitKpis({ cwd: rootDir });
14578
+ const hasSpecs = fs12__namespace.existsSync(path14__namespace.join(rootDir, "specs"));
14579
+ const coverageResult = hasSpecs ? verifyTestingCoverage({ rootDir }) : void 0;
14580
+ const traceabilityResult = hasSpecs ? verifyTraceability({ rootDir }) : void 0;
14581
+ return computeAgentPerformanceKpis({
14582
+ records,
14583
+ gateResult,
14584
+ coverageResult,
14585
+ traceabilityResult
14586
+ });
14019
14587
  }
14020
14588
  function writeQualityReportFile(rootDir, markdown) {
14021
14589
  const targetPath = path14__namespace.join(rootDir, "reports", "QUALITY_REPORT.md");
@@ -14031,7 +14599,8 @@ function generateQualityReport(options = {}) {
14031
14599
  const coverage = readCodeCoverageSummary(rootDir);
14032
14600
  const accumulated = accumulateMetrics(gateResult.results);
14033
14601
  const averages = computeQualityAverages(gateResult.totalFunctions, accumulated);
14034
- const markdown = renderQualityReportMarkdown(gateResult, coverage, accumulated, averages);
14602
+ const agentKpis = collectAgentKpisForReport(rootDir, gateResult);
14603
+ const markdown = renderQualityReportMarkdown(gateResult, coverage, accumulated, averages, agentKpis);
14035
14604
  writeQualityReportFile(rootDir, markdown);
14036
14605
  return {
14037
14606
  markdown,
@@ -14380,243 +14949,27 @@ function buildCheckoutSuccessResult(match, hierarchy, created, switchedBranch, s
14380
14949
  taskId: match.taskId,
14381
14950
  changeId: match.changeId,
14382
14951
  releaseBranch: hierarchy.releaseBranch,
14383
- featureBranch: hierarchy.featureBranch,
14384
- taskBranch: streamlined ? void 0 : hierarchy.taskBranch,
14385
- createdBranches: created,
14386
- switchedBranch
14387
- };
14388
- }
14389
- function checkoutTaskBranch(taskId, options = {}) {
14390
- const rootDir = options.rootDir || process.cwd();
14391
- const locateRes = locateTaskInActiveChanges(taskId, rootDir);
14392
- if (!locateRes.found || !locateRes.match) {
14393
- return buildLocateFailureResult(taskId, locateRes);
14394
- }
14395
- const match = locateRes.match;
14396
- const hierarchy = resolveBranchHierarchy(match, rootDir);
14397
- const created = [];
14398
- try {
14399
- const switched = cascadeBranchCheckout(hierarchy, rootDir, created, options.streamlined);
14400
- return buildCheckoutSuccessResult(match, hierarchy, created, switched, options.streamlined);
14401
- } catch (err) {
14402
- return buildCheckoutErrorResult(match, hierarchy, created, err);
14403
- }
14404
- }
14405
- var TRAILER_REGEXES = {
14406
- taskId: /^\s*Task-ID:\s*(.+)$/im,
14407
- parentRef: /^\s*Parent-Ref:\s*(.+)$/im,
14408
- authorType: /^\s*Author-Type:\s*(human|agent)/im,
14409
- aiModel: /^\s*AI-Model:\s*(.+)$/im,
14410
- promptTokens: /^\s*Prompt-Tokens:\s*([0-9]+)/im,
14411
- completionTokens: /^\s*Completion-Tokens:\s*([0-9]+)/im,
14412
- activeTimeSeconds: /^\s*Active-Time-Seconds:\s*([0-9]+)/im
14413
- };
14414
- function parseTrailerInt(match) {
14415
- if (!match) return 0;
14416
- const val = parseInt(match[1], 10);
14417
- return isNaN(val) ? 0 : val;
14418
- }
14419
- function resolveAuthorType(authorTypeMatch, aiModelMatch) {
14420
- if (authorTypeMatch) {
14421
- return authorTypeMatch[1].toLowerCase();
14422
- }
14423
- if (aiModelMatch && aiModelMatch[1].trim() !== "n/a") {
14424
- return "agent";
14425
- }
14426
- return "human";
14427
- }
14428
- function resolveTrailerOrScope(match, text, fallbackRegex) {
14429
- if (match) return match[1].trim();
14430
- const subMatch = text.match(fallbackRegex);
14431
- return subMatch ? subMatch[1].toUpperCase() : void 0;
14432
- }
14433
- function parseCommitTrailers(rawMessage) {
14434
- const text = rawMessage || "";
14435
- const authorTypeMatch = text.match(TRAILER_REGEXES.authorType);
14436
- const aiModelMatch = text.match(TRAILER_REGEXES.aiModel);
14437
- const authorType = resolveAuthorType(authorTypeMatch, aiModelMatch);
14438
- const rawModel = aiModelMatch ? aiModelMatch[1].trim() : void 0;
14439
- const aiModel = rawModel && rawModel !== "n/a" ? rawModel : void 0;
14440
- return {
14441
- taskId: resolveTrailerOrScope(text.match(TRAILER_REGEXES.taskId), text, /\b(TSK-[0-9A-Za-z_-]+)\b/i),
14442
- parentRef: resolveTrailerOrScope(text.match(TRAILER_REGEXES.parentRef), text, /\b((?:CHG|FEAT|BUG|FIX|PATCH)-[0-9A-Za-z_-]+)\b/i),
14443
- authorType,
14444
- aiModel,
14445
- promptTokens: parseTrailerInt(text.match(TRAILER_REGEXES.promptTokens)),
14446
- completionTokens: parseTrailerInt(text.match(TRAILER_REGEXES.completionTokens)),
14447
- activeTimeSeconds: parseTrailerInt(text.match(TRAILER_REGEXES.activeTimeSeconds))
14448
- };
14449
- }
14450
- function formatCommitTrailers(trailers) {
14451
- const lines = [];
14452
- if (trailers.taskId) lines.push(`Task-ID: ${trailers.taskId}`);
14453
- if (trailers.parentRef) lines.push(`Parent-Ref: ${trailers.parentRef}`);
14454
- lines.push(`Author-Type: ${trailers.authorType || "human"}`);
14455
- lines.push(`AI-Model: ${trailers.aiModel || "n/a"}`);
14456
- lines.push(`Prompt-Tokens: ${trailers.promptTokens ?? 0}`);
14457
- lines.push(`Completion-Tokens: ${trailers.completionTokens ?? 0}`);
14458
- lines.push(`Active-Time-Seconds: ${trailers.activeTimeSeconds ?? 0}`);
14459
- return lines.join("\n");
14460
- }
14461
- function parseRawLogChunks(rawLog) {
14462
- const rawCommits = rawLog.split("---AI_SDLC_COMMIT---").filter((c) => c.trim().length > 0);
14463
- const chunks = [];
14464
- for (const raw of rawCommits) {
14465
- const endBodyIdx = raw.indexOf("---AI_SDLC_BODY_END---");
14466
- if (endBodyIdx === -1) continue;
14467
- const beforeEnd = raw.slice(0, endBodyIdx).trimStart();
14468
- const afterEnd = raw.slice(endBodyIdx + "---AI_SDLC_BODY_END---".length);
14469
- const firstNewline = beforeEnd.indexOf("\n");
14470
- const header = firstNewline === -1 ? beforeEnd : beforeEnd.slice(0, firstNewline);
14471
- const body = firstNewline === -1 ? "" : beforeEnd.slice(firstNewline + 1);
14472
- const numstatLines = afterEnd.split("\n").filter((l) => /^\d+\s+\d+\s+/.test(l.trim()));
14473
- chunks.push({ header, body, numstatLines });
14474
- }
14475
- return chunks;
14476
- }
14477
- function sumNumstatLines(lines) {
14478
- let added = 0;
14479
- let deleted = 0;
14480
- for (const line of lines) {
14481
- const parts = line.trim().split(/\s+/);
14482
- if (parts.length >= 2) {
14483
- const a = parseInt(parts[0], 10);
14484
- const d = parseInt(parts[1], 10);
14485
- if (!isNaN(a)) added += a;
14486
- if (!isNaN(d)) deleted += d;
14487
- }
14488
- }
14489
- return { added, deleted };
14490
- }
14491
- function evaluateIsBugFix(subject, parentRef) {
14492
- if (parentRef && /^(BUG|FIX)-/i.test(parentRef)) return true;
14493
- return /^(fix|bug)(\([^)]+\))?:/i.test(subject.trim());
14494
- }
14495
- function inferAgentTrailers(trailers, linesAdded, linesDeleted) {
14496
- const res = { ...trailers };
14497
- const linesChanged = linesAdded + linesDeleted;
14498
- if (res.promptTokens === 0 && res.completionTokens === 0) {
14499
- res.completionTokens = linesChanged > 0 ? Math.max(linesAdded * 10, 50) : 0;
14500
- res.promptTokens = linesChanged > 0 ? Math.max(2500 + linesChanged * 25, 3e3) : 1e3;
14501
- }
14502
- if (res.activeTimeSeconds === 0) {
14503
- res.activeTimeSeconds = linesChanged > 0 ? Math.round(30 + linesChanged * 0.5) : 30;
14504
- }
14505
- return res;
14506
- }
14507
- function inferHumanTrailers(trailers, linesAdded, linesDeleted) {
14508
- const res = { ...trailers };
14509
- const linesChanged = linesAdded + linesDeleted;
14510
- if (res.activeTimeSeconds === 0) {
14511
- res.activeTimeSeconds = linesChanged > 0 ? Math.round(180 + linesChanged * 2) : 120;
14512
- }
14513
- return res;
14514
- }
14515
- function inferMissingTrailers(trailers, linesAdded, linesDeleted) {
14516
- return trailers.authorType === "agent" ? inferAgentTrailers(trailers, linesAdded, linesDeleted) : inferHumanTrailers(trailers, linesAdded, linesDeleted);
14517
- }
14518
- function resolveGitRef(cwd, ref) {
14519
- if (!ref || ref === "HEAD") return ref;
14520
- const candidates = [
14521
- ref,
14522
- `feat/${ref}`,
14523
- `release/${ref}`,
14524
- `bug/${ref}`,
14525
- `task/${ref}`,
14526
- `origin/${ref}`,
14527
- `origin/feat/${ref}`,
14528
- `origin/release/${ref}`,
14529
- `origin/bug/${ref}`
14530
- ];
14531
- for (const cand of candidates) {
14532
- try {
14533
- child_process3.execFileSync("git", ["rev-parse", "--verify", "--quiet", cand], {
14534
- cwd,
14535
- stdio: ["pipe", "pipe", "pipe"]
14536
- });
14537
- return cand;
14538
- } catch {
14539
- }
14540
- }
14541
- return ref;
14542
- }
14543
- function fetchGitLogOutput(cwd, baseRef, headRef, format) {
14544
- const rangeArg = baseRef ? `${baseRef}..${headRef}` : headRef;
14545
- try {
14546
- return child_process3.execFileSync("git", ["log", rangeArg, `--format=${format}`, "--numstat"], {
14547
- cwd,
14548
- encoding: "utf-8",
14549
- stdio: ["pipe", "pipe", "pipe"]
14550
- });
14551
- } catch {
14552
- if (baseRef && !baseRef.includes("/")) {
14553
- try {
14554
- return child_process3.execFileSync("git", ["log", `origin/${baseRef}..${headRef}`, `--format=${format}`, `--numstat`], {
14555
- cwd,
14556
- encoding: "utf-8",
14557
- stdio: ["pipe", "pipe", "pipe"]
14558
- });
14559
- } catch {
14560
- return "";
14561
- }
14562
- }
14563
- return "";
14564
- }
14565
- }
14566
- function buildCommitKpiRecord(chunk) {
14567
- const fields = chunk.header.split("");
14568
- if (fields.length < 5) return null;
14569
- const rawTrailers = parseCommitTrailers(chunk.body);
14570
- const { added, deleted } = sumNumstatLines(chunk.numstatLines);
14571
- const trailers = inferMissingTrailers(rawTrailers, added, deleted);
14572
- return {
14573
- sha: fields[0],
14574
- authorName: fields[1],
14575
- authorEmail: fields[2],
14576
- date: fields[3],
14577
- subject: fields[4],
14578
- trailers,
14579
- linesAdded: added,
14580
- linesDeleted: deleted,
14581
- isBugFix: evaluateIsBugFix(fields[4], trailers.parentRef)
14582
- };
14583
- }
14584
- function extractCommitKpisFromRange(baseRef, headRef = "HEAD", options = {}) {
14585
- const cwd = options.cwd || process.cwd();
14586
- const resolvedBase = resolveGitRef(cwd, baseRef);
14587
- const resolvedHead = resolveGitRef(cwd, headRef);
14588
- const format = "---AI_SDLC_COMMIT---%n%H%x1f%an%x1f%ae%x1f%aI%x1f%s%n%B%n---AI_SDLC_BODY_END---";
14589
- const rawOutput = fetchGitLogOutput(cwd, resolvedBase, resolvedHead, format);
14590
- if (!rawOutput) return [];
14591
- const chunks = parseRawLogChunks(rawOutput);
14592
- const records = [];
14593
- for (const chunk of chunks) {
14594
- const rec = buildCommitKpiRecord(chunk);
14595
- if (rec) records.push(rec);
14596
- }
14597
- return records;
14952
+ featureBranch: hierarchy.featureBranch,
14953
+ taskBranch: streamlined ? void 0 : hierarchy.taskBranch,
14954
+ createdBranches: created,
14955
+ switchedBranch
14956
+ };
14598
14957
  }
14599
- function extractAllRepoCommitKpis(options = {}) {
14600
- const cwd = options.cwd || process.cwd();
14601
- const format = "---AI_SDLC_COMMIT---%n%H%x1f%an%x1f%ae%x1f%aI%x1f%s%n%B%n---AI_SDLC_BODY_END---";
14602
- let rawOutput = "";
14603
- try {
14604
- rawOutput = child_process3.execFileSync("git", ["log", "--all", `--format=${format}`, "--numstat"], {
14605
- cwd,
14606
- encoding: "utf-8",
14607
- stdio: ["pipe", "pipe", "pipe"]
14608
- });
14609
- } catch {
14610
- return [];
14958
+ function checkoutTaskBranch(taskId, options = {}) {
14959
+ const rootDir = options.rootDir || process.cwd();
14960
+ const locateRes = locateTaskInActiveChanges(taskId, rootDir);
14961
+ if (!locateRes.found || !locateRes.match) {
14962
+ return buildLocateFailureResult(taskId, locateRes);
14611
14963
  }
14612
- if (!rawOutput) return [];
14613
- const chunks = parseRawLogChunks(rawOutput);
14614
- const records = [];
14615
- for (const chunk of chunks) {
14616
- const rec = buildCommitKpiRecord(chunk);
14617
- if (rec) records.push(rec);
14964
+ const match = locateRes.match;
14965
+ const hierarchy = resolveBranchHierarchy(match, rootDir);
14966
+ const created = [];
14967
+ try {
14968
+ const switched = cascadeBranchCheckout(hierarchy, rootDir, created, options.streamlined);
14969
+ return buildCheckoutSuccessResult(match, hierarchy, created, switched, options.streamlined);
14970
+ } catch (err) {
14971
+ return buildCheckoutErrorResult(match, hierarchy, created, err);
14618
14972
  }
14619
- return records;
14620
14973
  }
14621
14974
  var AGENT_ENV_MAPPINGS = [
14622
14975
  { envKey: "ANTIGRAVITY_AGENT", defaultModel: "gemini-3.8-flash", source: "antigravity" },
@@ -14963,219 +15316,6 @@ function installGitHooks(rootDir = process.cwd()) {
14963
15316
  };
14964
15317
  }
14965
15318
  }
14966
-
14967
- // src/reporters/pr-kpis.ts
14968
- var KPI_SUMMARY_START_TAG = "<!-- AI-SDLC-KPI-SUMMARY-START -->";
14969
- var KPI_SUMMARY_END_TAG = "<!-- AI-SDLC-KPI-SUMMARY-END -->";
14970
- function calculateEstimatedCost(promptTokens, completionTokens) {
14971
- const promptCost = promptTokens / 1e6 * 3;
14972
- const completionCost = completionTokens / 1e6 * 15;
14973
- return Math.round((promptCost + completionCost) * 1e4) / 1e4;
14974
- }
14975
- function createEmptySummary() {
14976
- return {
14977
- commits: 0,
14978
- linesAdded: 0,
14979
- linesDeleted: 0,
14980
- activeTimeSeconds: 0,
14981
- promptTokens: 0,
14982
- completionTokens: 0,
14983
- estimatedCostUsd: 0
14984
- };
14985
- }
14986
- function accumulateRecord(target, record) {
14987
- target.commits += 1;
14988
- target.linesAdded += record.linesAdded;
14989
- target.linesDeleted += record.linesDeleted;
14990
- target.activeTimeSeconds += record.trailers.activeTimeSeconds;
14991
- target.promptTokens += record.trailers.promptTokens;
14992
- target.completionTokens += record.trailers.completionTokens;
14993
- target.estimatedCostUsd = calculateEstimatedCost(target.promptTokens, target.completionTokens);
14994
- }
14995
- function accumulateAuthorSummaries(records, humanSummary, byModel) {
14996
- let linesAdded = 0;
14997
- let linesDeleted = 0;
14998
- let activeTime = 0;
14999
- let prompt = 0;
15000
- let completion = 0;
15001
- let isBugPr = false;
15002
- for (const record of records) {
15003
- linesAdded += record.linesAdded;
15004
- linesDeleted += record.linesDeleted;
15005
- activeTime += record.trailers.activeTimeSeconds;
15006
- prompt += record.trailers.promptTokens;
15007
- completion += record.trailers.completionTokens;
15008
- if (record.isBugFix) isBugPr = true;
15009
- if (record.trailers.authorType === "agent") {
15010
- const modelName = record.trailers.aiModel || "agent-generic";
15011
- if (!byModel[modelName]) byModel[modelName] = createEmptySummary();
15012
- accumulateRecord(byModel[modelName], record);
15013
- } else {
15014
- accumulateRecord(humanSummary, record);
15015
- }
15016
- }
15017
- return { linesAdded, linesDeleted, activeTime, prompt, completion, isBugPr };
15018
- }
15019
- function initTaskKpi(taskId) {
15020
- return {
15021
- taskId,
15022
- commits: 0,
15023
- fixCommits: 0,
15024
- linesAdded: 0,
15025
- linesDeleted: 0,
15026
- codeChurnPercent: 0,
15027
- totalTokens: 0,
15028
- activeTimeSeconds: 0,
15029
- reworkTokens: 0,
15030
- reworkTokensPercent: 0,
15031
- reworkTimeSeconds: 0,
15032
- reworkTimePercent: 0,
15033
- isFirstTimeRight: true
15034
- };
15035
- }
15036
- function processTaskCommit(tStat, record) {
15037
- const recTokens = record.trailers.promptTokens + record.trailers.completionTokens;
15038
- tStat.commits += 1;
15039
- tStat.linesAdded += record.linesAdded;
15040
- tStat.linesDeleted += record.linesDeleted;
15041
- tStat.totalTokens += recTokens;
15042
- tStat.activeTimeSeconds += record.trailers.activeTimeSeconds;
15043
- const isRework = record.isBugFix || /^(fix|bug|remediate|refactor)(([^)]+))?:/i.test((record.subject || "").trim());
15044
- if (isRework) {
15045
- tStat.fixCommits += 1;
15046
- tStat.reworkTokens += recTokens;
15047
- tStat.reworkTimeSeconds += record.trailers.activeTimeSeconds;
15048
- }
15049
- }
15050
- function accumulateTaskSummaries(records) {
15051
- const byTask = {};
15052
- for (const record of records) {
15053
- const tid = record.trailers.taskId || record.subject.match(/\b(TSK-[0-9A-Za-z_-]+)\b/i)?.[1]?.toUpperCase();
15054
- if (!tid) continue;
15055
- if (!byTask[tid]) byTask[tid] = initTaskKpi(tid);
15056
- processTaskCommit(byTask[tid], record);
15057
- }
15058
- for (const t of Object.values(byTask)) {
15059
- const churn = t.linesAdded > 0 ? t.linesDeleted / t.linesAdded * 100 : 0;
15060
- t.codeChurnPercent = Math.round(Math.min(churn, 100) * 10) / 10;
15061
- t.reworkTokensPercent = t.totalTokens > 0 ? Math.round(t.reworkTokens / t.totalTokens * 1e3) / 10 : 0;
15062
- t.reworkTimePercent = t.activeTimeSeconds > 0 ? Math.round(t.reworkTimeSeconds / t.activeTimeSeconds * 1e3) / 10 : 0;
15063
- t.isFirstTimeRight = t.fixCommits === 0 && t.codeChurnPercent < 25;
15064
- }
15065
- return byTask;
15066
- }
15067
- function assemblePrKpiAggregation(baseRef, headRef, records, totals, humanSummary, byModel, byTask) {
15068
- const totalTokens = totals.prompt + totals.completion;
15069
- return {
15070
- baseRef,
15071
- headRef,
15072
- totalCommits: records.length,
15073
- linesAdded: totals.linesAdded,
15074
- linesDeleted: totals.linesDeleted,
15075
- totalActiveTimeSeconds: totals.activeTime,
15076
- totalPromptTokens: totals.prompt,
15077
- totalCompletionTokens: totals.completion,
15078
- totalTokens,
15079
- estimatedCostUsd: calculateEstimatedCost(totals.prompt, totals.completion),
15080
- humanSummary,
15081
- byModel,
15082
- isBugPr: totals.isBugPr,
15083
- commitRecords: records,
15084
- byTask
15085
- };
15086
- }
15087
- function aggregateCommitKpiRecords(records, baseRef = "main", headRef = "HEAD") {
15088
- const humanSummary = createEmptySummary();
15089
- const byModel = {};
15090
- const totals = accumulateAuthorSummaries(records, humanSummary, byModel);
15091
- const byTask = accumulateTaskSummaries(records);
15092
- return assemblePrKpiAggregation(baseRef, headRef, records, totals, humanSummary, byModel, byTask);
15093
- }
15094
- function aggregatePrKpis(baseRef, headRef = "HEAD", options = {}) {
15095
- const records = extractCommitKpisFromRange(baseRef, headRef, options);
15096
- return aggregateCommitKpiRecords(records, baseRef, headRef);
15097
- }
15098
- function formatMinutes(seconds) {
15099
- const mins = Math.round(seconds / 60);
15100
- return mins > 0 ? `${mins} min` : `${seconds}s`;
15101
- }
15102
- function buildPrKpiRows(kpi, models) {
15103
- const human = kpi.humanSummary;
15104
- const commitCols = models.map((m) => kpi.byModel[m].commits).join(" | ");
15105
- const linesCols = models.map((m) => `+${kpi.byModel[m].linesAdded} / -${kpi.byModel[m].linesDeleted}`).join(" | ");
15106
- const timeCols = models.map((m) => formatMinutes(kpi.byModel[m].activeTimeSeconds)).join(" | ");
15107
- const tokenCols = models.map((m) => `${(kpi.byModel[m].promptTokens + kpi.byModel[m].completionTokens).toLocaleString("en-US")}`).join(" | ");
15108
- const costCols = models.map((m) => `$${kpi.byModel[m].estimatedCostUsd.toFixed(2)}`).join(" | ");
15109
- const totalLines = `+${kpi.linesAdded} / -${kpi.linesDeleted}`;
15110
- const totalTime = formatMinutes(kpi.totalActiveTimeSeconds);
15111
- const totalTokensStr = `${kpi.totalTokens.toLocaleString("en-US")} tokens`;
15112
- const totalCostStr = `$${kpi.estimatedCostUsd.toFixed(2)} USD`;
15113
- return [
15114
- `| **Commits** | ${human.commits} |${models.length > 0 ? ` ${commitCols} |` : ""} **${kpi.totalCommits}** |`,
15115
- `| **Lines Added / Deleted** | +${human.linesAdded} / -${human.linesDeleted} |${models.length > 0 ? ` ${linesCols} |` : ""} **${totalLines}** |`,
15116
- `| **Active Development Time** | ${formatMinutes(human.activeTimeSeconds)} |${models.length > 0 ? ` ${timeCols} |` : ""} **${totalTime}** |`,
15117
- `| **Tokens Consumed (In+Out)** | 0 |${models.length > 0 ? ` ${tokenCols} |` : ""} **${totalTokensStr}** |`,
15118
- `| **Estimated Cost (USD)** | \u2014 (Human labor) |${models.length > 0 ? ` ${costCols} |` : ""} **${totalCostStr}** |`
15119
- ];
15120
- }
15121
- function formatTaskKpiRows(byTask) {
15122
- const taskIds = Object.keys(byTask || {});
15123
- if (taskIds.length === 0) return [];
15124
- const rows = [
15125
- "",
15126
- "#### \u{1F4CB} Task-Level Rework & Code Churn Breakdown",
15127
- "",
15128
- "| Task ID | Commits (Total / Fix) | Lines (+ / -) | Code Churn % | Rework Tokens % | Rework Time % | Status |",
15129
- "| :--- | :---: | :---: | :---: | :---: | :---: | :---: |"
15130
- ];
15131
- for (const tid of taskIds) {
15132
- const t = byTask[tid];
15133
- const statusLabel = t.isFirstTimeRight ? "\u2714 First-Time-Right" : `\u26A0 ${t.fixCommits} rework commit${t.fixCommits === 1 ? "" : "s"}`;
15134
- rows.push(
15135
- `| \`${t.taskId}\` | ${t.commits} (${t.fixCommits} fix) | +${t.linesAdded} / -${t.linesDeleted} | ${t.codeChurnPercent}% | ${t.reworkTokensPercent}% | ${t.reworkTimePercent}% | ${statusLabel} |`
15136
- );
15137
- }
15138
- return rows;
15139
- }
15140
- function formatPrKpiMarkdown(kpi) {
15141
- const models = Object.keys(kpi.byModel);
15142
- const modelHeaders = models.map((m) => `Agent (${m})`).join(" | ");
15143
- const modelSeparator = models.map(() => ":---:").join(" | ");
15144
- const headers = `| Metric | Human |${models.length > 0 ? ` ${modelHeaders} |` : ""} Total PR |`;
15145
- const sep5 = `| :--- | :---: |${models.length > 0 ? ` ${modelSeparator} |` : ""} :---: |`;
15146
- const rows = buildPrKpiRows(kpi, models);
15147
- const taskRowsList = formatTaskKpiRows(kpi.byTask);
15148
- return [
15149
- KPI_SUMMARY_START_TAG,
15150
- "### \u{1F4CA} AI-SDLC: Aggregated PR KPI Summary",
15151
- "",
15152
- headers,
15153
- sep5,
15154
- ...rows,
15155
- ...taskRowsList,
15156
- "",
15157
- `> **Branch Attribution:** ${kpi.isBugPr ? "\u{1F6A8} *Defect Fix / Bugfix*" : "\u2728 *New Feature*"} (Range: \`${kpi.baseRef}..${kpi.headRef}\`)`,
15158
- KPI_SUMMARY_END_TAG
15159
- ].join("\n");
15160
- }
15161
- function injectKpisIntoPrBody(originalBody, kpiMarkdown) {
15162
- const body = originalBody || "";
15163
- const startIndex = body.indexOf(KPI_SUMMARY_START_TAG);
15164
- const endIndex = body.indexOf(KPI_SUMMARY_END_TAG);
15165
- if (startIndex !== -1 && endIndex !== -1) {
15166
- const before = body.slice(0, startIndex);
15167
- const after = body.slice(endIndex + KPI_SUMMARY_END_TAG.length);
15168
- return `${before.trimEnd()}
15169
-
15170
- ${kpiMarkdown}
15171
-
15172
- ${after.trimStart()}`.trim();
15173
- }
15174
- return `${body.trimEnd()}
15175
-
15176
- ${kpiMarkdown}
15177
- `;
15178
- }
15179
15319
  function calculateCost(prompt, completion) {
15180
15320
  const promptCost = prompt / 1e6 * 3;
15181
15321
  const completionCost = completion / 1e6 * 15;
@@ -17803,16 +17943,35 @@ function resolveBranchFilterKeys(metricsData) {
17803
17943
  }
17804
17944
  return keys;
17805
17945
  }
17946
+ function renderAgentKpisSectionHtml(kpis) {
17947
+ if (!kpis) return "";
17948
+ return [
17949
+ `<div class="chart-container" id="agent-performance-kpis">`,
17950
+ ` <h3>AI Agent Performance KPIs (SWE-bench &amp; DORA AI)</h3>`,
17951
+ ` <p style="color: var(--text-muted); font-size: 0.85rem; margin-bottom: 1rem;">Tasks: <strong>${kpis.totalTasks}</strong> | Commits: <strong>${kpis.totalCommits}</strong> | Audited Trailers: <strong>${kpis.auditedCommitsPercent}%</strong></p>`,
17952
+ ` <div class="grid-cards">`,
17953
+ ` <div class="metric-card"><span class="title">First-Pass Yield (FPY)</span><span class="value">${kpis.firstPassYieldPercent}%</span><span class="subtitle">Target &ge; 80%</span></div>`,
17954
+ ` <div class="metric-card"><span class="title">Rework Loops / Task</span><span class="value">${kpis.reworkLoopsPerTask}</span><span class="subtitle">Target &le; 0.5</span></div>`,
17955
+ ` <div class="metric-card"><span class="title">Spec Adherence Index</span><span class="value">${kpis.specificationAdherenceIndex}%</span><span class="subtitle">Target &ge; 95%</span></div>`,
17956
+ ` <div class="metric-card"><span class="title">Human Takeover Rate</span><span class="value">${kpis.humanTakeoverRatePercent}%</span><span class="subtitle">Target &le; 15%</span></div>`,
17957
+ ` <div class="metric-card"><span class="title">Token Cost Efficiency</span><span class="value">${kpis.tokenCostEfficiency.toLocaleString("en-US")}</span><span class="subtitle">tokens / CC</span></div>`,
17958
+ ` <div class="metric-card"><span class="title">Architecture Drift Rate</span><span class="value">${kpis.architectureDriftRatePercent}%</span><span class="subtitle">Target 0%</span></div>`,
17959
+ ` </div>`,
17960
+ `</div>`
17961
+ ].join("\n");
17962
+ }
17806
17963
  function renderKpisTabHtml(metricsData) {
17807
17964
  const displayKpi = metricsData.totalTelemetry || metricsData.activePrKpi;
17808
17965
  const branchKeys = resolveBranchFilterKeys(metricsData);
17809
17966
  const telemetryHtml = displayKpi ? renderActiveTelemetrySection(displayKpi, "Totalizador", branchKeys, metricsData.activeBranch) : '<p style="color: var(--text-muted); font-size: 0.85rem;">No telemetry recorded yet in this workspace.</p>';
17967
+ const agentKpisHtml = renderAgentKpisSectionHtml(metricsData.agentKpis);
17810
17968
  const historyContent = renderHistoricalKpisTable(metricsData.history);
17811
17969
  return [
17812
17970
  ` <div id="tab-kpis" class="tab-panel">`,
17813
17971
  ` <div class="content-container">`,
17814
17972
  ` <h2>Active Telemetry and KPI History</h2>`,
17815
17973
  telemetryHtml,
17974
+ agentKpisHtml,
17816
17975
  ` <div class="chart-container"><h3>Consolidated Release History</h3>${historyContent}</div>`,
17817
17976
  ` </div>`,
17818
17977
  ` </div>`
@@ -18174,6 +18333,12 @@ function collectDashboardMetrics(rootDir) {
18174
18333
  history,
18175
18334
  activePrKpi
18176
18335
  );
18336
+ const agentKpis = computeAgentPerformanceKpis({
18337
+ records: totalTelemetry?.commitRecords || [],
18338
+ gateResult: gate,
18339
+ coverageResult: cov,
18340
+ traceabilityResult: trace
18341
+ });
18177
18342
  return {
18178
18343
  gate,
18179
18344
  gov,
@@ -18186,6 +18351,7 @@ function collectDashboardMetrics(rootDir) {
18186
18351
  activeBranch,
18187
18352
  branchTelemetries,
18188
18353
  totalTelemetry,
18354
+ agentKpis,
18189
18355
  complianceBundle
18190
18356
  };
18191
18357
  }
@@ -24494,6 +24660,7 @@ exports.collectAgentFiles = collectAgentFiles;
24494
24660
  exports.collectDashboardMetrics = collectDashboardMetrics;
24495
24661
  exports.collectHistoricalKpis = collectHistoricalKpis;
24496
24662
  exports.compileTestPatterns = compileTestPatterns;
24663
+ exports.computeAgentPerformanceKpis = computeAgentPerformanceKpis;
24497
24664
  exports.computeCanonicalSha256 = computeCanonicalSha256;
24498
24665
  exports.computeLfSha256 = computeLfSha256;
24499
24666
  exports.computeRawSha256 = computeRawSha256;
@@ -24540,6 +24707,7 @@ exports.extractPythonFunctionsPolyglot = extractPythonFunctionsPolyglot;
24540
24707
  exports.extractSemanticContent = extractSemanticContent;
24541
24708
  exports.findChangeWorkspaceDir = findChangeWorkspaceDir;
24542
24709
  exports.findLicenseFile = findLicenseFile;
24710
+ exports.formatAgentKpisMarkdownSection = formatAgentKpisMarkdownSection;
24543
24711
  exports.formatCommitTrailers = formatCommitTrailers;
24544
24712
  exports.formatInspectResultMarkdown = formatInspectResultMarkdown;
24545
24713
  exports.formatPrKpiMarkdown = formatPrKpiMarkdown;