-
Notifications
You must be signed in to change notification settings - Fork 1
feat: local observability — token capture + cost tracking #618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+418
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * squads obs — observability commands | ||
| * | ||
| * squads obs history Execution history with tokens/cost | ||
| * squads obs cost Spend summary by squad and model | ||
| */ | ||
|
|
||
| import { Command } from 'commander'; | ||
| import { queryExecutions, calculateCostSummary } from '../lib/observability.js'; | ||
| import { colors, bold, RESET, writeLine } from '../lib/terminal.js'; | ||
|
|
||
| export function registerObservabilityCommands(program: Command): void { | ||
| const obs = program | ||
| .command('obs') | ||
| .description('Observability — execution history, token costs, and trends'); | ||
|
|
||
| obs | ||
| .command('history') | ||
| .description('Show execution history with tokens and cost') | ||
| .option('-s, --squad <squad>', 'Filter by squad') | ||
| .option('-a, --agent <agent>', 'Filter by agent') | ||
| .option('-n, --limit <n>', 'Number of records', '20') | ||
| .option('--since <date>', 'Since date (ISO or relative: 1d, 7d, 30d)') | ||
| .option('--json', 'Output as JSON') | ||
| .action((opts) => { | ||
| let since = opts.since; | ||
| if (since && /^\d+d$/.test(since)) { | ||
| const days = parseInt(since, 10); | ||
| since = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString(); | ||
| } | ||
|
|
||
| const records = queryExecutions({ | ||
| squad: opts.squad, agent: opts.agent, since, limit: parseInt(opts.limit, 10), | ||
| }); | ||
|
|
||
| if (records.length === 0) { | ||
| writeLine(`\n ${colors.dim}No executions found. Run \`squads run <squad>\` to generate data.${RESET}\n`); | ||
| return; | ||
| } | ||
|
|
||
| if (opts.json) { console.log(JSON.stringify(records, null, 2)); return; } | ||
|
|
||
| writeLine(`\n ${bold}Execution History${RESET} (${records.length} records)\n`); | ||
|
|
||
| for (const r of records) { | ||
| const icon = r.status === 'completed' ? `${colors.green}pass${RESET}` | ||
| : r.status === 'failed' ? `${colors.red}fail${RESET}` : `${colors.yellow}timeout${RESET}`; | ||
| const dur = r.duration_ms > 60000 ? `${Math.round(r.duration_ms / 60000)}m` : `${Math.round(r.duration_ms / 1000)}s`; | ||
| const cost = r.cost_usd > 0 ? `$${r.cost_usd.toFixed(3)}` : '$—'; | ||
| const tok = (r.input_tokens + r.output_tokens) > 0 ? `${(r.input_tokens + r.output_tokens).toLocaleString()} tok` : '— tok'; | ||
| const date = r.ts.slice(0, 16).replace('T', ' '); | ||
|
|
||
| writeLine(` ${icon} ${bold}${r.squad}/${r.agent}${RESET} ${colors.dim}${date} ${dur} ${tok} ${cost} ${r.model}${RESET}`); | ||
| if (r.error) writeLine(` ${colors.red}${r.error.slice(0, 80)}${RESET}`); | ||
| } | ||
| writeLine(); | ||
| }); | ||
|
|
||
| obs | ||
| .command('cost') | ||
| .description('Show token spend summary') | ||
| .option('-p, --period <period>', 'Time period: today, 7d, 30d, all', '7d') | ||
| .option('--json', 'Output as JSON') | ||
| .action((opts) => { | ||
| const summary = calculateCostSummary(opts.period); | ||
|
|
||
| if (summary.total_runs === 0) { | ||
| writeLine(`\n ${colors.dim}No executions in the last ${opts.period}.${RESET}\n`); | ||
| return; | ||
| } | ||
|
|
||
| if (opts.json) { console.log(JSON.stringify(summary, null, 2)); return; } | ||
|
|
||
| writeLine(`\n ${bold}Cost Summary${RESET} (${summary.period})`); | ||
| writeLine(`\n Total: ${bold}$${summary.total_cost.toFixed(2)}${RESET} across ${summary.total_runs} runs`); | ||
| writeLine(` Tokens: ${summary.total_input_tokens.toLocaleString()} in / ${summary.total_output_tokens.toLocaleString()} out\n`); | ||
|
|
||
| const squads = Object.entries(summary.by_squad).sort((a, b) => b[1].cost - a[1].cost); | ||
| if (squads.length > 0) { | ||
| writeLine(` ${colors.cyan}By Squad${RESET}`); | ||
| for (const [name, data] of squads) { | ||
| const bar = '█'.repeat(Math.max(1, Math.round(data.cost / (summary.total_cost || 1) * 20))); | ||
| writeLine(` ${name.padEnd(20)} ${colors.dim}${bar}${RESET} $${data.cost.toFixed(2)} (${data.runs} runs, avg $${data.avg_cost.toFixed(3)})`); | ||
| } | ||
| writeLine(); | ||
| } | ||
|
|
||
| const models = Object.entries(summary.by_model).sort((a, b) => b[1].cost - a[1].cost); | ||
| if (models.length > 0) { | ||
| writeLine(` ${colors.cyan}By Model${RESET}`); | ||
| for (const [name, data] of models) { | ||
| writeLine(` ${name.padEnd(30)} $${data.cost.toFixed(2)} (${data.runs} runs)`); | ||
| } | ||
| writeLine(); | ||
| } | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's significant code duplication between this
on('error')handler and theon('close')handler (lines 467-484) for creating theObservabilityRecord. This can make maintenance harder, as changes might need to be applied in two places.Consider creating a helper function to construct and log the
ObservabilityRecord. This function could take parameters like status, error, and session usage to handle both success and failure cases, reducing duplication and centralizing the logic.