@codyswann/lisa 6.0.10 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/all/copy-overwrite/scripts/lib/work-item-estimate-check.mjs +162 -0
  2. package/all/copy-overwrite/scripts/lib/work-item-estimate-config.d.mts +58 -0
  3. package/all/copy-overwrite/scripts/lib/work-item-estimate-config.mjs +210 -0
  4. package/all/copy-overwrite/scripts/lib/work-item-estimate.mjs +313 -0
  5. package/all/copy-overwrite/scripts/lisa-estimate.mjs +178 -0
  6. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  7. package/dist/core/lisa-owned-hash-ledger.js +42 -0
  8. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  9. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  10. package/dist/core/nightly-e2e-guard-behavior-certificate.js.map +1 -1
  11. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  12. package/dist/core/upstream-evidence-manifest.js +32 -8
  13. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  14. package/dist/sync/registry.d.ts.map +1 -1
  15. package/dist/sync/registry.js +2 -0
  16. package/dist/sync/registry.js.map +1 -1
  17. package/dist/sync/work-item-estimate-setting.d.ts +3 -0
  18. package/dist/sync/work-item-estimate-setting.d.ts.map +1 -0
  19. package/dist/sync/work-item-estimate-setting.js +24 -0
  20. package/dist/sync/work-item-estimate-setting.js.map +1 -0
  21. package/package.json +4 -4
  22. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  23. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  24. package/plugins/lisa/.codex-plugin/skills/lisa-github-validate-issue/SKILL.md +17 -1
  25. package/plugins/lisa/.codex-plugin/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  26. package/plugins/lisa/.codex-plugin/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  27. package/plugins/lisa/.codex-plugin/skills/lisa-usage-accounting/SKILL.md +7 -0
  28. package/plugins/lisa/.codex-plugin/skills/lisa-usage-accounting/references/estimate.md +71 -0
  29. package/plugins/lisa/rules/reference/config-resolution.md +1 -0
  30. package/plugins/lisa/rules/reference/work-item-definition-of-ready.md +18 -0
  31. package/plugins/lisa/skills/lisa-github-validate-issue/SKILL.md +17 -1
  32. package/plugins/lisa/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  33. package/plugins/lisa/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  34. package/plugins/lisa/skills/lisa-usage-accounting/SKILL.md +7 -0
  35. package/plugins/lisa/skills/lisa-usage-accounting/references/estimate.md +71 -0
  36. package/plugins/lisa-agy/plugin.json +1 -1
  37. package/plugins/lisa-agy/skills/lisa-github-validate-issue/SKILL.md +17 -1
  38. package/plugins/lisa-agy/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  39. package/plugins/lisa-agy/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  40. package/plugins/lisa-agy/skills/lisa-usage-accounting/SKILL.md +7 -0
  41. package/plugins/lisa-agy/skills/lisa-usage-accounting/references/estimate.md +71 -0
  42. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  43. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  44. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  45. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  46. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  48. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  49. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  50. package/plugins/lisa-copilot/rules/reference/config-resolution.md +1 -0
  51. package/plugins/lisa-copilot/rules/reference/work-item-definition-of-ready.md +18 -0
  52. package/plugins/lisa-copilot/skills/lisa-github-validate-issue/SKILL.md +17 -1
  53. package/plugins/lisa-copilot/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  54. package/plugins/lisa-copilot/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  55. package/plugins/lisa-copilot/skills/lisa-usage-accounting/SKILL.md +7 -0
  56. package/plugins/lisa-copilot/skills/lisa-usage-accounting/references/estimate.md +71 -0
  57. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  58. package/plugins/lisa-cursor/rules/config-resolution-reference.mdc +1 -0
  59. package/plugins/lisa-cursor/rules/work-item-definition-of-ready-reference.mdc +18 -0
  60. package/plugins/lisa-cursor/skills/lisa-github-validate-issue/SKILL.md +17 -1
  61. package/plugins/lisa-cursor/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  62. package/plugins/lisa-cursor/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  63. package/plugins/lisa-cursor/skills/lisa-usage-accounting/SKILL.md +7 -0
  64. package/plugins/lisa-cursor/skills/lisa-usage-accounting/references/estimate.md +71 -0
  65. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  66. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  67. package/plugins/lisa-devin/rules/reference/config-resolution.md +1 -0
  68. package/plugins/lisa-devin/rules/reference/work-item-definition-of-ready.md +18 -0
  69. package/plugins/lisa-devin/skills/lisa-github-validate-issue/SKILL.md +17 -1
  70. package/plugins/lisa-devin/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  71. package/plugins/lisa-devin/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  72. package/plugins/lisa-devin/skills/lisa-usage-accounting/SKILL.md +7 -0
  73. package/plugins/lisa-devin/skills/lisa-usage-accounting/references/estimate.md +71 -0
  74. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  76. package/plugins/lisa-expo-agy/plugin.json +1 -1
  77. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  78. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  79. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  80. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  81. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  83. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  84. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  85. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  86. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  87. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  88. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  90. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  91. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  92. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  93. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  94. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  95. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  96. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  97. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  98. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  99. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  100. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  101. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  102. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  103. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  104. package/plugins/lisa-rails-agy/plugin.json +1 -1
  105. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  106. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  107. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  108. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  109. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  110. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  111. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  112. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  113. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  114. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  115. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  116. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  117. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  118. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  119. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  120. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  121. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  122. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  123. package/plugins/src/base/rules/reference/config-resolution.md +1 -0
  124. package/plugins/src/base/rules/reference/work-item-definition-of-ready.md +18 -0
  125. package/plugins/src/base/skills/lisa-github-validate-issue/SKILL.md +17 -1
  126. package/plugins/src/base/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  127. package/plugins/src/base/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  128. package/plugins/src/base/skills/lisa-usage-accounting/SKILL.md +7 -0
  129. package/plugins/src/base/skills/lisa-usage-accounting/references/estimate.md +71 -0
  130. package/scripts/lisa-estimate.mjs +9 -0
  131. package/scripts/two-channel-couplings.json +1 -1
  132. package/typescript/copy-overwrite/.prettierignore +4 -0
@@ -0,0 +1,313 @@
1
+ // This file is managed by Lisa and IS replaced on each `lisa` run.
2
+ // Do not edit directly — durable changes belong upstream in Lisa.
3
+
4
+ /**
5
+ * The `## Lisa Estimate` section a work item carries: a token range, the
6
+ * matching dollar range, and a recommended model (CodySwannGT/lisa#4952,
7
+ * #4954).
8
+ *
9
+ * One managed section per item, rewritten in place. The visible table is for
10
+ * people; the single `<!-- lisa:estimate ... -->` token on its own line is what
11
+ * machines read. Every agent renders and checks it through this module, so the
12
+ * shape cannot drift between agents or trackers.
13
+ * @module scripts/lib/work-item-estimate
14
+ */
15
+ import {
16
+ costCents,
17
+ EstimateConfigError,
18
+ priceFor,
19
+ } from "./work-item-estimate-config.mjs";
20
+
21
+ /**
22
+ * @typedef {object} Estimate
23
+ * @property {number} tokensLow - Low end of the token range.
24
+ * @property {number} tokensHigh - High end of the token range.
25
+ * @property {number | null} costLow - Low end of the dollar range, in cents.
26
+ * @property {number | null} costHigh - High end of the dollar range, in cents.
27
+ * @property {string | null} currency - ISO currency of the dollar range.
28
+ * @property {string} pricingStatus - `estimated` or `missing`.
29
+ * @property {string | null} pricingSource - Price table the range came from.
30
+ * @property {string | null} model - Recommended model id or family.
31
+ * @property {string | null} basis - How the estimate was reached.
32
+ */
33
+
34
+ export const ESTIMATE_HEADING = "## Lisa Estimate";
35
+ const TOKEN_PATTERN = /<!-- lisa:estimate ((?:[a-z_]+=\S*\s+)+)-->/;
36
+ const MANAGED_NOTE =
37
+ "_Managed by Lisa. Rewritten in place on each estimate or tally; do not edit by hand._";
38
+
39
+ const encode = value => (value === null ? "null" : encodeURIComponent(value));
40
+ const decode = value => (value === "null" ? null : decodeURIComponent(value));
41
+ const dollars = cents => (cents / 100).toFixed(2);
42
+
43
+ /**
44
+ * Human-readable token count: 2000000 -> "2M", 1500 -> "1.5k".
45
+ * @param {number} tokens - Token count.
46
+ * @returns {string} Short form.
47
+ */
48
+ export function formatTokens(tokens) {
49
+ const short = (value, unit) =>
50
+ `${Number(value.toFixed(2)).toString()}${unit}`;
51
+ if (tokens >= 1_000_000) return short(tokens / 1_000_000, "M");
52
+ if (tokens >= 1_000) return short(tokens / 1_000, "k");
53
+ return String(tokens);
54
+ }
55
+
56
+ /**
57
+ * Parse a token count written as `2000000`, `2M`, `2.5m` or `500k`.
58
+ * @param {string} text - Count as written.
59
+ * @returns {number} Whole token count.
60
+ */
61
+ export function parseTokenCount(text) {
62
+ const match = /^(\d+(?:\.\d+)?)([kKmM]?)$/.exec(String(text).trim());
63
+ if (!match)
64
+ throw new EstimateConfigError(
65
+ `${JSON.stringify(text)} is not a token count. Write it as 2000000, 2M or 500k.`
66
+ );
67
+ const scale = { "": 1, k: 1_000, m: 1_000_000 }[match[2].toLowerCase()];
68
+ const tokens = Math.round(Number(match[1]) * scale);
69
+ if (tokens < 1)
70
+ throw new EstimateConfigError("A token estimate must be at least 1.");
71
+ return tokens;
72
+ }
73
+
74
+ /**
75
+ * Build an estimate from a token range and model, pricing it from config.
76
+ * @param {{tokensLow: number, tokensHigh: number, model: string, basis?: string}} input - Estimate inputs.
77
+ * @param {object} settings - Result of `resolveEstimateConfig`.
78
+ * @returns {Estimate} The estimate record.
79
+ */
80
+ export function buildEstimate(input, settings) {
81
+ const { tokensLow, tokensHigh, model } = input;
82
+ if (tokensLow > tokensHigh)
83
+ throw new EstimateConfigError(
84
+ `The low end (${tokensLow}) is above the high end (${tokensHigh}).`
85
+ );
86
+ if (!settings.models.includes(model))
87
+ throw new EstimateConfigError(
88
+ `Recommended model ${JSON.stringify(model)} is not in workItem.estimate.models: ${settings.models.join(", ")}.`
89
+ );
90
+ const price = priceFor(settings, model);
91
+ return {
92
+ tokensLow,
93
+ tokensHigh,
94
+ costLow: price
95
+ ? costCents(tokensLow, price.rates, settings.tokenMix)
96
+ : null,
97
+ costHigh: price
98
+ ? costCents(tokensHigh, price.rates, settings.tokenMix)
99
+ : null,
100
+ currency: price?.currency ?? null,
101
+ pricingStatus: price ? "estimated" : "missing",
102
+ pricingSource: price?.source ?? null,
103
+ model,
104
+ basis: input.basis?.trim() || "no calibration history",
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Render the managed section for an estimate.
110
+ * @param {Estimate} estimate - Result of {@link buildEstimate}.
111
+ * @returns {string} Markdown section, ending in a newline.
112
+ */
113
+ export function renderEstimateSection(estimate) {
114
+ const cost =
115
+ estimate.costLow === null
116
+ ? "unpriced"
117
+ : `$${dollars(estimate.costLow)}–$${dollars(estimate.costHigh)} ${estimate.currency}`;
118
+ const fields = [
119
+ `tokens_low=${estimate.tokensLow}`,
120
+ `tokens_high=${estimate.tokensHigh}`,
121
+ `cost_low=${estimate.costLow === null ? "null" : dollars(estimate.costLow)}`,
122
+ `cost_high=${estimate.costHigh === null ? "null" : dollars(estimate.costHigh)}`,
123
+ `currency=${encode(estimate.currency)}`,
124
+ `pricing_status=${estimate.pricingStatus}`,
125
+ `pricing_source=${encode(estimate.pricingSource)}`,
126
+ `model=${encode(estimate.model)}`,
127
+ `basis=${encode(estimate.basis)}`,
128
+ ];
129
+ return [
130
+ ESTIMATE_HEADING,
131
+ "",
132
+ MANAGED_NOTE,
133
+ "",
134
+ "| Row | Tokens | Cost | Model |",
135
+ "|---|---:|---:|---|",
136
+ `| Estimate | ${formatTokens(estimate.tokensLow)}–${formatTokens(estimate.tokensHigh)} | ${cost} | ${estimate.model} |`,
137
+ "",
138
+ `Basis: ${estimate.basis}`,
139
+ "",
140
+ `<!-- lisa:estimate ${fields.join(" ")} -->`,
141
+ "",
142
+ ].join("\n");
143
+ }
144
+
145
+ /**
146
+ * Locate every `## Lisa Estimate` section in a body, outside code fences.
147
+ * @param {string} body - Item body.
148
+ * @returns {{start: number, end: number}[]} Line spans, end exclusive.
149
+ */
150
+ function estimateSpans(body) {
151
+ const lines = body.split("\n");
152
+ const fenced = fencedLines(lines);
153
+ const heading = index => !fenced[index] && lines[index].startsWith("## ");
154
+ return lines.flatMap((line, index) => {
155
+ if (fenced[index] || line.trimEnd() !== ESTIMATE_HEADING) return [];
156
+ const next = lines.findIndex((_, at) => at > index && heading(at));
157
+ return [{ start: index, end: next === -1 ? lines.length : next }];
158
+ });
159
+ }
160
+
161
+ /**
162
+ * Which lines sit inside a fenced code block (fence lines included), so a
163
+ * section quoted in a fence is not mistaken for the managed one.
164
+ * @param {string[]} lines - Body lines.
165
+ * @returns {boolean[]} True for each line inside a fence.
166
+ */
167
+ function fencedLines(lines) {
168
+ let fence = null;
169
+ return lines.map(line => {
170
+ const marker = /^ {0,3}(`{3,}|~{3,})/.exec(line)?.[1];
171
+ if (fence === null) {
172
+ if (marker) fence = marker;
173
+ return fence !== null;
174
+ }
175
+ if (marker && marker[0] === fence[0] && marker.length >= fence.length)
176
+ fence = null;
177
+ return true;
178
+ });
179
+ }
180
+
181
+ /**
182
+ * Read the estimate token out of a section.
183
+ * @param {string} section - Section text.
184
+ * @returns {Estimate | null} Parsed estimate, or null when no token parses.
185
+ */
186
+ function parseToken(section) {
187
+ try {
188
+ return parseTokenFields(section);
189
+ } catch (error) {
190
+ // A malformed percent escape makes the token unparseable, not the gate.
191
+ if (error instanceof URIError) return null;
192
+ throw error;
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Decode the estimate token's fields; throws URIError on a bad escape.
198
+ * @param {string} section - Section text.
199
+ * @returns {Estimate | null} Parsed estimate, or null when no token matches.
200
+ */
201
+ function parseTokenFields(section) {
202
+ const match = TOKEN_PATTERN.exec(section);
203
+ if (!match) return null;
204
+ const fields = Object.fromEntries(
205
+ match[1]
206
+ .trim()
207
+ .split(/\s+/)
208
+ .map(pair => [
209
+ pair.slice(0, pair.indexOf("=")),
210
+ pair.slice(pair.indexOf("=") + 1),
211
+ ])
212
+ );
213
+ const cents = value =>
214
+ value === "null" || value === undefined
215
+ ? null
216
+ : Math.round(Number(value) * 100);
217
+ return {
218
+ tokensLow: Number(fields.tokens_low),
219
+ tokensHigh: Number(fields.tokens_high),
220
+ costLow: cents(fields.cost_low),
221
+ costHigh: cents(fields.cost_high),
222
+ currency: decode(fields.currency ?? "null"),
223
+ pricingStatus: fields.pricing_status,
224
+ pricingSource: decode(fields.pricing_source ?? "null"),
225
+ model: decode(fields.model ?? "null"),
226
+ basis: decode(fields.basis ?? "null"),
227
+ };
228
+ }
229
+
230
+ /**
231
+ * Parse the item's estimate section.
232
+ * @param {string} body - Item body.
233
+ * @returns {{count: number, estimate: Estimate | null}} Section count and its parsed token.
234
+ */
235
+ export function parseEstimateSection(body) {
236
+ const spans = estimateSpans(body);
237
+ if (spans.length !== 1) return { count: spans.length, estimate: null };
238
+ const lines = body.split("\n").slice(spans[0].start, spans[0].end);
239
+ return { count: 1, estimate: parseToken(lines.join("\n")) };
240
+ }
241
+
242
+ /**
243
+ * The lines Lisa itself wrote in a section: exactly what rendering its own
244
+ * token produces. Anything else in the section is a person's text. A section
245
+ * with no parseable token has no Lisa-written lines but its heading.
246
+ * @param {string} section - Section text, heading first.
247
+ * @returns {string[]} Trimmed, non-empty managed lines (a multiset).
248
+ */
249
+ export function managedEstimateLines(section) {
250
+ const estimate = parseToken(section);
251
+ const rendered = estimate
252
+ ? renderEstimateSection(estimate)
253
+ : ESTIMATE_HEADING;
254
+ return rendered
255
+ .split("\n")
256
+ .map(line => line.trim())
257
+ .filter(Boolean);
258
+ }
259
+
260
+ /**
261
+ * The lines of a section that Lisa did not write, in order.
262
+ * @param {string} section - Section text, heading first.
263
+ * @returns {string[]} A person's lines, blank lines dropped.
264
+ */
265
+ export function humanLines(section) {
266
+ const managed = managedEstimateLines(section);
267
+ return section.split("\n").filter(line => {
268
+ const at = managed.indexOf(line.trim());
269
+ if (at !== -1) managed.splice(at, 1);
270
+ return at === -1 && line.trim() !== "";
271
+ });
272
+ }
273
+
274
+ /**
275
+ * Put a person's lines back into a freshly rendered section, before the
276
+ * machine tokens.
277
+ * @param {string[]} rendered - Rendered section lines.
278
+ * @param {string[]} human - A person's lines from the old section.
279
+ * @returns {string[]} Section lines.
280
+ */
281
+ function withHumanLines(rendered, human) {
282
+ if (!human.length) return rendered;
283
+ const token = rendered.findIndex(line => line.startsWith("<!-- lisa:"));
284
+ const at = token === -1 ? rendered.length : token;
285
+ return [...rendered.slice(0, at), ...human, "", ...rendered.slice(at)];
286
+ }
287
+
288
+ /**
289
+ * Put the estimate section into a body, replacing an existing one in place.
290
+ * Lines a person added inside the old section are kept; only the lines Lisa
291
+ * wrote are replaced.
292
+ * @param {string} body - Item body.
293
+ * @param {string} section - Rendered section.
294
+ * @returns {string} Updated body.
295
+ */
296
+ export function upsertEstimateSection(body, section) {
297
+ const spans = estimateSpans(body);
298
+ if (spans.length > 1)
299
+ throw new EstimateConfigError(
300
+ "The body has more than one Lisa Estimate section; remove the extra copies first."
301
+ );
302
+ const lines = body.split("\n");
303
+ if (spans.length === 0) return `${body.trimEnd()}\n\n${section}`;
304
+ const previous = lines.slice(spans[0].start, spans[0].end).join("\n");
305
+ const replaced = [
306
+ ...lines.slice(0, spans[0].start),
307
+ ...withHumanLines(section.trimEnd().split("\n"), humanLines(previous)),
308
+ ...(spans[0].end < lines.length
309
+ ? ["", ...lines.slice(spans[0].end)]
310
+ : [""]),
311
+ ];
312
+ return replaced.join("\n");
313
+ }
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env node
2
+ // This file is managed by Lisa and IS replaced on each `lisa` run.
3
+ // Do not edit directly — durable changes belong upstream in Lisa.
4
+
5
+ /**
6
+ * @file lisa-estimate.mjs
7
+ * @description Render and check a work item's token estimate
8
+ * (CodySwannGT/lisa#4952, #4954).
9
+ *
10
+ * A plain Node script, so it runs the same under every coding agent Lisa
11
+ * supports. It never talks to a tracker: the caller reads the item body,
12
+ * passes it in, and writes back what this prints.
13
+ *
14
+ * Usage:
15
+ * node scripts/lisa-estimate.mjs config
16
+ * node scripts/lisa-estimate.mjs render --tokens-low 2M --tokens-high 4M
17
+ * --model claude-opus-5-5 [--basis <text>] [--body-file <path>]
18
+ * node scripts/lisa-estimate.mjs check [--phase ready] [--body-file <path>]
19
+ *
20
+ * `check` exits 0 on PASS or N/A and 1 on FAIL; any usage or config error
21
+ * exits 2. Without `--body-file`, the body is read from standard input.
22
+ * @module lisa-estimate
23
+ */
24
+ import { existsSync, readFileSync } from "node:fs";
25
+ import { join } from "node:path";
26
+
27
+ import { invokedAsScript } from "./lib/invoked-as-script.mjs";
28
+ import {
29
+ DEFAULT_ESTIMATE_PRICING,
30
+ EstimateConfigError,
31
+ resolveEstimateConfig,
32
+ } from "./lib/work-item-estimate-config.mjs";
33
+ import {
34
+ buildEstimate,
35
+ parseTokenCount,
36
+ renderEstimateSection,
37
+ upsertEstimateSection,
38
+ } from "./lib/work-item-estimate.mjs";
39
+ import { checkEstimate } from "./lib/work-item-estimate-check.mjs";
40
+
41
+ const VALUE_FLAGS = new Set([
42
+ "--tokens-low",
43
+ "--tokens-high",
44
+ "--model",
45
+ "--basis",
46
+ "--body-file",
47
+ "--phase",
48
+ "--cwd",
49
+ ]);
50
+
51
+ /**
52
+ * Parse `--flag value` pairs.
53
+ * @param {string[]} args - Arguments after the subcommand.
54
+ * @returns {Record<string, string>} Flag values keyed without the dashes.
55
+ */
56
+ function parseFlags(args) {
57
+ if (
58
+ args.length % 2 !== 0 ||
59
+ args.some((arg, i) => i % 2 === 0 && !VALUE_FLAGS.has(arg))
60
+ )
61
+ throw new EstimateConfigError(
62
+ `Unrecognised arguments ${JSON.stringify(args)}. Run with --help for the options.`
63
+ );
64
+ return Object.fromEntries(
65
+ args.flatMap((arg, i) => (i % 2 === 0 ? [[arg.slice(2), args[i + 1]]] : []))
66
+ );
67
+ }
68
+
69
+ const readJson = path =>
70
+ existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : {};
71
+
72
+ /**
73
+ * The project's Lisa config with `.lisa.config.local.json` layered on top,
74
+ * key by key.
75
+ * @param {string} cwd - Project root.
76
+ * @returns {object} Effective config.
77
+ */
78
+ export function readProjectConfig(cwd) {
79
+ const merge = (base, override) => {
80
+ const plain = value =>
81
+ value !== null && typeof value === "object" && !Array.isArray(value);
82
+ if (!plain(base) || !plain(override)) return override ?? base;
83
+ const keys = new Set([...Object.keys(base), ...Object.keys(override)]);
84
+ return Object.fromEntries(
85
+ [...keys].map(key => [
86
+ key,
87
+ key in override ? merge(base[key], override[key]) : base[key],
88
+ ])
89
+ );
90
+ };
91
+ return merge(
92
+ readJson(join(cwd, ".lisa.config.json")),
93
+ readJson(join(cwd, ".lisa.config.local.json"))
94
+ );
95
+ }
96
+
97
+ const readBody = flags =>
98
+ flags["body-file"] === undefined
99
+ ? readFileSync(0, "utf8")
100
+ : readFileSync(flags["body-file"], "utf8");
101
+
102
+ const COMMANDS = {
103
+ config: (_flags, settings) => ({
104
+ out: `${JSON.stringify({ ...settings, defaultPricing: DEFAULT_ESTIMATE_PRICING }, null, 2)}\n`,
105
+ code: 0,
106
+ }),
107
+ render: (flags, settings) => {
108
+ const section = renderEstimateSection(
109
+ buildEstimate(
110
+ {
111
+ tokensLow: parseTokenCount(flags["tokens-low"] ?? ""),
112
+ tokensHigh: parseTokenCount(flags["tokens-high"] ?? ""),
113
+ model: flags.model ?? "",
114
+ basis: flags.basis,
115
+ },
116
+ settings
117
+ )
118
+ );
119
+ return {
120
+ out:
121
+ flags["body-file"] === undefined
122
+ ? section
123
+ : upsertEstimateSection(readBody(flags), section),
124
+ code: 0,
125
+ };
126
+ },
127
+ check: (flags, settings) => {
128
+ const result = checkEstimate(readBody(flags), settings, flags.phase);
129
+ const lines = [`S21 token estimate: ${result.verdict}`, ...result.lines];
130
+ return {
131
+ out: `${lines.join("\n")}\n`,
132
+ code: result.verdict === "FAIL" ? 1 : 0,
133
+ };
134
+ },
135
+ };
136
+
137
+ const USAGE =
138
+ "Usage: lisa-estimate.mjs config | render --tokens-low <n> --tokens-high <n> --model <id> [--basis <text>] [--body-file <path>] | check [--phase ready] [--body-file <path>]\n";
139
+
140
+ /**
141
+ * Run the CLI.
142
+ * @param {string[]} argv - Arguments after the script name.
143
+ * @param {{stdout: {write: Function}, stderr: {write: Function}}} io - Output streams.
144
+ * @returns {number} Exit code.
145
+ */
146
+ export function runCli(argv = process.argv.slice(2), io = process) {
147
+ const [command, ...rest] = argv;
148
+ if (command === "--help" || command === "help") {
149
+ io.stdout.write(USAGE);
150
+ return 0;
151
+ }
152
+ if (!Object.hasOwn(COMMANDS, command ?? "")) {
153
+ io.stderr.write(USAGE);
154
+ return 2;
155
+ }
156
+ try {
157
+ const flags = parseFlags(rest);
158
+ const settings = resolveEstimateConfig(
159
+ readProjectConfig(flags.cwd ?? process.cwd())
160
+ );
161
+ const { out, code } = COMMANDS[command](flags, settings);
162
+ io.stdout.write(out);
163
+ return code;
164
+ } catch (error) {
165
+ if (
166
+ !(
167
+ error instanceof EstimateConfigError ||
168
+ error instanceof RangeError ||
169
+ error instanceof SyntaxError
170
+ )
171
+ )
172
+ throw error;
173
+ io.stderr.write(`lisa-estimate: ${error.message}\n`);
174
+ return 2;
175
+ }
176
+ }
177
+
178
+ if (invokedAsScript(import.meta.url)) process.exitCode = runCli();
@@ -1 +1 @@
1
- {"version":3,"file":"lisa-owned-hash-ledger.d.ts","sourceRoot":"","sources":["../../src/core/lisa-owned-hash-ledger.ts"],"names":[],"mappings":"AACA,6EAA6E;AAE7E;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAC3C,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAm6FjC,CAAC;AAEH;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,+BAA+B,EAAE,QAAQ,CACpD,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAs0FjC,CAAC"}
1
+ {"version":3,"file":"lisa-owned-hash-ledger.d.ts","sourceRoot":"","sources":["../../src/core/lisa-owned-hash-ledger.ts"],"names":[],"mappings":"AACA,6EAA6E;AAE7E;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAC3C,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAw7FjC,CAAC;AAEH;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,+BAA+B,EAAE,QAAQ,CACpD,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CA21FjC,CAAC"}
@@ -1535,6 +1535,23 @@ export const LISA_OWNED_HASH_LEDGER = Object.freeze({
1535
1535
  "5b978f201be9a035001f0cc4a7e8da76a67f26e6f99da91907747c4d65d58f1b",
1536
1536
  "d73b15152703d7f195d1e06f7d7cee5437373237fcd74fe68afbd79fccebc1ba",
1537
1537
  ]),
1538
+ "scripts/lib/work-item-estimate-check.mjs": Object.freeze([
1539
+ "445732d190b31aec81594bd5fb5a82bc5b1ed16d3a6ff6fa0fe5fb287706a878",
1540
+ "4dcea21ae9fe5114964abda7dbadeb5365241893e7e96d83411c27e1b777f5b9",
1541
+ "94e67b413ed4ec852bfa2d594a89deedf776f94d7fffde049a9fd6f2d4365bad",
1542
+ ]),
1543
+ "scripts/lib/work-item-estimate-config.d.mts": Object.freeze([
1544
+ "68ba3eba29ed10a0ca3488e35722882413a85eaa2105b0266f3edfa121d9bb86",
1545
+ ]),
1546
+ "scripts/lib/work-item-estimate-config.mjs": Object.freeze([
1547
+ "dea9d7e5440646b8bd4122846bf587a2f7cfff95e720b43302dda98b91c58b5f",
1548
+ ]),
1549
+ "scripts/lib/work-item-estimate.mjs": Object.freeze([
1550
+ "2f97bd0765cb17b4831532e4fc03558d83533312a1e864cd10249f1bb2398f9f",
1551
+ "87339af7a7ae14f53994f817566d77434769f8000e8da869696bfe664f822166",
1552
+ "aaedf6097e34404416cdb6eed186a34bf07f04b143df9e6ff5771e6e7420164f",
1553
+ "d936f38602d428ef26d41a09458c61b896106763255b2f6d475a7b963b917079",
1554
+ ]),
1538
1555
  "scripts/lib/worktree-dependencies.d.mts": Object.freeze([
1539
1556
  "66f77b767869f57185b819ca6f294ad0706348b50eba12f78af2430d9c8775df",
1540
1557
  "acc2b7a00f1e6f806359c6609e58743b6293f17f3a5ac30abd18b1a22fab8bfc",
@@ -1667,6 +1684,10 @@ export const LISA_OWNED_HASH_LEDGER = Object.freeze({
1667
1684
  "f8c8e276fa6d68ed661f07882f20ee095d18a5e8a90136f78f1ed2b032ce33d4",
1668
1685
  "f936bc8a038a7901ade1ba58cc52e5f5a8b8b2a52a9f0c1516567cc80eb02ccb",
1669
1686
  ]),
1687
+ "scripts/lisa-estimate.mjs": Object.freeze([
1688
+ "3e154026f7308947f1d33305d9563d6fdd951ccbfc7f4372f4e48211092bc119",
1689
+ "f5bcb51089ae8c0fb5f0a3e880a532f19ca7f9c5f0f7eadddb9c47cfccbf0965",
1690
+ ]),
1670
1691
  "scripts/lisa-evidence-prune.mjs": Object.freeze([
1671
1692
  "2a2862f78f8b50064bbd3a44133f2c8a4b4bb5715189b7218e4e0e8c30f51cb5",
1672
1693
  "878b2014be5a0bedc8b4899d0e961dfea5b1147ff0a694a9fac00f77121f035a",
@@ -4508,6 +4529,23 @@ export const LISA_OWNED_HASH_HISTORY_DERIVED = Object.freeze({
4508
4529
  "5b978f201be9a035001f0cc4a7e8da76a67f26e6f99da91907747c4d65d58f1b",
4509
4530
  "d73b15152703d7f195d1e06f7d7cee5437373237fcd74fe68afbd79fccebc1ba",
4510
4531
  ]),
4532
+ "scripts/lib/work-item-estimate-check.mjs": Object.freeze([
4533
+ "445732d190b31aec81594bd5fb5a82bc5b1ed16d3a6ff6fa0fe5fb287706a878",
4534
+ "4dcea21ae9fe5114964abda7dbadeb5365241893e7e96d83411c27e1b777f5b9",
4535
+ "94e67b413ed4ec852bfa2d594a89deedf776f94d7fffde049a9fd6f2d4365bad",
4536
+ ]),
4537
+ "scripts/lib/work-item-estimate-config.d.mts": Object.freeze([
4538
+ "68ba3eba29ed10a0ca3488e35722882413a85eaa2105b0266f3edfa121d9bb86",
4539
+ ]),
4540
+ "scripts/lib/work-item-estimate-config.mjs": Object.freeze([
4541
+ "dea9d7e5440646b8bd4122846bf587a2f7cfff95e720b43302dda98b91c58b5f",
4542
+ ]),
4543
+ "scripts/lib/work-item-estimate.mjs": Object.freeze([
4544
+ "2f97bd0765cb17b4831532e4fc03558d83533312a1e864cd10249f1bb2398f9f",
4545
+ "87339af7a7ae14f53994f817566d77434769f8000e8da869696bfe664f822166",
4546
+ "aaedf6097e34404416cdb6eed186a34bf07f04b143df9e6ff5771e6e7420164f",
4547
+ "d936f38602d428ef26d41a09458c61b896106763255b2f6d475a7b963b917079",
4548
+ ]),
4511
4549
  "scripts/lib/worktree-dependencies.d.mts": Object.freeze([
4512
4550
  "66f77b767869f57185b819ca6f294ad0706348b50eba12f78af2430d9c8775df",
4513
4551
  "acc2b7a00f1e6f806359c6609e58743b6293f17f3a5ac30abd18b1a22fab8bfc",
@@ -4639,6 +4677,10 @@ export const LISA_OWNED_HASH_HISTORY_DERIVED = Object.freeze({
4639
4677
  "f8c8e276fa6d68ed661f07882f20ee095d18a5e8a90136f78f1ed2b032ce33d4",
4640
4678
  "f936bc8a038a7901ade1ba58cc52e5f5a8b8b2a52a9f0c1516567cc80eb02ccb",
4641
4679
  ]),
4680
+ "scripts/lisa-estimate.mjs": Object.freeze([
4681
+ "3e154026f7308947f1d33305d9563d6fdd951ccbfc7f4372f4e48211092bc119",
4682
+ "f5bcb51089ae8c0fb5f0a3e880a532f19ca7f9c5f0f7eadddb9c47cfccbf0965",
4683
+ ]),
4642
4684
  "scripts/lisa-evidence-prune.mjs": Object.freeze([
4643
4685
  "2a2862f78f8b50064bbd3a44133f2c8a4b4bb5715189b7218e4e0e8c30f51cb5",
4644
4686
  "878b2014be5a0bedc8b4899d0e961dfea5b1147ff0a694a9fac00f77121f035a",