Skip to content

feat(scripts): add cost monitor with daily summary + threshold alerts - #13

Open
allenter wants to merge 3 commits into
MaxMiksa:mainfrom
allenter:pr/feat-cost-monitor
Open

feat(scripts): add cost monitor with daily summary + threshold alerts#13
allenter wants to merge 3 commits into
MaxMiksa:mainfrom
allenter:pr/feat-cost-monitor

Conversation

@allenter

@allenter allenter commented Aug 2, 2026

Copy link
Copy Markdown

PR: feat(scripts): 新增 cost-monitor.sh 成本监控(每日汇总 + 阈值告警)

类型:feature | 影响:新工具脚本,无破坏 | ~150 行

动机

Auto-Company 是自主 24/7 运行并消耗模型额度(Claude API / Codex)的循环,但没有任何成本追踪能力——每轮 cost: 只散落在 logs/auto-loop.log。用户无法回答"今天烧了多少钱""这个月预算还够不够"。

功能

新增 scripts/core/cost-monitor.sh,解析 logs/auto-loop.log 中每轮成本并按自然日汇总:

$ ./cost-monitor.sh
Date            Total$   Runs      Avg$      Max$
----------   --------- ------  --------  --------
2026-08-02       17.86      3      5.95      7.14
----------   ---------
Total           $17.86

用法

命令 功能
./cost-monitor.sh 今日汇总(总成本/轮数/均值/最大单轮)
./cost-monitor.sh --days 7 最近 N 天
./cost-monitor.sh --date 2026-08-02 / --date yesterday 单日查询
./cost-monitor.sh --check --threshold 20 告警模式:今日超 $20 → 写 logs/cost-alerts.log + exit 1(cron 友好)
COST_THRESHOLD_DAILY=25 环境变量设置默认阈值

设计要点

  • 数据源:正则提取日志中所有 cost: <float> 字段(match() 而非 /regex/ pattern,因 macOS awk 的 pattern 匹配不设置 RSTART/RLENGTH
  • 跨平台:兼容 macOS date -v 与 GNU date -d;纯 printf 对齐(macOS column-t
  • 保留尾换行:bash read 将无换行的末行视为 EOF 而跳过,因此 rows 保留尾部 \n
  • 告警日志:超阈值追加写 logs/cost-alerts.log,退出码 1 便于 cron/launchd 触发通知

验证

  • 今日/多天/单日汇总均正确
  • --check 低于阈值 exit 0,高于阈值 exit 1 且写告警日志
  • --help 正常
  • 无数据时优雅退出(exit 0)

变更文件

  • scripts/core/cost-monitor.sh(新增,核心:解析 + 按天汇总 + --check 阈值告警)
  • scripts/core/cost-alert.sh(新增,launchd wrapper:跑 --check,超阈值弹 macOS 桌面通知并记录 logs/cost-daily.log
  • 建议同时更新 INDEX.md / README.md 的脚本索引

配套 launchd 调度(可选,随附示例)

~/Library/LaunchAgents/com.autocompany.costmonitor.plist 每 4 小时触发 cost-alert.sh,环境变量 COST_THRESHOLD_DAILY(默认 20)控制每日阈值;RunAtLoad 启动即检查一次。

后续建议(可另开 PR)

  • 看板 /api 增加成本摘要端点,复用本脚本输出
  • Makefile 增加 cost 目标

Co-Authored-By: Claude noreply@anthropic.com

allenter and others added 3 commits August 2, 2026 08:50
Layer a zero-dependency, pure-Python vector memory store (memories/vault/)
on top of the existing single-file consensus baton:

- scripts/core/memory_vault.py: chunk consensus + docs, index into
  memories/vault/index.json (TF-IDF char n-grams + cosine similarity,
  no external deps), and semantic search with -top-k / min-score.
  Backend swappable: replace _embed_chunk() to plug in ChromaDB or a
  model embedding.
- auto-loop.sh: each cycle auto-retrieves the top relevant historical
  blocks (keyed off Next Action) and injects them into the prompt as
  '## Highly-relevant past memory'; after a successful/soft-timeout
  cycle it indexes the latest consensus + docs into the vault.
- .gitignore: ignore memories/vault/* runtime data.
- Docs updated: README(EN/ZH), CLAUDE.md, PROMPT.md, INDEX.md.

consensus.md remains the authoritative running-state baton; the vault
adds long-term recall of decisions/context that consensus collapses.
Add a Memory Vault panel to the control deck:

- server.py: new GET /api/vault endpoint. Reads memories/vault/index.json
  directly (no subprocess) and returns stats (chunk/source/term counts,
  size, last-indexed), per-source breakdown, latest entries, plus optional
  in-process cosine semantic search via ?q=.
- app.js: fetchVault() renderer with stat cards, a canvas bar chart of
  chunks per source, latest-entry list, and a live semantic-search box
  (Enter or button) showing scored hits with source tags.
- index.html: Memory Vault section between Consensus and Recent Log.
- styles.css: dark-theme styles for stats, canvas, entries, tags.

Pure stdlib on the server side; canvas drawn client-side.
Auto-Company burns model quota 24/7 but had zero cost tracking; each
cycle's `cost:` only scattered across logs/auto-loop.log. Add a small
zero-dependency tool:

- cost-monitor.sh: parses per-cycle cost from logs/auto-loop.log and
  summarizes by calendar day (total/runs/avg/max). Supports --days,
  --date (incl. "yesterday"), --threshold, and a --check mode that
  exits non-zero when today's spend exceeds the threshold and appends
  to logs/cost-alerts.log (cron/launchd friendly).
- cost-alert.sh: launchd wrapper running --check that raises a macOS
  desktop notification on threshold breach and records logs/cost-daily.log.

Portability notes baked in: uses match() (macOS awk doesn't set
RSTART/RLENGTH for /regex/ patterns), supports both `date -v` and
`date -d`, avoids `column -t` (absent on macOS), and keeps the final
newline (bash `read` treats a trailing line without \n as EOF).

Verified on macOS: today/7-day/single-day summaries, --check under and
over threshold (exit 0/1), --help, and no-data path.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant