@atolis-hq/wake 0.4.87 → 0.4.89

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,12 +1,14 @@
1
- import { createRunBreakdownReport, createRunOutcomesReport, createRunUsageReport, queryRunAnalytics, } from '../execution/index.js';
1
+ import { createRunBreakdownReport, createRunDurationReport, 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
9
  createRunUsageReport(journal),
10
+ createWorkThroughputReport(journal),
11
+ createRunDurationReport(journal),
10
12
  ];
11
13
  return {
12
14
  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.87";
127
+ export const wakeVersion = "v0.4.89";
128
128
  /** The registry-installable version used by packaged sandbox images. */
129
129
  export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
@@ -1,11 +1,13 @@
1
1
  import { type EventJournal } from '@atolis-hq/eventing';
2
- import { type BreakdownReport, type ReportDefinition, type ReportQuery, type TimeSeriesReport, type ValueOf } from '../../kernel/index.js';
2
+ import { type BreakdownReport, type DurationReport, type ReportDefinition, type ReportQuery, type TimeSeriesReport, type ValueOf } from '../../kernel/index.js';
3
3
  import type { RunView } from '../contracts/views.js';
4
4
  export declare const RunReportId: {
5
+ readonly Duration: "run-duration";
5
6
  readonly Outcomes: "run-outcomes-over-time";
6
7
  readonly Usage: "token-and-cost-usage";
7
8
  readonly Breakdown: "run-breakdowns-by-dimension";
8
9
  };
10
+ export declare function createRunDurationReport(journal: EventJournal): ReportDefinition;
9
11
  export declare const RunUsageMeasure: {
10
12
  readonly Tokens: "total-tokens";
11
13
  readonly CostUsd: "total-cost-usd";
@@ -41,6 +43,7 @@ export interface RunSummary {
41
43
  export declare function runReportOutcome(run: RunView): RunReportOutcome;
42
44
  export declare function createRunOutcomesReport(journal: EventJournal): ReportDefinition;
43
45
  export declare function queryRunAnalytics(journal: EventJournal, query: ReportQuery): Promise<{
46
+ duration: DurationReport;
44
47
  report: TimeSeriesReport;
45
48
  breakdown: BreakdownReport;
46
49
  usage: TimeSeriesReport;
@@ -6,10 +6,18 @@ import { agentTokenUsage } from '../contracts/runner.js';
6
6
  import { RunStatus } from '../contracts/vocabulary.js';
7
7
  import { foldRunEvent } from '../domain/run.js';
8
8
  export const RunReportId = defineClosedVocabulary({
9
+ Duration: 'run-duration',
9
10
  Outcomes: 'run-outcomes-over-time',
10
11
  Usage: 'token-and-cost-usage',
11
12
  Breakdown: 'run-breakdowns-by-dimension',
12
13
  });
14
+ const durationDescriptor = { id: RunReportId.Duration, title: 'Run duration' };
15
+ export function createRunDurationReport(journal) {
16
+ return {
17
+ ...durationDescriptor,
18
+ query: async (query) => (await queryRunAnalytics(journal, query)).duration,
19
+ };
20
+ }
13
21
  export const RunUsageMeasure = defineClosedVocabulary({
14
22
  Tokens: 'total-tokens',
15
23
  CostUsd: 'total-cost-usd',
@@ -86,22 +94,20 @@ export function createRunOutcomesReport(journal) {
86
94
  };
87
95
  }
88
96
  export async function queryRunAnalytics(journal, query) {
89
- const grouped = await readRuns(journal, query);
90
97
  const from = Date.parse(query.window.from);
91
98
  const to = Date.parse(query.window.to);
92
99
  const first = Math.floor(from / query.window.bucketMs) * query.window.bucketMs;
93
- const zero = () => Object.fromEntries(series.map(({ id }) => [id, 0]));
94
100
  const buckets = outcomeBuckets(first, to, query.window.bucketMs);
95
101
  const usageBuckets = buckets.map(({ from, to }) => ({
96
102
  from,
97
103
  to,
98
104
  values: { [RunUsageMeasure.Tokens]: 0, [RunUsageMeasure.CostUsd]: 0 },
99
105
  }));
100
- const totals = zero();
101
- const durations = [];
106
+ const totals = Object.fromEntries(series.map(({ id }) => [id, 0]));
107
+ const durationData = collectDurations(query, buckets);
102
108
  const counts = dimensions.map(() => new Map());
103
109
  let runs = 0, totalTokens = 0, totalCostUsd = 0, processed = 0;
104
- for (const run of grouped.values()) {
110
+ for (const run of (await readRuns(journal, query)).values()) {
105
111
  if (processed++ % 100 === 0)
106
112
  await yieldScan(query.signal);
107
113
  const start = Date.parse(run.startedAt);
@@ -124,12 +130,12 @@ export async function queryRunAnalytics(journal, query) {
124
130
  const bucket = usageBuckets[Math.floor((start - first) / query.window.bucketMs)];
125
131
  bucket.values[RunUsageMeasure.Tokens] += tokens;
126
132
  bucket.values[RunUsageMeasure.CostUsd] += cost;
127
- const duration = runDuration(run);
128
- if (duration !== null)
129
- durations.push(duration);
133
+ durationData.add(run, Math.floor((start - first) / query.window.bucketMs));
130
134
  }
135
+ const duration = await durationData.report();
131
136
  query.signal?.throwIfAborted();
132
137
  return {
138
+ duration,
133
139
  report: {
134
140
  report: { id: RunReportId.Outcomes, title: 'Run outcomes over time' },
135
141
  window: query.window,
@@ -152,7 +158,7 @@ export async function queryRunAnalytics(journal, query) {
152
158
  failed: totals[RunReportOutcome.Failed],
153
159
  totalTokens,
154
160
  totalCostUsd,
155
- medianRunDurationMs: await median(durations, () => yieldScan(query.signal)),
161
+ medianRunDurationMs: duration.statistics.medianMs,
156
162
  },
157
163
  };
158
164
  }
@@ -199,3 +205,64 @@ function outcomeBuckets(first, to, bucketMs) {
199
205
  });
200
206
  return buckets;
201
207
  }
208
+ function collectDurations(query, buckets) {
209
+ const durations = [];
210
+ const durationBuckets = buckets.map(() => []);
211
+ const durationGroups = dimensions.slice(0, 2).map(() => new Map());
212
+ return {
213
+ add(run, bucketIndex) {
214
+ const duration = runDuration(run);
215
+ if (duration !== null) {
216
+ durations.push(duration);
217
+ durationBuckets[bucketIndex].push(duration);
218
+ dimensions.slice(0, 2).forEach((dimension, index) => {
219
+ const value = dimension.value(run) ?? RunReportValue.Unknown;
220
+ const groups = durationGroups[index];
221
+ const values = groups.get(value) ?? [];
222
+ values.push(duration);
223
+ groups.set(value, values);
224
+ });
225
+ }
226
+ },
227
+ async report() {
228
+ const statistics = async (values) => {
229
+ let sum = 0;
230
+ for (const [index, value] of values.entries()) {
231
+ if (index % 1024 === 0)
232
+ await yieldScan(query.signal);
233
+ sum += value;
234
+ }
235
+ return {
236
+ count: values.length,
237
+ medianMs: await median(values, () => yieldScan(query.signal)),
238
+ averageMs: values.length === 0 ? null : sum / values.length,
239
+ };
240
+ };
241
+ const groups = [];
242
+ for (const [index, dimension] of dimensions.slice(0, 2).entries()) {
243
+ const rows = [];
244
+ for (const [value, values] of durationGroups[index]) {
245
+ await yieldScan(query.signal);
246
+ rows.push({ value, ...(await statistics(values)) });
247
+ }
248
+ rows.sort((a, b) => b.count - a.count || (a.value < b.value ? -1 : a.value > b.value ? 1 : 0));
249
+ groups.push({ id: dimension.id, title: dimension.title, rows });
250
+ }
251
+ const durationSeries = [];
252
+ for (const [index, bucket] of buckets.entries()) {
253
+ durationSeries.push({
254
+ from: bucket.from,
255
+ to: bucket.to,
256
+ ...(await statistics(durationBuckets[index])),
257
+ });
258
+ }
259
+ return {
260
+ report: durationDescriptor,
261
+ window: query.window,
262
+ statistics: await statistics(durations),
263
+ groups,
264
+ buckets: durationSeries,
265
+ };
266
+ },
267
+ };
268
+ }
@@ -21,6 +21,14 @@ 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
34
  readonly series: readonly (ReportDescriptor & {
@@ -45,7 +53,26 @@ export interface BreakdownReport {
45
53
  }[];
46
54
  })[];
47
55
  }
48
- export type AnalyticsReport = TimeSeriesReport | BreakdownReport;
56
+ export interface DurationStatistics {
57
+ readonly count: number;
58
+ readonly medianMs: number | null;
59
+ readonly averageMs: number | null;
60
+ }
61
+ export interface DurationReport {
62
+ readonly report: ReportDescriptor;
63
+ readonly window: ReportWindow;
64
+ readonly statistics: DurationStatistics;
65
+ readonly groups: readonly (ReportDescriptor & {
66
+ readonly rows: readonly (DurationStatistics & {
67
+ readonly value: string;
68
+ })[];
69
+ })[];
70
+ readonly buckets: readonly (DurationStatistics & {
71
+ readonly from: string;
72
+ readonly to: string;
73
+ })[];
74
+ }
75
+ export type AnalyticsReport = TimeSeriesReport | BreakdownReport | DurationReport;
49
76
  export interface ReportDefinition extends ReportDescriptor {
50
77
  query(query: ReportQuery): Promise<AnalyticsReport>;
51
78
  }
@@ -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
+ }
@@ -10,7 +10,7 @@ export interface MetricsResponse {
10
10
  readonly values: Readonly<Record<string, number>>;
11
11
  }
12
12
  export { ReportWindowId } from '../../../kernel/index.js';
13
- export type { AnalyticsReport, ReportWindowId as AnalyticsReportWindowId, BreakdownReport, ReportDescriptor, ReportWindow, TimeSeriesReport, } from '../../../kernel/index.js';
13
+ export type { AnalyticsReport, ReportWindowId as AnalyticsReportWindowId, BreakdownReport, DurationReport, DurationStatistics, ReportDescriptor, ReportWindow, TimeSeriesReport, } from '../../../kernel/index.js';
14
14
  export interface AnalyticsSummaryResponse {
15
15
  readonly window: ReportWindow;
16
16
  readonly runs: number;