@atolis-hq/wake 0.4.105 → 0.4.106

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.
Files changed (128) hide show
  1. package/dist/src/activities/agent/agent-activity.js +7 -0
  2. package/dist/src/activities/contracts/activity.d.ts +12 -0
  3. package/dist/src/activities/contracts/event-schema.d.ts +314 -8
  4. package/dist/src/activities/contracts/event-schema.js +20 -2
  5. package/dist/src/activities/contracts/events.d.ts +10 -0
  6. package/dist/src/activities/contracts/vocabulary.d.ts +2 -0
  7. package/dist/src/activities/contracts/vocabulary.js +2 -0
  8. package/dist/src/activities/index.d.ts +2 -0
  9. package/dist/src/activities/index.js +2 -0
  10. package/dist/src/activities/pr/activity-support.d.ts +1 -1
  11. package/dist/src/activities/pr/activity-support.js +10 -0
  12. package/dist/src/activities/pr/application.js +8 -1
  13. package/dist/src/activities/pr/approve.d.ts +2 -1
  14. package/dist/src/activities/pr/approve.js +33 -11
  15. package/dist/src/activities/pr/check-evidence.d.ts +10 -0
  16. package/dist/src/activities/pr/check-evidence.js +16 -0
  17. package/dist/src/activities/pr/contracts.d.ts +14 -0
  18. package/dist/src/activities/pr/event-data.js +5 -1
  19. package/dist/src/activities/pr/merge.d.ts +2 -1
  20. package/dist/src/activities/pr/merge.js +54 -38
  21. package/dist/src/activities/pr/parent-scope.d.ts +65 -0
  22. package/dist/src/activities/pr/parent-scope.js +24 -0
  23. package/dist/src/activities/pr/parent-target.d.ts +45 -0
  24. package/dist/src/activities/pr/parent-target.js +59 -0
  25. package/dist/src/activities/pr/projection.js +10 -2
  26. package/dist/src/activities/pr/vocabulary.d.ts +8 -0
  27. package/dist/src/activities/pr/vocabulary.js +8 -0
  28. package/dist/src/bootstrap/activity-registry.d.ts +2 -2
  29. package/dist/src/bootstrap/activity-registry.js +3 -3
  30. package/dist/src/bootstrap/composition-root.js +11 -2
  31. package/dist/src/bootstrap/config/root-schema.d.ts +6 -0
  32. package/dist/src/bootstrap/delivery-integration.d.ts +9 -0
  33. package/dist/src/bootstrap/delivery-integration.js +46 -0
  34. package/dist/src/bootstrap/surface-api-delivery.js +27 -0
  35. package/dist/src/bootstrap/version.js +1 -1
  36. package/dist/src/control-plane/application/activation-scheduler-ports.d.ts +3 -1
  37. package/dist/src/control-plane/application/advance-once-dispatch.js +10 -6
  38. package/dist/src/execution/application/execution-service.js +10 -0
  39. package/dist/src/execution/application/maintenance-workspaces.d.ts +17 -0
  40. package/dist/src/execution/application/maintenance-workspaces.js +84 -0
  41. package/dist/src/execution/application/run-lifecycle.js +1 -0
  42. package/dist/src/execution/contracts/commands.d.ts +2 -0
  43. package/dist/src/execution/contracts/events.d.ts +13 -0
  44. package/dist/src/execution/contracts/events.js +2 -0
  45. package/dist/src/execution/contracts/maintenance.d.ts +36 -0
  46. package/dist/src/execution/contracts/maintenance.js +44 -0
  47. package/dist/src/execution/contracts/views.d.ts +2 -0
  48. package/dist/src/execution/contracts/workspace.d.ts +2 -0
  49. package/dist/src/execution/index.d.ts +1 -0
  50. package/dist/src/execution/index.js +1 -0
  51. package/dist/src/integrations/application/delegation-outputs.js +1 -1
  52. package/dist/src/integrations/application/delivery-integration-verification.d.ts +13 -0
  53. package/dist/src/integrations/application/delivery-integration-verification.js +68 -0
  54. package/dist/src/integrations/delivery/application/delivery-projector.js +3 -0
  55. package/dist/src/integrations/delivery/application/merged-delivery-evidence.js +9 -1
  56. package/dist/src/integrations/delivery/contracts/views.d.ts +2 -0
  57. package/dist/src/integrations/fake/external-source.d.ts +5 -2
  58. package/dist/src/integrations/fake/external-source.js +12 -2
  59. package/dist/src/integrations/fake/inbound-translator.js +7 -2
  60. package/dist/src/integrations/fake/provider.d.ts +17 -0
  61. package/dist/src/integrations/fake/provider.js +18 -2
  62. package/dist/src/integrations/github/application/pull-request-translation.js +2 -0
  63. package/dist/src/integrations/github/contracts/events.d.ts +3 -1
  64. package/dist/src/integrations/github/contracts/events.js +3 -1
  65. package/dist/src/integrations/github/contracts/payloads.d.ts +23 -0
  66. package/dist/src/integrations/github/infrastructure/check-execution.d.ts +9 -0
  67. package/dist/src/integrations/github/infrastructure/check-execution.js +68 -0
  68. package/dist/src/integrations/github/infrastructure/checkout-revision.d.ts +2 -0
  69. package/dist/src/integrations/github/infrastructure/checkout-revision.js +23 -0
  70. package/dist/src/integrations/github/infrastructure/client.d.ts +2 -0
  71. package/dist/src/integrations/github/infrastructure/client.js +4 -0
  72. package/dist/src/integrations/github/infrastructure/pr-check-subject.d.ts +7 -0
  73. package/dist/src/integrations/github/infrastructure/pr-check-subject.js +77 -0
  74. package/dist/src/integrations/github/infrastructure/pr-evidence.d.ts +6 -3
  75. package/dist/src/integrations/github/infrastructure/pr-evidence.js +1 -1
  76. package/dist/src/integrations/github/infrastructure/pr-source.d.ts +3 -5
  77. package/dist/src/integrations/github/infrastructure/pr-source.js +11 -1
  78. package/dist/src/integrations/github/infrastructure/workflow-run-evidence.d.ts +5 -0
  79. package/dist/src/integrations/github/infrastructure/workflow-run-evidence.js +22 -0
  80. package/dist/src/integrations/index.d.ts +1 -0
  81. package/dist/src/integrations/index.js +1 -0
  82. package/dist/src/orchestration/application/delegation-commands.d.ts +3 -0
  83. package/dist/src/orchestration/application/delivery-coordinator.d.ts +70 -1
  84. package/dist/src/orchestration/application/delivery-coordinator.js +31 -5
  85. package/dist/src/orchestration/application/delivery-facts-reader.js +1 -0
  86. package/dist/src/orchestration/application/delivery-integration-reader.d.ts +9 -0
  87. package/dist/src/orchestration/application/delivery-integration-reader.js +23 -0
  88. package/dist/src/orchestration/application/delivery-integration-service.d.ts +50 -0
  89. package/dist/src/orchestration/application/delivery-integration-service.js +228 -0
  90. package/dist/src/orchestration/application/delivery-preparation.d.ts +1 -0
  91. package/dist/src/orchestration/application/delivery-preparation.js +7 -2
  92. package/dist/src/orchestration/application/orchestration-service.d.ts +74 -3
  93. package/dist/src/orchestration/application/orchestration-service.js +55 -2
  94. package/dist/src/orchestration/contracts/config.d.ts +6 -0
  95. package/dist/src/orchestration/contracts/config.js +6 -0
  96. package/dist/src/orchestration/contracts/delivery-ports.d.ts +44 -0
  97. package/dist/src/orchestration/contracts/delivery.d.ts +58 -0
  98. package/dist/src/orchestration/contracts/delivery.js +53 -1
  99. package/dist/src/orchestration/contracts/event-decoder.js +2 -1
  100. package/dist/src/orchestration/contracts/event-factory.js +2 -0
  101. package/dist/src/orchestration/contracts/events.d.ts +3 -1
  102. package/dist/src/orchestration/contracts/events.js +1 -0
  103. package/dist/src/orchestration/contracts/vocabulary.d.ts +44 -1
  104. package/dist/src/orchestration/contracts/vocabulary.js +40 -1
  105. package/dist/src/orchestration/domain/compile-diagnostics.d.ts +1 -1
  106. package/dist/src/orchestration/domain/compile-diagnostics.js +5 -0
  107. package/dist/src/orchestration/domain/delivery-candidate.js +13 -0
  108. package/dist/src/orchestration/domain/delivery-facts.d.ts +7 -1
  109. package/dist/src/orchestration/domain/delivery-facts.js +33 -1
  110. package/dist/src/orchestration/domain/delivery-integration-policy.d.ts +7 -0
  111. package/dist/src/orchestration/domain/delivery-integration-policy.js +15 -0
  112. package/dist/src/orchestration/domain/delivery-integration.d.ts +54 -0
  113. package/dist/src/orchestration/domain/delivery-integration.js +144 -0
  114. package/dist/src/orchestration/domain/delivery-qa.d.ts +2 -1
  115. package/dist/src/orchestration/domain/delivery-qa.js +24 -4
  116. package/dist/src/orchestration/domain/delivery-workflow-policy.d.ts +3 -1
  117. package/dist/src/orchestration/domain/delivery-workflow-policy.js +19 -0
  118. package/dist/src/orchestration/domain/workflow-instance-events.js +1 -0
  119. package/dist/src/orchestration/index.d.ts +3 -2
  120. package/dist/src/orchestration/index.js +1 -1
  121. package/dist/src/surfaces/api/contracts/transport-values.js +1 -0
  122. package/dist/src/surfaces/api/contracts/work.d.ts +26 -0
  123. package/dist/src/surfaces/api/presenters/execution.js +1 -0
  124. package/dist/src/surfaces/mcp/delegation.js +5 -1
  125. package/dist/src/surfaces/web-assets/assets/index-ih1h3dxC.js +1192 -0
  126. package/dist/src/surfaces/web-assets/index.html +1 -1
  127. package/package.json +1 -1
  128. package/dist/src/surfaces/web-assets/assets/index-BfhxlN_h.js +0 -1192
@@ -0,0 +1,228 @@
1
+ import { BuiltInActivityName, ParentActionKind, PullRequestDenialCode, PullRequestState, } from '../../activities/index.js';
2
+ import { resourceId } from '../../resources/index.js';
3
+ import { DeliveryRejectionError, deliveryMaintenanceCommandSchema, } from '../contracts/delivery.js';
4
+ import { OrchestrationEventType } from '../contracts/events.js';
5
+ import { DeliveryCompletion, DeliveryRejection, IntegrationState, QaVerdict, } from '../contracts/vocabulary.js';
6
+ import { currentSelection, isChange, outputKey, } from '../domain/delivery-facts.js';
7
+ import { targetRequirements } from '../domain/delivery-integration-policy.js';
8
+ import { known, sameRevision } from '../domain/delivery-integration.js';
9
+ import { deliveryState } from '../domain/delivery-qa.js';
10
+ import { readDeliveryFacts } from './delivery-facts-reader.js';
11
+ import { DeliveryIntegrationReader } from './delivery-integration-reader.js';
12
+ /**
13
+ * Governed integration of child-owned pull requests by their parent delivery. Wake derives the
14
+ * one planned next action from the parent's journal and provider facts, authorizes exactly that
15
+ * target, and verifies what a maintaining agent actually produced. The agent chooses the repair;
16
+ * nothing here authors a change, reopens a child or transfers a pull request.
17
+ */
18
+ export class DeliveryIntegrationService {
19
+ repository;
20
+ dependencies;
21
+ reader;
22
+ constructor(repository, dependencies) {
23
+ this.repository = repository;
24
+ this.dependencies = dependencies;
25
+ this.reader = new DeliveryIntegrationReader(dependencies);
26
+ }
27
+ assess(facts) {
28
+ return this.reader.assess(facts);
29
+ }
30
+ async authorize(request) {
31
+ const denied = (reason) => ({
32
+ kind: 'denied',
33
+ reason,
34
+ });
35
+ const parent = (await this.repository.load(request.workflowInstanceId)).view;
36
+ if (parent === null)
37
+ return denied(PullRequestDenialCode.OutsideDelivery);
38
+ if (!(await this.dispatchedMerge(parent, request)))
39
+ return denied(PullRequestDenialCode.OutsideDelivery);
40
+ const facts = await this.facts(parent);
41
+ const state = deliveryState(facts);
42
+ if (!state.candidateCurrent || !state.qaEffective || state.qa?.verdict !== QaVerdict.Pass)
43
+ return denied(PullRequestDenialCode.DeliveryNotReady);
44
+ // Current and effective imply a candidate and QA exist; narrow them for the context below.
45
+ if (state.candidate === null || state.qa === null)
46
+ return denied(PullRequestDenialCode.DeliveryNotReady);
47
+ const assessment = await this.reader.assess(facts);
48
+ if (assessment.complete)
49
+ return { kind: 'fully-integrated' };
50
+ const { next, blocking } = assessment;
51
+ if (next === null)
52
+ return denied(blockReason(blocking));
53
+ if (!(await this.verifySelected(facts, next.output, request.action)))
54
+ return denied(PullRequestDenialCode.DeliveryNotReady);
55
+ const context = {
56
+ requestingWorkItemId: parent.workItemId,
57
+ ownerWorkItemId: next.ownerWorkItemId,
58
+ output: { childWorkItemId: next.output.childWorkItemId, outputName: next.output.outputName },
59
+ candidateId: state.candidate.candidateId,
60
+ selectionRevision: state.selectionRevision,
61
+ qaId: state.qa.qaId,
62
+ };
63
+ return {
64
+ kind: 'target',
65
+ context,
66
+ resourceId: resourceId(next.resourceId),
67
+ revision: next.revision,
68
+ };
69
+ }
70
+ async verifySelected(facts, output, action) {
71
+ const { reference } = selectedChange(facts, output);
72
+ const maintenance = facts.maintenance
73
+ .filter((item) => outputKey(item.target) === outputKey(output))
74
+ .at(-1);
75
+ return ((await this.dependencies.integration?.verifyTarget({
76
+ ...reference,
77
+ maintenanceRunId: maintenance?.producer.runId,
78
+ maintenanceRecordedAt: maintenance?.recordedAt,
79
+ ownerWorkItemId: reference.workItemId,
80
+ ...targetRequirements(facts, output, action),
81
+ })) === true);
82
+ }
83
+ /**
84
+ * Validates one maintenance report against the selected subject and the provider. The result is
85
+ * a new immutable subject; the replaced head, its reviews, checks and QA stay historical.
86
+ */
87
+ async planMaintenance(facts, input, caller, recordedAt) {
88
+ const command = deliveryMaintenanceCommandSchema.parse(input);
89
+ const { reference, childWorkItemId } = selectedChange(facts, command.target);
90
+ const recorded = facts.maintenance.find((item) => item.maintenanceId === command.maintenanceId);
91
+ if (recorded !== undefined)
92
+ return replayPlan(recorded, command);
93
+ if (!sameRevision(command.expectedRevision, reference.revision))
94
+ throw new DeliveryRejectionError(DeliveryRejection.MaintenanceMismatch, `Maintenance must start from the current subject ${reference.revision}, not ${command.expectedRevision}`);
95
+ await this.assertAvailable(childWorkItemId);
96
+ const observed = await this.observeOpen(reference.resourceId);
97
+ if (!sameRevision(observed.headRevision, command.revision))
98
+ throw new DeliveryRejectionError(DeliveryRejection.MaintenanceMismatch, `The provider reports head ${observed.headRevision}, not the reported ${command.revision}`);
99
+ if (sameRevision(command.revision, reference.revision) &&
100
+ command.pullRequestBase === reference.pullRequestBase &&
101
+ observed.baseRevision === reference.baseRevision)
102
+ throw new DeliveryRejectionError(DeliveryRejection.NotMaintainable, 'Maintenance changed neither the head nor the base subject');
103
+ if (!(await this.dependencies.integration?.verifyTarget({
104
+ ...reference,
105
+ ownerWorkItemId: reference.workItemId,
106
+ revision: command.revision,
107
+ pullRequestBase: command.pullRequestBase,
108
+ requireReview: false,
109
+ requireChecks: false,
110
+ })))
111
+ throw new DeliveryRejectionError(DeliveryRejection.MaintenanceMismatch, 'The provider must verify the maintained repository, branch, head, base and original owner');
112
+ // The Run must hold this target's branch lease, and no earlier writer may be unresolved.
113
+ await this.dependencies.integration.assertMaintenanceWriter({
114
+ workflowInstanceId: caller.workflowInstanceId,
115
+ activationId: caller.activationId,
116
+ runId: caller.runId,
117
+ childWorkItemId: reference.workItemId,
118
+ resourceId: reference.resourceId,
119
+ revision: reference.revision,
120
+ });
121
+ const maintenance = {
122
+ maintenanceId: command.maintenanceId,
123
+ target: command.target,
124
+ kind: command.kind,
125
+ resourceId: reference.resourceId,
126
+ repositoryResourceId: reference.repositoryResourceId,
127
+ previous: {
128
+ revision: reference.revision,
129
+ pullRequestBase: reference.pullRequestBase,
130
+ ...(reference.baseRevision === undefined ? {} : { baseRevision: reference.baseRevision }),
131
+ },
132
+ current: {
133
+ revision: command.revision,
134
+ pullRequestBase: command.pullRequestBase,
135
+ ...(known(observed.baseRevision) ? { baseRevision: observed.baseRevision } : {}),
136
+ },
137
+ recordedAt,
138
+ producer: { runId: caller.runId, activationId: caller.activationId },
139
+ };
140
+ return {
141
+ result: { maintenance, replay: false },
142
+ draft: {
143
+ eventType: OrchestrationEventType.DeliveryMaintenanceRecorded,
144
+ payload: maintenance,
145
+ },
146
+ };
147
+ }
148
+ /** Only the invocation Wake dispatched for this parent may ask, and only for its own action. */
149
+ async dispatchedMerge(parent, request) {
150
+ const policy = (await this.dependencies.definitionFor(parent)).delegation;
151
+ const expected = request.action === ParentActionKind.Merge
152
+ ? BuiltInActivityName.PullRequestMerge
153
+ : BuiltInActivityName.PullRequestApprove;
154
+ return (policy?.qa !== undefined &&
155
+ policy.completion === DeliveryCompletion.MergeRequired &&
156
+ parent.pendingActivation?.activationId === request.activationId &&
157
+ parent.pendingActivation.activity === expected);
158
+ }
159
+ async assertAvailable(childWorkItemId) {
160
+ const unavailable = await this.dependencies.unavailable?.([childWorkItemId]);
161
+ if (unavailable?.has(childWorkItemId) === true)
162
+ throw new DeliveryRejectionError(DeliveryRejection.NotMaintainable, unavailable.get(childWorkItemId));
163
+ }
164
+ async observeOpen(resourceId) {
165
+ const integration = this.dependencies.integration;
166
+ if (integration === undefined)
167
+ throw new DeliveryRejectionError(DeliveryRejection.MaintenanceMismatch, 'Provider verification of maintenance is unavailable');
168
+ const observed = await integration.observe(resourceId);
169
+ if (observed === null)
170
+ throw new DeliveryRejectionError(DeliveryRejection.MaintenanceMismatch, 'The pull request has not been observed from the provider');
171
+ if (observed.state !== PullRequestState.Open)
172
+ throw new DeliveryRejectionError(DeliveryRejection.NotMaintainable, `A ${observed.state} pull request cannot be maintained`);
173
+ return observed;
174
+ }
175
+ /**
176
+ * The open pull requests a maintenance stage may lease. Merged, closed or held targets cannot
177
+ * be written to, so they are never offered.
178
+ */
179
+ async maintenanceTargets(parent) {
180
+ const assessment = await this.reader.assess(await this.facts(parent));
181
+ const open = [
182
+ IntegrationState.Ready,
183
+ IntegrationState.AwaitingLowerLayer,
184
+ IntegrationState.MaintenanceRequired,
185
+ ];
186
+ return assessment.targets.filter((target) => open.includes(target.state));
187
+ }
188
+ async facts(parent) {
189
+ const { events } = await this.repository.history(parent.workflowInstanceId);
190
+ return readDeliveryFacts(this.repository, parent, events);
191
+ }
192
+ }
193
+ function selectedChange(facts, target) {
194
+ const entry = currentSelection(facts)?.entries.find((candidate) => candidate.childWorkItemId === target.childWorkItemId &&
195
+ candidate.output.name === target.outputName);
196
+ if (entry === undefined || !isChange(entry.output))
197
+ throw new DeliveryRejectionError(DeliveryRejection.NotMaintainable, `${outputKey(target)} is not a selected change`);
198
+ return { reference: entry.output.reference, childWorkItemId: entry.childWorkItemId };
199
+ }
200
+ /** A repeated report is idempotent only when it says exactly what was recorded. */
201
+ function replayPlan(recorded, command) {
202
+ const same = outputKey(recorded.target) === outputKey(command.target) &&
203
+ recorded.kind === command.kind &&
204
+ sameRevision(recorded.current.revision, command.revision) &&
205
+ recorded.current.pullRequestBase === command.pullRequestBase &&
206
+ sameRevision(recorded.previous.revision, command.expectedRevision);
207
+ if (!same)
208
+ throw new DeliveryRejectionError(DeliveryRejection.Conflict, `Maintenance ${command.maintenanceId} was already recorded differently`);
209
+ return {
210
+ result: { maintenance: recorded, replay: true },
211
+ draft: undefined,
212
+ };
213
+ }
214
+ function blockReason(blocking) {
215
+ switch (blocking?.state) {
216
+ case IntegrationState.Held:
217
+ return PullRequestDenialCode.TargetHeld;
218
+ case IntegrationState.MaintenanceRequired:
219
+ case IntegrationState.AwaitingLowerLayer:
220
+ return PullRequestDenialCode.MaintenanceRequired;
221
+ case IntegrationState.Closed:
222
+ return PullRequestDenialCode.Closed;
223
+ case IntegrationState.Unobserved:
224
+ return PullRequestDenialCode.MissingResource;
225
+ default:
226
+ return PullRequestDenialCode.DeliveryNotReady;
227
+ }
228
+ }
@@ -6,6 +6,7 @@ import type { OrchestrationRepository } from './orchestration-repository.js';
6
6
  export declare class DeliveryPreparation {
7
7
  private readonly repository;
8
8
  private readonly dependencies;
9
+ private readonly integration;
9
10
  constructor(repository: OrchestrationRepository, dependencies: DeliveryDependencies);
10
11
  completionBlock(id: string): Promise<DeliveryCompletionBlock | undefined>;
11
12
  prepare(id: string): Promise<ExecutionCandidate | undefined>;
@@ -1,15 +1,19 @@
1
1
  import { WorkspaceMode } from '../../execution/index.js';
2
2
  import { OrchestrationEventType } from '../contracts/events.js';
3
3
  import { CandidateSourceKind, DeliveryCompletion } from '../contracts/vocabulary.js';
4
+ import { currentSelection } from '../domain/delivery-facts.js';
4
5
  import { completionDecision, deliveryState } from '../domain/delivery-qa.js';
5
6
  import { readDeliveryFacts } from './delivery-facts-reader.js';
7
+ import { DeliveryIntegrationReader } from './delivery-integration-reader.js';
6
8
  import { verifyCandidateSources } from './delivery-sources.js';
7
9
  export class DeliveryPreparation {
8
10
  repository;
9
11
  dependencies;
12
+ integration;
10
13
  constructor(repository, dependencies) {
11
14
  this.repository = repository;
12
15
  this.dependencies = dependencies;
16
+ this.integration = new DeliveryIntegrationReader(dependencies);
13
17
  }
14
18
  async completionBlock(id) {
15
19
  const parent = (await this.repository.loadRequired(id)).view;
@@ -20,7 +24,8 @@ export class DeliveryPreparation {
20
24
  const facts = await this.facts(parent);
21
25
  if (facts.reservations.length === 0)
22
26
  return undefined;
23
- const decision = completionDecision(deliveryState(facts), definition.delegation?.completion ?? DeliveryCompletion.QaApproved);
27
+ const mode = definition.delegation?.completion ?? DeliveryCompletion.QaApproved;
28
+ const decision = completionDecision(deliveryState(facts), mode, mode === DeliveryCompletion.MergeRequired ? await this.integration.assess(facts) : undefined);
24
29
  const retryStage = await this.delegatingStage(id, facts.reservations.at(-1));
25
30
  const blocked = (reason) => ({
26
31
  reason,
@@ -53,7 +58,7 @@ export class DeliveryPreparation {
53
58
  candidateId: candidate.candidateId,
54
59
  selectionRevision: candidate.selectionRevision,
55
60
  criteria: candidate.criteria,
56
- inputs: facts.selections.at(-1).entries.map((entry) => ({
61
+ inputs: currentSelection(facts).entries.map((entry) => ({
57
62
  workItemId: entry.output.reference.workItemId,
58
63
  outputName: entry.output.name,
59
64
  revision: 'revisionId' in entry.output.reference
@@ -1,10 +1,11 @@
1
1
  import type { DelegationArtifacts, DelegationAuthority, DelegationRepositories } from '../contracts/delegation-ports.js';
2
2
  import type { DelegationAttentionReply, DelegationCaller, DelegationSubmission } from '../contracts/delegation.js';
3
- import type { DeliveryCandidateCommand, DeliveryQaCommand, DeliverySelectionCommand } from '../contracts/delivery.js';
3
+ import type { DeliveryIntegration } from '../contracts/delivery-ports.js';
4
+ import type { DeliveryCandidateCommand, DeliveryMaintenanceCommand, DeliveryQaCommand, DeliverySelectionCommand } from '../contracts/delivery.js';
4
5
  import type { ResourceTransitionEvidence } from './resource-transition-evidence.js';
5
6
  import type { CommandContext, EventJournal, ProjectionStore } from '@atolis-hq/eventing';
6
- import { type ActivationId, type ActivityOutcome } from '../../activities/index.js';
7
- import type { ExecutionFailureCode } from '../../execution/index.js';
7
+ import { type ActivationId, type ActivityOutcome, type ParentActionRequest } from '../../activities/index.js';
8
+ import type { ExecutionFailureCode, ExecutionMaintenance } from '../../execution/index.js';
8
9
  import { type WorkItemId, type WorkItemView, type WorkService } from '../../work/index.js';
9
10
  import type { StartWorkflowInstance } from '../contracts/commands.js';
10
11
  import type { CompiledWorkflow, TransitionTarget } from '../contracts/config.js';
@@ -37,6 +38,32 @@ export declare class OrchestrationService {
37
38
  private readonly work;
38
39
  constructor(journal: EventJournal, work: WorkService, definitions: Readonly<Record<string, CompiledWorkflow>>, projections?: ProjectionStore, isAgentActivity?: AgentActivityClassifier, delegation?: DelegationDependenciesFactory);
39
40
  deliveryStateFor(workflowInstanceId: string): Promise<{
41
+ integration: import("../domain/delivery-integration.js").IntegrationAssessment | null;
42
+ maintenance: readonly {
43
+ maintenanceId: string;
44
+ target: {
45
+ childWorkItemId: WorkItemId;
46
+ outputName: string;
47
+ };
48
+ kind: "rebase" | "retarget" | "resolve-conflicts";
49
+ resourceId: string;
50
+ repositoryResourceId: string;
51
+ previous: {
52
+ revision: string;
53
+ pullRequestBase: string;
54
+ baseRevision?: string | undefined;
55
+ };
56
+ current: {
57
+ revision: string;
58
+ pullRequestBase: string;
59
+ baseRevision?: string | undefined;
60
+ };
61
+ producer: {
62
+ runId: string;
63
+ activationId: string;
64
+ };
65
+ recordedAt?: string | undefined;
66
+ }[];
40
67
  required: {
41
68
  cohortId: string;
42
69
  assignmentId: string;
@@ -249,6 +276,13 @@ export declare class OrchestrationService {
249
276
  outputName: string;
250
277
  }[];
251
278
  criteria: string[];
279
+ subjects?: {
280
+ childWorkItemId: WorkItemId;
281
+ outputName: string;
282
+ revision: string;
283
+ pullRequestBase: string;
284
+ baseRevision?: string | undefined;
285
+ }[] | undefined;
252
286
  recipe?: string | undefined;
253
287
  };
254
288
  replay: boolean;
@@ -277,8 +311,41 @@ export declare class OrchestrationService {
277
311
  };
278
312
  replay: boolean;
279
313
  }>;
314
+ recordDeliveryMaintenance(caller: DelegationCaller, input: DeliveryMaintenanceCommand, context: CommandContext): Promise<{
315
+ readonly maintenance: import("../contracts/delivery.js").DeliveryMaintenance;
316
+ readonly replay: boolean;
317
+ }>;
318
+ authorizeParentAction(request: ParentActionRequest): Promise<import("../../activities/index.js").ParentActionDecision>;
319
+ /** The branch leases Execution must hold for the configured maintenance stage's activation. */
320
+ deliveryMaintenancePreparation(workflowInstanceId: string): Promise<ExecutionMaintenance | undefined>;
280
321
  inspectDelegation(caller: DelegationCaller, context: CommandContext): Promise<{
281
322
  delivery: {
323
+ integration: import("../domain/delivery-integration.js").IntegrationAssessment | null;
324
+ maintenance: readonly {
325
+ maintenanceId: string;
326
+ target: {
327
+ childWorkItemId: WorkItemId;
328
+ outputName: string;
329
+ };
330
+ kind: "rebase" | "retarget" | "resolve-conflicts";
331
+ resourceId: string;
332
+ repositoryResourceId: string;
333
+ previous: {
334
+ revision: string;
335
+ pullRequestBase: string;
336
+ baseRevision?: string | undefined;
337
+ };
338
+ current: {
339
+ revision: string;
340
+ pullRequestBase: string;
341
+ baseRevision?: string | undefined;
342
+ };
343
+ producer: {
344
+ runId: string;
345
+ activationId: string;
346
+ };
347
+ recordedAt?: string | undefined;
348
+ }[];
282
349
  required: {
283
350
  cohortId: string;
284
351
  assignmentId: string;
@@ -360,6 +427,9 @@ export declare class OrchestrationService {
360
427
  stage: string;
361
428
  recipe?: string | undefined;
362
429
  } | undefined;
430
+ maintenance?: {
431
+ stage: string;
432
+ } | undefined;
363
433
  } | null;
364
434
  repositories: string[];
365
435
  planRevision: number;
@@ -657,6 +727,7 @@ type DelegationDependenciesFactory = (queries: {
657
727
  artifacts: DelegationArtifacts;
658
728
  authority?: DelegationAuthority;
659
729
  repositories?: DelegationRepositories;
730
+ integration?: DeliveryIntegration;
660
731
  runHasEnded: (runId: string) => Promise<boolean>;
661
732
  };
662
733
  export {};
@@ -71,7 +71,7 @@ export class OrchestrationService {
71
71
  resumeChild: this.resumeClarification,
72
72
  });
73
73
  this.delegationCommands = new DelegationCommands(repository, work, this.startWorkflow, this.definitions, this.delegatedWork, dependencies?.authority ?? unavailableDelegationAuthority);
74
- this.delivery = new DeliveryCoordinator(repository, deliveryDependencies(dependencies, async (parent) => this.startWorkflow.definitionFor(parent)));
74
+ this.delivery = new DeliveryCoordinator(repository, deliveryDependencies(dependencies, async (parent) => this.startWorkflow.definitionFor(parent), work));
75
75
  this.advanceWorkflow = new AdvanceWorkflow(repository, this.startWorkflow, (command, context) => this.acceptOutcome(command, context), isAgentActivity);
76
76
  this.acceptWorkflowSignal = new AcceptSignal(repository, this.startWorkflow, work);
77
77
  this.deferredSignals = new DeferredSignals(repository, (id, signal, context) => this.delegationGate.run(id, () => this.acceptWorkflowSignal.execute(id, signal, context)), (id, target, evidenceId, context) => this.delegationGate.run(id, () => this.advanceWorkflow.applyResourceTransition(id, target, evidenceId, context)), (ids) => this.delegationGate.fencedAmong(ids));
@@ -112,6 +112,38 @@ export class OrchestrationService {
112
112
  recordDeliveryQa(caller, input, context) {
113
113
  return this.delivery.recordQa(caller, input, context);
114
114
  }
115
+ recordDeliveryMaintenance(caller, input, context) {
116
+ return this.delivery.recordMaintenance(caller, input, context);
117
+ }
118
+ authorizeParentAction(request) {
119
+ return this.delivery.authorizeParentAction(request);
120
+ }
121
+ /** The branch leases Execution must hold for the configured maintenance stage's activation. */
122
+ async deliveryMaintenancePreparation(workflowInstanceId) {
123
+ const targets = await this.delivery.maintenanceTargets(workflowInstanceId);
124
+ if (targets === undefined || targets.length === 0)
125
+ return undefined;
126
+ const prepared = [];
127
+ for (const target of targets) {
128
+ // The same assignment that created the child's workspace, so its lease is reused exactly.
129
+ const repository = await this.delegatedWork.repositoryFor(target.ownerWorkItemId);
130
+ if (repository === undefined)
131
+ continue;
132
+ prepared.push({
133
+ childWorkItemId: target.ownerWorkItemId,
134
+ outputName: target.output.outputName,
135
+ resourceId: target.resourceId,
136
+ repository,
137
+ branch: target.branch,
138
+ revision: target.revision,
139
+ pullRequestBase: target.pullRequestBase,
140
+ ...(target.maintenance === undefined
141
+ ? {}
142
+ : { reason: `${target.maintenance}: ${target.detail ?? ''}`.trim() }),
143
+ });
144
+ }
145
+ return prepared.length === 0 ? undefined : { targets: prepared };
146
+ }
115
147
  async inspectDelegation(caller, context) {
116
148
  return {
117
149
  ...(await this.delegationCommands.inspect(caller, context)),
@@ -466,10 +498,31 @@ function isReviewConversationCommand(command) {
466
498
  async function unavailableRunCompletion() {
467
499
  throw new Error('Delegation adapters unavailable');
468
500
  }
469
- function deliveryDependencies(dependencies, definitionFor) {
501
+ function deliveryDependencies(dependencies, definitionFor, work) {
470
502
  return {
471
503
  authority: dependencies?.authority ?? unavailableDelegationAuthority,
472
504
  repositories: dependencies?.repositories,
505
+ integration: dependencies?.integration,
506
+ // A finished producing workflow (a Closed child) is deliberately not a hold.
507
+ unavailable: async (ids) => {
508
+ const snapshot = await work.deliverySnapshot();
509
+ const reasons = new Map();
510
+ for (const id of ids) {
511
+ const lineage = [id, ...(snapshot.get(id)?.delegation?.ancestors ?? [])];
512
+ const held = lineage.find((member) => {
513
+ const state = snapshot.get(member);
514
+ return (snapshot.invalid(member) ||
515
+ state === null ||
516
+ state.deleted === true ||
517
+ state.frozen === true ||
518
+ state.state === WorkStatus.Cancelled ||
519
+ (member !== id && state.state !== WorkStatus.Open));
520
+ });
521
+ if (held !== undefined)
522
+ reasons.set(id, `Delivery work ${held} is unavailable`);
523
+ }
524
+ return reasons;
525
+ },
473
526
  definitionFor,
474
527
  };
475
528
  }
@@ -228,6 +228,9 @@ export declare const delegationPolicySchema: z.ZodObject<{
228
228
  stage: z.ZodString;
229
229
  recipe: z.ZodOptional<z.ZodString>;
230
230
  }, z.core.$strict>>;
231
+ maintenance: z.ZodOptional<z.ZodObject<{
232
+ stage: z.ZodString;
233
+ }, z.core.$strict>>;
231
234
  }, z.core.$strict>;
232
235
  export type DelegationPolicy = z.infer<typeof delegationPolicySchema>;
233
236
  export declare const workflowDefinitionConfigSchema: z.ZodObject<{
@@ -246,6 +249,9 @@ export declare const workflowDefinitionConfigSchema: z.ZodObject<{
246
249
  stage: z.ZodString;
247
250
  recipe: z.ZodOptional<z.ZodString>;
248
251
  }, z.core.$strict>>;
252
+ maintenance: z.ZodOptional<z.ZodObject<{
253
+ stage: z.ZodString;
254
+ }, z.core.$strict>>;
249
255
  }, z.core.$strict>>;
250
256
  commands: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
251
257
  activity: z.ZodString;
@@ -164,6 +164,12 @@ export const delegationPolicySchema = z
164
164
  .object({ stage: identifier, recipe: z.string().trim().min(1).optional() })
165
165
  .strict()
166
166
  .optional(),
167
+ /**
168
+ * The agent stage that retargets, rebases or repairs selected pull requests between planned
169
+ * merges. Wake leases each target's recorded branch workspace for it; the agent chooses the
170
+ * repair.
171
+ */
172
+ maintenance: z.object({ stage: identifier }).strict().optional(),
167
173
  })
168
174
  .strict();
169
175
  export const workflowDefinitionConfigSchema = z
@@ -1,9 +1,53 @@
1
+ import type { PullRequestMergeability, PullRequestState } from '../../activities/index.js';
1
2
  import type { CompiledWorkflow } from './config.js';
2
3
  import type { DelegationAuthority, DelegationRepositories } from './delegation-ports.js';
3
4
  import type { WorkflowInstanceView } from './views.js';
5
+ export interface DeliveryPullRequest {
6
+ readonly state: PullRequestState;
7
+ readonly headRevision: string;
8
+ readonly baseRevision: string;
9
+ readonly mergeRevision?: string | undefined;
10
+ readonly mergeability?: PullRequestMergeability | undefined;
11
+ }
12
+ /** Trusted Bootstrap adapters over provider-observed facts and Execution's Run records. */
13
+ export interface DeliveryIntegration {
14
+ /** The current provider-observed pull request, or null when it has not been observed. */
15
+ observe(resourceId: string): Promise<DeliveryPullRequest | null>;
16
+ verifyTarget(request: {
17
+ readonly ownerWorkItemId: string;
18
+ readonly resourceId: string;
19
+ readonly repositoryResourceId: string;
20
+ readonly branch: string;
21
+ readonly revision: string;
22
+ readonly pullRequestBase: string;
23
+ readonly baseRevision?: string | undefined;
24
+ readonly maintenanceRecordedAt?: string | undefined;
25
+ readonly maintenanceRunId?: string | undefined;
26
+ readonly requireReview: boolean;
27
+ readonly requireChecks: boolean;
28
+ }): Promise<boolean>;
29
+ /**
30
+ * Proves the Run was durably prepared with the target's branch lease, and that no earlier
31
+ * maintenance Run of the same target is still unresolved or ambiguous.
32
+ */
33
+ assertMaintenanceWriter(request: {
34
+ readonly workflowInstanceId: string;
35
+ readonly activationId: string;
36
+ readonly runId: string;
37
+ readonly childWorkItemId: string;
38
+ readonly resourceId: string;
39
+ readonly revision: string;
40
+ }): Promise<void>;
41
+ }
4
42
  export interface DeliveryDependencies {
5
43
  readonly authority: DelegationAuthority;
6
44
  /** Resolves a repository source through the parent's own repository authority. */
7
45
  readonly repositories?: DelegationRepositories | undefined;
46
+ readonly integration?: DeliveryIntegration | undefined;
47
+ /**
48
+ * Reasons integration must not touch a child's output: the child or any ancestor is frozen,
49
+ * cancelled or deleted. A finished producing workflow is not a reason.
50
+ */
51
+ readonly unavailable?: ((childWorkItemIds: readonly string[]) => Promise<ReadonlyMap<string, string>>) | undefined;
8
52
  readonly definitionFor: (parent: WorkflowInstanceView) => Promise<CompiledWorkflow>;
9
53
  }
@@ -182,6 +182,13 @@ export declare const deliveryCandidateSchema: z.ZodObject<{
182
182
  outputName: z.ZodString;
183
183
  }, z.core.$strict>>;
184
184
  }, z.core.$strict>>;
185
+ subjects: z.ZodOptional<z.ZodArray<z.ZodObject<{
186
+ childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
187
+ outputName: z.ZodString;
188
+ revision: z.ZodString;
189
+ pullRequestBase: z.ZodString;
190
+ baseRevision: z.ZodOptional<z.ZodString>;
191
+ }, z.core.$strict>>>;
185
192
  context: z.ZodArray<z.ZodObject<{
186
193
  childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
187
194
  outputName: z.ZodString;
@@ -239,6 +246,57 @@ export declare const deliveryQaSchema: z.ZodObject<{
239
246
  activationId: z.ZodOptional<z.ZodString>;
240
247
  }, z.core.$strict>;
241
248
  export type DeliveryQa = z.infer<typeof deliveryQaSchema>;
249
+ /** Agent input: what it did to one selected change, for Wake to verify against the provider. */
250
+ export declare const deliveryMaintenanceCommandSchema: z.ZodObject<{
251
+ maintenanceId: z.ZodString;
252
+ target: z.ZodObject<{
253
+ childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
254
+ outputName: z.ZodString;
255
+ }, z.core.$strict>;
256
+ kind: z.ZodEnum<{
257
+ readonly Retarget: "retarget";
258
+ readonly Rebase: "rebase";
259
+ readonly Resolve: "resolve-conflicts";
260
+ }>;
261
+ expectedRevision: z.ZodString;
262
+ revision: z.ZodString;
263
+ pullRequestBase: z.ZodString;
264
+ }, z.core.$strict>;
265
+ export type DeliveryMaintenanceCommand = z.infer<typeof deliveryMaintenanceCommandSchema>;
266
+ /**
267
+ * An immutable new subject for a selected change. The earlier subject, its reviews, checks and
268
+ * QA stay historical; the PR keeps its original owner and the producer is the maintaining Run.
269
+ */
270
+ export declare const deliveryMaintenanceSchema: z.ZodObject<{
271
+ maintenanceId: z.ZodString;
272
+ target: z.ZodObject<{
273
+ childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
274
+ outputName: z.ZodString;
275
+ }, z.core.$strict>;
276
+ kind: z.ZodEnum<{
277
+ readonly Retarget: "retarget";
278
+ readonly Rebase: "rebase";
279
+ readonly Resolve: "resolve-conflicts";
280
+ }>;
281
+ resourceId: z.ZodString;
282
+ repositoryResourceId: z.ZodString;
283
+ previous: z.ZodObject<{
284
+ revision: z.ZodString;
285
+ pullRequestBase: z.ZodString;
286
+ baseRevision: z.ZodOptional<z.ZodString>;
287
+ }, z.core.$strict>;
288
+ current: z.ZodObject<{
289
+ revision: z.ZodString;
290
+ pullRequestBase: z.ZodString;
291
+ baseRevision: z.ZodOptional<z.ZodString>;
292
+ }, z.core.$strict>;
293
+ recordedAt: z.ZodOptional<z.ZodString>;
294
+ producer: z.ZodObject<{
295
+ runId: z.ZodString;
296
+ activationId: z.ZodString;
297
+ }, z.core.$strict>;
298
+ }, z.core.$strict>;
299
+ export type DeliveryMaintenance = z.infer<typeof deliveryMaintenanceSchema>;
242
300
  /** A final delivery gate can be reassessed through ordinary operator retry. */
243
301
  export interface DeliveryCompletionBlock {
244
302
  readonly reason: string;