leanprompt 0.0.0-stage → 0.1.0-dev.8
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/LICENSE +202 -0
- package/LICENSE-DOCS +396 -0
- package/NOTICE +11 -0
- package/README.md +89 -2
- package/cli/dist/detect.d.ts +23 -0
- package/cli/dist/detect.js +219 -0
- package/cli/dist/evalAudit.d.ts +32 -0
- package/cli/dist/evalAudit.js +161 -0
- package/cli/dist/facts.d.ts +14 -0
- package/cli/dist/facts.js +43 -0
- package/cli/dist/forms.d.ts +23 -0
- package/cli/dist/forms.js +199 -0
- package/cli/dist/frontmatter.d.ts +10 -0
- package/cli/dist/frontmatter.js +32 -0
- package/cli/dist/help.d.ts +27 -0
- package/cli/dist/help.js +118 -0
- package/cli/dist/init.d.ts +78 -0
- package/cli/dist/init.js +358 -0
- package/cli/dist/install.d.ts +20 -0
- package/cli/dist/install.js +225 -0
- package/cli/dist/isMain.d.ts +6 -0
- package/cli/dist/isMain.js +18 -0
- package/cli/dist/lesson.d.ts +16 -0
- package/cli/dist/lesson.js +205 -0
- package/cli/dist/lint/facts.d.ts +7 -0
- package/cli/dist/lint/facts.js +163 -0
- package/cli/dist/lint/numbers.d.ts +26 -0
- package/cli/dist/lint/numbers.js +163 -0
- package/cli/dist/lint/rules.d.ts +20 -0
- package/cli/dist/lint/rules.js +170 -0
- package/cli/dist/log/logger.d.ts +49 -0
- package/cli/dist/log/logger.js +148 -0
- package/cli/dist/log/record.d.ts +56 -0
- package/cli/dist/log/record.js +117 -0
- package/cli/dist/main.d.ts +6 -0
- package/cli/dist/main.js +67 -0
- package/cli/dist/profile.d.ts +93 -0
- package/cli/dist/profile.js +372 -0
- package/cli/dist/report/cost.d.ts +55 -0
- package/cli/dist/report/cost.js +151 -0
- package/cli/dist/report/html.d.ts +81 -0
- package/cli/dist/report/html.js +555 -0
- package/cli/dist/report/template.html +269 -0
- package/cli/dist/score/calibrate.d.ts +65 -0
- package/cli/dist/score/calibrate.js +213 -0
- package/cli/dist/score/measured.d.ts +66 -0
- package/cli/dist/score/measured.js +191 -0
- package/cli/dist/score/static.d.ts +112 -0
- package/cli/dist/score/static.js +172 -0
- package/cli/dist/score/write.d.ts +29 -0
- package/cli/dist/score/write.js +247 -0
- package/forms/AGENTS.block.md +25 -0
- package/forms/opencode/commands/leanprompt-audit.md +10 -0
- package/forms/opencode/commands/leanprompt.md +8 -0
- package/forms/skills/generation-economy/SKILL.md +89 -0
- package/forms/skills/generation-economy/rules/LP-GE-01.md +93 -0
- package/forms/skills/generation-economy/rules/LP-GE-02.md +82 -0
- package/forms/skills/generation-economy/rules/LP-GE-03.md +87 -0
- package/forms/skills/generation-economy/rules/LP-GE-04.md +118 -0
- package/forms/skills/generation-economy/rules/LP-GE-05.md +84 -0
- package/forms/skills/generation-economy/rules/LP-GE-06.md +76 -0
- package/forms/skills/generation-economy/rules/LP-GE-07.md +84 -0
- package/forms/skills/generation-economy/rules/LP-GE-08.md +71 -0
- package/forms/skills/generation-economy/rules/LP-GE-09.md +79 -0
- package/forms/skills/generation-economy/rules/LP-GE-10.md +104 -0
- package/forms/skills/leanprompt/SKILL.md +48 -0
- package/forms/skills/leanprompt/fix.md +15 -0
- package/forms/skills/leanprompt/init.md +30 -0
- package/forms/skills/llm-call-audit/SKILL.md +169 -0
- package/forms/skills/llm-call-audit/call-shapes.md +99 -0
- package/forms/skills/llm-call-audit/presenting.md +72 -0
- package/forms/skills/llm-call-audit/report-template.md +113 -0
- package/forms/skills/lmwarden/SKILL.md +97 -0
- package/forms/skills/lmwarden/rules/LP-LW-01.md +73 -0
- package/forms/skills/lmwarden/rules/LP-LW-02.md +78 -0
- package/forms/skills/lmwarden/rules/LP-LW-03.md +73 -0
- package/forms/skills/measure-loop/SKILL.md +155 -0
- package/forms/skills/measure-loop/templates/stable-head.template.ts +38 -0
- package/forms/skills/measure-loop/templates/stable_head_template.py +40 -0
- package/forms/skills/prefill-cache/SKILL.md +66 -0
- package/forms/skills/prefill-cache/rules/LP-PC-01.md +103 -0
- package/forms/skills/prefill-cache/rules/LP-PC-02.md +80 -0
- package/forms/skills/prefill-cache/rules/LP-PC-03.md +75 -0
- package/forms/skills/prefill-cache/rules/LP-PC-04.md +79 -0
- package/forms/skills/prefill-cache/rules/LP-PC-05.md +68 -0
- package/forms/skills/prefill-cache/rules/LP-PC-06.md +71 -0
- package/forms/skills/prefill-cache/rules/LP-PC-07.md +97 -0
- package/forms/skills/prefill-cache/rules/LP-PC-08.md +67 -0
- package/forms/skills/prefill-cache/rules/LP-PC-09.md +75 -0
- package/forms/skills/prefill-cache/rules/LP-PC-10.md +76 -0
- package/forms/skills/prefill-cache/rules/LP-PC-11.md +72 -0
- package/forms/skills/prefill-cache/rules/LP-PC-12.md +73 -0
- package/forms/skills/prompt-diet/SKILL.md +69 -0
- package/forms/skills/prompt-diet/rules/LP-PD-01.md +79 -0
- package/forms/skills/prompt-diet/rules/LP-PD-02.md +73 -0
- package/forms/skills/prompt-diet/rules/LP-PD-03.md +65 -0
- package/forms/skills/prompt-diet/rules/LP-PD-04.md +77 -0
- package/forms/skills/prompt-diet/rules/LP-PD-05.md +71 -0
- package/forms/skills/prompt-diet/rules/LP-PD-06.md +68 -0
- package/models/anthropic-thinking.md +38 -0
- package/models/openai-reasoning.md +35 -0
- package/models/qwen.md +34 -0
- package/package.json +32 -4
- package/providers/anthropic.md +83 -0
- package/providers/deepinfra.md +46 -0
- package/providers/gemini.md +61 -0
- package/providers/openai.md +73 -0
- package/providers/openrouter.md +66 -0
- package/providers/sglang.md +41 -0
- package/providers/vllm.md +52 -0
- package/runtimes/http-clients.md +20 -0
- package/skills/_template/RULE.md +31 -0
- package/skills/generation-economy/SKILL.md +88 -0
- package/skills/generation-economy/rules/LP-GE-01.md +92 -0
- package/skills/generation-economy/rules/LP-GE-02.md +81 -0
- package/skills/generation-economy/rules/LP-GE-03.md +86 -0
- package/skills/generation-economy/rules/LP-GE-04.md +117 -0
- package/skills/generation-economy/rules/LP-GE-05.md +83 -0
- package/skills/generation-economy/rules/LP-GE-06.md +75 -0
- package/skills/generation-economy/rules/LP-GE-07.md +83 -0
- package/skills/generation-economy/rules/LP-GE-08.md +70 -0
- package/skills/generation-economy/rules/LP-GE-09.md +78 -0
- package/skills/generation-economy/rules/LP-GE-10.md +103 -0
- package/skills/leanprompt/SKILL.md +47 -0
- package/skills/leanprompt/fix.md +14 -0
- package/skills/leanprompt/init.md +29 -0
- package/skills/llm-call-audit/SKILL.md +168 -0
- package/skills/llm-call-audit/call-shapes.md +98 -0
- package/skills/llm-call-audit/presenting.md +71 -0
- package/skills/llm-call-audit/report-template.md +112 -0
- package/skills/lmwarden/SKILL.md +96 -0
- package/skills/lmwarden/rules/LP-LW-01.md +72 -0
- package/skills/lmwarden/rules/LP-LW-02.md +77 -0
- package/skills/lmwarden/rules/LP-LW-03.md +72 -0
- package/skills/measure-loop/SKILL.md +154 -0
- package/skills/measure-loop/templates/stable-head.template.ts +38 -0
- package/skills/measure-loop/templates/stable_head_template.py +40 -0
- package/skills/prefill-cache/SKILL.md +65 -0
- package/skills/prefill-cache/rules/LP-PC-01.md +102 -0
- package/skills/prefill-cache/rules/LP-PC-02.md +79 -0
- package/skills/prefill-cache/rules/LP-PC-03.md +74 -0
- package/skills/prefill-cache/rules/LP-PC-04.md +78 -0
- package/skills/prefill-cache/rules/LP-PC-05.md +67 -0
- package/skills/prefill-cache/rules/LP-PC-06.md +70 -0
- package/skills/prefill-cache/rules/LP-PC-07.md +96 -0
- package/skills/prefill-cache/rules/LP-PC-08.md +66 -0
- package/skills/prefill-cache/rules/LP-PC-09.md +74 -0
- package/skills/prefill-cache/rules/LP-PC-10.md +75 -0
- package/skills/prefill-cache/rules/LP-PC-11.md +71 -0
- package/skills/prefill-cache/rules/LP-PC-12.md +72 -0
- package/skills/prompt-diet/SKILL.md +68 -0
- package/skills/prompt-diet/rules/LP-PD-01.md +78 -0
- package/skills/prompt-diet/rules/LP-PD-02.md +72 -0
- package/skills/prompt-diet/rules/LP-PD-03.md +64 -0
- package/skills/prompt-diet/rules/LP-PD-04.md +76 -0
- package/skills/prompt-diet/rules/LP-PD-05.md +70 -0
- package/skills/prompt-diet/rules/LP-PD-06.md +67 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="color-scheme" content="light dark">
|
|
7
|
+
<title>{{TITLE}}</title>
|
|
8
|
+
<style>
|
|
9
|
+
/*
|
|
10
|
+
Inspection sheet. One sans family on a 1.25 scale; figures in tabular numerals; monospace only for code.
|
|
11
|
+
Graphite on cool paper, one signal colour for the next action. Severity always carries its word.
|
|
12
|
+
*/
|
|
13
|
+
:root {
|
|
14
|
+
--paper: #f2f3f5; --sheet: #ffffff; --ink: #161b26; --slate: #535c6d; --rule: #d7dbe2; --rule-soft: #e7eaee;
|
|
15
|
+
--signal: #2f3db5; --signal-ink: #ffffff; --signal-tint: #eceefb; --signal-line: #b9bff0;
|
|
16
|
+
--panel: #29348f; --panel-ink: #ffffff; --panel-soft: #d6dafa; --panel-pre: #ffffff; --panel-pre-ink: #161b26; --panel-btn: #ffffff; --panel-btn-ink: #29348f;
|
|
17
|
+
--meter: #3b4456; --meter-track: #e3e6eb; --foot: #f8f9fa;
|
|
18
|
+
--high: #a3242b; --medium: #8b4f00; --low: #535c6d; --ok: #24683a;
|
|
19
|
+
--sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
|
20
|
+
--mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
|
|
21
|
+
/* 1.25 modular scale */
|
|
22
|
+
--t-1: .8rem; --t0: 1rem; --t1: 1.25rem; --t2: 1.5625rem; --t3: 1.953rem; --t4: 2.441rem; --t5: 3.052rem;
|
|
23
|
+
--gap: 1.5rem;
|
|
24
|
+
--measure: 58ch; /* about 80 characters of running text */
|
|
25
|
+
}
|
|
26
|
+
@media (prefers-color-scheme: dark) {
|
|
27
|
+
:root {
|
|
28
|
+
--paper: #12161d; --sheet: #1a1f28; --ink: #e5e8ee; --slate: #a0a8b6; --rule: #2e3542; --rule-soft: #252b36;
|
|
29
|
+
--signal: #a9b1ff; --signal-ink: #12161d; --signal-tint: #1f2444; --signal-line: #3d4687;
|
|
30
|
+
--panel: #232a5c; --panel-ink: #eef0ff; --panel-soft: #c3c8f5; --panel-pre: #12161d; --panel-pre-ink: #e5e8ee; --panel-btn: #eef0ff; --panel-btn-ink: #232a5c;
|
|
31
|
+
--meter: #c3c9d4; --meter-track: #2a303b; --foot: #171b23;
|
|
32
|
+
--high: #ff8f86; --medium: #e8b061; --low: #a0a8b6; --ok: #7fcb95;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
* { box-sizing: border-box; }
|
|
36
|
+
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 1rem; }
|
|
37
|
+
body { margin: 0; background: var(--paper); color: var(--ink); font: 400 var(--t0)/1.55 var(--sans);
|
|
38
|
+
font-kerning: normal; text-rendering: optimizeLegibility; }
|
|
39
|
+
.sheet { max-width: 72rem; margin: 0 auto; padding: 0 16px; display: grid; grid-template-columns: minmax(0, 1fr); }
|
|
40
|
+
main { min-width: 0; max-width: 46rem; padding: 2.5rem 0 1rem; }
|
|
41
|
+
h1, h2, h3, p, li, dd, td, summary { overflow-wrap: anywhere; }
|
|
42
|
+
h1 { font-size: clamp(var(--t3), 1.35rem + 2.3vw, var(--t4)); line-height: 1.08; font-weight: 750; letter-spacing: -.024em;
|
|
43
|
+
margin: .5rem 0 1rem; max-width: 22ch; text-wrap: balance; }
|
|
44
|
+
h2 { font-size: var(--t2); line-height: 1.2; font-weight: 700; letter-spacing: -.015em; margin: 0 0 .5rem; text-wrap: balance; }
|
|
45
|
+
h3 { font-size: var(--t1); line-height: 1.3; font-weight: 650; letter-spacing: -.008em; margin: 0 0 .45rem; text-wrap: balance; }
|
|
46
|
+
p { margin: 0 0 .75rem; }
|
|
47
|
+
section { margin-top: 4rem; }
|
|
48
|
+
section > h3 { margin-top: 1.75rem; font-size: var(--t0); }
|
|
49
|
+
a { color: var(--signal); text-decoration-thickness: 1px; text-underline-offset: 3px; }
|
|
50
|
+
a:hover { text-decoration-thickness: 2px; }
|
|
51
|
+
:focus-visible { outline: 2px solid var(--signal); outline-offset: 3px; border-radius: 2px; }
|
|
52
|
+
code { font-family: var(--mono); font-size: .875em; }
|
|
53
|
+
strong { font-weight: 650; }
|
|
54
|
+
.muted { color: var(--slate); }
|
|
55
|
+
.prose, .findings > p, .cost > p, .handoff > p, .share > p { max-width: var(--measure); text-wrap: pretty; }
|
|
56
|
+
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
57
|
+
.fig, .meter, .score, dl.inputs dd, .total strong, .cost .big strong, .rail .c { font-variant-numeric: tabular-nums lining-nums; }
|
|
58
|
+
|
|
59
|
+
/* Section index: a compact strip on narrow screens, a sticky rail from 62rem */
|
|
60
|
+
.rail { padding: .75rem 0 .7rem; border-bottom: 1px solid var(--rule); font-size: .875rem; }
|
|
61
|
+
.rail .mark, .rail .proj { display: none; }
|
|
62
|
+
.rail ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .1rem 1.1rem; }
|
|
63
|
+
.rail a { display: inline-flex; align-items: center; gap: .35rem; min-height: 2rem; color: var(--slate); text-decoration: none; }
|
|
64
|
+
.rail a:hover { color: var(--ink); }
|
|
65
|
+
.rail .c { color: var(--slate); font-variant-numeric: tabular-nums; }
|
|
66
|
+
.rail a[href="#s-fix"] { color: var(--signal); font-weight: 650; }
|
|
67
|
+
@media (min-width: 62rem) {
|
|
68
|
+
.sheet { grid-template-columns: 11rem minmax(0, 46rem); column-gap: 3rem; padding: 0 2rem; }
|
|
69
|
+
.rail .mark, .rail .proj { display: block; }
|
|
70
|
+
.rail ul { display: block; }
|
|
71
|
+
.rail { position: sticky; top: 0; align-self: start; padding: 2.85rem 0 2rem; font-size: .9rem; border-bottom: 0; }
|
|
72
|
+
.rail .mark { font-weight: 700; letter-spacing: -.01em; margin: 0; }
|
|
73
|
+
.rail .proj { color: var(--slate); margin: 0 0 1.5rem; overflow-wrap: anywhere; }
|
|
74
|
+
.rail ul { list-style: none; margin: 0; padding: 0; border-left: 1px solid var(--rule); }
|
|
75
|
+
.rail a { display: flex; justify-content: space-between; gap: .75rem; padding: .3rem 0 .3rem .9rem; margin-left: -1px;
|
|
76
|
+
border-left: 2px solid transparent; color: var(--slate); text-decoration: none; }
|
|
77
|
+
.rail a:hover { color: var(--ink); border-left-color: var(--rule); }
|
|
78
|
+
.rail a[href="#s-fix"] { border-left-color: var(--signal); }
|
|
79
|
+
}
|
|
80
|
+
@media (min-width: 70rem) { .sheet { grid-template-columns: 12.5rem minmax(0, 46rem); column-gap: 4.5rem; } }
|
|
81
|
+
|
|
82
|
+
/* Hero */
|
|
83
|
+
.lede { padding-bottom: .5rem; }
|
|
84
|
+
.lede .project { color: var(--slate); margin: 0; font-size: .95rem; }
|
|
85
|
+
.lede .project strong { color: var(--ink); font-weight: 600; }
|
|
86
|
+
.lede .sub { font-size: var(--t1); line-height: 1.45; margin: 0 0 1.75rem; max-width: 58ch; }
|
|
87
|
+
.lede .sub code { font-size: .8em; background: var(--rule-soft); padding: .08em .35em; border-radius: 3px; }
|
|
88
|
+
.lede .total { margin: 0 0 .25rem; color: var(--slate); padding-top: 1.25rem; border-top: 2px solid var(--ink); }
|
|
89
|
+
.lede .total .lbl { display: block; font-size: .925rem; }
|
|
90
|
+
.lede .total strong { display: block; color: var(--ink); font-size: clamp(var(--t2), 1rem + 2.2vw, var(--t4)); line-height: 1.1;
|
|
91
|
+
font-weight: 750; letter-spacing: -.02em; margin: .15rem 0 .35rem; }
|
|
92
|
+
.lede .total .oq { display: block; margin-top: .2rem; color: var(--ink); font-weight: 600; }
|
|
93
|
+
|
|
94
|
+
/* The meter: ranked rows, number + title and where + bar + figures */
|
|
95
|
+
.meter { margin: 1.5rem 0 1rem; }
|
|
96
|
+
.meter figcaption { font-size: var(--t-1); color: var(--slate); margin-bottom: .5rem; }
|
|
97
|
+
.meter ol { list-style: none; margin: 0; padding: 0; }
|
|
98
|
+
.meter li { border-top: 1px solid var(--rule); }
|
|
99
|
+
.meter li:last-child { border-bottom: 1px solid var(--rule); }
|
|
100
|
+
.meter a { display: grid; grid-template-columns: 1.75rem minmax(0, 17rem) minmax(4rem, 1fr) 3.5rem 4.75rem;
|
|
101
|
+
align-items: center; gap: .75rem; padding: .45rem .25rem; color: var(--ink); text-decoration: none; }
|
|
102
|
+
.meter a:hover { background: var(--sheet); }
|
|
103
|
+
.meter .mn { font-weight: 700; text-align: right; }
|
|
104
|
+
.meter .ml { grid-column: 2; grid-row: 1; display: flex; flex-direction: column; gap: .1rem; min-width: 0; }
|
|
105
|
+
.meter .mtt { font-size: .9rem; line-height: 1.3; overflow-wrap: anywhere; text-wrap: pretty; }
|
|
106
|
+
.meter .mw { font-size: .8rem; color: var(--slate); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
107
|
+
.meter .track { grid-column: 3; grid-row: 1; height: .7rem; border-radius: 1px;
|
|
108
|
+
background: linear-gradient(90deg, transparent calc(25% - 1px), var(--rule) calc(25% - 1px) 25%, transparent 25% calc(50% - 1px), var(--rule) calc(50% - 1px) 50%, transparent 50% calc(75% - 1px), var(--rule) calc(75% - 1px) 75%, transparent 75%), var(--meter-track); }
|
|
109
|
+
.meter .fill { display: block; height: 100%; min-width: 2px; background: var(--meter); border-radius: 1px; }
|
|
110
|
+
.meter .mt { grid-column: 4; text-align: right; font-weight: 650; }
|
|
111
|
+
.meter .mp { grid-column: 5; text-align: right; color: var(--slate); font-size: .875rem; }
|
|
112
|
+
.meter li:first-child .fill { background: var(--signal); }
|
|
113
|
+
|
|
114
|
+
/* Cost */
|
|
115
|
+
.cost .big { margin: .75rem 0 .5rem; max-width: 60ch; }
|
|
116
|
+
.cost .big strong { display: block; font-size: var(--t3); line-height: 1.15; letter-spacing: -.02em; font-weight: 750; text-wrap: balance; }
|
|
117
|
+
ul.inputs, ul.missing { padding-left: 1.1rem; margin: .75rem 0; max-width: var(--measure); color: var(--slate); font-size: .925rem; }
|
|
118
|
+
ul.inputs li, ul.missing li { margin: .25rem 0; }
|
|
119
|
+
ul.missing { color: var(--ink); }
|
|
120
|
+
.tablewrap { overflow-x: auto; margin: 1.25rem 0; border-top: 2px solid var(--ink); }
|
|
121
|
+
table { border-collapse: collapse; width: 100%; font-size: .925rem; }
|
|
122
|
+
th, td { text-align: left; padding: .55rem .75rem .55rem 0; border-bottom: 1px solid var(--rule); vertical-align: top; }
|
|
123
|
+
th { color: var(--slate); font-weight: 600; font-size: .85rem; }
|
|
124
|
+
th.fig, td.fig { text-align: right; padding-right: 0; padding-left: .75rem; }
|
|
125
|
+
td.rule { white-space: nowrap; color: var(--slate); }
|
|
126
|
+
td.loc code { font-size: .85em; }
|
|
127
|
+
|
|
128
|
+
/* Finding tickets */
|
|
129
|
+
.findings > p { margin-bottom: 1.5rem; }
|
|
130
|
+
.finding { display: grid; grid-template-columns: 2.75rem minmax(0, 1fr); gap: 0 1.1rem; background: var(--sheet);
|
|
131
|
+
border: 1px solid var(--rule); border-radius: 3px; padding: 1.4rem 1.5rem 0 1.1rem; margin: 0 0 1.25rem;
|
|
132
|
+
scroll-margin-top: 1rem; }
|
|
133
|
+
.finding:target { outline: 2px solid var(--signal); outline-offset: 2px; }
|
|
134
|
+
.finding .num { width: 2.75rem; height: 2.75rem; display: grid; place-items: center; font-weight: 750; font-size: var(--t1);
|
|
135
|
+
font-variant-numeric: tabular-nums; border: 2px solid var(--low); border-radius: 2px; color: var(--ink); }
|
|
136
|
+
.finding.sev-high .num { border-color: var(--high); }
|
|
137
|
+
.finding.sev-medium .num { border-color: var(--medium); }
|
|
138
|
+
.finding .body { min-width: 0; }
|
|
139
|
+
.finding .meta { margin: 0 0 .85rem; font-size: .875rem; color: var(--slate); display: flex; flex-wrap: wrap; gap: .3rem .9rem; align-items: baseline; }
|
|
140
|
+
.finding .meta code { color: var(--ink); font-size: .9em; }
|
|
141
|
+
.sev { font-weight: 650; text-transform: capitalize; }
|
|
142
|
+
.sev::before { content: ""; display: inline-block; width: .5rem; height: .5rem; margin-right: .35rem; background: currentColor; }
|
|
143
|
+
.sev-high .sev { color: var(--high); } .sev-high .sev::before { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
|
|
144
|
+
.sev-medium .sev { color: var(--medium); } .sev-medium .sev::before { transform: rotate(45deg) scale(.85); }
|
|
145
|
+
.sev-low .sev { color: var(--low); } .sev-low .sev::before { border-radius: 50%; }
|
|
146
|
+
.finding .rest { margin: 0 0 1rem; max-width: var(--measure); text-wrap: pretty; }
|
|
147
|
+
.finding.fixed { opacity: .85; } .tag { color: var(--ok); font-weight: 650; }
|
|
148
|
+
dl.why { margin: 0; display: grid; gap: 1rem; }
|
|
149
|
+
dl.why dt { font-size: .85rem; font-weight: 650; color: var(--slate); margin-bottom: .15rem; }
|
|
150
|
+
dl.why dd { margin: 0; max-width: var(--measure); text-wrap: pretty; }
|
|
151
|
+
dl.why .act { background: var(--signal-tint); padding: .75rem 1rem .8rem; margin: 0 -1rem; }
|
|
152
|
+
dl.why .act dt { color: var(--signal); }
|
|
153
|
+
dl.why .act.confirm { background: transparent; border-block: 1px dashed var(--signal-line); }
|
|
154
|
+
dl.why .estimate dd .est { font-weight: 600; font-variant-numeric: tabular-nums; }
|
|
155
|
+
details.assume { margin-top: .3rem; font-size: .925rem; }
|
|
156
|
+
details.assume summary { color: var(--slate); cursor: pointer; width: fit-content; min-height: 1.5rem; padding: .1rem 0; }
|
|
157
|
+
details.assume summary:hover { color: var(--ink); }
|
|
158
|
+
details.assume p { margin: .35rem 0 0; color: var(--slate); max-width: var(--measure); }
|
|
159
|
+
.finding footer.check { margin: 1.4rem -1.5rem 0 -4.95rem; padding: .85rem 1.5rem 1rem 4.95rem; background: var(--foot); border-top: 1px solid var(--rule-soft); font-size: .875rem; color: var(--slate); }
|
|
160
|
+
.finding footer.check p { margin: 0; max-width: var(--measure); text-wrap: pretty; }
|
|
161
|
+
.finding footer.check .rulelink a { padding: .3rem 0; }
|
|
162
|
+
.finding footer.check p + p { margin-top: .35rem; }
|
|
163
|
+
.finding footer.check .k { font-weight: 650; color: var(--ink); margin-right: .35rem; }
|
|
164
|
+
details.fixed-list > summary { cursor: pointer; color: var(--slate); margin-bottom: 1rem; }
|
|
165
|
+
details.fixed-list > summary h2 { display: inline; font-size: var(--t1); }
|
|
166
|
+
|
|
167
|
+
/* Hand-off: the next step */
|
|
168
|
+
.handoff { background: var(--panel); color: var(--panel-ink); border-radius: 4px; padding: 1.75rem 1.75rem 1.5rem; margin-top: 4.5rem; }
|
|
169
|
+
.handoff h2 { color: var(--panel-ink); font-size: var(--t3); letter-spacing: -.02em; }
|
|
170
|
+
.handoff > p { color: var(--panel-soft); }
|
|
171
|
+
.handoff :focus-visible { outline-color: var(--panel-ink); }
|
|
172
|
+
.copybox { display: flex; flex-direction: column; align-items: flex-start; gap: .75rem; margin: 1rem 0 0; }
|
|
173
|
+
.copybox pre { align-self: stretch; }
|
|
174
|
+
pre { margin: 0; padding: 1rem 1.1rem; background: var(--sheet); color: var(--ink); border: 1px solid var(--rule); border-radius: 3px;
|
|
175
|
+
font: .84rem/1.55 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 32rem; overflow: auto; user-select: all; }
|
|
176
|
+
.handoff pre { background: var(--panel-pre); color: var(--panel-pre-ink); border-color: transparent; }
|
|
177
|
+
button.copy { font: 650 .925rem/1 var(--sans); padding: .7rem 1.1rem; border-radius: 3px; border: 1px solid var(--signal);
|
|
178
|
+
background: var(--sheet); color: var(--signal); cursor: pointer; }
|
|
179
|
+
button.copy[hidden] { display: none; }
|
|
180
|
+
.handoff button.copy { background: var(--panel-btn); color: var(--panel-btn-ink); border-color: var(--panel-btn); font-size: 1rem; padding: .8rem 1.3rem; }
|
|
181
|
+
.handoff button.copy:hover { background: var(--panel-soft); border-color: var(--panel-soft); }
|
|
182
|
+
.share button.copy { font-size: .85rem; padding: .5rem .85rem; }
|
|
183
|
+
|
|
184
|
+
/* Score: secondary */
|
|
185
|
+
.score-sec .card { background: transparent; border-top: 2px solid var(--ink); padding: 1rem 0 0; }
|
|
186
|
+
p.card { border-top: 2px solid var(--ink); padding-top: 1rem; }
|
|
187
|
+
.score { display: flex; align-items: baseline; gap: .6rem; }
|
|
188
|
+
.score .n { font-size: var(--t4); font-weight: 750; letter-spacing: -.02em; line-height: 1; }
|
|
189
|
+
.kind { color: var(--slate); font-size: var(--t0); }
|
|
190
|
+
dl.inputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 1rem 1.5rem; margin: 1.25rem 0 1rem; }
|
|
191
|
+
dl.inputs div { border-top: 1px solid var(--rule); padding-top: .5rem; }
|
|
192
|
+
dl.inputs dt { color: var(--slate); font-size: .85rem; }
|
|
193
|
+
dl.inputs dd { margin: 0; font-size: var(--t1); font-weight: 600; }
|
|
194
|
+
.score-sec .card p { font-size: .925rem; max-width: var(--measure); margin-bottom: .4rem; }
|
|
195
|
+
|
|
196
|
+
/* Sharing: last and quiet */
|
|
197
|
+
.share { border-top: 1px solid var(--rule); padding-top: 2rem; margin-top: 5rem; color: var(--slate); font-size: .95rem; }
|
|
198
|
+
.share h2 { font-size: var(--t1); color: var(--ink); }
|
|
199
|
+
.share h3 { color: var(--ink); }
|
|
200
|
+
.share .star { font-weight: 650; padding: .3rem 0; }
|
|
201
|
+
.share pre { font-size: .8rem; }
|
|
202
|
+
.page-foot { grid-column: 1 / -1; margin: 3rem 0 0; padding: 1.25rem 0 2.5rem; border-top: 1px solid var(--rule); font-size: .85rem; }
|
|
203
|
+
@media (min-width: 62rem) { .page-foot { grid-column: 2; } }
|
|
204
|
+
|
|
205
|
+
@media (max-width: 40rem) {
|
|
206
|
+
main { padding-top: 1.75rem; }
|
|
207
|
+
section { margin-top: 3rem; }
|
|
208
|
+
.finding { grid-template-columns: minmax(0, 1fr); padding: 1.1rem 1rem 0; }
|
|
209
|
+
.finding .num { width: 2.25rem; height: 2.25rem; font-size: var(--t0); margin-bottom: .75rem; }
|
|
210
|
+
.finding footer.check { margin: 1.25rem -1rem 0; padding: .8rem 1rem .9rem; }
|
|
211
|
+
.meter a { grid-template-columns: 1.25rem minmax(0, 1fr) 3.25rem 3.75rem; grid-template-rows: auto auto; gap: .3rem .6rem; padding: .55rem 0; }
|
|
212
|
+
.meter .ml { grid-column: 2; grid-row: 1; }
|
|
213
|
+
.meter .mt { grid-column: 3; grid-row: 1; }
|
|
214
|
+
.meter .mp { grid-column: 4; grid-row: 1; }
|
|
215
|
+
.meter .track { grid-column: 2 / -1; grid-row: 2; height: .5rem; }
|
|
216
|
+
.handoff { padding: 1.25rem 1rem 1rem; margin-left: -16px; margin-right: -16px; border-radius: 0; }
|
|
217
|
+
.handoff h2 { font-size: var(--t2); }
|
|
218
|
+
.cost .big strong { font-size: var(--t2); }
|
|
219
|
+
pre { font-size: .78rem; padding: .85rem; }
|
|
220
|
+
.lede .total strong { text-wrap: balance; }
|
|
221
|
+
/* The cost table becomes one block per finding: rule and money, then where, then tokens. */
|
|
222
|
+
.tablewrap thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
|
|
223
|
+
.tablewrap tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .15rem 1rem; padding: .7rem 0; border-bottom: 1px solid var(--rule); }
|
|
224
|
+
.tablewrap td { padding: 0; border: 0; }
|
|
225
|
+
.tablewrap td.rule { grid-column: 1; }
|
|
226
|
+
.tablewrap td.mon { grid-column: 2; grid-row: 1; font-weight: 650; }
|
|
227
|
+
.tablewrap td.loc { grid-column: 1 / -1; }
|
|
228
|
+
.tablewrap td.tok { grid-column: 1; text-align: left; padding-left: 0; color: var(--slate); }
|
|
229
|
+
.tablewrap td.tok::after { content: " per 1,000 calls"; }
|
|
230
|
+
.tablewrap td.ttft { grid-column: 2; padding-left: 0; }
|
|
231
|
+
}
|
|
232
|
+
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; animation: none !important; } }
|
|
233
|
+
|
|
234
|
+
@media print {
|
|
235
|
+
:root { --paper: #fff; --sheet: #fff; --ink: #000; --slate: #333; --rule: #bbb; --rule-soft: #eee; --foot: #fff;
|
|
236
|
+
--signal: #000; --signal-ink: #fff; --signal-tint: #fff; --signal-line: #999;
|
|
237
|
+
--panel: #fff; --panel-ink: #000; --panel-soft: #333; --panel-pre: #fff; --panel-pre-ink: #000; --panel-btn: #fff; --panel-btn-ink: #000;
|
|
238
|
+
--meter: #000; --meter-track: #ddd; --high: #000; --medium: #000; --low: #333; --ok: #000; }
|
|
239
|
+
body { font-size: 10.5pt; }
|
|
240
|
+
.rail, button.copy, .share { display: none; }
|
|
241
|
+
.meter li:first-child .fill { background: #000; }
|
|
242
|
+
.sheet { display: block; max-width: none; padding: 0; }
|
|
243
|
+
main { max-width: none; padding: 0; }
|
|
244
|
+
section { margin-top: 1.5rem; }
|
|
245
|
+
h2, h3, .finding .meta { break-after: avoid; }
|
|
246
|
+
.finding { border-color: #999; }
|
|
247
|
+
.finding .num, dl.why > div, .finding footer.check, .meter li, tr { break-inside: avoid; }
|
|
248
|
+
.finding .sev { color: #000; }
|
|
249
|
+
dl.why .act { border-top: 1px solid #999; border-bottom: 1px solid #999; }
|
|
250
|
+
.handoff { border: 1px solid #999; padding: 1rem; break-before: page; }
|
|
251
|
+
pre { max-height: none; overflow: visible; border: 1px solid #bbb; }
|
|
252
|
+
.finding footer.check { background: none; }
|
|
253
|
+
details > summary { list-style: none; }
|
|
254
|
+
details::details-content { content-visibility: visible; display: block; }
|
|
255
|
+
}
|
|
256
|
+
</style>
|
|
257
|
+
</head>
|
|
258
|
+
<body>
|
|
259
|
+
<div class="sheet">
|
|
260
|
+
{{NAV}}
|
|
261
|
+
<main>
|
|
262
|
+
{{BODY}}
|
|
263
|
+
</main>
|
|
264
|
+
{{FOOTER}}
|
|
265
|
+
</div>
|
|
266
|
+
<p id="copy-status" class="visually-hidden" aria-live="polite"></p>
|
|
267
|
+
{{SCRIPT}}
|
|
268
|
+
</body>
|
|
269
|
+
</html>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type CacheFacts } from "../facts.js";
|
|
2
|
+
import { type UsageRecord } from "../log/record.js";
|
|
3
|
+
import { type Finding, type Weights } from "./static.js";
|
|
4
|
+
export type Side = "prefill" | "generation";
|
|
5
|
+
export interface CalibrateOptions {
|
|
6
|
+
source: "real" | "synthetic";
|
|
7
|
+
/** Cache facts per provider, as for `measuredScore`. */
|
|
8
|
+
factsFor: (provider: string) => CacheFacts;
|
|
9
|
+
/** ISO date written into the weights file; today when omitted. */
|
|
10
|
+
date?: string;
|
|
11
|
+
}
|
|
12
|
+
export type CalibrateResult = {
|
|
13
|
+
ok: true;
|
|
14
|
+
weights: Weights;
|
|
15
|
+
before: {
|
|
16
|
+
static: number;
|
|
17
|
+
measured: number;
|
|
18
|
+
};
|
|
19
|
+
after: {
|
|
20
|
+
static: number;
|
|
21
|
+
};
|
|
22
|
+
/** Measured E, S, G of the log the weights were fitted to. */
|
|
23
|
+
measured: {
|
|
24
|
+
E: number;
|
|
25
|
+
S: number;
|
|
26
|
+
G: number;
|
|
27
|
+
};
|
|
28
|
+
/** Share of all tokens in the log that are prompt tokens. Information only; not used in the fit. */
|
|
29
|
+
split: {
|
|
30
|
+
prefill_cost_share: number;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* How each side's k was found. "exact": the static value meets the measured one. "capped": the
|
|
34
|
+
* measured value lies beyond what the 0.95 share cap allows; k is the smallest that reaches the
|
|
35
|
+
* closest achievable value (`limit`). "unreachable": explicit shares already overshoot the
|
|
36
|
+
* measured value. "none": no finding uses a default share. The last two are not calibratable.
|
|
37
|
+
*/
|
|
38
|
+
fit: Record<Side, Fit>;
|
|
39
|
+
/** For a capped side, the closest achievable static E x S (prefill) or G (generation). */
|
|
40
|
+
limit: Partial<Record<Side, number>>;
|
|
41
|
+
/** Sides left at k = 1: "unreachable" or "none". */
|
|
42
|
+
not_calibratable: Side[];
|
|
43
|
+
} | {
|
|
44
|
+
ok: false;
|
|
45
|
+
reason: "too_few_calls" | "no_findings" | "not_calibratable";
|
|
46
|
+
calls?: number;
|
|
47
|
+
};
|
|
48
|
+
export type Fit = "exact" | "capped" | "unreachable" | "none";
|
|
49
|
+
/**
|
|
50
|
+
* Fit k_prefill so the static E x S equals the measured E x S, and k_gen so the static G equals the
|
|
51
|
+
* measured G. Only default severity shares are scaled (an explicit finding `share` is never touched),
|
|
52
|
+
* capped at 0.95. A side with no default-share finding is "not calibratable": its k is 1 and it is
|
|
53
|
+
* listed in `not_calibratable`. The 0.6 / 0.4 split and the measured score are not changed.
|
|
54
|
+
* Fixed findings are ignored, as in `staticScore`; if none remain the result is `no_findings`.
|
|
55
|
+
* Profile `ignore:` filtering is the caller's job.
|
|
56
|
+
*/
|
|
57
|
+
export declare function calibrate(findings: readonly Finding[], records: readonly UsageRecord[], o: CalibrateOptions): CalibrateResult;
|
|
58
|
+
export interface CalibrateIo {
|
|
59
|
+
/** Ask a yes/no question; the default reads one line from stdin. */
|
|
60
|
+
confirm?: (question: string) => Promise<boolean>;
|
|
61
|
+
/** Whether stdin is a terminal; defaults to `process.stdin.isTTY`. */
|
|
62
|
+
isTTY?: boolean;
|
|
63
|
+
}
|
|
64
|
+
/** `leanprompt calibrate`: print before/after, ask, then write `.leanprompt/weights.json`. */
|
|
65
|
+
export declare function calibrateCommand(args: string[], io?: CalibrateIo): Promise<number>;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// `leanprompt calibrate`: scale the static severity shares to fit a trial run (DESIGN D4).
|
|
2
|
+
// calibrate() is pure; calibrateCommand() reads the files, asks, and writes .leanprompt/weights.json.
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import { optionsFor, usageLine, spec } from "../help.js";
|
|
7
|
+
import { loadCacheFacts } from "../facts.js";
|
|
8
|
+
import { loadRuleIds } from "../lint/rules.js";
|
|
9
|
+
import { parseUsageLog } from "../log/record.js";
|
|
10
|
+
import { effectiveFacts, loadProfile } from "../profile.js";
|
|
11
|
+
import { measuredScore, MIN_CALLS } from "./measured.js";
|
|
12
|
+
import { FINDINGS_FILE, filterIgnored, PACKAGE_ROOT, USAGE_FILE, WEIGHTS_FILE } from "./write.js";
|
|
13
|
+
import { parseFindings, staticScore } from "./static.js";
|
|
14
|
+
const BASE_SHARES = { high: 0.5, medium: 0.2, low: 0.05 };
|
|
15
|
+
const SHARE_CAP = 0.95;
|
|
16
|
+
const K_DECIMALS = 1e4;
|
|
17
|
+
/**
|
|
18
|
+
* Find k with f(k) = target. `f` is non-increasing (`decreasing`) or non-decreasing in k and constant
|
|
19
|
+
* from `kCap` on, where every default share has reached the 0.95 cap.
|
|
20
|
+
* - target on the wrong side of f(0) (explicit shares already overshoot it): "unreachable", k = 1.
|
|
21
|
+
* - target beyond f(kCap): "capped", k = kCap (the smallest k reaching the closest achievable value).
|
|
22
|
+
* - otherwise "exact", by bisection on [0, kCap].
|
|
23
|
+
*/
|
|
24
|
+
function solve(f, target, decreasing, kCap) {
|
|
25
|
+
const EPS = 1e-9;
|
|
26
|
+
// Positive when x is on the far side of the target (below it for a decreasing f, above it otherwise).
|
|
27
|
+
const past = (x) => (decreasing ? target - x : x - target);
|
|
28
|
+
if (Math.abs(f(0) - target) <= EPS)
|
|
29
|
+
return { k: 0, fit: "exact" };
|
|
30
|
+
if (past(f(0)) > 0)
|
|
31
|
+
return { k: 1, fit: "unreachable" };
|
|
32
|
+
const kc = Math.ceil(kCap * K_DECIMALS) / K_DECIMALS;
|
|
33
|
+
if (past(f(kc)) < -EPS)
|
|
34
|
+
return { k: kc, fit: "capped", limit: f(kc) };
|
|
35
|
+
let lo = 0;
|
|
36
|
+
let hi = kc;
|
|
37
|
+
for (let i = 0; i < 60; i++) {
|
|
38
|
+
const mid = (lo + hi) / 2;
|
|
39
|
+
if (past(f(mid)) < 0)
|
|
40
|
+
lo = mid;
|
|
41
|
+
else
|
|
42
|
+
hi = mid;
|
|
43
|
+
}
|
|
44
|
+
return { k: Math.round(((lo + hi) / 2) * K_DECIMALS) / K_DECIMALS, fit: "exact" };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Fit k_prefill so the static E x S equals the measured E x S, and k_gen so the static G equals the
|
|
48
|
+
* measured G. Only default severity shares are scaled (an explicit finding `share` is never touched),
|
|
49
|
+
* capped at 0.95. A side with no default-share finding is "not calibratable": its k is 1 and it is
|
|
50
|
+
* listed in `not_calibratable`. The 0.6 / 0.4 split and the measured score are not changed.
|
|
51
|
+
* Fixed findings are ignored, as in `staticScore`; if none remain the result is `no_findings`.
|
|
52
|
+
* Profile `ignore:` filtering is the caller's job.
|
|
53
|
+
*/
|
|
54
|
+
export function calibrate(findings, records, o) {
|
|
55
|
+
const live = findings.filter((f) => !f.fixed);
|
|
56
|
+
if (live.length === 0)
|
|
57
|
+
return { ok: false, reason: "no_findings" };
|
|
58
|
+
const m = measuredScore(records, o.factsFor);
|
|
59
|
+
if (!m.ok)
|
|
60
|
+
return { ok: false, reason: "too_few_calls", calls: m.calls - m.unmeasured };
|
|
61
|
+
const sides = (side) => {
|
|
62
|
+
let fixed = 1;
|
|
63
|
+
const defaults = [];
|
|
64
|
+
for (const f of live) {
|
|
65
|
+
if (side === "prefill" ? f.saves === "generation" : f.saves === "prefill")
|
|
66
|
+
continue;
|
|
67
|
+
if (f.share !== undefined)
|
|
68
|
+
fixed *= 1 - f.share;
|
|
69
|
+
else
|
|
70
|
+
defaults.push(BASE_SHARES[f.severity]);
|
|
71
|
+
}
|
|
72
|
+
return { fixed, defaults };
|
|
73
|
+
};
|
|
74
|
+
// Product of (1 - share) on one side as a function of k.
|
|
75
|
+
const keep = (s) => (k) => s.defaults.reduce((acc, d) => acc * (1 - Math.min(SHARE_CAP, d * k)), s.fixed);
|
|
76
|
+
const pre = sides("prefill");
|
|
77
|
+
const gen = sides("generation");
|
|
78
|
+
const kCap = (d) => SHARE_CAP / Math.min(...d);
|
|
79
|
+
const fits = {
|
|
80
|
+
prefill: pre.defaults.length === 0 ? { k: 1, fit: "none" } : solve(keep(pre), m.inputs.E * m.inputs.S, true, kCap(pre.defaults)),
|
|
81
|
+
generation: gen.defaults.length === 0 ? { k: 1, fit: "none" } : solve((k) => 1 - keep(gen)(k), m.inputs.G, false, kCap(gen.defaults)),
|
|
82
|
+
};
|
|
83
|
+
const not_calibratable = ["prefill", "generation"].filter((x) => fits[x].fit === "unreachable" || fits[x].fit === "none");
|
|
84
|
+
if (not_calibratable.length === 2)
|
|
85
|
+
return { ok: false, reason: "not_calibratable" };
|
|
86
|
+
const k_prefill = fits.prefill.k;
|
|
87
|
+
const k_gen = fits.generation.k;
|
|
88
|
+
const weights = {
|
|
89
|
+
v: 1,
|
|
90
|
+
shares: { ...BASE_SHARES },
|
|
91
|
+
k_prefill,
|
|
92
|
+
k_gen,
|
|
93
|
+
source: o.source,
|
|
94
|
+
calls: m.calls - m.unmeasured,
|
|
95
|
+
date: o.date ?? new Date().toISOString().slice(0, 10),
|
|
96
|
+
};
|
|
97
|
+
let prompt = 0;
|
|
98
|
+
let output = 0;
|
|
99
|
+
for (const r of records) {
|
|
100
|
+
prompt += r.prompt_tokens;
|
|
101
|
+
output += r.output_tokens;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
weights,
|
|
106
|
+
before: { static: staticScore(live).score, measured: m.score },
|
|
107
|
+
after: { static: staticScore(live, weights).score },
|
|
108
|
+
measured: m.inputs,
|
|
109
|
+
split: { prefill_cost_share: prompt + output > 0 ? prompt / (prompt + output) : 0 },
|
|
110
|
+
fit: { prefill: fits.prefill.fit, generation: fits.generation.fit },
|
|
111
|
+
limit: Object.fromEntries(["prefill", "generation"].flatMap((x) => (fits[x].limit === undefined ? [] : [[x, fits[x].limit]]))),
|
|
112
|
+
not_calibratable,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
async function askStdin(question) {
|
|
116
|
+
const { createInterface } = await import("node:readline/promises");
|
|
117
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
118
|
+
try {
|
|
119
|
+
return /^y(es)?$/i.test((await rl.question(question)).trim());
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
rl.close();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const USAGE_TEXT = `usage: ${usageLine(spec("calibrate"))}
|
|
126
|
+
`;
|
|
127
|
+
/** `leanprompt calibrate`: print before/after, ask, then write `.leanprompt/weights.json`. */
|
|
128
|
+
export async function calibrateCommand(args, io = {}) {
|
|
129
|
+
let values;
|
|
130
|
+
try {
|
|
131
|
+
values = parseArgs({
|
|
132
|
+
args,
|
|
133
|
+
options: optionsFor("calibrate"),
|
|
134
|
+
strict: true,
|
|
135
|
+
}).values;
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
process.stderr.write(`leanprompt: ${e.message}\n${USAGE_TEXT}`);
|
|
139
|
+
return 2;
|
|
140
|
+
}
|
|
141
|
+
const dir = resolve(values.dir ?? ".");
|
|
142
|
+
const findingsPath = resolve(values.findings ?? join(dir, FINDINGS_FILE));
|
|
143
|
+
const usagePath = resolve(values.usage ?? join(dir, USAGE_FILE));
|
|
144
|
+
for (const p of [findingsPath, usagePath])
|
|
145
|
+
if (!existsSync(p))
|
|
146
|
+
throw new Error(`${p}: no such file`);
|
|
147
|
+
const parsed = parseFindings(readFileSync(findingsPath, "utf8"), loadRuleIds(PACKAGE_ROOT));
|
|
148
|
+
if (parsed.errors.length > 0)
|
|
149
|
+
throw new Error(`${findingsPath}: ${parsed.errors.join("; ")}`);
|
|
150
|
+
const profile = loadProfile(dir);
|
|
151
|
+
const findings = filterIgnored(parsed.findings, profile);
|
|
152
|
+
const { records, bad } = parseUsageLog(readFileSync(usagePath, "utf8"));
|
|
153
|
+
if (bad > 0)
|
|
154
|
+
process.stderr.write(`leanprompt: skipped ${bad} unreadable line(s) in ${usagePath}\n`);
|
|
155
|
+
const facts = loadCacheFacts(PACKAGE_ROOT);
|
|
156
|
+
const r = calibrate(findings, records, {
|
|
157
|
+
source: values.synthetic ? "synthetic" : "real",
|
|
158
|
+
factsFor: (provider) => effectiveFacts(profile, facts, provider),
|
|
159
|
+
});
|
|
160
|
+
if (!r.ok) {
|
|
161
|
+
const msg = {
|
|
162
|
+
too_few_calls: `calibration needs at least ${MIN_CALLS} measured calls; found ${r.calls}. Log more calls, or run without calibrating to keep the default weights.\n`,
|
|
163
|
+
no_findings: "no findings to calibrate: the findings file has none left after removing fixed and ignored ones.\n",
|
|
164
|
+
not_calibratable: "nothing to calibrate: every finding carries an explicit share, so scaling the severity defaults would change nothing. No weights written.\n",
|
|
165
|
+
}[r.reason];
|
|
166
|
+
process.stderr.write(`leanprompt calibrate: ${msg}`);
|
|
167
|
+
return 1;
|
|
168
|
+
}
|
|
169
|
+
const f = (x) => x.toFixed(2);
|
|
170
|
+
const w = r.weights;
|
|
171
|
+
const side = (s, k) => {
|
|
172
|
+
const quantity = s === "prefill" ? "E x S" : "G";
|
|
173
|
+
const dir = s === "prefill" ? "lower" : "higher";
|
|
174
|
+
switch (r.fit[s]) {
|
|
175
|
+
case "none":
|
|
176
|
+
return "not calibratable (no finding uses a default share), left at 1";
|
|
177
|
+
case "unreachable":
|
|
178
|
+
return `not calibratable (explicit shares already overshoot the measured ${quantity}), left at 1`;
|
|
179
|
+
case "capped":
|
|
180
|
+
return `x${k}, capped: shares hit the 0.95 limit; static ${quantity} cannot go ${dir} than ${f(r.limit[s])}`;
|
|
181
|
+
default:
|
|
182
|
+
return `x${k}`;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const out = [
|
|
186
|
+
`leanprompt calibrate (${w.source}, ${w.calls} measured calls)`,
|
|
187
|
+
` measured score: ${r.before.measured} (E=${f(r.measured.E)} S=${f(r.measured.S)} G=${f(r.measured.G)})`,
|
|
188
|
+
` static score before: ${r.before.static} (default shares)`,
|
|
189
|
+
` static score after: ${r.after.static} (calibrated)`,
|
|
190
|
+
` k_prefill: ${side("prefill", w.k_prefill)}`,
|
|
191
|
+
` k_gen: ${side("generation", w.k_gen)}`,
|
|
192
|
+
` prompt tokens are ${(r.split.prefill_cost_share * 100).toFixed(0)}% of all tokens in this log (token counts, not prices; for information only)`,
|
|
193
|
+
"",
|
|
194
|
+
];
|
|
195
|
+
process.stdout.write(out.join("\n"));
|
|
196
|
+
const target = join(dir, WEIGHTS_FILE);
|
|
197
|
+
if (!values.yes) {
|
|
198
|
+
const tty = io.isTTY ?? process.stdin.isTTY === true;
|
|
199
|
+
if (!tty && !io.confirm) {
|
|
200
|
+
process.stderr.write(`leanprompt calibrate: stdin is not a terminal, so nothing was written. Re-run with --yes to write ${WEIGHTS_FILE}.\n`);
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
const yes = await (io.confirm ?? askStdin)(`write ${WEIGHTS_FILE}? [y/N] `);
|
|
204
|
+
if (!yes) {
|
|
205
|
+
process.stdout.write(`not written\n`);
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
210
|
+
writeFileSync(target, `${JSON.stringify(w, null, 2)}\n`);
|
|
211
|
+
process.stdout.write(`wrote ${target}\n`);
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type CacheFacts } from "../facts.js";
|
|
2
|
+
import type { UsageRecord } from "../log/record.js";
|
|
3
|
+
/** A measured score needs at least this many measured calls (DESIGN §8). */
|
|
4
|
+
export declare const MIN_CALLS = 50;
|
|
5
|
+
/** Why a call was left out of E and S. */
|
|
6
|
+
export type UnmeasuredReason = "no_usage" | "no_ids" | "bad_node_chars" | "bad_ts" | "unknown_provider" | "ambiguous_target" | "call_failed";
|
|
7
|
+
export interface MeasuredScore {
|
|
8
|
+
ok: true;
|
|
9
|
+
score: number;
|
|
10
|
+
inputs: {
|
|
11
|
+
E: number;
|
|
12
|
+
S: number;
|
|
13
|
+
G: number;
|
|
14
|
+
};
|
|
15
|
+
/** Every record given. G is computed over all of them; E and S over `calls - unmeasured`. */
|
|
16
|
+
calls: number;
|
|
17
|
+
/** Records left out of E and S, never counted as zero reuse. */
|
|
18
|
+
unmeasured: number;
|
|
19
|
+
unmeasured_by_reason: Record<UnmeasuredReason, number>;
|
|
20
|
+
/** Reusable tokens are estimated by character share: prompt_tokens x node_chars[i] / node_chars[last]. */
|
|
21
|
+
estimate: "char-share";
|
|
22
|
+
/**
|
|
23
|
+
* Head churn, for information only: not part of E, S, G or the score. Counted over records with ids.
|
|
24
|
+
* The usage record keeps one `head` id (not separate system and tools ids), so this is distinct heads.
|
|
25
|
+
*/
|
|
26
|
+
heads: {
|
|
27
|
+
distinct_head: number;
|
|
28
|
+
conversations: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface TooFewCalls {
|
|
32
|
+
ok: false;
|
|
33
|
+
reason: "too_few_calls";
|
|
34
|
+
/** Every record given. */
|
|
35
|
+
calls: number;
|
|
36
|
+
/** Of which left out of E and S; the threshold applies to `calls - unmeasured`. */
|
|
37
|
+
unmeasured: number;
|
|
38
|
+
unmeasured_by_reason: Record<UnmeasuredReason, number>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The measured score of a usage log (DESIGN §8).
|
|
42
|
+
*
|
|
43
|
+
* In time order (`ts`; equal ts keep file order), for each measured record r:
|
|
44
|
+
* - shared(r) is the deepest i such that r.ids.nodes[0..i] equals the nodes prefix of an earlier
|
|
45
|
+
* measured record seen within the provider's `ttl_seconds` (0 = no limit), across all `conv`s.
|
|
46
|
+
* - reusable(r) = cap(facts, round(prompt_tokens x node_chars[i] / node_chars[last])), or 0 when
|
|
47
|
+
* nothing is shared. Missing Anthropic breakpoints do not reduce it (owner ruling D1).
|
|
48
|
+
* - stable(r) is the same estimate over the deepest prefix shared with an earlier measured record of
|
|
49
|
+
* any age, then cap: the structure the app kept, whether or not the cache still held it.
|
|
50
|
+
* - E = sum cached / sum reusable, clamped to [0, 1]; 1 when nothing is reusable.
|
|
51
|
+
* - S = sum stable / sum prompt over records that continue an earlier record with the same conv; 1 when
|
|
52
|
+
* none. The cache lifetime does not affect S: a user's pause is not the app rewriting its prefix.
|
|
53
|
+
* G = output tokens of wasted calls / all output tokens, over every record. A call is wasted when
|
|
54
|
+
* (a) an earlier call has the same last node and the same params, (b) flags.retry_of is the
|
|
55
|
+
* position (0-based, in `records`, earlier than the retry) of a call with finish "length" and the
|
|
56
|
+
* same params, or (c) flags.parse_failed.
|
|
57
|
+
*
|
|
58
|
+
* A record is unmeasured (out of E and S, never zero reuse) when its finish is "error" (call_failed),
|
|
59
|
+
* its prompt_tokens is 0 or less (no_usage: the logger got no usage counts, so a broken integration
|
|
60
|
+
* refuses instead of scoring a perfect E), it has no ids, its node_chars is missing, empty, misaligned with ids.nodes, not non-decreasing
|
|
61
|
+
* or ends at 0, its ts is unreadable,
|
|
62
|
+
* or `factsFor` has no facts for its provider (throws or returns nothing: unknown_provider; throws an
|
|
63
|
+
* error with `code: "ambiguous_target"`: ambiguous_target, the profile lists several targets and the
|
|
64
|
+
* record's provider matches none of them).
|
|
65
|
+
*/
|
|
66
|
+
export declare function measuredScore(records: readonly UsageRecord[], factsFor: (provider: string) => CacheFacts): MeasuredScore | TooFewCalls;
|