@atolis-hq/wake 0.3.33 → 0.3.35

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.
@@ -37,7 +37,10 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
37
37
  const resources = createResourceService(journal, lookup);
38
38
  const pullRequests = createPullRequestService(journal, work, resources);
39
39
  const activities = options.activities ??
40
- createBuiltInActivityRegistry(journal, pullRequests, resources, wakeRoot, createGitHubAgentContextReader(journal, resources));
40
+ createBuiltInActivityRegistry(journal, pullRequests, resources, wakeRoot, createGitHubAgentContextReader(journal, resources, {
41
+ publicUiUrl: config.surfaces.web.publicUrl,
42
+ githubAdapters: githubAdapters(config),
43
+ }));
41
44
  const definitions = Object.fromEntries(Object.entries(config.orchestration.workflows).map(([name, definition]) => [
42
45
  name,
43
46
  compileWorkflow(name, definition, activities, Object.keys(config.orchestration.workflows)),
@@ -156,3 +159,6 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
156
159
  ...runtime,
157
160
  };
158
161
  }
162
+ function githubAdapters(config) {
163
+ return Object.entries(config.integrations).flatMap(([adapter, integration]) => integration.enabled && (integration.provider ?? adapter) === 'github' ? [adapter] : []);
164
+ }
@@ -108,4 +108,4 @@ export function resolveWakeVersion(options = {}) {
108
108
  return `g${headHash.slice(0, 7)}`;
109
109
  return '0.1.0-dev';
110
110
  }
111
- export const wakeVersion = "g0a8e435";
111
+ export const wakeVersion = "g4327396";
@@ -16,48 +16,42 @@ export class DeliveryOutcomeReactor {
16
16
  async runOnce() {
17
17
  const consumer = 'reactor:delivery-outcomes';
18
18
  const events = await this.journal.readAll(await this.checkpoints.load(consumer));
19
+ const resolvedDeliveryEventIds = new Set();
19
20
  for (const event of events) {
20
- const delivery = selectDeliveryEvent(event);
21
- if (delivery !== null) {
22
- const command = {
23
- workflowInstanceId: workflowInstanceId(delivery.payload.workflowInstanceId),
24
- activationId: activationId(delivery.payload.activationId),
25
- };
26
- if (await this.isAwaitingThisDelivery(command, delivery.payload.intentEventId)) {
27
- if (delivery.eventType === DeliveryEventType.Confirmed ||
28
- (delivery.eventType === DeliveryEventType.Reconciled &&
29
- delivery.payload.result === DeliveryResultKind.Confirmed))
30
- await this.orchestration.acceptOutcome({
31
- ...command,
32
- outcome: {
33
- kind: ActivityOutcomeKind.Done,
34
- data: { deliveryEventId: delivery.eventId },
35
- },
36
- }, {
37
- commandId: delivery.eventId,
38
- correlationId: event.correlationId,
39
- actor: { kind: EventActorKind.System, id: 'delivery-outcome-reactor' },
40
- occurredAt: event.recordedAt,
41
- });
42
- if (delivery.eventType === DeliveryEventType.Failed)
43
- await this.orchestration.acceptOutcome({
44
- ...command,
45
- outcome: {
46
- kind: ActivityOutcomeKind.Failed,
47
- data: { reason: delivery.payload.code },
48
- },
49
- }, {
50
- commandId: delivery.eventId,
51
- correlationId: event.correlationId,
52
- actor: { kind: EventActorKind.System, id: 'delivery-outcome-reactor' },
53
- occurredAt: event.recordedAt,
54
- });
55
- }
56
- }
21
+ await this.reconcile(event, resolvedDeliveryEventIds);
57
22
  await this.checkpoints.save(consumer, event.globalPosition);
58
23
  }
24
+ for (const event of await this.journal.readAll(0))
25
+ await this.reconcile(event, resolvedDeliveryEventIds);
59
26
  return events.length;
60
27
  }
28
+ async reconcile(event, seen) {
29
+ const delivery = selectDeliveryEvent(event);
30
+ if (delivery === null || seen.has(delivery.eventId))
31
+ return;
32
+ const outcome = delivery.eventType === DeliveryEventType.Confirmed ||
33
+ (delivery.eventType === DeliveryEventType.Reconciled &&
34
+ delivery.payload.result === DeliveryResultKind.Confirmed)
35
+ ? { kind: ActivityOutcomeKind.Done, data: { deliveryEventId: delivery.eventId } }
36
+ : delivery.eventType === DeliveryEventType.Failed
37
+ ? { kind: ActivityOutcomeKind.Failed, data: { reason: delivery.payload.code } }
38
+ : null;
39
+ if (outcome === null)
40
+ return;
41
+ const command = {
42
+ workflowInstanceId: workflowInstanceId(delivery.payload.workflowInstanceId),
43
+ activationId: activationId(delivery.payload.activationId),
44
+ };
45
+ if (!(await this.isAwaitingThisDelivery(command, delivery.payload.intentEventId)))
46
+ return;
47
+ seen.add(delivery.eventId);
48
+ await this.orchestration.acceptOutcome({ ...command, outcome }, {
49
+ commandId: delivery.eventId,
50
+ correlationId: event.correlationId,
51
+ actor: { kind: EventActorKind.System, id: 'delivery-outcome-reactor' },
52
+ occurredAt: event.recordedAt,
53
+ });
54
+ }
61
55
  /**
62
56
  * A delivery's own completion may only resolve the activation that
63
57
  * actually asked to wait on it (an activity outcome of `waiting` with
@@ -4,9 +4,9 @@ import { adapterId } from '../../contracts/identifiers.js';
4
4
  import { integrationStream } from '../../contracts/streams.js';
5
5
  import { boundedDiagnosticEvidence } from '../contracts/check-evidence.js';
6
6
  import { GitHubEventType, selectGitHubAdapterEvent } from '../contracts/events.js';
7
- import { createCommentHistoryReader } from './comment-history-reader.js';
8
- export function createGitHubAgentContextReader(journal, resources) {
9
- const commentHistory = createCommentHistoryReader(journal, resources);
7
+ import { createCommentHistoryReader, } from './comment-history-reader.js';
8
+ export function createGitHubAgentContextReader(journal, resources, options = {}) {
9
+ const commentHistory = createCommentHistoryReader(journal, resources, options);
10
10
  return {
11
11
  async forWorkItem(workItemId, options) {
12
12
  const comments = await commentHistory.forWorkItem(workItemId, options);
@@ -1,40 +1,149 @@
1
1
  import { ResourceCorrelationRole } from '../../../resources/index.js';
2
2
  import { adapterId } from '../../contracts/identifiers.js';
3
+ import { DeliveryEventType, selectDeliveryEvent } from '../../delivery/contracts/events.js';
4
+ import { DeliveryIntentEventType, selectDeliveryIntentEvent, } from '../../delivery/contracts/intents.js';
5
+ import { DeliveryResultKind } from '../../delivery/contracts/vocabulary.js';
3
6
  import { GitHubEventType, selectGitHubAdapterEvent } from '../contracts/events.js';
4
- export function createCommentHistoryReader(journal, resources) {
7
+ import { GitHubAdapter, UnknownGitHubIdentity } from '../contracts/vocabulary.js';
8
+ import { formatAgentRunComment } from './agent-run-comment.js';
9
+ export function createCommentHistoryReader(journal, resources, readerOptions = {}) {
5
10
  return {
6
11
  async forWorkItem(workItemId, options) {
7
- const keys = new Set((await Promise.all((await resources.correlationsForWork(workItemId))
12
+ const resourcesById = new Map((await Promise.all((await resources.correlationsForWork(workItemId))
8
13
  .filter((correlation) => correlation.role === ResourceCorrelationRole.Primary)
9
- .map((correlation) => resources.get(correlation.resourceId)))).flatMap((resource) => {
10
- if (resource === null || parseAdapterId(resource.externalKey.adapter) === null)
14
+ .map((correlation) => resources.get(correlation.resourceId)))).flatMap((resource) => resource === null ? [] : [[resource.resourceId, resource]]));
15
+ const keys = new Set([...resourcesById.values()].flatMap((resource) => {
16
+ if (parseAdapterId(resource.externalKey.adapter) === null)
11
17
  return [];
12
18
  return [`${resource.externalKey.adapter}:${resource.externalKey.key}`];
13
19
  }));
14
20
  if (keys.size === 0)
15
21
  return [];
16
- return (await journal.readAll(0)).flatMap((event) => {
17
- if (options?.observedSince !== undefined && event.occurredAt <= options.observedSince)
18
- return [];
19
- const observed = selectGitHubAdapterEvent(event);
20
- if (observed?.eventType !== GitHubEventType.CommentObserved)
21
- return [];
22
- if (!keys.has(`${observed.stream.id}:${observed.payload.externalKey}`))
23
- return [];
24
- return [
25
- {
26
- author: observed.payload.actor.id,
27
- occurredAt: observed.occurredAt,
28
- body: observed.payload.body,
29
- ...(observed.payload.reviewKind !== 'issue' || observed.payload.location === undefined
30
- ? {}
31
- : { location: observed.payload.location }),
32
- },
33
- ];
34
- });
22
+ const githubAdapters = new Set(readerOptions.githubAdapters ?? [GitHubAdapter]);
23
+ const githubResourceIds = new Set([...resourcesById.entries()].flatMap(([resourceId, resource]) => githubAdapters.has(resource.externalKey.adapter) ? [resourceId] : []));
24
+ return readCommentHistory(await journal.readAll(0), resourcesById, keys, githubResourceIds, options?.observedSince, readerOptions.publicUiUrl);
25
+ },
26
+ };
27
+ }
28
+ function readCommentHistory(events, resourcesById, keys, githubResourceIds, observedSince, publicUiUrl) {
29
+ const intents = commentIntents(events, githubResourceIds);
30
+ const observed = providerComments(events, keys);
31
+ const confirmed = confirmations(events, intents);
32
+ return reconcileCommentHistory(observed, confirmed, resourcesById, observedSince, publicUiUrl);
33
+ }
34
+ function commentIntents(events, githubResourceIds) {
35
+ const intents = new Map();
36
+ for (const event of events) {
37
+ const intent = selectDeliveryIntentEvent(event);
38
+ if (intent !== null &&
39
+ isCommentIntent(intent) &&
40
+ githubResourceIds.has(intent.payload.resourceId))
41
+ intents.set(intent.eventId, intent);
42
+ }
43
+ return intents;
44
+ }
45
+ function providerComments(events, keys) {
46
+ const observed = new Map();
47
+ for (const event of events) {
48
+ const comment = providerComment(event, keys);
49
+ if (comment !== null)
50
+ observed.set(event.eventId, comment);
51
+ }
52
+ return observed;
53
+ }
54
+ function confirmations(events, intents) {
55
+ const confirmed = new Map();
56
+ for (const event of events) {
57
+ const delivery = selectDeliveryEvent(event);
58
+ if (delivery === null || !isConfirmed(delivery))
59
+ continue;
60
+ const intent = intents.get(delivery.payload.intentEventId);
61
+ if (intent === undefined)
62
+ continue;
63
+ const candidate = { event, intent };
64
+ const current = confirmed.get(delivery.payload.intentEventId);
65
+ if (current === undefined || candidate.event.globalPosition < current.event.globalPosition)
66
+ confirmed.set(delivery.payload.intentEventId, candidate);
67
+ }
68
+ return confirmed;
69
+ }
70
+ function reconcileCommentHistory(observed, confirmed, resourcesById, observedSince, publicUiUrl) {
71
+ const history = new Map();
72
+ for (const entry of observed.values())
73
+ history.set(entry.event.eventId, {
74
+ entry: entry.value,
75
+ occurredAt: entry.event.occurredAt,
76
+ globalPosition: entry.event.globalPosition,
77
+ });
78
+ for (const [intentEventId, confirmation] of confirmed) {
79
+ const resource = resourcesById.get(confirmation.intent.payload.resourceId);
80
+ if (resource === undefined)
81
+ continue;
82
+ const resourceKey = `${resource.externalKey.adapter}:${resource.externalKey.key}`;
83
+ const matchingProvider = [...observed.values()].find((entry) => entry.resourceKey === resourceKey && deliveryMarker(entry.value.body) === intentEventId);
84
+ history.set(intentEventId, {
85
+ entry: matchingProvider?.value ??
86
+ syntheticComment(confirmation.intent, confirmation.event.occurredAt, publicUiUrl),
87
+ occurredAt: confirmation.event.occurredAt,
88
+ globalPosition: confirmation.event.globalPosition,
89
+ });
90
+ if (matchingProvider !== undefined)
91
+ history.delete(matchingProvider.event.eventId);
92
+ }
93
+ return [...history.values()]
94
+ .filter((entry) => observedSince === undefined || entry.occurredAt > observedSince)
95
+ .sort((left, right) => left.globalPosition - right.globalPosition)
96
+ .map((entry) => entry.entry);
97
+ }
98
+ function isCommentIntent(value) {
99
+ return (value.eventType === DeliveryIntentEventType.StatusPublishRequested ||
100
+ value.eventType === DeliveryIntentEventType.ReplyPublishRequested ||
101
+ value.eventType === DeliveryIntentEventType.AgentRunPublishRequested);
102
+ }
103
+ function isConfirmed(value) {
104
+ return (value.eventType === DeliveryEventType.Confirmed ||
105
+ (value.eventType === DeliveryEventType.Reconciled &&
106
+ value.payload.result === DeliveryResultKind.Confirmed));
107
+ }
108
+ function providerComment(event, keys) {
109
+ const observed = selectGitHubAdapterEvent(event);
110
+ if (observed?.eventType !== GitHubEventType.CommentObserved ||
111
+ !keys.has(`${observed.stream.id}:${observed.payload.externalKey}`))
112
+ return null;
113
+ return {
114
+ event,
115
+ resourceKey: `${observed.stream.id}:${observed.payload.externalKey}`,
116
+ value: {
117
+ author: observed.payload.actor.id,
118
+ occurredAt: observed.occurredAt,
119
+ body: observed.payload.body,
120
+ ...(observed.payload.reviewKind !== 'issue' || observed.payload.location === undefined
121
+ ? {}
122
+ : { location: observed.payload.location }),
35
123
  },
36
124
  };
37
125
  }
126
+ function syntheticComment(intent, occurredAt, publicUiUrl) {
127
+ return {
128
+ author: UnknownGitHubIdentity,
129
+ occurredAt,
130
+ body: deliveredCommentBody(intent, publicUiUrl),
131
+ };
132
+ }
133
+ function deliveredCommentBody(intent, publicUiUrl) {
134
+ const marker = `<!-- wake:delivery:${intent.eventId} -->`;
135
+ const body = intent.eventType === DeliveryIntentEventType.AgentRunPublishRequested
136
+ ? formatAgentRunComment({
137
+ idempotencyKey: intent.eventId,
138
+ ...intent.payload.report,
139
+ publicUiUrl,
140
+ })
141
+ : intent.payload.body;
142
+ return `${body}\n${marker}`.trim();
143
+ }
144
+ function deliveryMarker(body) {
145
+ return /<!--\s*wake:delivery:([^\s>]+)\s*-->/.exec(body)?.[1];
146
+ }
38
147
  function parseAdapterId(value) {
39
148
  try {
40
149
  return adapterId(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.33",
3
+ "version": "0.3.35",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {