akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
87 lines (86 loc) • 4.1 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { binomialQuantile, dayRange, isoWeekStart } from "./stats.js";
/** Who made the decision. One recorded before `decidedBy` existed is "unknown" unless the gate auto-accepted it. */
export function deciderOf(proposal) {
const by = proposal.review?.decidedBy;
if (by === "triage")
return "judge";
if (by === "human")
return "you";
return proposal.gateDecision?.outcome === "auto-accepted" ? "judge" : "unknown";
}
/** Who decided an accepted proposal, or what became of it. */
export function classifyOutcome(proposal) {
if (proposal.status === "pending")
return "pending";
if (proposal.status === "rejected")
return "rejected";
if (proposal.status === "reverted")
return "reverted";
return deciderOf(proposal);
}
/**
* When the proposal's current decision was made. A revert overwrites `review` with the revert time, so a reverted
* proposal is dated by its accept: the change took effect then, and the revert is a separate event (`updatedAt`).
*/
export function decidedAt(proposal) {
if (proposal.status === "reverted" && proposal.acceptedAt)
return proposal.acceptedAt;
return proposal.review?.decidedAt ?? proposal.updatedAt;
}
const MIN_BASELINE = 30;
const MIN_CURRENT = 10;
/**
* Judge `x` of `n` now against a baseline of `baselineX` of `baselineN`. The usual range is the central 95% of what a
* Binomial(n, baseline rate) produces, so a small current sample gets a wide range. Too little data on either side is
* "low-data", never a verdict.
*/
export function againstUsual(x, n, baselineX, baselineN) {
if (baselineN < MIN_BASELINE || n < MIN_CURRENT)
return { rate: baselineN > 0 ? baselineX / baselineN : null, low: null, high: null, state: "low-data" };
const rate = baselineX / baselineN;
const low = binomialQuantile(n, rate, 0.025) / n;
const high = binomialQuantile(n, rate, 0.975) / n;
const share = x / n;
return { rate, low, high, state: share > high ? "above" : share < low ? "below" : "within" };
}
/** Whole weeks with observations a usual range needs before it is claimed. */
export const MIN_USUAL_WEEKS = 4;
/**
* Weekly proportions with p-chart control limits (centre ± 3 standard errors at each week's own n). A week with no
* observations has no rate and no limits. The centre is the pooled share over the whole weeks only, and only when
* there are `MIN_USUAL_WEEKS` of them with observations: otherwise there is no usual (`enough` is false, the centre
* and every limit are null). A partial week has its rate and no limits.
*/
export function pChart(weeks) {
const whole = weeks.filter((w) => !w.partial && w.n > 0);
const enough = whole.length >= MIN_USUAL_WEEKS;
const totalN = whole.reduce((sum, w) => sum + w.n, 0);
const centre = enough ? whole.reduce((sum, w) => sum + w.x, 0) / totalN : null;
return {
centre,
enough,
points: weeks.map((w) => {
if (w.n === 0)
return { ...w, rate: null, lower: null, upper: null, signal: false };
const rate = w.x / w.n;
if (centre === null || w.partial)
return { ...w, rate, lower: null, upper: null, signal: false };
const margin = 3 * Math.sqrt((centre * (1 - centre)) / w.n);
const lower = Math.max(0, centre - margin);
const upper = Math.min(1, centre + margin);
return { ...w, rate, lower, upper, signal: rate < lower || rate > upper };
}),
};
}
/** Every ISO week start (Monday) from the week of `from` to the week of `to`, oldest first. */
export function weekRange(from, to) {
const weeks = [];
for (const day of dayRange(isoWeekStart(from), to))
if (day === isoWeekStart(day))
weeks.push(day);
return weeks;
}
export const weekOf = (iso) => isoWeekStart(iso.slice(0, 10));