@atolis-hq/wake 0.4.86 → 0.4.87

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.
@@ -1,4 +1,4 @@
1
- import { createRunBreakdownReport, createRunOutcomesReport, queryRunAnalytics, } from '../execution/index.js';
1
+ import { createRunBreakdownReport, createRunOutcomesReport, createRunUsageReport, queryRunAnalytics, } from '../execution/index.js';
2
2
  import { reportWindow } from '../kernel/index.js';
3
3
  import { queryWorkCompletionSummary } from '../orchestration/index.js';
4
4
  import { sampledMeta } from './surface-api-metadata.js';
@@ -6,6 +6,7 @@ export function createAnalyticsReports(journal, now) {
6
6
  const definitions = [
7
7
  createRunOutcomesReport(journal),
8
8
  createRunBreakdownReport(journal),
9
+ createRunUsageReport(journal),
9
10
  ];
10
11
  return {
11
12
  catalogue: () => definitions.map(({ id, title }) => ({ id, title })),
@@ -124,6 +124,6 @@ export function resolveWakeVersion(options = {}) {
124
124
  return `g${headHash.slice(0, 7)}`;
125
125
  return '0.1.0-dev';
126
126
  }
127
- export const wakeVersion = "v0.4.86";
127
+ export const wakeVersion = "v0.4.87";
128
128
  /** The registry-installable version used by packaged sandbox images. */
129
129
  export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
@@ -3,8 +3,14 @@ import { type BreakdownReport, type ReportDefinition, type ReportQuery, type Tim
3
3
  import type { RunView } from '../contracts/views.js';
4
4
  export declare const RunReportId: {
5
5
  readonly Outcomes: "run-outcomes-over-time";
6
+ readonly Usage: "token-and-cost-usage";
6
7
  readonly Breakdown: "run-breakdowns-by-dimension";
7
8
  };
9
+ export declare const RunUsageMeasure: {
10
+ readonly Tokens: "total-tokens";
11
+ readonly CostUsd: "total-cost-usd";
12
+ };
13
+ export declare function createRunUsageReport(journal: EventJournal): ReportDefinition;
8
14
  export declare const RunReportDimension: {
9
15
  readonly Action: "run-action";
10
16
  readonly Stage: "run-stage";
@@ -37,5 +43,6 @@ export declare function createRunOutcomesReport(journal: EventJournal): ReportDe
37
43
  export declare function queryRunAnalytics(journal: EventJournal, query: ReportQuery): Promise<{
38
44
  report: TimeSeriesReport;
39
45
  breakdown: BreakdownReport;
46
+ usage: TimeSeriesReport;
40
47
  summary: RunSummary;
41
48
  }>;
@@ -7,8 +7,24 @@ import { RunStatus } from '../contracts/vocabulary.js';
7
7
  import { foldRunEvent } from '../domain/run.js';
8
8
  export const RunReportId = defineClosedVocabulary({
9
9
  Outcomes: 'run-outcomes-over-time',
10
+ Usage: 'token-and-cost-usage',
10
11
  Breakdown: 'run-breakdowns-by-dimension',
11
12
  });
13
+ export const RunUsageMeasure = defineClosedVocabulary({
14
+ Tokens: 'total-tokens',
15
+ CostUsd: 'total-cost-usd',
16
+ });
17
+ const usageSeries = [
18
+ { id: RunUsageMeasure.Tokens, title: 'Total tokens', unit: 'tokens' },
19
+ { id: RunUsageMeasure.CostUsd, title: 'Total cost (USD)', unit: 'USD' },
20
+ ];
21
+ const usageDescriptor = { id: RunReportId.Usage, title: 'Token and cost usage' };
22
+ export function createRunUsageReport(journal) {
23
+ return {
24
+ ...usageDescriptor,
25
+ query: async (query) => (await queryRunAnalytics(journal, query)).usage,
26
+ };
27
+ }
12
28
  export const RunReportDimension = defineClosedVocabulary({
13
29
  Action: 'run-action',
14
30
  Stage: 'run-stage',
@@ -70,24 +86,17 @@ export function createRunOutcomesReport(journal) {
70
86
  };
71
87
  }
72
88
  export async function queryRunAnalytics(journal, query) {
73
- const grouped = new Map();
74
- for await (const event of scanJournal(journal, query.signal)) {
75
- const owned = selectRunExecutionEvent(event);
76
- if (owned === null)
77
- continue;
78
- grouped.set(owned.stream.id, foldRunEvent(grouped.get(owned.stream.id) ?? null, owned));
79
- }
89
+ const grouped = await readRuns(journal, query);
80
90
  const from = Date.parse(query.window.from);
81
91
  const to = Date.parse(query.window.to);
82
92
  const first = Math.floor(from / query.window.bucketMs) * query.window.bucketMs;
83
93
  const zero = () => Object.fromEntries(series.map(({ id }) => [id, 0]));
84
- const buckets = [];
85
- for (let start = first; start < to; start += query.window.bucketMs)
86
- buckets.push({
87
- from: new Date(start).toISOString(),
88
- to: new Date(start + query.window.bucketMs).toISOString(),
89
- values: zero(),
90
- });
94
+ const buckets = outcomeBuckets(first, to, query.window.bucketMs);
95
+ const usageBuckets = buckets.map(({ from, to }) => ({
96
+ from,
97
+ to,
98
+ values: { [RunUsageMeasure.Tokens]: 0, [RunUsageMeasure.CostUsd]: 0 },
99
+ }));
91
100
  const totals = zero();
92
101
  const durations = [];
93
102
  const counts = dimensions.map(() => new Map());
@@ -108,8 +117,13 @@ export async function queryRunAnalytics(journal, query) {
108
117
  groups.set(value, (groups.get(value) ?? 0) + 1);
109
118
  });
110
119
  const usage = agentTokenUsage(run.agent?.metadata);
111
- totalTokens += finiteOrZero(usage.tokens);
112
- totalCostUsd += finiteOrZero(usage.costUsd);
120
+ const tokens = finiteOrZero(usage.tokens);
121
+ const cost = finiteOrZero(usage.costUsd);
122
+ totalTokens += tokens;
123
+ totalCostUsd += cost;
124
+ const bucket = usageBuckets[Math.floor((start - first) / query.window.bucketMs)];
125
+ bucket.values[RunUsageMeasure.Tokens] += tokens;
126
+ bucket.values[RunUsageMeasure.CostUsd] += cost;
113
127
  const duration = runDuration(run);
114
128
  if (duration !== null)
115
129
  durations.push(duration);
@@ -123,18 +137,14 @@ export async function queryRunAnalytics(journal, query) {
123
137
  totals,
124
138
  buckets,
125
139
  },
126
- breakdown: {
127
- report: { id: RunReportId.Breakdown, title: 'Run breakdowns by dimension' },
140
+ usage: {
141
+ report: usageDescriptor,
128
142
  window: query.window,
129
- total: runs,
130
- dimensions: dimensions.map(({ id, title }, index) => ({
131
- id,
132
- title,
133
- rows: [...counts[index].entries()]
134
- .sort(([a, ac], [b, bc]) => bc - ac || (a < b ? -1 : a > b ? 1 : 0))
135
- .map(([value, count]) => ({ value, count, share: count / runs })),
136
- })),
143
+ series: usageSeries,
144
+ totals: { [RunUsageMeasure.Tokens]: totalTokens, [RunUsageMeasure.CostUsd]: totalCostUsd },
145
+ buckets: usageBuckets,
137
146
  },
147
+ breakdown: runBreakdown(query, runs, counts),
138
148
  summary: {
139
149
  runs,
140
150
  completed: totals[RunReportOutcome.Completed],
@@ -155,3 +165,37 @@ function runDuration(run) {
155
165
  const duration = Date.parse(run.finishedAt) - Date.parse(run.startedAt);
156
166
  return Number.isFinite(duration) && duration >= 0 ? duration : null;
157
167
  }
168
+ async function readRuns(journal, query) {
169
+ const grouped = new Map();
170
+ for await (const event of scanJournal(journal, query.signal)) {
171
+ const owned = selectRunExecutionEvent(event);
172
+ if (owned === null)
173
+ continue;
174
+ grouped.set(owned.stream.id, foldRunEvent(grouped.get(owned.stream.id) ?? null, owned));
175
+ }
176
+ return grouped;
177
+ }
178
+ function runBreakdown(query, runs, counts) {
179
+ return {
180
+ report: { id: RunReportId.Breakdown, title: 'Run breakdowns by dimension' },
181
+ window: query.window,
182
+ total: runs,
183
+ dimensions: dimensions.map(({ id, title }, index) => ({
184
+ id,
185
+ title,
186
+ rows: [...counts[index].entries()]
187
+ .sort(([a, ac], [b, bc]) => bc - ac || (a < b ? -1 : a > b ? 1 : 0))
188
+ .map(([value, count]) => ({ value, count, share: count / runs })),
189
+ })),
190
+ };
191
+ }
192
+ function outcomeBuckets(first, to, bucketMs) {
193
+ const buckets = [];
194
+ for (let start = first; start < to; start += bucketMs)
195
+ buckets.push({
196
+ from: new Date(start).toISOString(),
197
+ to: new Date(start + bucketMs).toISOString(),
198
+ values: Object.fromEntries(series.map(({ id }) => [id, 0])),
199
+ });
200
+ return buckets;
201
+ }
@@ -23,7 +23,9 @@ export interface ReportDescriptor {
23
23
  export interface TimeSeriesReport {
24
24
  readonly report: ReportDescriptor;
25
25
  readonly window: ReportWindow;
26
- readonly series: readonly ReportDescriptor[];
26
+ readonly series: readonly (ReportDescriptor & {
27
+ readonly unit?: string;
28
+ })[];
27
29
  readonly totals: Readonly<Record<string, number>>;
28
30
  readonly buckets: readonly {
29
31
  readonly from: string;