@codyswann/lisa 6.0.9 → 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 (137) 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/all/copy-overwrite/scripts/lisa-hooks/sonar-secrets.sh +130 -47
  7. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  8. package/dist/core/lisa-owned-hash-ledger.js +44 -0
  9. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  10. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  11. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  12. package/dist/core/upstream-evidence-manifest.js +35 -10
  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/hooks/sonar-secrets.sh +130 -47
  30. package/plugins/lisa/rules/reference/config-resolution.md +1 -0
  31. package/plugins/lisa/rules/reference/work-item-definition-of-ready.md +18 -0
  32. package/plugins/lisa/skills/lisa-github-validate-issue/SKILL.md +17 -1
  33. package/plugins/lisa/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  34. package/plugins/lisa/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  35. package/plugins/lisa/skills/lisa-usage-accounting/SKILL.md +7 -0
  36. package/plugins/lisa/skills/lisa-usage-accounting/references/estimate.md +71 -0
  37. package/plugins/lisa-agy/plugin.json +1 -1
  38. package/plugins/lisa-agy/skills/lisa-github-validate-issue/SKILL.md +17 -1
  39. package/plugins/lisa-agy/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  40. package/plugins/lisa-agy/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  41. package/plugins/lisa-agy/skills/lisa-usage-accounting/SKILL.md +7 -0
  42. package/plugins/lisa-agy/skills/lisa-usage-accounting/references/estimate.md +71 -0
  43. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  44. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  45. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  46. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  48. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  50. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  51. package/plugins/lisa-copilot/hooks/sonar-secrets.sh +130 -47
  52. package/plugins/lisa-copilot/rules/reference/config-resolution.md +1 -0
  53. package/plugins/lisa-copilot/rules/reference/work-item-definition-of-ready.md +18 -0
  54. package/plugins/lisa-copilot/skills/lisa-github-validate-issue/SKILL.md +17 -1
  55. package/plugins/lisa-copilot/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  56. package/plugins/lisa-copilot/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  57. package/plugins/lisa-copilot/skills/lisa-usage-accounting/SKILL.md +7 -0
  58. package/plugins/lisa-copilot/skills/lisa-usage-accounting/references/estimate.md +71 -0
  59. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  60. package/plugins/lisa-cursor/hooks/sonar-secrets.sh +130 -47
  61. package/plugins/lisa-cursor/rules/config-resolution-reference.mdc +1 -0
  62. package/plugins/lisa-cursor/rules/work-item-definition-of-ready-reference.mdc +18 -0
  63. package/plugins/lisa-cursor/skills/lisa-github-validate-issue/SKILL.md +17 -1
  64. package/plugins/lisa-cursor/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  65. package/plugins/lisa-cursor/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  66. package/plugins/lisa-cursor/skills/lisa-usage-accounting/SKILL.md +7 -0
  67. package/plugins/lisa-cursor/skills/lisa-usage-accounting/references/estimate.md +71 -0
  68. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  69. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  70. package/plugins/lisa-devin/hooks/sonar-secrets.sh +130 -47
  71. package/plugins/lisa-devin/rules/reference/config-resolution.md +1 -0
  72. package/plugins/lisa-devin/rules/reference/work-item-definition-of-ready.md +18 -0
  73. package/plugins/lisa-devin/skills/lisa-github-validate-issue/SKILL.md +17 -1
  74. package/plugins/lisa-devin/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  75. package/plugins/lisa-devin/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  76. package/plugins/lisa-devin/skills/lisa-usage-accounting/SKILL.md +7 -0
  77. package/plugins/lisa-devin/skills/lisa-usage-accounting/references/estimate.md +71 -0
  78. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  79. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  80. package/plugins/lisa-expo-agy/plugin.json +1 -1
  81. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  83. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  84. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  85. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  86. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  87. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  88. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  90. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  91. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  92. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  93. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  94. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  95. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  96. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  97. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  98. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  99. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  100. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  101. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  102. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  103. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  104. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  105. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  106. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  107. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  108. package/plugins/lisa-rails-agy/plugin.json +1 -1
  109. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  110. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  111. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  112. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  113. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  114. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  115. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  116. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  117. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  118. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  119. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  120. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  121. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  122. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  123. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  124. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  125. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  126. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  127. package/plugins/src/base/hooks/sonar-secrets.sh +130 -47
  128. package/plugins/src/base/rules/reference/config-resolution.md +1 -0
  129. package/plugins/src/base/rules/reference/work-item-definition-of-ready.md +18 -0
  130. package/plugins/src/base/skills/lisa-github-validate-issue/SKILL.md +17 -1
  131. package/plugins/src/base/skills/lisa-jira-validate-ticket/SKILL.md +17 -1
  132. package/plugins/src/base/skills/lisa-linear-validate-issue/SKILL.md +16 -0
  133. package/plugins/src/base/skills/lisa-usage-accounting/SKILL.md +7 -0
  134. package/plugins/src/base/skills/lisa-usage-accounting/references/estimate.md +71 -0
  135. package/scripts/lisa-estimate.mjs +9 -0
  136. package/scripts/two-channel-couplings.json +1 -1
  137. package/typescript/copy-overwrite/.prettierignore +4 -0
@@ -0,0 +1,162 @@
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 work-item estimate gate (CodySwannGT/lisa#4954): validator gate S21.
6
+ *
7
+ * It runs at ready time. An estimate is not required when an item is created
8
+ * or made ready, because deterministic sources (Sentry, New Relic, monitors)
9
+ * cannot produce one; the claim step writes it before work begins. What this
10
+ * gate refuses is an estimate that is present but wrong: unparseable, an
11
+ * inverted range, a model the project does not allow, a dollar range that is
12
+ * not a sound range of its own, or one that disagrees with the price table it
13
+ * names. When `workItem.estimate.required`
14
+ * is false the gate is `N/A` and blocks nothing.
15
+ * @module scripts/lib/work-item-estimate-check
16
+ */
17
+ import { costCents, priceFor } from "./work-item-estimate-config.mjs";
18
+ import { parseEstimateSection } from "./work-item-estimate.mjs";
19
+
20
+ const PHASES = new Set(["ready"]);
21
+
22
+ /**
23
+ * Problems with a parsed estimate's own fields.
24
+ * @param {object} estimate - Parsed estimate.
25
+ * @param {object} settings - Effective estimate settings.
26
+ * @returns {string[]} Problems, empty when the fields are sound.
27
+ */
28
+ function fieldProblems(estimate, settings) {
29
+ const { tokensLow, tokensHigh, model } = estimate;
30
+ const whole = value => Number.isSafeInteger(value) && value >= 1;
31
+ return [
32
+ ...(whole(tokensLow) && whole(tokensHigh)
33
+ ? []
34
+ : ["The token range must be two whole numbers of at least 1."]),
35
+ ...(tokensLow > tokensHigh
36
+ ? [`The low end (${tokensLow}) is above the high end (${tokensHigh}).`]
37
+ : []),
38
+ ...(settings.models.includes(model)
39
+ ? []
40
+ : [
41
+ `Recommended model ${JSON.stringify(model)} is not in workItem.estimate.models: ${settings.models.join(", ")}.`,
42
+ ]),
43
+ ];
44
+ }
45
+
46
+ /**
47
+ * Problems a priced dollar range has on its own, whichever price table it
48
+ * names: each end a finite amount of at least zero, the low end not above the
49
+ * high end, and an ISO 4217 currency code.
50
+ * @param {object} estimate - Parsed estimate carrying both ends and a currency.
51
+ * @returns {string[]} Problems, empty when the range is sound.
52
+ */
53
+ function rangeProblems(estimate) {
54
+ const { costLow, costHigh, currency } = estimate;
55
+ const amount = cents => Number.isFinite(cents) && cents >= 0;
56
+ if (!amount(costLow) || !amount(costHigh))
57
+ return ["The dollar range must be two finite amounts of at least 0."];
58
+ return [
59
+ ...(costLow > costHigh
60
+ ? [
61
+ `The low cost (${(costLow / 100).toFixed(2)}) is above the high cost (${(costHigh / 100).toFixed(2)}).`,
62
+ ]
63
+ : []),
64
+ ...(/^[A-Z]{3}$/.test(currency)
65
+ ? []
66
+ : [
67
+ `The currency ${JSON.stringify(currency)} is not a three-letter ISO 4217 code such as USD.`,
68
+ ]),
69
+ ];
70
+ }
71
+
72
+ /**
73
+ * Problems with a parsed estimate's dollar range.
74
+ * @param {object} estimate - Parsed estimate.
75
+ * @param {object} settings - Effective estimate settings.
76
+ * @returns {string[]} Problems, empty when the dollar range is sound.
77
+ */
78
+ function pricingProblems(estimate, settings) {
79
+ if (estimate.pricingStatus === "missing")
80
+ return estimate.costLow === null && estimate.costHigh === null
81
+ ? []
82
+ : [
83
+ "An unpriced estimate (pricing_status=missing) must not carry a dollar range.",
84
+ ];
85
+ if (estimate.pricingStatus !== "estimated")
86
+ return [
87
+ `pricing_status must be estimated or missing, not ${JSON.stringify(estimate.pricingStatus)}.`,
88
+ ];
89
+ if (
90
+ estimate.costLow === null ||
91
+ estimate.costHigh === null ||
92
+ !estimate.currency
93
+ )
94
+ return ["A priced estimate needs a dollar range and a currency."];
95
+ const ownProblems = rangeProblems(estimate);
96
+ if (ownProblems.length) return ownProblems;
97
+ const price = priceFor(settings, estimate.model);
98
+ // A price table that has moved on since the estimate was written is not a
99
+ // defect in the item; only a range that disagrees with its own table is.
100
+ if (!price || price.source !== estimate.pricingSource) return [];
101
+ const low = costCents(estimate.tokensLow, price.rates, settings.tokenMix);
102
+ const high = costCents(estimate.tokensHigh, price.rates, settings.tokenMix);
103
+ return low === estimate.costLow &&
104
+ high === estimate.costHigh &&
105
+ estimate.currency === price.currency
106
+ ? []
107
+ : [
108
+ `The dollar range does not match ${price.source}: expected ${(low / 100).toFixed(2)}–${(high / 100).toFixed(2)} ${price.currency}. Re-render it with lisa-estimate render.`,
109
+ ];
110
+ }
111
+
112
+ /**
113
+ * Run the estimate gate on an item body.
114
+ * @param {string} body - Item body as stored.
115
+ * @param {object} settings - Result of `resolveEstimateConfig`.
116
+ * @param {string} [phase] - Lifecycle moment; only `ready` exists so far.
117
+ * @returns {{verdict: "PASS" | "FAIL" | "N/A", lines: string[]}} Gate result.
118
+ */
119
+ export function checkEstimate(body, settings, phase = "ready") {
120
+ if (!PHASES.has(phase))
121
+ throw new RangeError(
122
+ `Unknown phase ${JSON.stringify(phase)}; expected ready.`
123
+ );
124
+ const warnings = settings.warnings ?? [];
125
+ if (!settings.required)
126
+ return {
127
+ verdict: "N/A",
128
+ lines: ["workItem.estimate.required is false; no estimate is required."],
129
+ };
130
+ const { count, estimate } = parseEstimateSection(body);
131
+ if (count > 1)
132
+ return {
133
+ verdict: "FAIL",
134
+ lines: [
135
+ ...warnings,
136
+ `The body has ${count} Lisa Estimate sections; keep exactly one.`,
137
+ ],
138
+ };
139
+ if (count === 0)
140
+ return {
141
+ verdict: "PASS",
142
+ lines: [
143
+ ...warnings,
144
+ "No estimate yet. That is allowed until the item is claimed: the claiming agent estimates it and writes it before any work begins.",
145
+ ],
146
+ };
147
+ if (!estimate)
148
+ return {
149
+ verdict: "FAIL",
150
+ lines: [
151
+ ...warnings,
152
+ "The Lisa Estimate section has no parseable <!-- lisa:estimate ... --> token. Re-render it with lisa-estimate render.",
153
+ ],
154
+ };
155
+ const problems = [
156
+ ...fieldProblems(estimate, settings),
157
+ ...pricingProblems(estimate, settings),
158
+ ];
159
+ return problems.length
160
+ ? { verdict: "FAIL", lines: [...warnings, ...problems] }
161
+ : { verdict: "PASS", lines: [...warnings, "The estimate is well formed."] };
162
+ }
@@ -0,0 +1,58 @@
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
+ /** Per-million-token rates for one model. */
5
+ export interface EstimateRates {
6
+ readonly inputPer1M?: number;
7
+ readonly cachedInputPer1M?: number;
8
+ readonly outputPer1M?: number;
9
+ }
10
+ /** Shares of an estimate billed at each rate; they add up to 1. */
11
+ export interface EstimateTokenMix {
12
+ readonly input: number;
13
+ readonly cachedInput: number;
14
+ readonly output: number;
15
+ }
16
+ /** The effective `workItem.estimate` settings for a project. */
17
+ export interface EstimateSettings {
18
+ readonly required: boolean;
19
+ readonly warnings: readonly string[];
20
+ readonly models: readonly string[];
21
+ readonly tokenMix: EstimateTokenMix;
22
+ readonly usagePricing: unknown;
23
+ }
24
+ /** Recommended-model values a project gets when it does not list its own. */
25
+ export declare const DEFAULT_ESTIMATE_MODELS: readonly string[];
26
+ /** The `workItem.estimate` value `lisa sync` writes into a host config. */
27
+ export declare const DEFAULT_WORK_ITEM_ESTIMATE: Readonly<{
28
+ required: true;
29
+ models: readonly string[];
30
+ }>;
31
+ /** Default token mix: mostly cached input. */
32
+ export declare const DEFAULT_TOKEN_MIX: EstimateTokenMix;
33
+ /** Shipped default price table (Anthropic API list prices). */
34
+ export declare const DEFAULT_ESTIMATE_PRICING: Readonly<{
35
+ currency: string;
36
+ source: string;
37
+ snapshot: string;
38
+ models: Readonly<Record<string, EstimateRates>>;
39
+ }>;
40
+ /** A configuration value that cannot be used. */
41
+ export declare class EstimateConfigError extends Error {}
42
+ /** Validate a `workItem.estimate` value; throws when it cannot be used. */
43
+ export declare function validateEstimateConfig(value: unknown): object;
44
+ /** The effective estimate settings for a merged Lisa config. */
45
+ export declare function resolveEstimateConfig(
46
+ config: unknown
47
+ ): EstimateSettings;
48
+ /** The price that covers a model, or null when unpriced. */
49
+ export declare function priceFor(
50
+ settings: EstimateSettings,
51
+ model: string
52
+ ): { rates: EstimateRates; currency: string; source: string } | null;
53
+ /** Dollar cost of a token count, in whole cents. */
54
+ export declare function costCents(
55
+ tokens: number,
56
+ rates: EstimateRates,
57
+ mix: EstimateTokenMix
58
+ ): number;
@@ -0,0 +1,210 @@
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
+ * Configuration and pricing for work-item token estimates
6
+ * (CodySwannGT/lisa#4952, #4954).
7
+ *
8
+ * `workItem.estimate` in `.lisa.config.json` decides whether every work item
9
+ * must carry a token estimate and which recommended-model values are allowed.
10
+ * It is required by default: an absent key means `required: true`, and a value
11
+ * that is present but not a boolean fails safe to `true`, the stricter option.
12
+ *
13
+ * Dollar figures come from `usage.pricing` (the existing pricing key the
14
+ * usage-accounting contract reads) when it prices the model, and otherwise from
15
+ * {@link DEFAULT_ESTIMATE_PRICING}: Anthropic's published first-party API list
16
+ * prices. A model neither table prices gets no dollar figure, never a guess.
17
+ * @module scripts/lib/work-item-estimate-config
18
+ */
19
+
20
+ /** Recommended-model values a project gets when it does not list its own. */
21
+ export const DEFAULT_ESTIMATE_MODELS = Object.freeze([
22
+ "claude-fable-5-2",
23
+ "claude-opus-5-5",
24
+ "claude-sonnet-5-5",
25
+ "claude-haiku-5-5",
26
+ "fable",
27
+ "opus",
28
+ "sonnet",
29
+ "haiku",
30
+ ]);
31
+
32
+ /** The `workItem.estimate` value `lisa sync` writes into a host config. */
33
+ export const DEFAULT_WORK_ITEM_ESTIMATE = Object.freeze({
34
+ required: true,
35
+ models: DEFAULT_ESTIMATE_MODELS,
36
+ });
37
+
38
+ /**
39
+ * Share of an estimate's tokens billed at each rate. Agent work rereads its
40
+ * context far more than it writes, so most tokens are cached input. This is an
41
+ * assumption, not a measurement: a project overrides it with
42
+ * `workItem.estimate.tokenMix`, and calibration against actual cost corrects
43
+ * it over time.
44
+ */
45
+ export const DEFAULT_TOKEN_MIX = Object.freeze({
46
+ input: 0.1,
47
+ cachedInput: 0.85,
48
+ output: 0.05,
49
+ });
50
+
51
+ const rates = (inputPer1M, cachedInputPer1M, outputPer1M) =>
52
+ Object.freeze({ inputPer1M, cachedInputPer1M, outputPer1M });
53
+ const FABLE = rates(8, 0.2, 40);
54
+ const OPUS = rates(4, 0.2, 20);
55
+ const SONNET = rates(2, 0.1, 10);
56
+ // Haiku's cache-read price is not listed with the others; 0.1x the input
57
+ // price is the standard cache-read multiplier.
58
+ const HAIKU = rates(0.1, 0.01, 0.5);
59
+
60
+ /**
61
+ * Shipped default price table, in USD per million tokens. Source: Anthropic's
62
+ * first-party API list prices as published in the Claude API reference on
63
+ * 2026-11-03 (Bedrock and Vertex AI bill separately). A family name is priced
64
+ * as the current model of that family.
65
+ */
66
+ export const DEFAULT_ESTIMATE_PRICING = Object.freeze({
67
+ currency: "USD",
68
+ source: "anthropic-api-list-prices",
69
+ snapshot: "2026-11-03",
70
+ models: Object.freeze({
71
+ "anthropic/claude-fable-5-2": FABLE,
72
+ "anthropic/claude-opus-5-5": OPUS,
73
+ "anthropic/claude-sonnet-5-5": SONNET,
74
+ "anthropic/claude-haiku-5-5": HAIKU,
75
+ "anthropic/fable": FABLE,
76
+ "anthropic/opus": OPUS,
77
+ "anthropic/sonnet": SONNET,
78
+ "anthropic/haiku": HAIKU,
79
+ }),
80
+ });
81
+
82
+ /** A configuration value that cannot be used. */
83
+ export class EstimateConfigError extends Error {}
84
+
85
+ const isPlainObject = value =>
86
+ value !== null && typeof value === "object" && !Array.isArray(value);
87
+
88
+ /**
89
+ * Validate the allowed recommended-model list.
90
+ * @param {unknown} models - Configured list.
91
+ * @returns {readonly string[]} The list, unchanged.
92
+ */
93
+ function validModels(models) {
94
+ const valid =
95
+ Array.isArray(models) &&
96
+ models.length > 0 &&
97
+ models.every(model => typeof model === "string" && /^\S+$/.test(model));
98
+ if (!valid)
99
+ throw new EstimateConfigError(
100
+ "workItem.estimate.models must be a non-empty list of model ids or family names without spaces."
101
+ );
102
+ return models;
103
+ }
104
+
105
+ /**
106
+ * Validate the token mix: three shares that add up to 1.
107
+ * @param {unknown} mix - Configured mix.
108
+ * @returns {{input: number, cachedInput: number, output: number}} The mix.
109
+ */
110
+ function validMix(mix) {
111
+ const keys = ["input", "cachedInput", "output"];
112
+ const valid =
113
+ isPlainObject(mix) &&
114
+ keys.every(key => Number.isFinite(mix[key]) && mix[key] >= 0) &&
115
+ Math.abs(keys.reduce((sum, key) => sum + mix[key], 0) - 1) < 0.001;
116
+ if (!valid)
117
+ throw new EstimateConfigError(
118
+ "workItem.estimate.tokenMix must give input, cachedInput and output shares that add up to 1."
119
+ );
120
+ return mix;
121
+ }
122
+
123
+ /**
124
+ * Validate a `workItem.estimate` value, as `lisa sync` does before keeping it.
125
+ * @param {unknown} value - Configured value.
126
+ * @returns {object} The value, unchanged.
127
+ */
128
+ export function validateEstimateConfig(value) {
129
+ if (!isPlainObject(value))
130
+ throw new EstimateConfigError("workItem.estimate must be an object.");
131
+ if (value.required !== undefined && typeof value.required !== "boolean")
132
+ throw new EstimateConfigError(
133
+ "workItem.estimate.required must be true or false."
134
+ );
135
+ if (value.models !== undefined) validModels(value.models);
136
+ if (value.tokenMix !== undefined) validMix(value.tokenMix);
137
+ return value;
138
+ }
139
+
140
+ /**
141
+ * The effective estimate settings for a project.
142
+ * @param {object} config - Merged Lisa config.
143
+ * @returns {{required: boolean, warnings: string[], models: readonly string[], tokenMix: object, usagePricing: unknown}} Settings.
144
+ */
145
+ export function resolveEstimateConfig(config) {
146
+ const raw = config?.workItem?.estimate;
147
+ const estimate = isPlainObject(raw) ? raw : {};
148
+ const invalidRequired =
149
+ estimate.required !== undefined && typeof estimate.required !== "boolean";
150
+ return {
151
+ required: invalidRequired ? true : estimate.required !== false,
152
+ warnings: invalidRequired
153
+ ? [
154
+ `workItem.estimate.required is ${JSON.stringify(estimate.required)}, not true or false; treating it as true, the stricter option.`,
155
+ ]
156
+ : [],
157
+ models: validModels(estimate.models ?? DEFAULT_ESTIMATE_MODELS),
158
+ tokenMix: validMix(estimate.tokenMix ?? DEFAULT_TOKEN_MIX),
159
+ usagePricing: config?.usage?.pricing,
160
+ };
161
+ }
162
+
163
+ /**
164
+ * Find a model's rates in one price table. Keys are `<provider>/<model>`, as
165
+ * in the usage-accounting pricing schema; a bare `<model>` key also matches.
166
+ * @param {unknown} table - A pricing object with `models`.
167
+ * @param {string} model - Model id or family.
168
+ * @returns {object | undefined} Rates, when the table prices the model.
169
+ */
170
+ function ratesIn(table, model) {
171
+ if (!isPlainObject(table?.models)) return undefined;
172
+ const key = Object.keys(table.models).find(
173
+ name => name === model || name.split("/").slice(1).join("/") === model
174
+ );
175
+ return key === undefined ? undefined : table.models[key];
176
+ }
177
+
178
+ /**
179
+ * The price source that covers a model: configured pricing first, then the
180
+ * shipped default.
181
+ * @param {object} settings - Result of {@link resolveEstimateConfig}.
182
+ * @param {string} model - Model id or family.
183
+ * @returns {{rates: object, currency: string, source: string} | null} Price, or null when unpriced.
184
+ */
185
+ export function priceFor(settings, model) {
186
+ const tables = [settings.usagePricing, DEFAULT_ESTIMATE_PRICING];
187
+ const table = tables.find(candidate => ratesIn(candidate, model));
188
+ if (!table) return null;
189
+ const snapshot = table.snapshot ? `@${table.snapshot}` : "";
190
+ return {
191
+ rates: ratesIn(table, model),
192
+ currency: table.currency ?? "USD",
193
+ source: `${table.source ?? "usage.pricing"}${snapshot}`,
194
+ };
195
+ }
196
+
197
+ /**
198
+ * Dollar cost of a token count, in whole cents, under a price and mix.
199
+ * @param {number} tokens - Token count.
200
+ * @param {object} rates - Per-million rates.
201
+ * @param {object} mix - Token mix.
202
+ * @returns {number} Cost in cents.
203
+ */
204
+ export function costCents(tokens, rates, mix) {
205
+ const blendedPer1M =
206
+ mix.input * (rates.inputPer1M ?? 0) +
207
+ mix.cachedInput * (rates.cachedInputPer1M ?? rates.inputPer1M ?? 0) +
208
+ mix.output * (rates.outputPer1M ?? 0);
209
+ return Math.round(((tokens * blendedPer1M) / 1_000_000) * 100);
210
+ }