@atolis-hq/wake 0.3.97 → 0.3.98
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.
- package/dist/src/bootstrap/integration-runtime.js +3 -1
- package/dist/src/bootstrap/version.js +1 -1
- package/dist/src/integrations/application/poll-service.js +20 -11
- package/dist/src/integrations/fake/inbound-translator.js +13 -2
- package/dist/src/integrations/github/application/inbound-translator.js +252 -33
- package/dist/src/integrations/github/contracts/events.js +37 -0
- package/dist/src/resources/application/resource-correlation-retry.js +110 -0
- package/dist/src/resources/application/resource-projections.js +58 -6
- package/dist/src/resources/application/resource-repository.js +15 -1
- package/dist/src/resources/application/resource-service.js +16 -0
- package/dist/src/resources/contracts/events.js +45 -1
- package/dist/src/resources/contracts/vocabulary.js +4 -0
- package/dist/src/resources/domain/resource.js +50 -1
- package/dist/src/surfaces/web-assets/assets/{index-0vTT3Aar.js → index-YyzRROrS.js} +12 -12
- package/dist/src/surfaces/web-assets/index.html +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
1
2
|
import { ControlStreamKind, ScheduleService, createIntakePipeline, createRunnerPipeline, createWorkCancellationPolicy, } from '../control-plane/index.js';
|
|
2
3
|
import { RunRepository, createRuntimeMemoryProfile, } from '../execution/index.js';
|
|
3
4
|
import { AgentRunPublicationReactor, ArtifactRegistrationReactor, DeliveryOutcomeReactor, DeliveryService, IntegrationStreamKind, PollService, ProviderRegistry, fakeProviderDefinition, } from '../integrations/index.js';
|
|
4
5
|
import { EventActorKind, } from '../kernel/index.js';
|
|
5
6
|
import { compileWorkflowSelectors, createPullRequestTransitionEvidence, createResourceTransitionReactor, createWatchReactor, selectWorkflow, workflowName, } from '../orchestration/index.js';
|
|
7
|
+
import { withFileLock } from '../persistence/index.js';
|
|
6
8
|
import { BuiltInResourceCapability, resourceId, } from '../resources/index.js';
|
|
7
9
|
import { hydrateFakeProviderEvidence } from './fake-provider-files.js';
|
|
8
10
|
import { createRuntimeProjectionRunner } from './projection-runtime.js';
|
|
@@ -133,7 +135,7 @@ export async function composeIntegrationRuntime(input) {
|
|
|
133
135
|
return observeMemory(memoryProfile, 'intake.poll', async () => {
|
|
134
136
|
let appended = 0;
|
|
135
137
|
for (const provider of providers)
|
|
136
|
-
appended += await new PollService(input.journal, provider).pollOnce(signal);
|
|
138
|
+
appended += await withFileLock(join(input.wakeRoot, '.wake', 'locks', `poll-${provider.adapter}.lock`), async () => (await new PollService(input.journal, provider).pollOnce(signal)).appended);
|
|
137
139
|
return appended;
|
|
138
140
|
});
|
|
139
141
|
},
|
|
@@ -6,21 +6,30 @@ export class PollService {
|
|
|
6
6
|
this.journal = journal;
|
|
7
7
|
this.instance = instance;
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/** Persists every independently durable draft and reports partial progress. */
|
|
10
10
|
async pollOnce(signal) {
|
|
11
|
+
const adapter = this.instance.adapter;
|
|
11
12
|
const drafts = await this.instance.source.poll(signal);
|
|
12
13
|
let appended = 0;
|
|
14
|
+
let failed = 0;
|
|
13
15
|
for (const draft of drafts) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
try {
|
|
17
|
+
if (!this.instance.eventTypes.includes(draft.eventType))
|
|
18
|
+
throw new Error(`Provider ${adapter} emitted an unknown event type`);
|
|
19
|
+
const stream = integrationStream(adapter);
|
|
20
|
+
const existing = await this.journal.readStream(stream);
|
|
21
|
+
if (existing.some((event) => event.eventId === draft.eventId))
|
|
22
|
+
continue;
|
|
23
|
+
await this.journal.append(stream, existing.length, [{ ...draft, stream }]);
|
|
24
|
+
appended += 1;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
failed += 1;
|
|
28
|
+
console.error(`Polling ${adapter} could not persist ${draft.eventId}`, error);
|
|
29
|
+
}
|
|
22
30
|
}
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
if (failed === 0)
|
|
32
|
+
await this.instance.source.markPollPersisted?.();
|
|
33
|
+
return { appended, failed };
|
|
25
34
|
}
|
|
26
35
|
}
|
|
@@ -32,6 +32,7 @@ export class FakeInboundTranslator {
|
|
|
32
32
|
this.services = services;
|
|
33
33
|
}
|
|
34
34
|
async runOnce(limit = 100) {
|
|
35
|
+
await this.services.resources.retryPendingWorkCorrelations();
|
|
35
36
|
const checkpoint = `reactor:integration.${this.adapter}.inbound`;
|
|
36
37
|
const events = await this.services.journal.readAll(await this.services.checkpoints.load(checkpoint), limit);
|
|
37
38
|
for (const event of events) {
|
|
@@ -88,8 +89,18 @@ export class FakeInboundTranslator {
|
|
|
88
89
|
if (existing === null)
|
|
89
90
|
return false;
|
|
90
91
|
const correlation = (await this.services.resources.correlations(existing.resourceId)).find((candidate) => candidate.role === ResourceCorrelationRole.Primary);
|
|
91
|
-
if (correlation === undefined)
|
|
92
|
-
|
|
92
|
+
if (correlation === undefined) {
|
|
93
|
+
if (existing.revision !== evidence.revision)
|
|
94
|
+
await this.services.resources.discover({
|
|
95
|
+
resourceId: existing.resourceId,
|
|
96
|
+
kind: existing.kind,
|
|
97
|
+
externalKey,
|
|
98
|
+
capabilities: existing.capabilities,
|
|
99
|
+
...(evidence.revision === undefined ? {} : { revision: evidence.revision }),
|
|
100
|
+
}, context);
|
|
101
|
+
await this.services.resources.noteMissingPrimaryCorrelation(existing.resourceId, 'Resource has no active primary WorkItem correlation', context);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
93
104
|
if (existing.revision !== evidence.revision)
|
|
94
105
|
await this.services.resources.discover({
|
|
95
106
|
resourceId: existing.resourceId,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable max-lines */
|
|
2
2
|
import { ActivityEventType, createPullRequestService, } from '../../../activities/index.js';
|
|
3
|
-
import { createEventDraft, EventActorKind, EventSourceKind, UlidIdGenerator, WrongExpectedSequenceError, } from '../../../kernel/index.js';
|
|
3
|
+
import { correlationId, createEventDraft, EventActorKind, EventSourceKind, UlidIdGenerator, WrongExpectedSequenceError, } from '../../../kernel/index.js';
|
|
4
4
|
import { BuiltInResourceCapability, BuiltInResourceKind, ResourceCorrelationRole, ResourceEventType, resourceId, resourceStream, } from '../../../resources/index.js';
|
|
5
5
|
import { workItemId } from '../../../work/index.js';
|
|
6
6
|
import { admitObservedWork } from '../../application/work-admission.js';
|
|
@@ -69,7 +69,6 @@ export class InboundTranslator {
|
|
|
69
69
|
intake;
|
|
70
70
|
conclusion;
|
|
71
71
|
// Adapter filtering, checkpointing, and typed event dispatch must stay together.
|
|
72
|
-
// eslint-disable-next-line complexity
|
|
73
72
|
async runOnce(limit = 100) {
|
|
74
73
|
if (this.journal === undefined ||
|
|
75
74
|
this.checkpoints === undefined ||
|
|
@@ -77,35 +76,190 @@ export class InboundTranslator {
|
|
|
77
76
|
this.resources === undefined) {
|
|
78
77
|
throw new Error('InboundTranslator services are required to run evidence translation');
|
|
79
78
|
}
|
|
79
|
+
// Retry state is resource-scoped, so it progresses even when the provider
|
|
80
|
+
// has no further observations for the broken resource.
|
|
81
|
+
await this.resources.retryPendingWorkCorrelations();
|
|
82
|
+
await this.applyDeferredExternalOutcomes();
|
|
80
83
|
const checkpoint = `reactor:integration.${this.adapter}.inbound`;
|
|
84
|
+
await this.retryPendingTranslations();
|
|
81
85
|
const position = await this.checkpoints.load(checkpoint);
|
|
82
86
|
const events = await this.journal.readAll(position, limit);
|
|
83
87
|
for (const event of events) {
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
await this.translateEvent(event);
|
|
89
|
+
await this.checkpoints.save(checkpoint, event.globalPosition);
|
|
90
|
+
}
|
|
91
|
+
return events.length;
|
|
92
|
+
}
|
|
93
|
+
async retryPendingTranslations() {
|
|
94
|
+
const events = await this.journal.readStream(integrationStream(this.adapter));
|
|
95
|
+
const owned = events
|
|
96
|
+
.map(selectGitHubAdapterEvent)
|
|
97
|
+
.filter((event) => event !== null);
|
|
98
|
+
const failures = new Set(owned
|
|
99
|
+
.filter((event) => event.eventType === GitHubEventType.InboundTranslationFailed)
|
|
100
|
+
.map((event) => event.payload.sourceEventId));
|
|
101
|
+
const pending = new Set(owned
|
|
102
|
+
.filter((event) => event.eventType === GitHubEventType.InboundTranslationRetried)
|
|
103
|
+
.map((event) => event.payload.sourceEventId));
|
|
104
|
+
const recovered = new Set(owned
|
|
105
|
+
.filter((event) => event.eventType === GitHubEventType.InboundTranslationRecovered)
|
|
106
|
+
.map((event) => event.payload.sourceEventId));
|
|
107
|
+
for (const event of owned) {
|
|
108
|
+
if (pending.has(event.eventId) &&
|
|
109
|
+
!failures.has(event.eventId) &&
|
|
110
|
+
!recovered.has(event.eventId) &&
|
|
111
|
+
this.isTranslatable(event))
|
|
112
|
+
await this.translateEvent(event);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
isTranslatable(event) {
|
|
116
|
+
return (event.stream.id === this.adapter &&
|
|
117
|
+
(event.eventType === GitHubEventType.WorkObserved ||
|
|
118
|
+
event.eventType === GitHubEventType.CommentObserved));
|
|
119
|
+
}
|
|
120
|
+
async translateEvent(event) {
|
|
121
|
+
const owned = selectGitHubAdapterEvent(event);
|
|
122
|
+
if (owned === null || !this.isTranslatable(owned))
|
|
123
|
+
return;
|
|
124
|
+
if (await this.failureRecorded(owned.eventId))
|
|
125
|
+
return;
|
|
126
|
+
try {
|
|
127
|
+
if (owned.eventType === GitHubEventType.WorkObserved)
|
|
86
128
|
await this.apply(owned);
|
|
87
|
-
if (owned
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
129
|
+
if (owned.eventType === GitHubEventType.CommentObserved) {
|
|
130
|
+
if (!(await this.suppressWorkItemEffects(owned)))
|
|
131
|
+
await applyReviewSignal({
|
|
132
|
+
event: owned,
|
|
133
|
+
journal: this.journal,
|
|
134
|
+
resources: this.resources,
|
|
135
|
+
work: this.work,
|
|
136
|
+
lookup: this.lookup,
|
|
137
|
+
pullRequests: this.pullRequests,
|
|
138
|
+
ids: this.ids,
|
|
139
|
+
adapter: this.adapter,
|
|
140
|
+
orchestration: this.orchestration,
|
|
141
|
+
});
|
|
100
142
|
await applyWatchGateVerdictSignal({
|
|
101
143
|
event: owned,
|
|
102
144
|
runs: this.runs,
|
|
103
145
|
orchestration: this.orchestration,
|
|
104
146
|
});
|
|
105
147
|
}
|
|
106
|
-
await this.
|
|
148
|
+
if (await this.retryRecorded(owned.eventId))
|
|
149
|
+
await this.recordTranslationRecovery(owned);
|
|
107
150
|
}
|
|
108
|
-
|
|
151
|
+
catch (error) {
|
|
152
|
+
await this.recordTranslationFailure(owned, error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async failureRecorded(sourceEventId) {
|
|
156
|
+
return (await this.journal.readStream(integrationStream(this.adapter))).some((event) => {
|
|
157
|
+
const owned = selectGitHubAdapterEvent(event);
|
|
158
|
+
return (owned?.eventType === GitHubEventType.InboundTranslationFailed &&
|
|
159
|
+
owned.payload.sourceEventId === sourceEventId);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
async retryRecorded(sourceEventId) {
|
|
163
|
+
return (await this.journal.readStream(integrationStream(this.adapter))).some((event) => {
|
|
164
|
+
const owned = selectGitHubAdapterEvent(event);
|
|
165
|
+
return (owned?.eventType === GitHubEventType.InboundTranslationRetried &&
|
|
166
|
+
owned.payload.sourceEventId === sourceEventId);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async recordTranslationRecovery(event) {
|
|
170
|
+
const stream = integrationStream(this.adapter);
|
|
171
|
+
const eventId = `github:inbound-translation-recovered:${this.adapter}:${event.eventId}`;
|
|
172
|
+
const existing = await this.journal.readStream(stream);
|
|
173
|
+
if (existing.some((candidate) => candidate.eventId === eventId))
|
|
174
|
+
return;
|
|
175
|
+
await this.journal.append(stream, existing.length, [
|
|
176
|
+
createEventDraft({
|
|
177
|
+
eventId,
|
|
178
|
+
eventType: GitHubEventType.InboundTranslationRecovered,
|
|
179
|
+
occurredAt: event.occurredAt,
|
|
180
|
+
correlationId: event.correlationId,
|
|
181
|
+
causationId: event.eventId,
|
|
182
|
+
actor: { kind: EventActorKind.Integration, id: this.adapter },
|
|
183
|
+
source: { kind: EventSourceKind.Internal, id: 'github-inbound-translator' },
|
|
184
|
+
stream,
|
|
185
|
+
payload: { adapter: this.adapter, sourceEventId: event.eventId },
|
|
186
|
+
}),
|
|
187
|
+
]);
|
|
188
|
+
}
|
|
189
|
+
async recordTranslationFailure(event, error) {
|
|
190
|
+
const stream = integrationStream(this.adapter);
|
|
191
|
+
const existing = await this.journal.readStream(stream);
|
|
192
|
+
const retries = existing.reduce((count, candidate) => {
|
|
193
|
+
const owned = selectGitHubAdapterEvent(candidate);
|
|
194
|
+
return (count +
|
|
195
|
+
Number(owned?.eventType === GitHubEventType.InboundTranslationRetried &&
|
|
196
|
+
owned.payload.sourceEventId === event.eventId));
|
|
197
|
+
}, 0);
|
|
198
|
+
const attempt = retries + 1;
|
|
199
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
200
|
+
const terminal = retries >= 3;
|
|
201
|
+
const eventId = terminal
|
|
202
|
+
? `github:inbound-translation-failed:${this.adapter}:${event.eventId}`
|
|
203
|
+
: `github:inbound-translation-retried:${this.adapter}:${event.eventId}:${attempt}`;
|
|
204
|
+
const payload = terminal
|
|
205
|
+
? {
|
|
206
|
+
adapter: this.adapter,
|
|
207
|
+
sourceEventId: event.eventId,
|
|
208
|
+
attempt,
|
|
209
|
+
message,
|
|
210
|
+
globalPosition: event.globalPosition,
|
|
211
|
+
eventType: event.eventType,
|
|
212
|
+
correlationId: event.correlationId,
|
|
213
|
+
causationId: event.causationId,
|
|
214
|
+
failedAt: event.occurredAt,
|
|
215
|
+
}
|
|
216
|
+
: { adapter: this.adapter, sourceEventId: event.eventId, attempt, message };
|
|
217
|
+
await this.appendTranslationDiagnostic(eventId, terminal
|
|
218
|
+
? GitHubEventType.InboundTranslationFailed
|
|
219
|
+
: GitHubEventType.InboundTranslationRetried, event, payload);
|
|
220
|
+
console.error(`Inbound translation failed for ${event.eventId} (attempt ${attempt})`, error);
|
|
221
|
+
}
|
|
222
|
+
async appendTranslationDiagnostic(eventId, eventType, source, payload) {
|
|
223
|
+
const stream = integrationStream(this.adapter);
|
|
224
|
+
for (let contentionAttempts = 0; contentionAttempts < 3; contentionAttempts += 1) {
|
|
225
|
+
const existing = await this.journal.readStream(stream);
|
|
226
|
+
if (existing.some((event) => event.eventId === eventId))
|
|
227
|
+
return;
|
|
228
|
+
try {
|
|
229
|
+
await this.journal.append(stream, existing.length, [
|
|
230
|
+
createEventDraft({
|
|
231
|
+
eventId,
|
|
232
|
+
eventType,
|
|
233
|
+
occurredAt: source.occurredAt,
|
|
234
|
+
correlationId: source.correlationId,
|
|
235
|
+
causationId: source.eventId,
|
|
236
|
+
actor: { kind: EventActorKind.Integration, id: this.adapter },
|
|
237
|
+
source: { kind: EventSourceKind.Internal, id: 'github-inbound-translator' },
|
|
238
|
+
stream,
|
|
239
|
+
payload,
|
|
240
|
+
}),
|
|
241
|
+
]);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
if (!(error instanceof WrongExpectedSequenceError))
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
throw new Error(`Could not record inbound translation diagnostic ${eventId} after contention`);
|
|
250
|
+
}
|
|
251
|
+
async suppressWorkItemEffects(event) {
|
|
252
|
+
const resourceIdValue = await this.lookup?.resourceIdForExternalKey({
|
|
253
|
+
adapter: this.adapter,
|
|
254
|
+
key: event.payload.externalKey,
|
|
255
|
+
});
|
|
256
|
+
if (resourceIdValue === null || resourceIdValue === undefined)
|
|
257
|
+
return false;
|
|
258
|
+
const hasPrimary = (await this.resources.correlations(resourceIdValue)).some((correlation) => correlation.role === ResourceCorrelationRole.Primary);
|
|
259
|
+
if (hasPrimary)
|
|
260
|
+
return false;
|
|
261
|
+
await this.resources.noteMissingPrimaryCorrelation(resourceIdValue, 'Resource has no active primary WorkItem correlation', commandContext(event));
|
|
262
|
+
return true;
|
|
109
263
|
}
|
|
110
264
|
async apply(event) {
|
|
111
265
|
if (this.work === undefined || this.resources === undefined)
|
|
@@ -119,21 +273,14 @@ export class InboundTranslator {
|
|
|
119
273
|
const identity = await this.resolveIdentity({ adapter: this.adapter, key: payload.externalKey }, intake.admitted);
|
|
120
274
|
if (identity === null)
|
|
121
275
|
return;
|
|
122
|
-
if (identity.deleted) {
|
|
123
|
-
await this.recordDeletedWorkObservation(event, identity.workItemId);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
276
|
if (!identity.created) {
|
|
127
|
-
await this.
|
|
128
|
-
payload,
|
|
129
|
-
context,
|
|
130
|
-
pullRequests,
|
|
131
|
-
resourceId: identity.resourceId,
|
|
132
|
-
workItemId: identity.workItemId,
|
|
133
|
-
});
|
|
277
|
+
await this.applyExistingObservation(event, payload, context, pullRequests, identity);
|
|
134
278
|
return;
|
|
135
279
|
}
|
|
136
|
-
const
|
|
280
|
+
const resourceIdValue = identity.resourceId;
|
|
281
|
+
const workItemIdValue = identity.workItemId;
|
|
282
|
+
if (workItemIdValue === null)
|
|
283
|
+
throw new Error('Created identity is missing a WorkItem');
|
|
137
284
|
const isPullRequest = payload.kind === 'pull-request';
|
|
138
285
|
await admitObservedWork(this.admissionServices(), {
|
|
139
286
|
adapter: this.adapter,
|
|
@@ -159,6 +306,66 @@ export class InboundTranslator {
|
|
|
159
306
|
}
|
|
160
307
|
: undefined);
|
|
161
308
|
}
|
|
309
|
+
async applyExistingObservation(event, payload, context, pullRequests, identity) {
|
|
310
|
+
if (identity.missingPrimary) {
|
|
311
|
+
await this.recordMissingPrimaryObservation(identity.resourceId, payload, context);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
const workItemIdValue = identity.workItemId;
|
|
315
|
+
if (workItemIdValue === null)
|
|
316
|
+
throw new Error('Resolved identity is missing a WorkItem');
|
|
317
|
+
if (identity.deleted) {
|
|
318
|
+
await this.recordDeletedWorkObservation(event, workItemIdValue);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
await this.applyReobservation({
|
|
322
|
+
payload,
|
|
323
|
+
context,
|
|
324
|
+
pullRequests,
|
|
325
|
+
resourceId: identity.resourceId,
|
|
326
|
+
workItemId: workItemIdValue,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
async recordMissingPrimaryObservation(resourceIdValue, payload, context) {
|
|
330
|
+
const current = await this.resources.get(resourceIdValue);
|
|
331
|
+
if (current === null)
|
|
332
|
+
throw new Error(`Resource ${resourceIdValue} could not be loaded`);
|
|
333
|
+
if (current.revision !== payload.revision)
|
|
334
|
+
await this.resources.discover({
|
|
335
|
+
resourceId: current.resourceId,
|
|
336
|
+
kind: current.kind,
|
|
337
|
+
externalKey: current.externalKey,
|
|
338
|
+
capabilities: current.capabilities,
|
|
339
|
+
revision: payload.revision,
|
|
340
|
+
...(current.title === undefined ? {} : { title: current.title }),
|
|
341
|
+
}, context);
|
|
342
|
+
await this.resources.noteMissingPrimaryCorrelation(resourceIdValue, 'Resource has no active primary WorkItem correlation', context);
|
|
343
|
+
await this.resources.observeExternalOutcome(resourceIdValue, {
|
|
344
|
+
sourceObservationId: context.commandId,
|
|
345
|
+
...(payload.outcome === undefined ? {} : { outcome: payload.outcome }),
|
|
346
|
+
revision: payload.revision,
|
|
347
|
+
}, context);
|
|
348
|
+
}
|
|
349
|
+
async applyDeferredExternalOutcomes() {
|
|
350
|
+
if (this.conclusion === undefined)
|
|
351
|
+
return;
|
|
352
|
+
for (const pending of await this.resources.pendingExternalOutcomes()) {
|
|
353
|
+
const primary = (await this.resources.correlations(pending.resourceId)).find((correlation) => correlation.role === ResourceCorrelationRole.Primary);
|
|
354
|
+
if (primary === undefined)
|
|
355
|
+
continue;
|
|
356
|
+
await concludeObservedWork({ work: this.work, conclusion: this.conclusion }, {
|
|
357
|
+
workItemId: primary.workItemId,
|
|
358
|
+
outcome: pending.outcome,
|
|
359
|
+
reason: `${this.adapter} external outcome observed while correlation was unresolved`,
|
|
360
|
+
});
|
|
361
|
+
await this.resources.consumeExternalOutcome(pending.resourceId, pending.sourceObservationId, {
|
|
362
|
+
commandId: `github:consume-external-outcome:${pending.sourceObservationId}`,
|
|
363
|
+
correlationId: correlationId(`github:external-outcome:${pending.sourceObservationId}`),
|
|
364
|
+
occurredAt: new Date().toISOString(),
|
|
365
|
+
actor: { kind: EventActorKind.Integration, id: this.adapter },
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
162
369
|
async applyReobservation(input) {
|
|
163
370
|
const { payload, context, pullRequests, resourceId: resourceIdValue, workItemId: workItemIdValue, } = input;
|
|
164
371
|
const current = await this.resources.get(resourceIdValue);
|
|
@@ -278,7 +485,13 @@ export class InboundTranslator {
|
|
|
278
485
|
async historicalIdentity(resourceIdValue) {
|
|
279
486
|
const historical = await this.resources.primaryCorrelation(resourceIdValue);
|
|
280
487
|
if (historical === null)
|
|
281
|
-
|
|
488
|
+
return {
|
|
489
|
+
resourceId: resourceIdValue,
|
|
490
|
+
workItemId: null,
|
|
491
|
+
created: false,
|
|
492
|
+
deleted: false,
|
|
493
|
+
missingPrimary: true,
|
|
494
|
+
};
|
|
282
495
|
const historicalWork = await this.work?.get(historical.workItemId);
|
|
283
496
|
if (historicalWork?.deleted)
|
|
284
497
|
return {
|
|
@@ -287,7 +500,13 @@ export class InboundTranslator {
|
|
|
287
500
|
created: false,
|
|
288
501
|
deleted: true,
|
|
289
502
|
};
|
|
290
|
-
|
|
503
|
+
return {
|
|
504
|
+
resourceId: resourceIdValue,
|
|
505
|
+
workItemId: historical.workItemId,
|
|
506
|
+
created: false,
|
|
507
|
+
deleted: false,
|
|
508
|
+
missingPrimary: true,
|
|
509
|
+
};
|
|
291
510
|
}
|
|
292
511
|
async recordDeletedWorkObservation(event, workItemId) {
|
|
293
512
|
const stream = integrationStream(this.adapter);
|
|
@@ -10,6 +10,9 @@ export const GitHubEventType = {
|
|
|
10
10
|
CommentObserved: 'integration.github.comment-observed',
|
|
11
11
|
DeliveryObserved: 'integration.github.delivery-observed',
|
|
12
12
|
DeletedWorkObservationSkipped: 'integration.github.deleted-work-observation-skipped',
|
|
13
|
+
InboundTranslationRetried: 'integration.github.inbound-translation-retried',
|
|
14
|
+
InboundTranslationRecovered: 'integration.github.inbound-translation-recovered',
|
|
15
|
+
InboundTranslationFailed: 'integration.github.inbound-translation-failed',
|
|
13
16
|
};
|
|
14
17
|
const streamSchema = z
|
|
15
18
|
.object({
|
|
@@ -129,6 +132,40 @@ const eventSchema = z.discriminatedUnion('eventType', [
|
|
|
129
132
|
})
|
|
130
133
|
.strict(),
|
|
131
134
|
}),
|
|
135
|
+
eventEnvelopeSchema.extend({
|
|
136
|
+
eventType: z.literal(GitHubEventType.InboundTranslationRetried),
|
|
137
|
+
stream: streamSchema,
|
|
138
|
+
payload: z
|
|
139
|
+
.object({
|
|
140
|
+
adapter: z.string(),
|
|
141
|
+
sourceEventId: z.string(),
|
|
142
|
+
attempt: z.number().int().positive(),
|
|
143
|
+
message: z.string(),
|
|
144
|
+
})
|
|
145
|
+
.strict(),
|
|
146
|
+
}),
|
|
147
|
+
eventEnvelopeSchema.extend({
|
|
148
|
+
eventType: z.literal(GitHubEventType.InboundTranslationRecovered),
|
|
149
|
+
stream: streamSchema,
|
|
150
|
+
payload: z.object({ adapter: z.string(), sourceEventId: z.string() }).strict(),
|
|
151
|
+
}),
|
|
152
|
+
eventEnvelopeSchema.extend({
|
|
153
|
+
eventType: z.literal(GitHubEventType.InboundTranslationFailed),
|
|
154
|
+
stream: streamSchema,
|
|
155
|
+
payload: z
|
|
156
|
+
.object({
|
|
157
|
+
adapter: z.string(),
|
|
158
|
+
sourceEventId: z.string(),
|
|
159
|
+
attempt: z.number().int().positive(),
|
|
160
|
+
message: z.string(),
|
|
161
|
+
globalPosition: z.number().int().positive(),
|
|
162
|
+
eventType: z.string(),
|
|
163
|
+
correlationId: z.string(),
|
|
164
|
+
causationId: z.string(),
|
|
165
|
+
failedAt: z.string(),
|
|
166
|
+
})
|
|
167
|
+
.strict(),
|
|
168
|
+
}),
|
|
132
169
|
]);
|
|
133
170
|
export function decodeGitHubAdapterEvent(event) {
|
|
134
171
|
const result = eventSchema.safeParse(event);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { correlationId, createEventDraft, EventActorKind, EventSourceKind, } from '../../kernel/index.js';
|
|
2
|
+
import { ResourceEventType, } from '../contracts/events.js';
|
|
3
|
+
import { resourceStream } from '../contracts/streams.js';
|
|
4
|
+
import { ResourceCorrelationRole } from '../contracts/vocabulary.js';
|
|
5
|
+
export async function observeExternalOutcome(repository, resourceId, observation, context) {
|
|
6
|
+
if (observation.outcome === undefined) {
|
|
7
|
+
await appendResourceEvent(repository, resourceId, resourceDraft(resourceId, context, ResourceEventType.ExternalOutcomeReopened, {
|
|
8
|
+
revision: observation.revision,
|
|
9
|
+
}));
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
await appendResourceEvent(repository, resourceId, resourceDraft(resourceId, context, ResourceEventType.ExternalOutcomeObserved, {
|
|
13
|
+
sourceObservationId: observation.sourceObservationId,
|
|
14
|
+
outcome: observation.outcome,
|
|
15
|
+
revision: observation.revision,
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
export async function pendingExternalOutcomes(repository) {
|
|
19
|
+
const pending = [];
|
|
20
|
+
for (const resourceId of await repository.listResourceIds()) {
|
|
21
|
+
const resource = (await repository.load(resourceId)).resource;
|
|
22
|
+
if (resource?.view.pendingExternalOutcome !== undefined && hasPrimary(resource.correlations))
|
|
23
|
+
pending.push({ resourceId, ...resource.view.pendingExternalOutcome });
|
|
24
|
+
}
|
|
25
|
+
return pending;
|
|
26
|
+
}
|
|
27
|
+
export async function noteMissingPrimaryCorrelation(repository, resourceId, reason, context) {
|
|
28
|
+
const loaded = await repository.load(resourceId);
|
|
29
|
+
if (loaded.resource === null || hasPrimary(loaded.resource.correlations))
|
|
30
|
+
return;
|
|
31
|
+
if (loaded.resource.view.correlationStatus === 'unresolvable')
|
|
32
|
+
return;
|
|
33
|
+
if (retryAttempts(loaded.resource.events) > 0)
|
|
34
|
+
return;
|
|
35
|
+
await repository.append(resourceId, loaded.sequence, [
|
|
36
|
+
resourceDraft(resourceId, context, ResourceEventType.WorkCorrelationRetryPending, {
|
|
37
|
+
attemptCount: 1,
|
|
38
|
+
lastFailureReason: reason,
|
|
39
|
+
}),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
export async function retryPendingWorkCorrelations(repository) {
|
|
43
|
+
let processed = 0;
|
|
44
|
+
for (const resourceId of await repository.listResourceIds()) {
|
|
45
|
+
const loaded = await repository.load(resourceId);
|
|
46
|
+
if (loaded.resource === null || hasPrimary(loaded.resource.correlations))
|
|
47
|
+
continue;
|
|
48
|
+
if (loaded.resource.view.correlationStatus === 'unresolvable')
|
|
49
|
+
continue;
|
|
50
|
+
const attempts = retryAttempts(loaded.resource.events);
|
|
51
|
+
if (attempts === 0)
|
|
52
|
+
continue;
|
|
53
|
+
const last = [...loaded.resource.events]
|
|
54
|
+
.reverse()
|
|
55
|
+
.find((event) => event.eventType === ResourceEventType.WorkCorrelationRetryPending);
|
|
56
|
+
const reason = last?.payload.lastFailureReason ?? 'Resource has no active primary WorkItem correlation';
|
|
57
|
+
const attemptCount = attempts + 1;
|
|
58
|
+
const context = {
|
|
59
|
+
commandId: `resource:${resourceId}:missing-primary:${attemptCount}`,
|
|
60
|
+
correlationId: correlationId(`resource:${resourceId}:missing-primary`),
|
|
61
|
+
occurredAt: new Date().toISOString(),
|
|
62
|
+
actor: { kind: EventActorKind.System, id: 'resource-service' },
|
|
63
|
+
};
|
|
64
|
+
const draft = attemptCount >= 4
|
|
65
|
+
? resourceDraft(resourceId, context, ResourceEventType.WorkCorrelationUnresolvable, {
|
|
66
|
+
externalKey: loaded.resource.view.externalKey,
|
|
67
|
+
attemptCount,
|
|
68
|
+
lastFailureReason: reason,
|
|
69
|
+
})
|
|
70
|
+
: resourceDraft(resourceId, context, ResourceEventType.WorkCorrelationRetryPending, {
|
|
71
|
+
attemptCount,
|
|
72
|
+
lastFailureReason: reason,
|
|
73
|
+
});
|
|
74
|
+
await repository.append(resourceId, loaded.sequence, [draft]);
|
|
75
|
+
processed += 1;
|
|
76
|
+
}
|
|
77
|
+
return processed;
|
|
78
|
+
}
|
|
79
|
+
function hasPrimary(correlations) {
|
|
80
|
+
return correlations.some((value) => value.role === ResourceCorrelationRole.Primary);
|
|
81
|
+
}
|
|
82
|
+
function retryAttempts(events) {
|
|
83
|
+
let attempts = 0;
|
|
84
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
85
|
+
const event = events[index];
|
|
86
|
+
if (event.eventType === ResourceEventType.WorkCorrelationEstablished &&
|
|
87
|
+
event.payload.role === ResourceCorrelationRole.Primary)
|
|
88
|
+
break;
|
|
89
|
+
if (event.eventType === ResourceEventType.WorkCorrelationRetryPending)
|
|
90
|
+
attempts = Math.max(attempts, event.payload.attemptCount);
|
|
91
|
+
}
|
|
92
|
+
return attempts;
|
|
93
|
+
}
|
|
94
|
+
async function appendResourceEvent(repository, resourceId, draft) {
|
|
95
|
+
const loaded = await repository.load(resourceId);
|
|
96
|
+
await repository.append(resourceId, loaded.sequence, [draft]);
|
|
97
|
+
}
|
|
98
|
+
function resourceDraft(resourceId, context, eventType, payload) {
|
|
99
|
+
return createEventDraft({
|
|
100
|
+
eventId: `${context.commandId}:${eventType}`,
|
|
101
|
+
eventType,
|
|
102
|
+
occurredAt: context.occurredAt,
|
|
103
|
+
correlationId: context.correlationId,
|
|
104
|
+
causationId: context.commandId,
|
|
105
|
+
actor: context.actor,
|
|
106
|
+
source: { kind: EventSourceKind.Internal, id: 'resource-service' },
|
|
107
|
+
stream: resourceStream(resourceId),
|
|
108
|
+
payload,
|
|
109
|
+
});
|
|
110
|
+
}
|