Compare commits

..

2 Commits

Author SHA1 Message Date
Alexander Whitestone
e7b9ec8c50 feat: add fleet cost report for #520
Some checks failed
Self-Healing Smoke / self-healing-smoke (pull_request) Failing after 26s
Smoke Test / smoke (pull_request) Failing after 28s
Agent PR Gate / gate (pull_request) Failing after 36s
Agent PR Gate / report (pull_request) Successful in 9s
2026-04-22 03:58:25 -04:00
Alexander Whitestone
2f490e7087 test: define fleet cost report for #520 2026-04-22 03:45:30 -04:00
3 changed files with 322 additions and 206 deletions

View File

@@ -1,206 +0,0 @@
# MATH-005 Attack Packet: √2 Continued Fraction [2;2] Pattern
**Parent:** MATH-002 Scout List — Candidate #1 (Rank S)
**Source:** OEIS A002193 comments — open question about continued fraction patterns
**Issue:** timmy-home#881
**Attack Date:** 2026-04-29
**Agent:** Timmy (sovereign first-attack)
---
## Candidate Summary (from Scout List)
> **Question:** Investigate why the [2;2] continued fraction period appears in the convergents of √2 — and whether this pattern appears with unusual frequency in "non-quadratic" approximants.
- **Source:** OEIS A002193 (comments section)
- **Domain:** Number Theory / Continued Fractions
- **Why bounded:** Computationally checkable across 10^6 convergents; requires only modular arithmetic and comparison.
- **Expected artifact:** Computational evidence note + OEIS comment / short arXiv:num-th note.
- **Verification path:** Compute convergents of √2 via recurrence, detect whether [2,2] snippet appears patterned vs. random in quadratic field approximants.
---
## Literature Search
### Known facts about √2 continued fraction
√2 has the simplest non-trivial periodic continued fraction:
```
√2 = [1; 2, 2, 2, 2, ...] (pure periodic after first term)
```
This follows from the Pell equation: if x = √2, then x satisfies x² = 2, giving the recurrence.
The convergents are:
| n | Fraction (p/q) | Decimal approximation | Error |
|---|----------------|----------------------|-------|
| 1 | 1/1 | 1.0 | 0.4142 |
| 2 | 3/2 | 1.5 | 0.0858 |
| 3 | 7/5 | 1.4 | 0.0142 |
| 4 | 17/12 | 1.416666... | 0.00245 |
| ... | ... | ... | ... |
The [2,2] snippet corresponds to: `1 + 1/(2 + 1/2) = 1 + 1/(2.5) = 7/5 = 1.4` — exactly convergent #3.
### OEIS A002193 background
A002193: Continued fraction for √2 = 1.4142... The comments section (as of 2026) contains an open question phrased:
> "Is there a reason why the [2;2] period appears with prominence in non-quadratic approximants, or is this a coincidence?"
The phrasing "non-quadratic approximants" is ambiguous. Interpretation options:
1. **Rational approximants** (the convergents themselves are degree-1, not quadratic)
2. **Approximants of non-quadratic irrationals** (e.g., π, e, √[3]{2})
### Prior work references
- Hurwitz's theorem on Diophantine approximation
- Khinchin's "Continued Fractions" (standard reference)
- OEIS entries for periodic CF patterns in √n
---
## Computational Evidence
### √2 CF extraction
First 20 CF terms for √2:
```
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
```
The [2,2] pattern appears at positions (1,2), (2,3), ... — continuous infinite repetition.
### Other quadratic irrationals sampled
| n | √n CF (first 12 terms) | [2,2] count |
|----|------------------------|-------------|
| 2 | [1,2,2,2,2,2,2,2,2,2,2,2] | ∞ (pure period) |
| 3 | [1,1,2,1,4,1,2,1,4,1,2,1,...] | 0 |
| 5 | [2,4,4,4,4,4,4,4,4,4,4,4,...] | 0 |
| 6 | [2,2,4,2,4,2,4,2,4,2,4,2,...] | 2 |
| 7 | [2,1,1,1,4,1,1,1,4,1,1,1,...] | 0 |
| 10 | [3,6,6,6,6,6,6,6,6,6,7,1,...] | 0 |
| 13 | [3,1,1,1,1,6,1,1,1,1,6,1,...] | 0 |
| 17 | [4,8,8,8,8,8,8,8,8,8,8,8,...] | 0 |
| 41 | [6,2,2,12,2,2,12,2,2,12,2,2,...] | 6 |
Among 43 non-square √n (n < 50), **17 contain [2,2]** at least once (~39%).
### Transcendentals and random reals sampled
| x | CF (first 12 terms) | [2,2] count |
|---|---------------------|-------------|
| π | [3,7,15,1,292,1,1,1,2,1,3,1,...] | 0 |
| e | [2,1,2,1,1,4,1,1,6,1,1,8,...] | 0 |
| φ | [1,1] (pure periodic) | 0 |
| rand(2.7) | [2,1,2,2,1,469124..., ...] | 1 |
[2,2] appears by chance in random numbers as well. Among 10 random draws in [1,5], 2 showed at least one [2,2] occurrence.
### Convergent values of interest
The snippet [2;2] as a finite CF evaluates exactly to:
```
[2;2] = 2 + 1/2 = 5/2? No — careful:
[2;2] interpreted as standalone CF = 2 + 1/2 = 2.5
But in context of √2: [1;2,2] = 1 + 1/(2 + 1/2) = 1 + 1/(2.5) = 1 + 0.4 = 1.4 = 7/5
```
So the [2,2] "snippet" means two consecutive 2s in the CF term sequence after the first term.
---
## Attempted Analysis
### Why √2 yields [2,2]
The quadratic equation x² = 2 gives the recurrence:
```
x = 1 + 1/x => x = (x+1)/x after rearranging?
Actually: x = 1 + 1/(1 + 1/x)? Let me derive properly:
√2 = 1 + (√2 - 1) = 1 + 1/(1/(√2-1)) = 1 + 1/((√2+1)/1) = 1 + 1/(√2+1)
But √2+1 ≈ 2.414, whose integer part is 2. So a₂ = 2.
Then 1/(√2+1 - 2) = 1/(√2-1) = √2+1 again — period 1 with a=2 repeated.
```
This pure period-1 of constant term 2 is special to √2 and other "silver ratios" like [n; 2n, 2n, ...].
Actually, numbers with form √(m²+1) sometimes have continued fraction [m; 2m, 2m, ...]. For √2: m=1 → [1; 2,2,2,...]. For √5: m=2 → [2;4,4,4,...]. For √10: m=3 → [3;6,6,6,...].
So [2,2] appears for √2 because it belongs to the family √(1+1) with period-1 term 2.
### Why [2,2] appears in other quadratic irrationals
Examining √6: CF = [2;2,4,2,4,2,4,...] — this has a period-2 pattern: [2; (2,4)]. The [2,2] occurs crossing period boundaries: terms 1-2: [2,2] then [2,4] then [2,4]...
√41: CF period [6,2,2,12] — contains [2,2] as a contiguous pair within the period.
The pattern arises naturally in periodic CFs that have consecutive 2s somewhere in the period.
### About "non-quadratic approximants"
Interpretation 1: The **convergents themselves** are rational numbers (algebraic degree 1, not quadratic). The convergent sequence of √2 includes 7/5 — a rational number whose continued fraction (if computed self-referentially) is [1;2,2] — which contains the [2,2] snippet. This is tautological: any convergent is a rational approximant of √2, and the snippet simply encodes that convergent's own CF structure.
Interpretation 2: **Approximants of non-quadratic numbers**. Our random sample shows [2,2] appears by chance in transcendentals (e.g., rand(2.7) had it). The frequency is not obviously elevated.
### Computational limitations
Our survey only inspects first 3040 CF terms and 50 small quadratic radicands. The OEIS comment may refer to a deeper statistical study across thousands of numbers. We did not perform hypothesis testing.
---
## Gap Analysis
| What we know | What remains open |
|---|---|
| √2 has CF [1;2,2,2,...] → [2,2] appears infinitely | The original OEIS question's framing ("non-quadratic approximants") remains ambiguous — we need the exact wording |
| Other √n sometimes have [2,2] in their period | No statistical comparison: is [2,2] more frequent than, say, [1,1] or [3,3]? |
| Random numbers occasionally hit [2,2] by chance | No analysis of "why prominence?" — what metric defines prominence? |
| No connection proven between [2,2] and approximation quality | Open: Is there an information-theoretic reason [2,2] maximizes something? |
**Speculative hypothesis:** [2,2] is the shortest repeating pattern >1 in a periodic CF. For √2, the fundamental unit in (√2) is 1+√2 ≈ 2.414, which has CF [2;2,2,2,...]. This might reflect group structure of the unit group.
---
## Outcome Classification
**Partial progress**
We have:
- ✓ Located the candidate and verified the [2,2] snippet in √2 CF
- ✓ Computed statistical evidence across 40+ numbers
- ✓ Identified that other √n also exhibit [2,2] when their period contains consecutive 2s
- ✓ Clarified the ambiguity in "non-quadratic approximants"
We have *not*:
- ✗ Provided a rigorous proof of why the pattern appears in √2 (this is a standard result about simple periodic CFs)
- ✗ Answered the OEIS question conclusively
- ✗ Submitted an OEIS comment / created a short note
---
## Artifacts Generated
This attack packet itself is the primary artifact. A companion Python script could be created to reproduce the surveys, but for this smallest-attack we embed computed tables directly.
**Verification path:** Readers can recompute √2 convergents via standard recurrence and observe the [2,2] pattern.
---
## Next Attack Recommendations
Based on this first pass:
1. **If classification is Partial:** Attack the next-ranked candidate from MATH-002 (either #2 or next Rank S if multiple exist).
2. **If this proves too elementary:** Move to a Rank A candidate with computational flavor.
3. **If a rigorous proof is desired:** Study the theory of continued fractions for quadratic irrationals in Cassels' "An Introduction to Diophantine Approximation."
---
*"An honest first attack means showing your work, your ignorance, and your next step — all in the same document."*

View File

@@ -0,0 +1,245 @@
#!/usr/bin/env python3
"""Fleet cost report generator.
Reads Timmy's sovereignty metrics database and estimates paid API spend by
agent/provider lane. Default output targets the local timmy-config reports
folder so the cost report can be filed from the sidecar repo.
"""
from __future__ import annotations
import argparse
import sqlite3
from datetime import datetime, timedelta
from pathlib import Path
from typing import Iterable
DB_PATH = Path.home() / ".timmy" / "metrics" / "model_metrics.db"
AGENT_LANES = (
{
"agent": "Timmy Cloud Lane",
"provider": "OpenRouter",
"patterns": ("openrouter/", "google/", "deepseek/", "x-ai/", "mistral/"),
"notes": "Cloud fallback and external reasoning routed through OpenRouter-compatible lanes.",
},
{
"agent": "Ezra",
"provider": "Anthropic",
"patterns": ("claude-", "anthropic/claude"),
"notes": "Archivist / long-form reasoning house on Claude-family models.",
},
{
"agent": "Bezalel",
"provider": "OpenAI",
"patterns": ("gpt-", "openai/", "codex"),
"notes": "Forge / implementation house on Codex/OpenAI-backed execution lanes.",
},
{
"agent": "Allegro",
"provider": "Kimi / Moonshot",
"patterns": ("kimi", "moonshot"),
"notes": "Tempo-and-dispatch house on Kimi / Moonshot direct API lanes.",
},
)
def default_report_path(report_date: str | None = None) -> Path:
if report_date is None:
report_date = datetime.now().strftime("%Y-%m-%d")
return Path.home() / "code" / "timmy-config" / "reports" / "production" / f"{report_date}-fleet-cost-report.md"
def match_lane(model: str) -> dict | None:
lowered = (model or "").lower()
for lane in AGENT_LANES:
if any(pattern in lowered for pattern in lane["patterns"]):
return lane
return None
def load_cost_rows(days: int = 30, db_path: Path = DB_PATH) -> list[tuple[str, int, int, int, float]]:
if not db_path.exists():
return []
cutoff = (datetime.now() - timedelta(days=days)).timestamp()
with sqlite3.connect(str(db_path)) as conn:
rows = conn.execute(
"""
SELECT model, SUM(sessions), SUM(messages), SUM(tool_calls), SUM(est_cost_usd)
FROM session_stats
WHERE timestamp > ? AND is_local = 0
GROUP BY model
ORDER BY SUM(est_cost_usd) DESC, model ASC
""",
(cutoff,),
).fetchall()
return [
(model, int(sessions or 0), int(messages or 0), int(tool_calls or 0), float(cost or 0.0))
for model, sessions, messages, tool_calls, cost in rows
]
def summarize_rows(rows: Iterable[tuple[str, int, int, int, float]], days: int = 30) -> dict:
rows = list(rows)
agents: dict[str, dict] = {}
providers_seen: set[str] = set()
inventory = [
{
"agent": lane["agent"],
"provider": lane["provider"],
"notes": lane["notes"],
}
for lane in AGENT_LANES
]
for lane in AGENT_LANES:
agents[lane["agent"]] = {
"provider": lane["provider"],
"models": [],
"sessions": 0,
"messages": 0,
"tool_calls": 0,
"monthly_cost_usd": 0.0,
"daily_cost_usd": 0.0,
"notes": lane["notes"],
}
unassigned = {
"provider": "Unassigned",
"models": [],
"sessions": 0,
"messages": 0,
"tool_calls": 0,
"monthly_cost_usd": 0.0,
"daily_cost_usd": 0.0,
"notes": "Observed paid-model spend not yet mapped to a named wizard house.",
}
for model, sessions, messages, tool_calls, monthly_cost in rows:
lane = match_lane(model)
if lane is None:
bucket = unassigned
else:
bucket = agents[lane["agent"]]
providers_seen.add(lane["provider"])
bucket["models"].append(
{
"model": model,
"sessions": sessions,
"messages": messages,
"tool_calls": tool_calls,
"monthly_cost_usd": round(monthly_cost, 4),
}
)
bucket["sessions"] += sessions
bucket["messages"] += messages
bucket["tool_calls"] += tool_calls
bucket["monthly_cost_usd"] += monthly_cost
for bucket in list(agents.values()) + [unassigned]:
bucket["monthly_cost_usd"] = round(bucket["monthly_cost_usd"], 4)
bucket["daily_cost_usd"] = round(bucket["monthly_cost_usd"] / max(days, 1), 4)
if unassigned["models"]:
agents["Unassigned"] = unassigned
providers_seen.add("Unassigned")
total_monthly = round(sum(item["monthly_cost_usd"] for item in agents.values()), 4)
total_daily = round(sum(item["daily_cost_usd"] for item in agents.values()), 4)
provider_order = sorted(providers_seen)
if "Unassigned" in provider_order:
provider_order = [p for p in provider_order if p != "Unassigned"] + ["Unassigned"]
return {
"days": days,
"providers": provider_order,
"inventory": inventory,
"agents": agents,
"total_monthly_cost_usd": total_monthly,
"total_daily_cost_usd": total_daily,
}
def render_markdown(summary: dict, report_date: str | None = None) -> str:
if report_date is None:
report_date = datetime.now().strftime("%Y-%m-%d")
lines = [
f"# Fleet Cost Report — {report_date}",
"",
f"Window: last {summary['days']} days of paid-model session stats from `~/.timmy/metrics/model_metrics.db`.",
"",
"## Paid API inventory",
"",
"| Agent | Provider | Notes |",
"| --- | --- | --- |",
]
for item in summary["inventory"]:
lines.append(f"| {item['agent']} | {item['provider']} | {item['notes']} |")
lines.extend(
[
"",
"## Estimated cost per agent per day",
"",
"| Agent | Provider | Daily cost | Monthly estimate | Sessions | Messages | Tool calls |",
"| --- | --- | ---: | ---: | ---: | ---: | ---: |",
]
)
for agent, data in summary["agents"].items():
lines.append(
f"| {agent} | {data['provider']} | ${data['daily_cost_usd']:.2f} | ${data['monthly_cost_usd']:.2f} | {data['sessions']} | {data['messages']} | {data['tool_calls']} |"
)
lines.extend(
[
"",
f"Total estimated daily paid spend: ${summary['total_daily_cost_usd']:.2f}",
f"Total estimated monthly paid spend: ${summary['total_monthly_cost_usd']:.2f}",
"",
"## Model evidence",
"",
]
)
for agent, data in summary["agents"].items():
lines.append(f"### {agent}")
if not data["models"]:
lines.append("- No paid-model sessions observed in the selected window.")
else:
for model in data["models"]:
lines.append(
f"- `{model['model']}` — {model['sessions']} sessions / {model['messages']} messages / {model['tool_calls']} tool calls / ${model['monthly_cost_usd']:.2f} est."
)
lines.append("")
lines.append("Generated by `python3 scripts/fleet_cost_report.py --days 30`. Default output path targets the local timmy-config report lane.")
lines.append("")
return "\n".join(lines)
def write_report(output_path: Path, summary: dict, report_date: str | None = None) -> Path:
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(render_markdown(summary, report_date=report_date), encoding="utf-8")
return output_path
def main() -> int:
parser = argparse.ArgumentParser(description="Estimate paid API spend per fleet agent")
parser.add_argument("--days", type=int, default=30, help="Lookback window in days")
parser.add_argument("--db-path", default=str(DB_PATH), help="Path to model_metrics.db")
parser.add_argument("--output", help="Optional markdown output path")
parser.add_argument("--date", help="Override report date (YYYY-MM-DD)")
args = parser.parse_args()
rows = load_cost_rows(days=args.days, db_path=Path(args.db_path).expanduser())
summary = summarize_rows(rows, days=args.days)
report_date = args.date or datetime.now().strftime("%Y-%m-%d")
output_path = Path(args.output).expanduser() if args.output else default_report_path(report_date)
write_report(output_path, summary, report_date=report_date)
print(output_path)
return 0
if __name__ == "__main__":
raise SystemExit(main())

View File

@@ -0,0 +1,77 @@
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
import tempfile
import unittest
ROOT = Path(__file__).resolve().parent.parent
SCRIPT_PATH = ROOT / "scripts" / "fleet_cost_report.py"
def load_module():
spec = spec_from_file_location("fleet_cost_report", SCRIPT_PATH)
module = module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
class TestFleetCostReport(unittest.TestCase):
def test_default_output_targets_timmy_config_report_path(self):
module = load_module()
output_path = module.default_report_path("2026-04-22")
self.assertIn("timmy-config", str(output_path))
self.assertTrue(str(output_path).endswith("2026-04-22-fleet-cost-report.md"))
def test_summary_groups_paid_costs_by_agent_and_provider(self):
module = load_module()
rows = [
("claude-sonnet-4-6", 12, 120, 24, 6.0),
("gpt-5.4", 6, 60, 12, 3.0),
("openrouter/google/gemini-2.5-pro", 4, 40, 8, 2.0),
("kimi-k2", 2, 20, 4, 1.0),
]
summary = module.summarize_rows(rows, days=30)
self.assertEqual(summary["providers"], ["Anthropic", "Kimi / Moonshot", "OpenAI", "OpenRouter"])
self.assertAlmostEqual(summary["agents"]["Ezra"]["monthly_cost_usd"], 6.0)
self.assertAlmostEqual(summary["agents"]["Bezalel"]["monthly_cost_usd"], 3.0)
self.assertAlmostEqual(summary["agents"]["Timmy Cloud Lane"]["monthly_cost_usd"], 2.0)
self.assertAlmostEqual(summary["agents"]["Allegro"]["monthly_cost_usd"], 1.0)
self.assertAlmostEqual(summary["agents"]["Ezra"]["daily_cost_usd"], 0.2)
def test_report_render_mentions_inventory_and_agent_costs(self):
module = load_module()
rows = [
("claude-sonnet-4-6", 12, 120, 24, 6.0),
("gpt-5.4", 6, 60, 12, 3.0),
("openrouter/google/gemini-2.5-pro", 4, 40, 8, 2.0),
]
summary = module.summarize_rows(rows, days=30)
report = module.render_markdown(summary, report_date="2026-04-22")
self.assertIn("# Fleet Cost Report — 2026-04-22", report)
self.assertIn("## Paid API inventory", report)
self.assertIn("Anthropic", report)
self.assertIn("OpenRouter", report)
self.assertIn("OpenAI", report)
self.assertIn("## Estimated cost per agent per day", report)
self.assertIn("Timmy Cloud Lane", report)
self.assertIn("Ezra", report)
self.assertIn("Bezalel", report)
def test_write_report_creates_markdown_file(self):
module = load_module()
rows = [("claude-sonnet-4-6", 1, 10, 2, 0.5)]
summary = module.summarize_rows(rows, days=30)
with tempfile.TemporaryDirectory() as tmpdir:
dest = Path(tmpdir) / "fleet-cost.md"
module.write_report(dest, summary, report_date="2026-04-22")
self.assertTrue(dest.exists())
text = dest.read_text()
self.assertIn("Fleet Cost Report", text)
self.assertIn("Ezra", text)
if __name__ == "__main__":
unittest.main()