@atolis-hq/wake 0.4.86 → 0.4.88

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,11 +1,13 @@
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
- import { queryWorkCompletionSummary } from '../orchestration/index.js';
3
+ import { createWorkThroughputReport, queryWorkCompletionSummary } from '../orchestration/index.js';
4
4
  import { sampledMeta } from './surface-api-metadata.js';
5
5
  export function createAnalyticsReports(journal, now) {
6
6
  const definitions = [
7
7
  createRunOutcomesReport(journal),
8
8
  createRunBreakdownReport(journal),
9
+ createRunUsageReport(journal),
10
+ createWorkThroughputReport(journal),
9
11
  ];
10
12
  return {
11
13
  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.88";
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
+ }
@@ -21,9 +21,19 @@ export interface ReportDescriptor {
21
21
  readonly title: string;
22
22
  }
23
23
  export interface TimeSeriesReport {
24
+ readonly durationRows?: readonly {
25
+ readonly id: string;
26
+ readonly title: string;
27
+ readonly createdAt: string;
28
+ readonly completedAt: string;
29
+ readonly durationMs: number;
30
+ }[];
31
+ readonly excludedDurationCount?: number;
24
32
  readonly report: ReportDescriptor;
25
33
  readonly window: ReportWindow;
26
- readonly series: readonly ReportDescriptor[];
34
+ readonly series: readonly (ReportDescriptor & {
35
+ readonly unit?: string;
36
+ })[];
27
37
  readonly totals: Readonly<Record<string, number>>;
28
38
  readonly buckets: readonly {
29
39
  readonly from: string;
@@ -1,6 +1,14 @@
1
1
  import { type EventJournal } from '@atolis-hq/eventing';
2
- import { type ReportQuery } from '../../kernel/index.js';
2
+ import { type ReportDefinition, type ReportQuery, type TimeSeriesReport } from '../../kernel/index.js';
3
3
  export declare function queryWorkCompletionSummary(journal: EventJournal, query: ReportQuery): Promise<{
4
4
  workItemsDone: number;
5
5
  medianEndToEndDurationMs: number | null;
6
6
  }>;
7
+ export declare const WorkReportId: {
8
+ readonly Throughput: "work-item-throughput";
9
+ };
10
+ export declare const WorkReportMeasure: {
11
+ readonly Completed: "work-items-done";
12
+ };
13
+ export declare function createWorkThroughputReport(journal: EventJournal): ReportDefinition;
14
+ export declare function queryWorkThroughput(journal: EventJournal, query: ReportQuery): Promise<TimeSeriesReport>;
@@ -1,12 +1,10 @@
1
1
  import { scanJournal, yieldScan } from '@atolis-hq/eventing';
2
- import { median } from '../../kernel/index.js';
2
+ import { defineClosedVocabulary, median, } from '../../kernel/index.js';
3
3
  import { selectWorkEvent, WorkEventType } from '../../work/index.js';
4
4
  import { selectWorkflowOrchestrationEvent } from '../contracts/event-decoder.js';
5
5
  import { OrchestrationEventType } from '../contracts/events.js';
6
6
  export async function queryWorkCompletionSummary(journal, query) {
7
- const facts = { created: new Map(), instances: new Map(), completed: new Map() };
8
- for await (const event of scanJournal(journal, query.signal))
9
- acceptFact(facts, event);
7
+ const facts = await readCompletionFacts(journal, query);
10
8
  const from = Date.parse(query.window.from), to = Date.parse(query.window.to);
11
9
  let workItemsDone = 0, processed = 0;
12
10
  const durations = [];
@@ -27,8 +25,10 @@ export async function queryWorkCompletionSummary(journal, query) {
27
25
  }
28
26
  function acceptFact(facts, event) {
29
27
  const work = selectWorkEvent(event);
30
- if (work?.event.eventType === WorkEventType.ItemCreated)
28
+ if (work?.event.eventType === WorkEventType.ItemCreated) {
31
29
  facts.created.set(work.stream.id, Date.parse(work.event.occurredAt));
30
+ facts.titles.set(work.stream.id, work.event.payload.objective);
31
+ }
32
32
  const workflow = selectWorkflowOrchestrationEvent(event);
33
33
  if (workflow === null)
34
34
  return;
@@ -47,3 +47,89 @@ function acceptFact(facts, event) {
47
47
  return;
48
48
  }
49
49
  }
50
+ async function readCompletionFacts(journal, query) {
51
+ const facts = {
52
+ created: new Map(),
53
+ titles: new Map(),
54
+ instances: new Map(),
55
+ completed: new Map(),
56
+ };
57
+ for await (const event of scanJournal(journal, query.signal))
58
+ acceptFact(facts, event);
59
+ return facts;
60
+ }
61
+ export const WorkReportId = defineClosedVocabulary({ Throughput: 'work-item-throughput' });
62
+ export const WorkReportMeasure = defineClosedVocabulary({ Completed: 'work-items-done' });
63
+ const descriptor = { id: WorkReportId.Throughput, title: 'Work item throughput' };
64
+ export function createWorkThroughputReport(journal) {
65
+ return { ...descriptor, query: (query) => queryWorkThroughput(journal, query) };
66
+ }
67
+ export async function queryWorkThroughput(journal, query) {
68
+ const facts = await readCompletionFacts(journal, query);
69
+ const from = Date.parse(query.window.from), to = Date.parse(query.window.to);
70
+ const first = Math.floor(from / query.window.bucketMs) * query.window.bucketMs;
71
+ const key = WorkReportMeasure.Completed;
72
+ const buckets = [];
73
+ for (let start = first; start < to; start += query.window.bucketMs)
74
+ buckets.push({
75
+ from: new Date(start).toISOString(),
76
+ to: new Date(start + query.window.bucketMs).toISOString(),
77
+ values: { [key]: 0 },
78
+ });
79
+ const rows = [];
80
+ let total = 0, processed = 0;
81
+ for (const [id, end] of facts.completed) {
82
+ if (processed++ % 100 === 0)
83
+ await yieldScan(query.signal);
84
+ if (!(end >= from && end < to))
85
+ continue;
86
+ total++;
87
+ buckets[Math.floor((end - first) / query.window.bucketMs)].values[key]++;
88
+ const start = facts.created.get(id);
89
+ if (start === undefined || !Number.isFinite(start) || end < start)
90
+ continue;
91
+ rows.push({
92
+ id,
93
+ title: facts.titles.get(id) ?? id,
94
+ createdAt: new Date(start).toISOString(),
95
+ completedAt: new Date(end).toISOString(),
96
+ durationMs: end - start,
97
+ });
98
+ }
99
+ const sorted = await sortDurationRows(rows, query.signal);
100
+ const report = {
101
+ report: descriptor,
102
+ window: query.window,
103
+ series: [{ id: key, title: 'Work items done', unit: 'work items' }],
104
+ totals: { [key]: total },
105
+ buckets,
106
+ durationRows: sorted,
107
+ excludedDurationCount: total - rows.length,
108
+ };
109
+ return report;
110
+ }
111
+ async function sortDurationRows(rows, signal) {
112
+ // Sort bounded batches and yield during merging to keep large reports cancellable.
113
+ let sorted = [];
114
+ const compare = (a, b) => b.completedAt.localeCompare(a.completedAt) || a.id.localeCompare(b.id);
115
+ for (let offset = 0; offset < rows.length; offset += 1024) {
116
+ await yieldScan(signal);
117
+ sorted.push(...rows.slice(offset, offset + 1024).sort(compare));
118
+ }
119
+ for (let width = 1024; width < sorted.length; width *= 2) {
120
+ const merged = [];
121
+ for (let start = 0; start < sorted.length; start += width * 2) {
122
+ const middle = Math.min(start + width, sorted.length), end = Math.min(start + width * 2, sorted.length);
123
+ let left = start, right = middle;
124
+ while (left < middle || right < end) {
125
+ if (merged.length % 1024 === 0)
126
+ await yieldScan(signal);
127
+ merged.push(right === end || (left < middle && compare(sorted[left], sorted[right]) <= 0)
128
+ ? sorted[left++]
129
+ : sorted[right++]);
130
+ }
131
+ }
132
+ sorted = merged;
133
+ }
134
+ return sorted;
135
+ }