-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cost): surface cost-by-phase in the model usage dashboard (P5.T3) #407
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
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,61 @@ | ||
| /** | ||
| * Tests for orderPhaseCosts (P5.T3): ordering the model-usage | ||
| * "Cost by phase" breakdown for display. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { orderPhaseCosts } from '../lib/phase-costs'; | ||
|
|
||
| describe('orderPhaseCosts', () => { | ||
| it('returns [] for empty, null, or undefined input', () => { | ||
| expect(orderPhaseCosts({})).toEqual([]); | ||
| expect(orderPhaseCosts(null)).toEqual([]); | ||
| expect(orderPhaseCosts(undefined)).toEqual([]); | ||
| }); | ||
|
|
||
| it('orders known phases by pipeline order regardless of input order', () => { | ||
| const result = orderPhaseCosts({ | ||
| validation: 0.5, | ||
| planning: 1, | ||
| coding: 2, | ||
| }); | ||
| expect(result.map((p) => p.phase)).toEqual([ | ||
| 'planning', | ||
| 'coding', | ||
| 'validation', | ||
| ]); | ||
| }); | ||
|
|
||
| it('drops zero and negative costs', () => { | ||
| const result = orderPhaseCosts({ | ||
| planning: 0, | ||
| coding: 1.25, | ||
| validation: -3, | ||
| }); | ||
| expect(result).toEqual([{ phase: 'coding', cost: 1.25 }]); | ||
| }); | ||
|
|
||
| it('appends unknown phases after known ones, sorted alphabetically', () => { | ||
| const result = orderPhaseCosts({ | ||
| zeta: 1, | ||
| coding: 2, | ||
| unknown: 3, | ||
| planning: 4, | ||
| }); | ||
| expect(result.map((p) => p.phase)).toEqual([ | ||
| 'planning', | ||
| 'coding', | ||
| 'unknown', | ||
| 'zeta', | ||
| ]); | ||
| }); | ||
|
|
||
| it('ignores non-finite costs', () => { | ||
| const result = orderPhaseCosts({ | ||
| planning: Number.NaN, | ||
| coding: Number.POSITIVE_INFINITY, | ||
| validation: 0.99, | ||
| }); | ||
| expect(result).toEqual([{ phase: 'validation', cost: 0.99 }]); | ||
| }); | ||
| }); |
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,45 @@ | ||
| /** | ||
| * Ordering for the model-usage "Cost by phase" breakdown. | ||
| * | ||
| * The backend keys `cost_by_phase` by execution phase (see | ||
| * apps/backend/core/token_stats.py / save_token_stats callers). We render the | ||
| * phases in pipeline order, with any unrecognized keys ('unknown', future | ||
| * phases) appended alphabetically so nothing is silently dropped. | ||
| */ | ||
|
|
||
| export interface PhaseCost { | ||
| phase: string; | ||
| cost: number; | ||
| } | ||
|
|
||
| /** Canonical pipeline order of known phases. */ | ||
| export const PHASE_ORDER: readonly string[] = [ | ||
| 'planning', | ||
| 'coding', | ||
| 'test_generation', | ||
| 'validation', | ||
| 'performance_profiling', | ||
| ]; | ||
|
|
||
| /** | ||
| * Turn a `cost_by_phase` map into an ordered list, dropping non-positive | ||
| * entries. Known phases come first in pipeline order; unknown keys follow, | ||
| * sorted alphabetically. | ||
| */ | ||
| export function orderPhaseCosts( | ||
| costByPhase: Record<string, number> | undefined | null, | ||
| ): PhaseCost[] { | ||
| if (!costByPhase) return []; | ||
| const entries = Object.entries(costByPhase).filter( | ||
| ([, cost]) => Number.isFinite(cost) && cost > 0, | ||
| ); | ||
| entries.sort(([a], [b]) => { | ||
| const ia = PHASE_ORDER.indexOf(a); | ||
| const ib = PHASE_ORDER.indexOf(b); | ||
| if (ia !== -1 && ib !== -1) return ia - ib; | ||
| if (ia !== -1) return -1; | ||
| if (ib !== -1) return 1; | ||
| return a.localeCompare(b); | ||
| }); | ||
| return entries.map(([phase, cost]) => ({ phase, cost })); | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.