sigrank 0.19.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +158 -0
  2. package/MCP_SERVER_DESCRIPTION.md +172 -0
  3. package/README.md +89 -41
  4. package/adapters/index.mjs +1100 -0
  5. package/adapters/tokenpull.mjs +568 -0
  6. package/adapters.mjs +1 -865
  7. package/analytics/cascade.mjs +338 -0
  8. package/cascade.mjs +1 -274
  9. package/cli.mjs +1 -1853
  10. package/connect.mjs +1 -13
  11. package/identity/connect.mjs +13 -0
  12. package/identity/keystore.mjs +231 -0
  13. package/identity/sign.mjs +106 -0
  14. package/index.mjs +47 -162
  15. package/keystore.mjs +1 -242
  16. package/lib/constants.mjs +137 -0
  17. package/lib/pkg-version.mjs +59 -0
  18. package/manifest.json +46 -0
  19. package/narrate.mjs +1 -77
  20. package/package.json +41 -16
  21. package/preflight.mjs +1 -191
  22. package/presentation/cli.mjs +1926 -0
  23. package/presentation/narrate.mjs +219 -0
  24. package/presentation/tui-themes.mjs +189 -0
  25. package/presentation/tui.mjs +3316 -0
  26. package/prompts/check-my-efficiency.md +1 -0
  27. package/prompts/compare-with-leader.md +1 -0
  28. package/prompts/simulate-improvement.md +1 -0
  29. package/proxy.mjs +492 -0
  30. package/resources/class-tiers.md +81 -0
  31. package/resources/data-policy.md +31 -0
  32. package/resources/install-guide.md +59 -0
  33. package/resources/privacy-model.md +44 -0
  34. package/resources/scoring-formula.md +24 -0
  35. package/review.mjs +263 -0
  36. package/sign.mjs +1 -106
  37. package/submit/index.mjs +297 -0
  38. package/submit/preflight.mjs +191 -0
  39. package/submit.mjs +1 -303
  40. package/tokenpull.mjs +1 -576
  41. package/tokscale_analytics.mjs +864 -0
  42. package/tools/_framing.mjs +217 -0
  43. package/tools/_helpers.mjs +253 -0
  44. package/tools/_peer-matching.mjs +361 -0
  45. package/tools/_report.mjs +262 -0
  46. package/tools/_schemas.mjs +492 -0
  47. package/tools/_verifiers.mjs +148 -0
  48. package/tools/compare-operators.mjs +80 -0
  49. package/tools/compare-self.mjs +124 -0
  50. package/tools/describe-power-user.mjs +78 -0
  51. package/tools/diagnose-cascade.mjs +192 -0
  52. package/tools/discover-peers.mjs +198 -0
  53. package/tools/enroll.mjs +119 -0
  54. package/tools/get-best-operator.mjs +59 -0
  55. package/tools/get-leaderboard.mjs +27 -0
  56. package/tools/get-operator.mjs +33 -0
  57. package/tools/index.mjs +229 -0
  58. package/tools/optimize-efficiency.mjs +170 -0
  59. package/tools/rank-paste.mjs +51 -0
  60. package/tools/rank-windows.mjs +120 -0
  61. package/tools/self-improve.mjs +423 -0
  62. package/tools/simulate-change.mjs +207 -0
  63. package/tools/submit-paste.mjs +121 -0
  64. package/tools/submit-verified.mjs +153 -0
  65. package/tools/suggest-improvements.mjs +168 -0
  66. package/tools/tokenpull-compare.mjs +123 -0
  67. package/tools/tokenpull-submit.mjs +235 -0
  68. package/tools/tokenpull.mjs +113 -0
  69. package/tools/tokscale-breakdown.mjs +119 -0
  70. package/tools/tokscale-competitive-intel.mjs +45 -0
  71. package/tools/tokscale-cost-analysis.mjs +31 -0
  72. package/tools/tokscale-developer-profile.mjs +36 -0
  73. package/tools/tokscale-device-profile.mjs +35 -0
  74. package/tools/tokscale-market-share.mjs +38 -0
  75. package/tools/tokscale-mcp-usage.mjs +33 -0
  76. package/tools/tokscale-model-trends.mjs +32 -0
  77. package/tools/watch-tokenpull.mjs +134 -0
  78. package/tools.mjs +1 -3897
  79. package/tui.mjs +4 -2650
  80. package/banner.svg +0 -7
@@ -0,0 +1,207 @@
1
+ /**
2
+ * tools/simulate-change.mjs — simulate_change tool.
3
+ */
4
+
5
+ import { cascade, parsePillars } from "../analytics/cascade.mjs";
6
+ import { SIMULATE_OUTPUT, ANNOTATIONS } from "./_schemas.mjs";
7
+ import { MAX_INPUT, withParseWarnings } from "./_helpers.mjs";
8
+
9
+ export const TOOL_DEF = {
10
+ name: "simulate_change",
11
+ description:
12
+ "The first PRESCRIPTIVE SigRank tool — 'what if I changed my token mix?' Takes your current 4 pillars (input/output/cacheCreate/cacheRead) and one or more proposed changes, runs the canonical cascade on BOTH the current and simulated values, and returns the exact Υ Yield delta, class change, and per-metric diffs. This is the 'show me the payoff before I do the work' primitive: no network, no submission, pure local math. Use it to answer 'would increasing my cache-read by 50k tokens actually move my class?' before you change your workflow. Accepts the current pillars as JSON or 4 numbers (same as rank_paste) plus a `changes` object with any of the 4 pillar names mapped to new absolute values OR relative deltas (e.g. {cacheRead: '+50000'} or {input: 800000}).",
13
+ annotations: {
14
+ title: "Simulate metric change",
15
+ ...ANNOTATIONS.readOnlyHint,
16
+ ...ANNOTATIONS.idempotentHint,
17
+ ...ANNOTATIONS.openWorldHint,
18
+ },
19
+ inputSchema: {
20
+ type: "object",
21
+ properties: {
22
+ text: {
23
+ type: "string",
24
+ description:
25
+ 'Current token pillars — ccusage JSON or "input output cacheCreate cacheRead" (same format as rank_paste).',
26
+ },
27
+ changes: {
28
+ type: "object",
29
+ description:
30
+ "Proposed changes to apply. Keys: input, output, cacheCreate, cacheRead. Values are either absolute numbers (replace) or strings starting with +/- for relative deltas (add/subtract). Omitted pillars are unchanged.",
31
+ properties: {
32
+ input: {
33
+ type: ["number", "string"],
34
+ description:
35
+ 'new input token count (absolute) or "+/-N" for a relative delta',
36
+ },
37
+ output: {
38
+ type: ["number", "string"],
39
+ description:
40
+ 'new output token count (absolute) or "+/-N" for a relative delta',
41
+ },
42
+ cacheCreate: {
43
+ type: ["number", "string"],
44
+ description:
45
+ 'new cache-create token count (absolute) or "+/-N" for a relative delta',
46
+ },
47
+ cacheRead: {
48
+ type: ["number", "string"],
49
+ description:
50
+ 'new cache-read token count (absolute) or "+/-N" for a relative delta',
51
+ },
52
+ },
53
+ },
54
+ },
55
+ required: ["text", "changes"],
56
+ },
57
+ outputSchema: SIMULATE_OUTPUT,
58
+ };
59
+
60
+ export async function handleSimulateChange(args) {
61
+ // The first prescriptive tool — "what if I changed my token mix?"
62
+ // Pure local math: parse current pillars, apply proposed changes, run the
63
+ // cascade on both, return the delta. No network, no submission.
64
+ if (!args?.text)
65
+ throw new Error(
66
+ "simulate_change requires a non-empty `text` argument (current pillars).",
67
+ );
68
+ if (typeof args.text === "string" && args.text.length > MAX_INPUT) {
69
+ return {
70
+ status: "error",
71
+ reason: "input_too_large",
72
+ detail: `text exceeds ${MAX_INPUT} chars.`,
73
+ };
74
+ }
75
+ if (!args?.changes || typeof args.changes !== "object") {
76
+ throw new Error(
77
+ "simulate_change requires a `changes` object with at least one pillar change.",
78
+ );
79
+ }
80
+
81
+ const currentPillars = parsePillars(args.text);
82
+ const current = withParseWarnings(currentPillars, cascade(currentPillars));
83
+
84
+ // Apply changes: each pillar is either an absolute number (replace) or a
85
+ // string starting with +/- (relative delta). Omitted pillars are unchanged.
86
+ const PILLAR_KEYS = ["input", "output", "cacheCreate", "cacheRead"];
87
+ const simulated = { ...currentPillars };
88
+ const appliedChanges = {};
89
+
90
+ for (const key of PILLAR_KEYS) {
91
+ if (args.changes[key] == null) continue;
92
+ const raw = args.changes[key];
93
+ let newVal;
94
+
95
+ if (typeof raw === "number") {
96
+ newVal = raw;
97
+ } else if (typeof raw === "string") {
98
+ const trimmed = raw.trim();
99
+ if (trimmed.startsWith("+") || trimmed.startsWith("-")) {
100
+ // Relative delta: add/subtract from current value
101
+ const delta = Number(trimmed);
102
+ if (!Number.isFinite(delta)) {
103
+ return {
104
+ status: "error",
105
+ reason: "invalid_change",
106
+ detail: `changes.${key}: "${raw}" is not a valid relative delta.`,
107
+ };
108
+ }
109
+ newVal = currentPillars[key] + delta;
110
+ } else {
111
+ // Absolute value as a string
112
+ newVal = Number(trimmed);
113
+ }
114
+ } else {
115
+ return {
116
+ status: "error",
117
+ reason: "invalid_change",
118
+ detail: `changes.${key}: expected number or string, got ${typeof raw}.`,
119
+ };
120
+ }
121
+
122
+ if (!Number.isFinite(newVal)) {
123
+ return {
124
+ status: "error",
125
+ reason: "invalid_change",
126
+ detail: `changes.${key}: result is not a finite number.`,
127
+ };
128
+ }
129
+ // Clamp to non-negative — token counts can't be negative
130
+ if (newVal < 0) {
131
+ return {
132
+ status: "error",
133
+ reason: "invalid_change",
134
+ detail: `changes.${key}: result ${newVal} is negative — token counts must be >= 0.`,
135
+ };
136
+ }
137
+
138
+ simulated[key] = newVal;
139
+ appliedChanges[key] = {
140
+ from: currentPillars[key],
141
+ to: newVal,
142
+ delta: newVal - currentPillars[key],
143
+ };
144
+ }
145
+
146
+ if (Object.keys(appliedChanges).length === 0) {
147
+ return {
148
+ status: "error",
149
+ reason: "no_changes",
150
+ detail: "No pillar changes specified in the `changes` object.",
151
+ };
152
+ }
153
+
154
+ const simulatedResult = cascade(simulated);
155
+
156
+ // Compute deltas for every cascade metric
157
+ const metricDelta = (curr, sim) => {
158
+ if (curr == null && sim == null) return null;
159
+ if (curr == null) return { from: null, to: sim, delta: null };
160
+ if (sim == null) return { from: curr, to: null, delta: null };
161
+ return { from: curr, to: sim, delta: Number((sim - curr).toFixed(4)) };
162
+ };
163
+
164
+ const classChanged = current.class !== simulatedResult.class;
165
+
166
+ return {
167
+ current: {
168
+ pillars: currentPillars,
169
+ yield: current.yield,
170
+ snr: current.snr,
171
+ leverage: current.leverage,
172
+ velocity: current.velocity,
173
+ dev10x: current.dev10x,
174
+ class: current.class,
175
+ },
176
+ simulated: {
177
+ pillars: {
178
+ input: simulated.input,
179
+ output: simulated.output,
180
+ cacheCreate: simulated.cacheCreate,
181
+ cacheRead: simulated.cacheRead,
182
+ },
183
+ yield: simulatedResult.yield,
184
+ snr: simulatedResult.snr,
185
+ leverage: simulatedResult.leverage,
186
+ velocity: simulatedResult.velocity,
187
+ dev10x: simulatedResult.dev10x,
188
+ class: simulatedResult.class,
189
+ },
190
+ changes: appliedChanges,
191
+ deltas: {
192
+ yield: metricDelta(current.yield, simulatedResult.yield),
193
+ snr: metricDelta(current.snr, simulatedResult.snr),
194
+ leverage: metricDelta(current.leverage, simulatedResult.leverage),
195
+ velocity: metricDelta(current.velocity, simulatedResult.velocity),
196
+ dev10x: metricDelta(current.dev10x, simulatedResult.dev10x),
197
+ },
198
+ class_changed: classChanged,
199
+ ...(classChanged
200
+ ? { class_transition: `${current.class} → ${simulatedResult.class}` }
201
+ : {}),
202
+ ...(simulatedResult.warnings
203
+ ? { simulated_warnings: simulatedResult.warnings }
204
+ : {}),
205
+ note: "Local simulation only — no submission. The actual score depends on server-side RS.xx weights and class thresholds.",
206
+ };
207
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * tools/submit-paste.mjs — submit_paste tool.
3
+ */
4
+
5
+ import { cascade, parsePillars } from "../analytics/cascade.mjs";
6
+ import { narrate } from "../presentation/narrate.mjs";
7
+ import { SUBMIT_OUTPUT, ANNOTATIONS } from "./_schemas.mjs";
8
+ import {
9
+ MAX_INPUT,
10
+ uploadStamp,
11
+ withParseWarnings,
12
+ } from "./_helpers.mjs";
13
+ import { TERMS_VERSION, PRIVACY_VERSION, isRankedAck } from "../lib/constants.mjs";
14
+
15
+ export const TOOL_DEF = {
16
+ name: "submit_paste",
17
+ description:
18
+ "Ranks a paste of token counts AND publishes it to the live SigRank board at signalaf.com in one call. First computes the cascade locally for an instant preview (yield, leverage, velocity, class, card), then submits the raw paste to the board's web-paste endpoint, which re-parses and re-scores it server-side. The server score is authoritative — it may differ from the local preview if the board applies additional validation. Returns both the local preview and the server response (including the operator's new rank if accepted). A codename is required to publish — omit it for a local preview only (no board submission). Token-only, no auth required. Use this when you have token counts from ccusage or a dashboard and want to both see your score and publish it. Do NOT use this if you want to pull your local usage automatically — use tokenpull_submit for the zero-paste flow. Do NOT use this for multi-window dashboard pastes — use rank_windows to rank them first, then submit each window. After calling this, use get_operator with your codename to verify your submission appeared on the board.",
19
+ annotations: {
20
+ title: "Submit paste to board",
21
+ ...ANNOTATIONS.destructiveHint,
22
+ ...ANNOTATIONS.idempotentHint,
23
+ ...ANNOTATIONS.openWorldHint,
24
+ },
25
+ inputSchema: {
26
+ type: "object",
27
+ properties: {
28
+ text: {
29
+ type: "string",
30
+ description:
31
+ 'Token counts to rank and submit. Two formats: (1) JSON {"input":N,"output":N,"cacheCreate":N,"cacheRead":N} from ccusage (preferred — the board parses this reliably), or (2) four whitespace-separated numbers: input output cacheCreate cacheRead. The 4-number form ranks locally but the board may reject it. Example: {"input":1000000,"output":500000,"cacheCreate":50000,"cacheRead":800000}',
32
+ },
33
+ codename: {
34
+ type: "string",
35
+ description:
36
+ 'Operator codename to publish under on the leaderboard (e.g. "Ghost Falcon"). Required to submit — omit for local preview only (no board submission, just returns the local cascade result). Must be a non-empty string.',
37
+ },
38
+ },
39
+ required: ["text"],
40
+ description:
41
+ "Requires token counts (text). Codename is optional but required for board submission — omit it for preview-only mode.",
42
+ },
43
+ outputSchema: SUBMIT_OUTPUT,
44
+ };
45
+
46
+ export async function handleSubmitPaste(args, ctx) {
47
+ if (!args?.text)
48
+ throw new Error("submit_paste requires a non-empty `text` argument.");
49
+ if (typeof args.text === "string" && args.text.length > MAX_INPUT) {
50
+ return {
51
+ status: "error",
52
+ reason: "input_too_large",
53
+ detail: `text exceeds ${MAX_INPUT} chars (${args.text.length}). Paste only the token-count table, not full output.`,
54
+ };
55
+ }
56
+ // Local preview first — also validates the paste is parseable before any POST.
57
+ const pillars = parsePillars(args.text);
58
+ const c = withParseWarnings(pillars, cascade(pillars));
59
+ const codename = String(args?.codename || "").trim();
60
+ const card = narrate(c, codename || "This operator");
61
+
62
+ // No codename → cannot publish (the board endpoint requires it). Fail fast at the
63
+ // tool boundary with a clear message instead of an opaque server 400.
64
+ if (!codename) {
65
+ return {
66
+ ...c,
67
+ card,
68
+ submission: {
69
+ status: "not_submitted",
70
+ reason: "codename_required",
71
+ detail:
72
+ "Pass a codename to publish to the board. Showing local preview only.",
73
+ },
74
+ };
75
+ }
76
+
77
+ // Privacy: send ONLY the 4 parsed token numbers (canonical "input output
78
+ // cacheCreate cacheRead" form), never the raw user text. parsePillars above
79
+ // already validated the input — we reconstruct from the parsed pillars so
80
+ // no conversation content or prose can leak to the server. The server's
81
+ // ingestMeta() re-parses these 4 numbers authoritatively (Strategy 3:
82
+ // four bare numbers). This makes every submit path — signed and paste —
83
+ // send only token counts.
84
+ const canonicalPaste = `${pillars.input} ${pillars.output} ${pillars.cacheCreate} ${pillars.cacheRead}`;
85
+ const stamp = uploadStamp({
86
+ codename,
87
+ pillars: c.pillars,
88
+ source: "web_paste",
89
+ });
90
+ const res = await ctx.doFetch(`${ctx.apiBase}/api/v1/ingest-paste`, {
91
+ method: "POST",
92
+ headers: {
93
+ "content-type": "application/json",
94
+ accept: "application/json",
95
+ },
96
+ body: JSON.stringify({
97
+ codename,
98
+ raw_paste: canonicalPaste,
99
+ consent_acknowledged: true,
100
+ terms_version: TERMS_VERSION,
101
+ privacy_version: PRIVACY_VERSION,
102
+ ...stamp,
103
+ }),
104
+ });
105
+ let ack;
106
+ try {
107
+ ack = await res.json();
108
+ } catch {
109
+ ack = {
110
+ status: "error",
111
+ detail: `HTTP ${res.status} (non-JSON response)`,
112
+ };
113
+ }
114
+ const ranked = isRankedAck(res, ack);
115
+ return {
116
+ ...c,
117
+ card,
118
+ ranked,
119
+ submission: { ...stamp, httpStatus: res.status, ranked, ...ack },
120
+ };
121
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * tools/submit-verified.mjs — submit_verified tool.
3
+ */
4
+
5
+ import { ALL_PLATFORMS } from "../adapters/index.mjs";
6
+ import { ensureIdentity } from "../identity/keystore.mjs";
7
+ import { submitSignedWindow } from "../submit/index.mjs";
8
+ import { SUBMIT_OUTPUT, ANNOTATIONS } from "./_schemas.mjs";
9
+ import { pullByPlatform, WINDOW_TYPE } from "./_helpers.mjs";
10
+ import { _computeReportBlock } from "./_report.mjs";
11
+
12
+ export const TOOL_DEF = {
13
+ name: "submit_verified",
14
+ description:
15
+ "Publish your LOCAL token runs to the SigRank board as a VERIFIED operator — the enrolled, signed path. Reads your pillars (tokenpull), builds the canonical Schema 1.0 snapshot per window, ed25519-signs it with your device key, and POSTs to /api/v1/snapshots. Requires `npx sigrank-mcp enroll` first (a bound device). Only signed submissions from a trusted device rank on the board. Token-only; the private key never leaves your machine. Pass dry_run:true to inspect the exact signed payload without publishing.",
16
+ annotations: {
17
+ title: "Submit verified score",
18
+ ...ANNOTATIONS.destructiveHint,
19
+ ...ANNOTATIONS.idempotentHint,
20
+ ...ANNOTATIONS.openWorldHint,
21
+ },
22
+ inputSchema: {
23
+ type: "object",
24
+ properties: {
25
+ window: {
26
+ type: "string",
27
+ enum: ["7d", "30d", "90d", "all"],
28
+ description: "submit only this window (default: all 4)",
29
+ },
30
+ platform: {
31
+ type: "string",
32
+ enum: [...ALL_PLATFORMS, "multi"],
33
+ description:
34
+ "source platform (default: claude). 'multi' = combined cascade summed across all locally-detected platforms (needs 2+ active); empty windows are skipped.",
35
+ },
36
+ dry_run: {
37
+ type: "boolean",
38
+ description:
39
+ "build + sign but do NOT publish — returns the exact payload that would be POSTed (token counts only), so you can inspect before submitting",
40
+ },
41
+ },
42
+ },
43
+ outputSchema: SUBMIT_OUTPUT,
44
+ };
45
+
46
+ export async function handleSubmitVerified(args, ctx) {
47
+ // The enrolled, signed publish path → /api/v1/snapshots (only verified rows rank).
48
+ const id = ctx.opts.identity || ensureIdentity();
49
+ if (!id.codename || !id.operator_id) {
50
+ return {
51
+ status: "not_enrolled",
52
+ detail: "Run `npx sigrank-mcp enroll` to bind this device first.",
53
+ };
54
+ }
55
+ const platform = args?.platform || "claude";
56
+
57
+ // MULTI: the combined cross-platform cascade. The dashboard already SUMS every
58
+ // active platform's pillars (a "claude+codex" row) but never submitted it — this
59
+ // is that missing submission. Aggregate every locally-detected platform's pillars
60
+ // per window and publish as platform='multi' = the operator's TOTAL usage. Empty
61
+ // windows are skipped so a no-activity window never lands as a degenerate row.
62
+ if (platform === "multi") {
63
+ const detected = [];
64
+ for (const p of ALL_PLATFORMS) {
65
+ const r = await pullByPlatform(p, ctx.opts).catch(() => null);
66
+ const live =
67
+ r &&
68
+ (r.windows || []).some(
69
+ (w) =>
70
+ w.pillars.input +
71
+ w.pillars.output +
72
+ w.pillars.cacheCreate +
73
+ w.pillars.cacheRead >
74
+ 0,
75
+ );
76
+ if (live) detected.push(r);
77
+ }
78
+ if (detected.length < 2) {
79
+ return {
80
+ platform: "multi",
81
+ codename: id.codename,
82
+ operator_id: id.operator_id,
83
+ status: "skipped",
84
+ reason: "need_2_platforms",
85
+ detail: `multi needs 2+ active platforms; found ${detected.length} (${detected.map((d) => d.platform).join(", ") || "none"}).`,
86
+ windows: [],
87
+ };
88
+ }
89
+ const winKeys = args?.window
90
+ ? [args.window]
91
+ : ["7d", "30d", "90d", "all"];
92
+ const out = [];
93
+ for (const wk of winKeys) {
94
+ const sum = { input: 0, output: 0, cacheCreate: 0, cacheRead: 0 };
95
+ let msgs = 0;
96
+ for (const d of detected) {
97
+ const w = (d.windows || []).find((x) => x.window === wk);
98
+ if (!w) continue;
99
+ sum.input += w.pillars.input || 0;
100
+ sum.output += w.pillars.output || 0;
101
+ sum.cacheCreate += w.pillars.cacheCreate || 0;
102
+ sum.cacheRead += w.pillars.cacheRead || 0;
103
+ msgs += w.messages || 0;
104
+ }
105
+ if (sum.input + sum.output + sum.cacheCreate + sum.cacheRead <= 0)
106
+ continue; // skip empty window
107
+ const report = _computeReportBlock(sum);
108
+ const r = await submitSignedWindow(wk, sum, msgs, id, {
109
+ apiBase: ctx.apiBase,
110
+ fetchImpl: ctx.doFetch,
111
+ platform: "multi",
112
+ now: ctx.opts.now,
113
+ dryRun: !!args?.dry_run,
114
+ report,
115
+ });
116
+ out.push({ window: wk, pillars: sum, ...r });
117
+ }
118
+ return {
119
+ platform: "multi",
120
+ codename: id.codename,
121
+ operator_id: id.operator_id,
122
+ sources: detected.map((d) => d.platform),
123
+ windows: out,
124
+ };
125
+ }
126
+
127
+ const pulled = await pullByPlatform(platform, ctx.opts);
128
+ const targets = args?.window
129
+ ? pulled.windows.filter((w) => w.window === args.window)
130
+ : pulled.windows;
131
+ const out = [];
132
+ for (const w of targets) {
133
+ // Compute the cascade report block (mode + badges + health) for this window.
134
+ // Pure math, computed locally, submitted alongside the 4 token pillars.
135
+ const report = _computeReportBlock(w.pillars);
136
+ const r = await submitSignedWindow(w.window, w.pillars, w.messages, id, {
137
+ apiBase: ctx.apiBase,
138
+ fetchImpl: ctx.doFetch,
139
+ platform: pulled.platform,
140
+ now: ctx.opts.now,
141
+ dryRun: !!args?.dry_run,
142
+ report,
143
+ });
144
+ out.push({ window: w.window, pillars: w.pillars, ...r });
145
+ }
146
+ return {
147
+ platform: pulled.platform,
148
+ codename: id.codename,
149
+ operator_id: id.operator_id,
150
+ generatedAt: pulled.generatedAt,
151
+ windows: out,
152
+ };
153
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * tools/suggest-improvements.mjs — suggest_improvements tool.
3
+ */
4
+
5
+ import { cascade, parsePillars } from "../analytics/cascade.mjs";
6
+ import { SUGGEST_OUTPUT, ANNOTATIONS } from "./_schemas.mjs";
7
+ import { MAX_INPUT, withParseWarnings } from "./_helpers.mjs";
8
+
9
+ export const TOOL_DEF = {
10
+ name: "suggest_improvements",
11
+ description:
12
+ "Generates ranked, simulated improvement suggestions for your token cascade. Takes your 4 pillars, tests multiple improvement strategies (increase cache reads, reduce fresh input, increase output, optimize cache creation), simulates each with the canonical cascade engine, and returns them ranked by Υ yield impact. Each suggestion includes: the action, which pillar to change, how much to change it, the projected Υ after the change, the yield delta, the projected class tier, and a rationale. Also returns the single highest-impact change (best_single_change). Pure local math — no network, no submission. Use this after diagnose_cascade to get actionable next steps, then use simulate_change to fine-tune before committing. Accepts the same input formats as rank_paste.",
13
+ annotations: {
14
+ title: "Suggest improvements",
15
+ ...ANNOTATIONS.readOnlyHint,
16
+ ...ANNOTATIONS.idempotentHint,
17
+ ...ANNOTATIONS.openWorldHint,
18
+ },
19
+ inputSchema: {
20
+ type: "object",
21
+ properties: {
22
+ text: {
23
+ type: "string",
24
+ description:
25
+ 'Token pillars — ccusage JSON or "input output cacheCreate cacheRead" (same format as rank_paste).',
26
+ },
27
+ },
28
+ required: ["text"],
29
+ },
30
+ outputSchema: SUGGEST_OUTPUT,
31
+ };
32
+
33
+ export async function handleSuggestImprovements(args) {
34
+ if (!args?.text)
35
+ throw new Error(
36
+ "suggest_improvements requires a non-empty `text` argument (token pillars).",
37
+ );
38
+ if (typeof args.text === "string" && args.text.length > MAX_INPUT) {
39
+ return {
40
+ status: "error",
41
+ reason: "input_too_large",
42
+ detail: `text exceeds ${MAX_INPUT} chars.`,
43
+ };
44
+ }
45
+
46
+ const p = parsePillars(args.text);
47
+ const current = withParseWarnings(p, cascade(p));
48
+ const { input: i, output: o, cacheCreate: cw, cacheRead: cr } = p;
49
+
50
+ // Generate candidate improvements, simulate each, rank by Υ impact
51
+ const candidates = [];
52
+
53
+ // Strategy 1: Increase cache reads (the #1 lever)
54
+ const crBoosts = cr > 0 ? [1.5, 2, 3, 5] : [];
55
+ for (const mult of crBoosts) {
56
+ const sim = cascade({ ...p, cacheRead: Math.round(cr * mult) });
57
+ if (sim.yield !== null) {
58
+ candidates.push({
59
+ action: `Increase cache reads by ${Math.round((mult - 1) * 100)}%`,
60
+ pillar: "cacheRead",
61
+ delta: `+${Math.round(cr * (mult - 1)).toLocaleString()}`,
62
+ simulated_yield: sim.yield,
63
+ yield_delta: Number((sim.yield - (current.yield ?? 0)).toFixed(2)),
64
+ class_after: sim.class,
65
+ rationale:
66
+ "Cache reads are the strongest Υ multiplier. More reuse = higher leverage = higher yield.",
67
+ });
68
+ }
69
+ }
70
+ // For zero-cache operators, suggest enabling caching with a starter amount
71
+ if (cr === 0 && cw === 0) {
72
+ const starterAmounts = [
73
+ Math.round(i * 10),
74
+ Math.round(i * 50),
75
+ Math.round(i * 100),
76
+ ];
77
+ for (const amt of starterAmounts) {
78
+ const sim = cascade({
79
+ ...p,
80
+ cacheCreate: Math.round(amt * 0.5),
81
+ cacheRead: amt,
82
+ });
83
+ if (sim.yield !== null && sim.yield > 0) {
84
+ candidates.push({
85
+ action: `Enable caching with ${amt.toLocaleString()} cache reads`,
86
+ pillar: "cacheRead",
87
+ delta: `+${amt.toLocaleString()}`,
88
+ simulated_yield: sim.yield,
89
+ yield_delta: Number((sim.yield - (current.yield ?? 0)).toFixed(2)),
90
+ class_after: sim.class,
91
+ rationale:
92
+ "You have no cache — enabling it unlocks the cascade. Start by reusing prior context.",
93
+ });
94
+ }
95
+ }
96
+ }
97
+
98
+ // Strategy 2: Reduce fresh input (Υ = Cr·O/I² — input is squared in the denominator)
99
+ const inputReductions = [0.9, 0.75, 0.5];
100
+ for (const mult of inputReductions) {
101
+ const newInput = Math.round(i * mult);
102
+ if (newInput < 1) continue;
103
+ const sim = cascade({ ...p, input: newInput });
104
+ if (sim.yield !== null) {
105
+ candidates.push({
106
+ action: `Reduce fresh input by ${Math.round((1 - mult) * 100)}%`,
107
+ pillar: "input",
108
+ delta: `-${Math.round(i * (1 - mult)).toLocaleString()}`,
109
+ simulated_yield: sim.yield,
110
+ yield_delta: Number((sim.yield - (current.yield ?? 0)).toFixed(2)),
111
+ class_after: sim.class,
112
+ rationale:
113
+ "Input is squared in the Υ denominator (Υ = Cr·O/I²). Reducing input has a quadratic payoff.",
114
+ });
115
+ }
116
+ }
117
+
118
+ // Strategy 3: Increase output
119
+ const outputBoosts = [1.25, 1.5, 2];
120
+ for (const mult of outputBoosts) {
121
+ const sim = cascade({ ...p, output: Math.round(o * mult) });
122
+ if (sim.yield !== null) {
123
+ candidates.push({
124
+ action: `Increase output by ${Math.round((mult - 1) * 100)}%`,
125
+ pillar: "output",
126
+ delta: `+${Math.round(o * (mult - 1)).toLocaleString()}`,
127
+ simulated_yield: sim.yield,
128
+ yield_delta: Number((sim.yield - (current.yield ?? 0)).toFixed(2)),
129
+ class_after: sim.class,
130
+ rationale:
131
+ "Output is a linear multiplier in Υ. More output per session = higher yield.",
132
+ });
133
+ }
134
+ }
135
+
136
+ // Strategy 4: Optimize cache creation (reduce if over-committing)
137
+ if (cw > o * 10) {
138
+ const sim = cascade({ ...p, cacheCreate: Math.round(o * 5) });
139
+ if (sim.yield !== null) {
140
+ candidates.push({
141
+ action: "Reduce cache creation to 5× output",
142
+ pillar: "cacheCreate",
143
+ delta: `-${Math.round(cw - o * 5).toLocaleString()}`,
144
+ simulated_yield: sim.yield,
145
+ yield_delta: Number((sim.yield - (current.yield ?? 0)).toFixed(2)),
146
+ class_after: sim.class,
147
+ rationale:
148
+ "You're over-committing cache (cw >> output). Trimming to a healthy ratio reduces cost without hurting yield.",
149
+ });
150
+ }
151
+ }
152
+
153
+ // Sort by yield_delta descending, take top 8
154
+ candidates.sort((a, b) => b.yield_delta - a.yield_delta);
155
+ const top = candidates
156
+ .slice(0, 8)
157
+ .map((c, idx) => ({ rank: idx + 1, ...c }));
158
+
159
+ const best = top[0];
160
+ return {
161
+ suggestions: top,
162
+ current_yield: current.yield,
163
+ current_class: current.class,
164
+ best_single_change: best
165
+ ? `${best.action} (Υ ${current.yield} → ${best.simulated_yield}, +${best.yield_delta} yield, class ${best.class_after})`
166
+ : "No improvements found — your cascade is already optimized.",
167
+ };
168
+ }