@atolis-hq/wake 0.3.35 → 0.3.37

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,6 +1,6 @@
1
1
  import { createPullRequestService } from '../activities/index.js';
2
2
  import { ControlStreamKind, DispatchPolicy, createAdvanceOnce, createControlPlaneService, createRunnerControlService, ineligibleRunners, } from '../control-plane/index.js';
3
- import { ExternalExecutionState, GitWorkspaceProvider, RecoveryService, TranscriptStore, createExecutionService, } from '../execution/index.js';
3
+ import { ExecutionCancellationReason, ExternalExecutionState, GitWorkspaceProvider, RecoveryService, TranscriptStore, createExecutionService, } from '../execution/index.js';
4
4
  import { createGitHubAgentContextReader, gitHubProviderDefinition, resolveGitHubResourceUrl, } from '../integrations/github/index.js';
5
5
  import { SystemClock, UlidIdGenerator, } from '../kernel/index.js';
6
6
  import { compileWorkflow, createOrchestrationService } from '../orchestration/index.js';
@@ -73,6 +73,9 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
73
73
  : {}),
74
74
  workspaces,
75
75
  });
76
+ orchestration.setWatchChildCancellation({
77
+ cancelSupersededWatchChildren: (workflowInstanceIds) => execution.cancelActive(workflowInstanceIds, ExecutionCancellationReason.WorkflowSuperseded),
78
+ });
76
79
  const recovery = new RecoveryService(journal, clock, {
77
80
  async inspect() {
78
81
  // Unknown external work follows the safe ambiguity path until runners expose inspection.
@@ -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 = "g4327396";
111
+ export const wakeVersion = "g82e4db8";
@@ -4,6 +4,7 @@ import { WorkflowStatus } from '../../orchestration/index.js';
4
4
  import { WorkStatus } from '../../work/index.js';
5
5
  import { ControlStreamKind } from '../contracts/streams.js';
6
6
  import { DispatchPolicy } from '../domain/dispatch-policy.js';
7
+ import { findUnresolvedTerminal, isExecutionFailureTerminal } from './execution-reconciliation.js';
7
8
  export function createAdvanceOnce(orchestration, execution, resources, clock, dependencies) {
8
9
  const runnerIneligibility = dependencies.runnerIneligibility ?? (async () => new Set());
9
10
  const isDispatchPaused = dependencies.isDispatchPaused ?? (async () => false);
@@ -133,6 +134,8 @@ export function createAdvanceOnce(orchestration, execution, resources, clock, de
133
134
  // Recheck at the dispatch boundary so maintenance cannot race a selected activation.
134
135
  if (await isDispatchPaused())
135
136
  return { kind: 'paused' };
137
+ if ((await orchestration.validateActivationDispatch?.(selected.workflow.workflowInstanceId, context(selected.activation.activationId))) === false)
138
+ return { kind: 'no-work' };
136
139
  await orchestration.markActivationStarted(selected.workflow.workflowInstanceId, selected.activation.activationId, context(selected.activation.activationId));
137
140
  const correlated = await resources.correlationsForWork(selected.workflow.workItemId);
138
141
  const resourceViews = (await Promise.all(correlated.map((entry) => resources.get(entry.resourceId)))).filter((resource) => resource !== null);
@@ -170,15 +173,3 @@ export function createAdvanceOnce(orchestration, execution, resources, clock, de
170
173
  };
171
174
  };
172
175
  }
173
- async function findUnresolvedTerminal(pending, execution) {
174
- for (const item of pending) {
175
- const run = (await execution.list(item.activation.activationId)).find((candidate) => (candidate.status === RunStatus.Succeeded && candidate.outcome !== undefined) ||
176
- isExecutionFailureTerminal(candidate.status));
177
- if (run !== undefined && !item.workflow.acceptedOutcomes.includes(item.activation.activationId))
178
- return { item, run };
179
- }
180
- return undefined;
181
- }
182
- function isExecutionFailureTerminal(status) {
183
- return (status === RunStatus.Failed || status === RunStatus.Cancelled || status === RunStatus.Ambiguous);
184
- }
@@ -0,0 +1,13 @@
1
+ import { RunStatus } from '../../execution/index.js';
2
+ export async function findUnresolvedTerminal(pending, execution) {
3
+ for (const item of pending) {
4
+ const run = (await execution.list(item.activation.activationId)).find((candidate) => (candidate.status === RunStatus.Succeeded && candidate.outcome !== undefined) ||
5
+ isExecutionFailureTerminal(candidate.status));
6
+ if (run !== undefined && !item.workflow.acceptedOutcomes.includes(item.activation.activationId))
7
+ return { item, run };
8
+ }
9
+ return undefined;
10
+ }
11
+ export function isExecutionFailureTerminal(status) {
12
+ return (status === RunStatus.Failed || status === RunStatus.Cancelled || status === RunStatus.Ambiguous);
13
+ }
@@ -0,0 +1,21 @@
1
+ import { ReviewActorKind } from '../../../activities/index.js';
2
+ export function isHumanNonWakeReply(actorKind, body) {
3
+ return actorKind === ReviewActorKind.Human && !body.includes('<!-- wake:');
4
+ }
5
+ export function recognizedCommand(body) {
6
+ const normalized = body.trim().toLowerCase();
7
+ if (normalized === '/approved')
8
+ return '/approved';
9
+ if (normalized === '/changes' || normalized.startsWith('/changes '))
10
+ return '/changes';
11
+ if (normalized === '/retry')
12
+ return '/retry';
13
+ return null;
14
+ }
15
+ export function isPlainReply(body) {
16
+ const normalized = body.trim();
17
+ return normalized.length > 0 && !normalized.startsWith('/');
18
+ }
19
+ export function shouldResumeBlockedStage(command, plainReply) {
20
+ return command === '/changes' || plainReply;
21
+ }
@@ -3,6 +3,7 @@ import { ApprovalAuthorityKind } from '../../../orchestration/index.js';
3
3
  import { BuiltInResourceKind, ResourceCorrelationRole, ResourceStreamKind, resourceId, } from '../../../resources/index.js';
4
4
  import { WorkStatus } from '../../../work/index.js';
5
5
  import { UnknownGitHubIdentity } from '../contracts/vocabulary.js';
6
+ import { isHumanNonWakeReply, isPlainReply, recognizedCommand, shouldResumeBlockedStage, } from './inbound-comment-syntax.js';
6
7
  import { commandContext } from './inbound-context.js';
7
8
  import { ignoreIneligibleOperatorRetry } from './operator-retry-command.js';
8
9
  import { translateGitHubReviewCommand } from './review-command-translator.js';
@@ -61,7 +62,7 @@ async function applyFormalReviewSignal(input) {
61
62
  }
62
63
  async function applyIssueReviewSignal(input) {
63
64
  const { event, resources, work, lookup, orchestration, adapter } = input;
64
- if (event.payload.actor.kind !== ReviewActorKind.Human || isWakeDelivery(event.payload.body))
65
+ if (!isHumanNonWakeReply(event.payload.actor.kind, event.payload.body))
65
66
  return;
66
67
  if (resources === undefined || lookup === undefined || orchestration === undefined)
67
68
  return;
@@ -73,15 +74,15 @@ async function applyIssueReviewSignal(input) {
73
74
  return;
74
75
  const resource = await resources.get(resourceIdValue);
75
76
  const command = recognizedCommand(event.payload.body);
77
+ const plainReply = isPlainReply(event.payload.body);
76
78
  if (command === '/retry') {
77
- await applyIssueRetrySignal({
79
+ return applyIssueRetrySignal({
78
80
  event,
79
81
  resources,
80
82
  work,
81
83
  orchestration,
82
84
  resourceId: resourceIdValue,
83
85
  });
84
- return;
85
86
  }
86
87
  if (resource?.kind === BuiltInResourceKind.PullRequest) {
87
88
  await applyWorkflowSignal({
@@ -90,11 +91,24 @@ async function applyIssueReviewSignal(input) {
90
91
  orchestration,
91
92
  resourceId: resourceIdValue,
92
93
  outcome: command === '/approved' ? ActivityOutcomeKind.Done : ActivityOutcomeKind.Rejected,
94
+ acceptWaitingSignal: command !== null,
95
+ resumeBlockedOnChanges: shouldResumeBlockedStage(command, plainReply),
93
96
  });
94
97
  return;
95
98
  }
96
99
  if (command !== null)
97
100
  await applyIssueApprovalSignal({ ...input, command });
101
+ else if (plainReply) {
102
+ await applyWorkflowSignal({
103
+ event,
104
+ resources,
105
+ orchestration,
106
+ resourceId: resourceIdValue,
107
+ outcome: ActivityOutcomeKind.Rejected,
108
+ acceptWaitingSignal: false,
109
+ resumeBlockedOnChanges: true,
110
+ });
111
+ }
98
112
  }
99
113
  async function applyIssueRetrySignal(input) {
100
114
  const { event, resources, work, orchestration, resourceId: resourceIdValue } = input;
@@ -161,14 +175,14 @@ async function applyPullRequestWorkflowSignal(input) {
161
175
  });
162
176
  }
163
177
  async function applyWorkflowSignal(input) {
164
- const { event, resources, orchestration, resourceId: resourceIdValue, outcome, resumeBlockedOnChanges = false, } = input;
178
+ const { event, resources, orchestration, resourceId: resourceIdValue, outcome, acceptWaitingSignal = true, resumeBlockedOnChanges = false, } = input;
165
179
  const workItemIds = (await resources.correlations(resourceIdValue))
166
180
  .filter((correlation) => correlation.role === ResourceCorrelationRole.Primary)
167
181
  .map((correlation) => correlation.workItemId);
168
182
  for (const workflow of await orchestration.listAll()) {
169
183
  if (!workItemIds.includes(workflow.workItemId))
170
184
  continue;
171
- if (workflow.waitingFor !== undefined) {
185
+ if (workflow.waitingFor !== undefined && acceptWaitingSignal) {
172
186
  await orchestration.acceptSignal(workflow.workflowInstanceId, {
173
187
  kind: workflow.waitingFor.signalKind,
174
188
  outcome,
@@ -188,16 +202,3 @@ async function applyWorkflowSignal(input) {
188
202
  }
189
203
  }
190
204
  }
191
- function isWakeDelivery(body) {
192
- return body.includes('<!-- wake:');
193
- }
194
- function recognizedCommand(body) {
195
- const normalized = body.trim().toLowerCase();
196
- if (normalized === '/approved')
197
- return '/approved';
198
- if (normalized === '/changes' || normalized.startsWith('/changes '))
199
- return '/changes';
200
- if (normalized === '/retry')
201
- return '/retry';
202
- return null;
203
- }
@@ -11,6 +11,7 @@ export function isGitHubWakeEcho(input) {
11
11
  input.labels.some(isGitHubWakeMarker));
12
12
  }
13
13
  export function createGitHubWakeLabelReconciler(input) {
14
+ const syncedDesiredLabels = new Map();
14
15
  const openWorkItemIds = async (workItemIds) => {
15
16
  const open = new Set();
16
17
  for (const workItemId of workItemIds) {
@@ -50,17 +51,22 @@ export function createGitHubWakeLabelReconciler(input) {
50
51
  const locator = parseGitHubIssueKey(resource.externalKey.key);
51
52
  if (locator === null)
52
53
  continue;
54
+ const fingerprint = desired.join('\u0000');
55
+ if (syncedDesiredLabels.get(resource.resourceId) === fingerprint)
56
+ continue;
53
57
  // One issue's persistent failure (rate limit, permissions, a stale
54
58
  // resource) must not stop every other open work item from being
55
59
  // reconciled this pass — each correlation is an independent GitHub
56
60
  // call with no ordering dependency on the others.
57
61
  try {
58
- const current = await input.getLabels(locator.owner, locator.repo, locator.number);
62
+ const current = await request(input, () => input.getLabels(locator.owner, locator.repo, locator.number));
59
63
  const next = reconcileGitHubWakeLabels(current, desired);
60
64
  if (!sameLabels(current, next))
61
- await input.setLabels(locator.owner, locator.repo, locator.number, next);
65
+ await request(input, () => input.setLabels(locator.owner, locator.repo, locator.number, next));
66
+ syncedDesiredLabels.set(resource.resourceId, fingerprint);
62
67
  }
63
68
  catch (error) {
69
+ syncedDesiredLabels.delete(resource.resourceId);
64
70
  onError({ workItemId: workflow.workItemId, ...locator }, error);
65
71
  }
66
72
  }
@@ -68,6 +74,9 @@ export function createGitHubWakeLabelReconciler(input) {
68
74
  },
69
75
  };
70
76
  }
77
+ function request(input, operation) {
78
+ return input.requests === undefined ? operation() : input.requests.run(operation);
79
+ }
71
80
  function defaultOnError(failure, error) {
72
81
  process.stderr.write(`GitHub label reconcile failed for ${failure.owner}/${failure.repo}#${failure.number}: ${error instanceof Error ? error.message : String(error)}\n`);
73
82
  }
@@ -33,11 +33,12 @@ export const gitHubConfigSchema = z
33
33
  polling: z
34
34
  .object({
35
35
  maxPerRepo: z.number().int().positive().default(25),
36
+ maxConcurrent: z.number().int().positive().default(4),
36
37
  commentPageSize: z.number().int().positive().max(100).default(25),
37
38
  lookbackMs: z.number().int().nonnegative().default(60_000),
38
39
  })
39
40
  .strict()
40
- .default({ maxPerRepo: 25, commentPageSize: 25, lookbackMs: 60_000 }),
41
+ .default({ maxPerRepo: 25, maxConcurrent: 4, commentPageSize: 25, lookbackMs: 60_000 }),
41
42
  intake: z.array(intakeRuleSchema).default([]),
42
43
  publication: z
43
44
  .object({ postStatusComments: z.boolean().default(true) })
@@ -0,0 +1,123 @@
1
+ export class GitHubRequestCooldownError extends Error {
2
+ retryAt;
3
+ constructor(retryAt) {
4
+ super(`GitHub provider is cooling down until ${new Date(retryAt).toISOString()}`);
5
+ this.retryAt = retryAt;
6
+ this.name = 'GitHubRequestCooldownError';
7
+ }
8
+ }
9
+ export function createGitHubRequestCoordinator(options) {
10
+ return new CoordinatedGitHubRequests(options);
11
+ }
12
+ class CoordinatedGitHubRequests {
13
+ options;
14
+ queue = [];
15
+ now;
16
+ active = 0;
17
+ draining = false;
18
+ cooldownUntil = 0;
19
+ transientFailureCount = 0;
20
+ constructor(options) {
21
+ this.options = options;
22
+ this.now = options.now ?? Date.now;
23
+ }
24
+ run(request) {
25
+ return new Promise((resolve, reject) => {
26
+ this.queue.push({
27
+ request: request,
28
+ resolve: resolve,
29
+ reject,
30
+ });
31
+ void this.drain();
32
+ });
33
+ }
34
+ async drain() {
35
+ if (this.draining)
36
+ return;
37
+ this.draining = true;
38
+ try {
39
+ while (this.active < this.options.maxConcurrent && this.queue.length > 0) {
40
+ if (this.cooldownUntil > this.now()) {
41
+ const error = new GitHubRequestCooldownError(this.cooldownUntil);
42
+ this.queue.splice(0).forEach((pending) => pending.reject(error));
43
+ return;
44
+ }
45
+ const pending = this.queue.shift();
46
+ this.active += 1;
47
+ void Promise.resolve()
48
+ .then(pending.request)
49
+ .then((value) => {
50
+ this.transientFailureCount = 0;
51
+ pending.resolve(value);
52
+ }, (error) => {
53
+ this.recordFailure(error);
54
+ pending.reject(error);
55
+ })
56
+ .finally(() => {
57
+ this.active -= 1;
58
+ void this.drain();
59
+ });
60
+ }
61
+ }
62
+ finally {
63
+ this.draining = false;
64
+ }
65
+ }
66
+ recordFailure(error) {
67
+ const retryAfterMs = retryAfterMilliseconds(error, this.now());
68
+ if (statusOf(error) === 429) {
69
+ this.cooldownUntil = this.now() + (retryAfterMs ?? 60_000);
70
+ this.transientFailureCount = 0;
71
+ return;
72
+ }
73
+ if (isTransient(error)) {
74
+ const delay = Math.min(5_000 * 2 ** this.transientFailureCount, 60_000);
75
+ this.transientFailureCount += 1;
76
+ this.cooldownUntil = this.now() + delay;
77
+ }
78
+ }
79
+ }
80
+ function statusOf(error) {
81
+ if (typeof error !== 'object' || error === null || !('status' in error))
82
+ return undefined;
83
+ return typeof error.status === 'number' ? error.status : undefined;
84
+ }
85
+ function isTransient(error) {
86
+ const status = statusOf(error);
87
+ if (status !== undefined)
88
+ return status >= 500;
89
+ return (error instanceof TypeError ||
90
+ (typeof error === 'object' &&
91
+ error !== null &&
92
+ 'code' in error &&
93
+ typeof error.code === 'string'));
94
+ }
95
+ function retryAfterMilliseconds(error, now) {
96
+ const value = retryAfterValue(error);
97
+ if (value === undefined || value === null)
98
+ return undefined;
99
+ const seconds = Number(value);
100
+ if (Number.isFinite(seconds) && seconds >= 0)
101
+ return seconds * 1_000;
102
+ const timestamp = Date.parse(value);
103
+ return Number.isFinite(timestamp) ? Math.max(0, timestamp - now) : undefined;
104
+ }
105
+ function retryAfterValue(error) {
106
+ if (typeof error !== 'object' || error === null || !('response' in error))
107
+ return undefined;
108
+ const response = error.response;
109
+ if (typeof response !== 'object' || response === null || !('headers' in response))
110
+ return undefined;
111
+ return headerValue(response.headers);
112
+ }
113
+ function headerValue(headers) {
114
+ if (typeof headers !== 'object' || headers === null)
115
+ return undefined;
116
+ if ('get' in headers && typeof headers.get === 'function') {
117
+ const value = headers.get('retry-after');
118
+ return typeof value === 'string' ? value : undefined;
119
+ }
120
+ if ('retry-after' in headers && typeof headers['retry-after'] === 'string')
121
+ return headers['retry-after'];
122
+ return undefined;
123
+ }
@@ -2,8 +2,11 @@ import { ReviewerAuthorizationSource, } from '../../../activities/index.js';
2
2
  import { GitHubEventType } from '../contracts/events.js';
3
3
  import { issueCommentObservation, issueObservation } from './issue-source.js';
4
4
  import { createGitHubPullRequestSource } from './pr-source.js';
5
+ import { createGitHubRequestCoordinator, } from './request-coordinator.js';
5
6
  import { githubReviewObservation } from './review-source.js';
6
- export function createGitHubSource(config, client, adapter) {
7
+ export function createGitHubSource(config, client, adapter, requests = createGitHubRequestCoordinator({
8
+ maxConcurrent: config.polling.maxConcurrent,
9
+ })) {
7
10
  let nextPollAt = 0;
8
11
  // Draft eventIds are already content fingerprints (see issue-source.ts/pr-source.ts),
9
12
  // so the journal itself is idempotent per item. This cache only avoids re-appending
@@ -16,7 +19,14 @@ export function createGitHubSource(config, client, adapter) {
16
19
  if (Date.now() < nextPollAt)
17
20
  return [];
18
21
  nextPollAt = Date.now() + config.polling.lookbackMs;
19
- const perRepository = await Promise.all(config.repositories.map(({ owner, repo }) => pollRepository({ client, config, adapter, signal, owner, repo })));
22
+ const perRepository = await Promise.all(config.repositories.map(({ owner, repo }) => pollRepository({
23
+ client: limitGitHubSourceClient(client, requests),
24
+ config,
25
+ adapter,
26
+ signal,
27
+ owner,
28
+ repo,
29
+ })));
20
30
  return perRepository.flat().filter((draft) => {
21
31
  if (draft.eventType !== GitHubEventType.WorkObserved)
22
32
  return true;
@@ -27,6 +37,28 @@ export function createGitHubSource(config, client, adapter) {
27
37
  },
28
38
  };
29
39
  }
40
+ function limitGitHubSourceClient(client, requests) {
41
+ return {
42
+ ...client,
43
+ listIssues: (owner, repo, maxResults) => requests.run(() => client.listIssues(owner, repo, maxResults)),
44
+ listPullRequests: (owner, repo, maxResults) => requests.run(() => client.listPullRequests(owner, repo, maxResults)),
45
+ listCheckRunsForRef: (owner, repo, ref) => requests.run(() => client.listCheckRunsForRef(owner, repo, ref)),
46
+ getCombinedStatusForRef: (owner, repo, ref) => requests.run(() => client.getCombinedStatusForRef(owner, repo, ref)),
47
+ listPullRequestFiles: (owner, repo, pullNumber) => requests.run(() => client.listPullRequestFiles(owner, repo, pullNumber)),
48
+ listIssueComments: (owner, repo, issueNumber, pageSize) => requests.run(() => client.listIssueComments(owner, repo, issueNumber, pageSize)),
49
+ listReviews: (owner, repo, pullNumber, pageSize) => requests.run(() => client.listReviews(owner, repo, pullNumber, pageSize)),
50
+ ...(client.listReviewComments === undefined
51
+ ? {}
52
+ : {
53
+ listReviewComments: (owner, repo, pullNumber, pageSize) => requests.run(() => client.listReviewComments(owner, repo, pullNumber, pageSize)),
54
+ }),
55
+ ...(client.collaboratorPermission === undefined
56
+ ? {}
57
+ : {
58
+ collaboratorPermission: (owner, repo, login) => requests.run(() => client.collaboratorPermission(owner, repo, login)),
59
+ }),
60
+ };
61
+ }
30
62
  async function pollRepository(input) {
31
63
  const { client, config, adapter, signal, owner, repo } = input;
32
64
  const context = {
@@ -9,6 +9,7 @@ import { GitHubEventType } from './contracts/events.js';
9
9
  import { createGitHubClient } from './infrastructure/client.js';
10
10
  import { createGitHubDelivery } from './infrastructure/delivery.js';
11
11
  import { resolveGitHubCliToken } from './infrastructure/gh-auth.js';
12
+ import { createGitHubRequestCoordinator } from './infrastructure/request-coordinator.js';
12
13
  import { createGitHubSource } from './infrastructure/source.js';
13
14
  export const gitHubProviderDefinition = {
14
15
  provider: 'github',
@@ -20,16 +21,20 @@ export const gitHubProviderDefinition = {
20
21
  if (services === undefined)
21
22
  throw new Error('GitHub provider requires composed services');
22
23
  const client = createGitHubClient(config.token ?? resolveGitHubCliToken());
24
+ const requests = createGitHubRequestCoordinator({
25
+ maxConcurrent: config.polling.maxConcurrent,
26
+ });
23
27
  return {
24
28
  adapter,
25
29
  eventTypes: Object.values(GitHubEventType),
26
- source: createGitHubSource(config, client, adapter),
30
+ source: createGitHubSource(config, client, adapter, requests),
27
31
  maintenance: createGitHubWakeLabelReconciler({
28
32
  orchestration: services.orchestration,
29
33
  resources: services.resources,
30
34
  work: services.work,
31
35
  getLabels: client.getIssueLabels,
32
36
  setLabels: client.setIssueLabels,
37
+ requests,
33
38
  }),
34
39
  delivery: createGitHubDelivery(async (intent, idempotencyKey) => {
35
40
  const resource = await services.resources.get(resourceId(intent.resourceId));
@@ -6,6 +6,7 @@ import { GroupBudgetRecorder } from './group-budget-recorder.js';
6
6
  import { OrchestrationRepository } from './orchestration-repository.js';
7
7
  import { RequestChild } from './request-child.js';
8
8
  import { StartWorkflow } from './start-workflow.js';
9
+ import { continuesWaitingForSameWatchGate } from './watch-child-transitions.js';
9
10
  import { WorkflowDefinitionRegistry } from './workflow-definition-registry.js';
10
11
  export class OrchestrationService {
11
12
  startWorkflow;
@@ -14,6 +15,7 @@ export class OrchestrationService {
14
15
  advanceWorkflow;
15
16
  childWorkflows;
16
17
  coordinateAcceptSignal = (operation) => operation();
18
+ watchChildCancellation;
17
19
  constructor(journal, work, definitions, projections) {
18
20
  const repository = new OrchestrationRepository(journal);
19
21
  const claims = new CoordinationClaims(journal);
@@ -33,34 +35,40 @@ export class OrchestrationService {
33
35
  return this.childWorkflows.rejectCausalActivation(request, context);
34
36
  }
35
37
  acceptOutcome(command, context) {
36
- return this.acceptActivityOutcome.execute(command, context);
38
+ return this.transitionWatchChildren(context, () => this.acceptActivityOutcome.execute(command, context));
37
39
  }
38
40
  waitForSignal(workflowInstanceId, expectation, context) {
39
- return this.acceptWorkflowSignal.wait(workflowInstanceId, expectation, context);
41
+ return this.transitionWatchChildren(context, () => this.acceptWorkflowSignal.wait(workflowInstanceId, expectation, context));
40
42
  }
41
43
  acceptSignal(workflowInstanceId, signal, context) {
42
- return this.coordinateAcceptSignal(() => this.acceptWorkflowSignal.execute(workflowInstanceId, signal, context));
44
+ return this.coordinateAcceptSignal(() => this.transitionWatchChildren(context, () => this.acceptWorkflowSignal.execute(workflowInstanceId, signal, context)));
43
45
  }
44
46
  setAcceptSignalOperationCoordinator(coordinator) {
45
47
  this.coordinateAcceptSignal = coordinator;
46
48
  }
49
+ setWatchChildCancellation(cancellation) {
50
+ this.watchChildCancellation = cancellation;
51
+ }
47
52
  requestSupplementalActivity(workflowInstanceId, request, context) {
48
53
  return this.advanceWorkflow.requestSupplementalActivity(workflowInstanceId, request, context);
49
54
  }
50
55
  markActivationStarted(workflowInstanceId, activationId, context) {
51
56
  return this.advanceWorkflow.markActivationStarted(workflowInstanceId, activationId, context);
52
57
  }
58
+ validateActivationDispatch(workflowInstanceId, context) {
59
+ return this.childWorkflows.validateChildDispatch(workflowInstanceId, context);
60
+ }
53
61
  block(workflowInstanceId, reason, context) {
54
- return this.advanceWorkflow.block(workflowInstanceId, reason, context);
62
+ return this.transitionWatchChildren(context, () => this.advanceWorkflow.block(workflowInstanceId, reason, context));
55
63
  }
56
64
  resolveExecutionFailure(workflowInstanceId, input, context) {
57
- return this.advanceWorkflow.resolveExecutionFailure(workflowInstanceId, input, context);
65
+ return this.transitionWatchChildren(context, () => this.advanceWorkflow.resolveExecutionFailure(workflowInstanceId, input, context));
58
66
  }
59
67
  retryBlockedFailedStage(workflowInstanceId, context) {
60
- return this.advanceWorkflow.retryBlockedFailedStage(workflowInstanceId, context);
68
+ return this.transitionWatchChildren(context, () => this.advanceWorkflow.retryBlockedFailedStage(workflowInstanceId, context));
61
69
  }
62
70
  resumeBlockedStageForChanges(workflowInstanceId, context) {
63
- return this.advanceWorkflow.resumeBlockedStageForChanges(workflowInstanceId, context);
71
+ return this.transitionWatchChildren(context, () => this.advanceWorkflow.resumeBlockedStageForChanges(workflowInstanceId, context));
64
72
  }
65
73
  get(id) {
66
74
  return this.advanceWorkflow.get(id);
@@ -78,7 +86,23 @@ export class OrchestrationService {
78
86
  return this.advanceWorkflow.listAll();
79
87
  }
80
88
  reconcileChildCompletions(context) {
81
- return this.childWorkflows.reconcileChildCompletions(context);
89
+ return this.transitionWatchChildren(context, () => this.childWorkflows.reconcileChildCompletions(context));
90
+ }
91
+ async transitionWatchChildren(context, operation) {
92
+ const before = await this.advanceWorkflow.listAll();
93
+ const result = await operation();
94
+ const after = new Map((await this.advanceWorkflow.listAll()).map((workflow) => [
95
+ workflow.workflowInstanceId,
96
+ workflow,
97
+ ]));
98
+ for (const prior of before) {
99
+ if (continuesWaitingForSameWatchGate(prior, after.get(prior.workflowInstanceId) ?? null))
100
+ continue;
101
+ const superseded = await this.childWorkflows.supersedeChildrenForWait(prior.workflowInstanceId, prior.waitingFor, context);
102
+ if (superseded.length > 0)
103
+ await this.watchChildCancellation?.cancelSupersededWatchChildren(superseded);
104
+ }
105
+ return result;
82
106
  }
83
107
  isCausalRepeat(workflowInstanceId, triggerId, causalCycleId, requestId) {
84
108
  return this.childWorkflows.isCausalRepeat(workflowInstanceId, triggerId, causalCycleId, requestId);
@@ -90,7 +114,7 @@ export class OrchestrationService {
90
114
  return this.advanceWorkflow.listResourceTransitionMatches(event);
91
115
  }
92
116
  applyResourceTransition(workflowInstanceId, target, evidenceId, context) {
93
- return this.advanceWorkflow.applyResourceTransition(workflowInstanceId, target, evidenceId, context);
117
+ return this.transitionWatchChildren(context, () => this.advanceWorkflow.applyResourceTransition(workflowInstanceId, target, evidenceId, context));
94
118
  }
95
119
  }
96
120
  export const createOrchestrationService = (journal, work, definitions, projections) => new OrchestrationService(journal, work, definitions, projections);
@@ -1,7 +1,7 @@
1
1
  import { OrchestrationEventType } from '../contracts/events.js';
2
- import { signalName } from '../contracts/identifiers.js';
2
+ import { signalName, watchId } from '../contracts/identifiers.js';
3
3
  import { childOrchestrationGroupStream } from '../contracts/streams.js';
4
- import { WorkflowStatus } from '../contracts/vocabulary.js';
4
+ import { ApprovalAuthorityKind, WorkflowStatus } from '../contracts/vocabulary.js';
5
5
  import { childMetadata, childRequestId, coordinationMetadata } from '../domain/child-policy.js';
6
6
  import { coordinationDraft } from '../domain/coordination-events.js';
7
7
  import { stateDraft } from '../domain/decision-events.js';
@@ -65,6 +65,39 @@ export class RequestChild {
65
65
  });
66
66
  }
67
67
  }
68
+ async supersedeChildrenForWait(parentWorkflowInstanceId, wait, context) {
69
+ const watchIds = watchIdsFor(wait);
70
+ if (watchIds.length === 0)
71
+ return [];
72
+ const children = (await this.advance.listAll()).filter((child) => child.parentWorkflowInstanceId === parentWorkflowInstanceId &&
73
+ child.watchId !== undefined &&
74
+ watchIds.includes(child.watchId) &&
75
+ child.status !== WorkflowStatus.Completed &&
76
+ child.status !== WorkflowStatus.Superseded);
77
+ for (const child of children) {
78
+ const loaded = await this.repository.loadRequired(child.workflowInstanceId);
79
+ if (loaded.view.status === WorkflowStatus.Completed ||
80
+ loaded.view.status === WorkflowStatus.Superseded)
81
+ continue;
82
+ await this.repository.append(child.workflowInstanceId, loaded.sequence, [
83
+ stateDraft(loaded.view, { occurredAt: context.occurredAt, causationId: context.commandId }, OrchestrationEventType.InstanceSuperseded, {}, 1),
84
+ ]);
85
+ }
86
+ return children.map((child) => child.workflowInstanceId);
87
+ }
88
+ async validateChildDispatch(childWorkflowInstanceId, context) {
89
+ const child = await this.repository.loadRequired(childWorkflowInstanceId);
90
+ if (child.view.parentWorkflowInstanceId === undefined)
91
+ return true;
92
+ const parent = await this.repository.loadRequired(child.view.parentWorkflowInstanceId);
93
+ if (child.view.watchId !== undefined && parentWaitsForWatch(parent.view, child.view.watchId))
94
+ return true;
95
+ if (child.view.status !== WorkflowStatus.Superseded)
96
+ await this.repository.append(childWorkflowInstanceId, child.sequence, [
97
+ stateDraft(child.view, { occurredAt: context.occurredAt, causationId: context.commandId }, OrchestrationEventType.InstanceSuperseded, {}, 1),
98
+ ]);
99
+ return false;
100
+ }
68
101
  async isCausalRepeat(workflowInstanceId, triggerId, causalCycleId, requestId) {
69
102
  const parent = await this.repository.loadRequired(workflowInstanceId);
70
103
  return (await this.advance.listAll()).some((view) => view.orchestrationGroupId === parent.view.orchestrationGroupId &&
@@ -78,6 +111,8 @@ export class RequestChild {
78
111
  }
79
112
  async complete(child, context) {
80
113
  const metadata = childMetadata(child);
114
+ if (child.watchId === undefined)
115
+ throw new Error('Child workflow is missing watch provenance');
81
116
  const childLoaded = await this.repository.loadRequired(child.workflowInstanceId);
82
117
  if (!childLoaded.view.childCompletionRecorded)
83
118
  await this.repository.append(child.workflowInstanceId, childLoaded.sequence, [
@@ -91,6 +126,7 @@ export class RequestChild {
91
126
  actorId: 'orchestration',
92
127
  actorDecision: { authorized: true, evidenceId: child.workflowInstanceId },
93
128
  providerEventId: child.workflowInstanceId,
129
+ authority: { kind: ApprovalAuthorityKind.Watch, watch: watchId(child.watchId) },
94
130
  childWorkflowInstanceId: child.workflowInstanceId,
95
131
  requestId: metadata.requestId,
96
132
  };
@@ -119,3 +155,14 @@ export class RequestChild {
119
155
  }, eventType, payload, ordinal);
120
156
  }
121
157
  }
158
+ function parentWaitsForWatch(parent, watchId) {
159
+ if (parent.status !== WorkflowStatus.Waiting)
160
+ return false;
161
+ const watchIds = watchIdsFor(parent.waitingFor);
162
+ return watchIds.includes(watchId);
163
+ }
164
+ function watchIdsFor(wait) {
165
+ if (wait?.from === undefined)
166
+ return [];
167
+ return (wait.from ?? []).flatMap((authority) => authority.kind === ApprovalAuthorityKind.Watch ? [authority.watch] : []);
168
+ }
@@ -0,0 +1,13 @@
1
+ import { ApprovalAuthorityKind } from '../contracts/vocabulary.js';
2
+ export function continuesWaitingForSameWatchGate(before, after) {
3
+ if (before.waitingFor === undefined || after?.waitingFor === undefined)
4
+ return false;
5
+ if (before.status !== after.status ||
6
+ before.waitingFor.signalKind !== after.waitingFor.signalKind)
7
+ return false;
8
+ const watches = (wait) => (wait.from ?? [])
9
+ .filter((authority) => authority.kind === ApprovalAuthorityKind.Watch)
10
+ .map((authority) => authority.watch)
11
+ .sort();
12
+ return JSON.stringify(watches(before.waitingFor)) === JSON.stringify(watches(after.waitingFor));
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.3.35",
3
+ "version": "0.3.37",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {