@atolis-hq/wake 0.4.94 → 0.4.96

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 (74) hide show
  1. package/dist/src/artifacts/application/delegation-artifacts.d.ts +26 -0
  2. package/dist/src/artifacts/application/delegation-artifacts.js +33 -0
  3. package/dist/src/artifacts/index.d.ts +1 -0
  4. package/dist/src/artifacts/index.js +1 -0
  5. package/dist/src/bootstrap/composition-root.js +16 -3
  6. package/dist/src/bootstrap/doctor-processor-health.js +19 -3
  7. package/dist/src/bootstrap/integration-runtime.d.ts +2 -1
  8. package/dist/src/bootstrap/integration-runtime.js +13 -16
  9. package/dist/src/bootstrap/poll-coordinator.d.ts +36 -0
  10. package/dist/src/bootstrap/poll-coordinator.js +93 -0
  11. package/dist/src/bootstrap/version.js +1 -1
  12. package/dist/src/control-plane/application/activation-scheduler.js +28 -43
  13. package/dist/src/control-plane/application/advance-once-dispatch.d.ts +0 -13
  14. package/dist/src/control-plane/application/advance-once-dispatch.js +10 -24
  15. package/dist/src/control-plane/application/in-process-scheduler-serialiser.d.ts +1 -0
  16. package/dist/src/control-plane/application/in-process-scheduler-serialiser.js +15 -0
  17. package/dist/src/control-plane/application/runner-quota-outcome.d.ts +13 -0
  18. package/dist/src/control-plane/application/runner-quota-outcome.js +23 -0
  19. package/dist/src/integrations/application/adapter-health-tracker.d.ts +2 -0
  20. package/dist/src/integrations/application/adapter-health-tracker.js +3 -0
  21. package/dist/src/integrations/application/poll-service.js +4 -1
  22. package/dist/src/integrations/contracts/provider.d.ts +4 -0
  23. package/dist/src/integrations/github/infrastructure/source.js +13 -5
  24. package/dist/src/integrations/github/provider.d.ts +2 -0
  25. package/dist/src/integrations/github/provider.js +28 -2
  26. package/dist/src/orchestration/application/accept-signal.d.ts +3 -1
  27. package/dist/src/orchestration/application/accept-signal.js +2 -0
  28. package/dist/src/orchestration/application/advance-workflow.d.ts +0 -1
  29. package/dist/src/orchestration/application/advance-workflow.js +0 -4
  30. package/dist/src/orchestration/application/deferred-signals.d.ts +21 -0
  31. package/dist/src/orchestration/application/deferred-signals.js +119 -0
  32. package/dist/src/orchestration/application/delegated-work.d.ts +51 -0
  33. package/dist/src/orchestration/application/delegated-work.js +420 -0
  34. package/dist/src/orchestration/application/delegation-gate.d.ts +17 -0
  35. package/dist/src/orchestration/application/delegation-gate.js +31 -0
  36. package/dist/src/orchestration/application/delegation-ledger.d.ts +28 -0
  37. package/dist/src/orchestration/application/delegation-ledger.js +79 -0
  38. package/dist/src/orchestration/application/delegation-reservation.d.ts +15 -0
  39. package/dist/src/orchestration/application/delegation-reservation.js +54 -0
  40. package/dist/src/orchestration/application/expire-timed-out-waits.d.ts +1 -1
  41. package/dist/src/orchestration/application/expire-timed-out-waits.js +2 -2
  42. package/dist/src/orchestration/application/orchestration-projection.js +5 -1
  43. package/dist/src/orchestration/application/orchestration-repository.d.ts +5 -0
  44. package/dist/src/orchestration/application/orchestration-repository.js +69 -11
  45. package/dist/src/orchestration/application/orchestration-service.d.ts +28 -3
  46. package/dist/src/orchestration/application/orchestration-service.js +69 -20
  47. package/dist/src/orchestration/application/resource-transition-matching.js +11 -6
  48. package/dist/src/orchestration/application/start-workflow.js +4 -2
  49. package/dist/src/orchestration/application/workflow-definition-registry.d.ts +5 -1
  50. package/dist/src/orchestration/application/workflow-definition-registry.js +16 -3
  51. package/dist/src/orchestration/contracts/commands.d.ts +1 -0
  52. package/dist/src/orchestration/contracts/delegation-ports.d.ts +14 -0
  53. package/dist/src/orchestration/contracts/delegation-ports.js +1 -0
  54. package/dist/src/orchestration/contracts/delegation.d.ts +98 -0
  55. package/dist/src/orchestration/contracts/delegation.js +70 -0
  56. package/dist/src/orchestration/contracts/event-decoder.js +19 -0
  57. package/dist/src/orchestration/contracts/event-factory.js +16 -0
  58. package/dist/src/orchestration/contracts/events.d.ts +39 -0
  59. package/dist/src/orchestration/contracts/events.js +8 -0
  60. package/dist/src/orchestration/contracts/views.d.ts +8 -0
  61. package/dist/src/orchestration/domain/workflow-completion.d.ts +3 -0
  62. package/dist/src/orchestration/domain/workflow-completion.js +9 -0
  63. package/dist/src/orchestration/domain/workflow-instance-events.d.ts +2 -0
  64. package/dist/src/orchestration/domain/workflow-instance-events.js +37 -0
  65. package/dist/src/orchestration/index.d.ts +2 -0
  66. package/dist/src/orchestration/index.js +1 -0
  67. package/dist/src/work/application/work-projection.js +3 -0
  68. package/dist/src/work/application/work-service.js +16 -0
  69. package/dist/src/work/contracts/commands.d.ts +6 -0
  70. package/dist/src/work/contracts/events.d.ts +2 -1
  71. package/dist/src/work/contracts/events.js +8 -0
  72. package/dist/src/work/contracts/views.d.ts +2 -1
  73. package/dist/src/work/domain/work-item.js +3 -0
  74. package/package.json +1 -1
@@ -0,0 +1,70 @@
1
+ import { z } from 'zod';
2
+ import { activationId } from '../../activities/index.js';
3
+ import { brandedStringSchema } from '../../kernel/index.js';
4
+ import { workItemId } from '../../work/index.js';
5
+ import { workflowInstanceId, workflowName } from './identifiers.js';
6
+ export const delegationRevisionReferenceSchema = z
7
+ .object({
8
+ workItemId: brandedStringSchema(workItemId),
9
+ revisionId: z.string().min(1),
10
+ })
11
+ .strict();
12
+ export const delegatedAssignmentSchema = z
13
+ .object({
14
+ assignmentId: z.string().min(1),
15
+ objective: z.string().trim().min(1),
16
+ criteria: z.array(z.string().trim().min(1)).min(1),
17
+ workflowName: brandedStringSchema(workflowName),
18
+ workflowFingerprint: z.string().min(1),
19
+ context: z.array(delegationRevisionReferenceSchema),
20
+ outputs: z
21
+ .array(z.object({ name: z.string().min(1), path: z.string().min(1) }).strict())
22
+ .min(1),
23
+ })
24
+ .strict();
25
+ export const delegationReservationSchema = z
26
+ .object({
27
+ cohortId: z.string().min(1),
28
+ parentWorkflowInstanceId: brandedStringSchema(workflowInstanceId),
29
+ parentWorkItemId: brandedStringSchema(workItemId),
30
+ rootWorkItemId: brandedStringSchema(workItemId),
31
+ activationId: brandedStringSchema(activationId),
32
+ runId: z.string().min(1),
33
+ ancestors: z.array(brandedStringSchema(workItemId)),
34
+ maxDepth: z.number().int().positive(),
35
+ maxDescendants: z.number().int().positive(),
36
+ children: z
37
+ .array(delegatedAssignmentSchema
38
+ .extend({
39
+ workItemId: brandedStringSchema(workItemId),
40
+ workflowInstanceId: brandedStringSchema(workflowInstanceId),
41
+ grantId: z.string().min(1),
42
+ })
43
+ .strict())
44
+ .min(1),
45
+ })
46
+ .strict();
47
+ export const delegationResultSchema = z
48
+ .object({
49
+ cohortId: z.string().min(1),
50
+ childWorkItemId: brandedStringSchema(workItemId),
51
+ outputs: z
52
+ .array(z
53
+ .object({ name: z.string().min(1), reference: delegationRevisionReferenceSchema })
54
+ .strict())
55
+ .min(1),
56
+ })
57
+ .strict();
58
+ export const delegationWaitSchema = z
59
+ .object({ cohortId: z.string().min(1), activationId: brandedStringSchema(activationId) })
60
+ .strict();
61
+ export const delegationJoinSchema = z
62
+ .object({ cohortId: z.string().min(1), results: z.array(delegationResultSchema).min(1) })
63
+ .strict();
64
+ /** Retryable rejection: callers retain their signal or execution evidence until the cohort settles. */
65
+ export class DelegationPendingError extends Error {
66
+ constructor(id) {
67
+ super(`Workflow ${id} has an outstanding delegation; retry after it settles`);
68
+ this.name = 'DelegationPendingError';
69
+ }
70
+ }
@@ -1,4 +1,6 @@
1
+ /* eslint-disable max-lines */
1
2
  import { z } from 'zod';
3
+ import { delegationJoinSchema, delegationReservationSchema, delegationResultSchema, delegationWaitSchema, } from './delegation.js';
2
4
  import { eventDataSchema, eventEnvelopeSchema } from '@atolis-hq/eventing';
3
5
  import { activationId, ActivityEventType, activityName, ActivityOutcomeKind, PullRequestCheckState, PullRequestMergeability, PullRequestState, } from '../../activities/index.js';
4
6
  import { WorkspaceMode } from '../../execution/index.js';
@@ -214,6 +216,13 @@ const groupBudgetGrantedEnvelope = childGroupEnvelope(OrchestrationEventType.Gro
214
216
  });
215
217
  });
216
218
  const eventSchemas = {
219
+ [OrchestrationEventType.DelegationFailed]: workflowEnvelope(OrchestrationEventType.DelegationFailed, z.object({ cohortId: z.string().min(1), reason: z.string().min(1) }).strict()),
220
+ [OrchestrationEventType.DelegationReconciliationFailed]: workflowEnvelope(OrchestrationEventType.DelegationReconciliationFailed, z.object({ cohortId: z.string().min(1), reason: z.string().min(1) }).strict()),
221
+ [OrchestrationEventType.DelegationReserved]: workflowEnvelope(OrchestrationEventType.DelegationReserved, delegationReservationSchema),
222
+ [OrchestrationEventType.DelegationRegistered]: workflowEnvelope(OrchestrationEventType.DelegationRegistered, z.object({ cohortId: z.string().min(1) }).strict()),
223
+ [OrchestrationEventType.DelegationResultSelected]: workflowEnvelope(OrchestrationEventType.DelegationResultSelected, delegationResultSchema),
224
+ [OrchestrationEventType.DelegationWaitStarted]: workflowEnvelope(OrchestrationEventType.DelegationWaitStarted, delegationWaitSchema),
225
+ [OrchestrationEventType.DelegationJoined]: workflowEnvelope(OrchestrationEventType.DelegationJoined, delegationJoinSchema),
217
226
  [OrchestrationEventType.InstanceStarted]: workflowEnvelope(OrchestrationEventType.InstanceStarted, z.union([
218
227
  z
219
228
  .object({
@@ -278,6 +287,16 @@ const eventSchemas = {
278
287
  })
279
288
  .strict()),
280
289
  [OrchestrationEventType.SignalWaitStarted]: workflowEnvelope(OrchestrationEventType.SignalWaitStarted, expectationSchema),
290
+ [OrchestrationEventType.SignalDeferred]: workflowEnvelope(OrchestrationEventType.SignalDeferred, z
291
+ .object({
292
+ stage: brandedStringSchema(stageName),
293
+ intentEventId: z.string().optional(),
294
+ resourceId: z.string().optional(),
295
+ revision: z.string().optional(),
296
+ signal: signalSchema,
297
+ target: transitionTargetSchema.optional(),
298
+ })
299
+ .strict()),
281
300
  [OrchestrationEventType.SignalAccepted]: workflowEnvelope(OrchestrationEventType.SignalAccepted, signalSchema),
282
301
  [OrchestrationEventType.SupplementalActivityQueued]: workflowEnvelope(OrchestrationEventType.SupplementalActivityQueued, z
283
302
  .object({
@@ -4,6 +4,20 @@ import { OrchestrationEventType, } from './events.js';
4
4
  // eslint-disable-next-line complexity
5
5
  export function createOrchestrationEventData(input) {
6
6
  switch (input.eventType) {
7
+ case OrchestrationEventType.DelegationFailed:
8
+ return createEventData(input);
9
+ case OrchestrationEventType.DelegationReconciliationFailed:
10
+ return createEventData(input);
11
+ case OrchestrationEventType.DelegationReserved:
12
+ return createEventData(input);
13
+ case OrchestrationEventType.DelegationRegistered:
14
+ return createEventData(input);
15
+ case OrchestrationEventType.DelegationResultSelected:
16
+ return createEventData(input);
17
+ case OrchestrationEventType.DelegationWaitStarted:
18
+ return createEventData(input);
19
+ case OrchestrationEventType.DelegationJoined:
20
+ return createEventData(input);
7
21
  case OrchestrationEventType.WorkflowDefinitionRegistered:
8
22
  return createEventData(input);
9
23
  case OrchestrationEventType.InstanceStarted:
@@ -24,6 +38,8 @@ export function createOrchestrationEventData(input) {
24
38
  return createEventData(input);
25
39
  case OrchestrationEventType.SignalWaitStarted:
26
40
  return createEventData(input);
41
+ case OrchestrationEventType.SignalDeferred:
42
+ return createEventData(input);
27
43
  case OrchestrationEventType.SignalAccepted:
28
44
  return createEventData(input);
29
45
  case OrchestrationEventType.SupplementalActivityQueued:
@@ -3,9 +3,17 @@ import type { ActivationId, ActivityName, ActivityOutcome, ActivityOutcomeKind }
3
3
  import type { WorkItemId } from '../../work/index.js';
4
4
  import type { OrchestrationWaitingActivityOutcome } from './activity-outcome.js';
5
5
  import type { ApprovalAuthority, CompiledResourceTransition, CompiledWorkflow, StageConfig, TransitionTarget } from './config.js';
6
+ import type { DelegationReservation, DelegationResult } from './delegation.js';
6
7
  import type { CommandName, OrchestrationGroupId, SignalName, StageName, WorkflowInstanceId, WorkflowName } from './identifiers.js';
7
8
  import type { ChildOrchestrationGroupStreamId, ChildOrchestrationGroupStreamRef, PrimaryOrchestrationGroupStreamRef, WorkflowDefinitionsStreamRef, WorkflowInstanceStreamRef } from './streams.js';
8
9
  export declare const OrchestrationEventType: {
10
+ readonly DelegationReserved: "orchestration.delegation-reserved";
11
+ readonly DelegationRegistered: "orchestration.delegation-registered";
12
+ readonly DelegationResultSelected: "orchestration.delegation-result-selected";
13
+ readonly DelegationWaitStarted: "orchestration.delegation-wait-started";
14
+ readonly DelegationJoined: "orchestration.delegation-joined";
15
+ readonly DelegationFailed: "orchestration.delegation-failed";
16
+ readonly DelegationReconciliationFailed: "orchestration.delegation-reconciliation-failed";
9
17
  readonly WorkflowDefinitionRegistered: "orchestration.workflow-definition-registered";
10
18
  readonly InstanceStarted: "orchestration.instance-started";
11
19
  readonly StageEntered: "orchestration.stage-entered";
@@ -17,6 +25,7 @@ export declare const OrchestrationEventType: {
17
25
  readonly ActivityWaiting: "orchestration.activity-waiting";
18
26
  readonly SignalWaitStarted: "orchestration.signal-wait-started";
19
27
  readonly SignalAccepted: "orchestration.signal-accepted";
28
+ readonly SignalDeferred: "orchestration.signal-deferred";
20
29
  readonly SupplementalActivityQueued: "orchestration.supplemental-activity-queued";
21
30
  readonly SupplementalActivityDequeued: "orchestration.supplemental-activity-dequeued";
22
31
  readonly RepeatCounted: "orchestration.repeat-counted";
@@ -35,7 +44,37 @@ export declare const OrchestrationEventType: {
35
44
  readonly GroupClaimed: "orchestration.group-claimed";
36
45
  readonly GroupBudgetGranted: "orchestration.group-budget-granted";
37
46
  };
47
+ export interface DeferredWorkflowSignal {
48
+ readonly stage: StageName;
49
+ readonly intentEventId?: string;
50
+ readonly resourceId?: string;
51
+ readonly revision?: string;
52
+ readonly signal: OrchestrationSignal;
53
+ readonly target?: TransitionTarget;
54
+ }
38
55
  export interface OrchestrationEventPayloads {
56
+ readonly [OrchestrationEventType.SignalDeferred]: DeferredWorkflowSignal;
57
+ readonly [OrchestrationEventType.DelegationFailed]: {
58
+ readonly cohortId: string;
59
+ readonly reason: string;
60
+ };
61
+ readonly [OrchestrationEventType.DelegationReconciliationFailed]: {
62
+ readonly cohortId: string;
63
+ readonly reason: string;
64
+ };
65
+ readonly [OrchestrationEventType.DelegationReserved]: DelegationReservation;
66
+ readonly [OrchestrationEventType.DelegationRegistered]: {
67
+ readonly cohortId: string;
68
+ };
69
+ readonly [OrchestrationEventType.DelegationResultSelected]: DelegationResult;
70
+ readonly [OrchestrationEventType.DelegationWaitStarted]: {
71
+ readonly cohortId: string;
72
+ readonly activationId: ActivationId;
73
+ };
74
+ readonly [OrchestrationEventType.DelegationJoined]: {
75
+ readonly cohortId: string;
76
+ readonly results: readonly DelegationResult[];
77
+ };
39
78
  readonly [OrchestrationEventType.InstanceStarted]: {
40
79
  readonly workItemId: WorkItemId;
41
80
  readonly workflowName: WorkflowName;
@@ -1,5 +1,12 @@
1
1
  import { signalName } from './identifiers.js';
2
2
  export const OrchestrationEventType = {
3
+ DelegationReserved: 'orchestration.delegation-reserved',
4
+ DelegationRegistered: 'orchestration.delegation-registered',
5
+ DelegationResultSelected: 'orchestration.delegation-result-selected',
6
+ DelegationWaitStarted: 'orchestration.delegation-wait-started',
7
+ DelegationJoined: 'orchestration.delegation-joined',
8
+ DelegationFailed: 'orchestration.delegation-failed',
9
+ DelegationReconciliationFailed: 'orchestration.delegation-reconciliation-failed',
3
10
  WorkflowDefinitionRegistered: 'orchestration.workflow-definition-registered',
4
11
  InstanceStarted: 'orchestration.instance-started',
5
12
  StageEntered: 'orchestration.stage-entered',
@@ -11,6 +18,7 @@ export const OrchestrationEventType = {
11
18
  ActivityWaiting: 'orchestration.activity-waiting',
12
19
  SignalWaitStarted: 'orchestration.signal-wait-started',
13
20
  SignalAccepted: 'orchestration.signal-accepted',
21
+ SignalDeferred: 'orchestration.signal-deferred',
14
22
  SupplementalActivityQueued: 'orchestration.supplemental-activity-queued',
15
23
  SupplementalActivityDequeued: 'orchestration.supplemental-activity-dequeued',
16
24
  RepeatCounted: 'orchestration.repeat-counted',
@@ -50,6 +50,14 @@ export interface WorkflowInstanceView {
50
50
  readonly triggerId?: string;
51
51
  readonly causalCycleId?: string;
52
52
  readonly requestId?: string;
53
+ readonly delegationFence?: {
54
+ readonly cohortId: string;
55
+ readonly activationId: ActivationId;
56
+ };
57
+ readonly delegationWait?: {
58
+ readonly cohortId: string;
59
+ readonly activationId: ActivationId;
60
+ };
53
61
  readonly status: WorkflowStatus;
54
62
  readonly blockReason?: string;
55
63
  readonly currentStage: StageName;
@@ -0,0 +1,3 @@
1
+ import type { WorkflowInstanceView } from '../contracts/views.js';
2
+ /** Configured terminal completion may follow a custom outcome or a satisfied signal gate. */
3
+ export declare function isSuccessfulWorkflowCompletion(view: WorkflowInstanceView): boolean;
@@ -0,0 +1,9 @@
1
+ import { ActivityOutcomeKind } from '../../activities/index.js';
2
+ import { WorkflowStatus } from '../contracts/vocabulary.js';
3
+ /** Configured terminal completion may follow a custom outcome or a satisfied signal gate. */
4
+ export function isSuccessfulWorkflowCompletion(view) {
5
+ return (view.status === WorkflowStatus.Completed &&
6
+ view.lastOutcome?.kind !== ActivityOutcomeKind.Failed &&
7
+ view.lastOutcome?.kind !== ActivityOutcomeKind.Blocked &&
8
+ view.lastOutcome?.kind !== ActivityOutcomeKind.Rejected);
9
+ }
@@ -22,6 +22,8 @@ export type MutableWorkflowInstance = {
22
22
  triggerId?: string;
23
23
  causalCycleId?: string;
24
24
  requestId?: string;
25
+ delegationFence?: WorkflowInstanceView['delegationFence'];
26
+ delegationWait?: WorkflowInstanceView['delegationWait'];
25
27
  status: WorkflowInstanceView['status'];
26
28
  blockReason?: string;
27
29
  currentStage: WorkflowInstanceView['currentStage'];
@@ -11,6 +11,7 @@ import { ActivityActivationStatus, WorkflowStatus } from '../contracts/vocabular
11
11
  */
12
12
  export const orchestrationStatusTransitions = {
13
13
  [OrchestrationEventType.ActivityRequested]: WorkflowStatus.Active,
14
+ [OrchestrationEventType.DelegationWaitStarted]: WorkflowStatus.Waiting,
14
15
  [OrchestrationEventType.ActivityWaiting]: WorkflowStatus.Waiting,
15
16
  [OrchestrationEventType.SignalWaitStarted]: WorkflowStatus.Waiting,
16
17
  [OrchestrationEventType.SignalAccepted]: WorkflowStatus.Active,
@@ -127,11 +128,13 @@ function applyLifecycleFact(state, event) {
127
128
  state.retryCounts[event.payload.retryKey] = event.payload.count;
128
129
  return;
129
130
  case OrchestrationEventType.InstanceCompleted:
131
+ delete state.delegationWait;
130
132
  state.status = orchestrationStatusTransitions[OrchestrationEventType.InstanceCompleted];
131
133
  delete state.pendingActivation;
132
134
  delete state.waitingFor;
133
135
  return;
134
136
  case OrchestrationEventType.InstanceBlocked:
137
+ delete state.delegationWait;
135
138
  state.status = orchestrationStatusTransitions[OrchestrationEventType.InstanceBlocked];
136
139
  state.blockReason = event.payload.reason;
137
140
  return;
@@ -139,19 +142,49 @@ function applyLifecycleFact(state, event) {
139
142
  state.operatorRetryCommandIds.push(event.payload.commandId);
140
143
  return;
141
144
  case OrchestrationEventType.InstanceSuperseded:
145
+ delete state.delegationWait;
142
146
  state.status = orchestrationStatusTransitions[OrchestrationEventType.InstanceSuperseded];
143
147
  return;
144
148
  default:
145
149
  assertNever(event);
146
150
  }
147
151
  }
152
+ function applyDelegationFact(state, event) {
153
+ if (event.eventType === OrchestrationEventType.DelegationJoined) {
154
+ delete state.delegationWait;
155
+ delete state.delegationFence;
156
+ return;
157
+ }
158
+ state.delegationWait = event.payload;
159
+ state.status = WorkflowStatus.Waiting;
160
+ }
161
+ // Exhaustive dispatch over the closed coordination event set.
162
+ // eslint-disable-next-line complexity
148
163
  function applyCoordinationFact(state, event) {
149
164
  switch (event.eventType) {
150
165
  case OrchestrationEventType.InstanceStarted:
151
166
  case OrchestrationEventType.ChildRequested:
152
167
  case OrchestrationEventType.ChildStarted:
153
168
  case OrchestrationEventType.GroupBudgetExhausted:
169
+ case OrchestrationEventType.DelegationFailed:
170
+ delete state.delegationFence;
171
+ return;
172
+ case OrchestrationEventType.DelegationReconciliationFailed:
173
+ case OrchestrationEventType.SignalDeferred:
174
+ return;
175
+ case OrchestrationEventType.DelegationReserved:
176
+ if (event.payload.parentWorkflowInstanceId === state.workflowInstanceId)
177
+ state.delegationFence = {
178
+ cohortId: event.payload.cohortId,
179
+ activationId: event.payload.activationId,
180
+ };
181
+ return;
182
+ case OrchestrationEventType.DelegationRegistered:
183
+ case OrchestrationEventType.DelegationResultSelected:
154
184
  return;
185
+ case OrchestrationEventType.DelegationWaitStarted:
186
+ case OrchestrationEventType.DelegationJoined:
187
+ return applyDelegationFact(state, event);
155
188
  case OrchestrationEventType.ChildCompleted:
156
189
  state.childCompletionRecorded = true;
157
190
  return;
@@ -217,6 +250,8 @@ function applyActivityRequested(state, payload) {
217
250
  ...(payload.watchDriven === true ? { watchDriven: true } : {}),
218
251
  };
219
252
  }
253
+ // One optional-field projection per view property; branching mirrors the view shape.
254
+ // eslint-disable-next-line complexity
220
255
  export function immutableWorkflowInstanceView(state) {
221
256
  return {
222
257
  workflowInstanceId: state.workflowInstanceId,
@@ -233,6 +268,8 @@ export function immutableWorkflowInstanceView(state) {
233
268
  ...(state.triggerId === undefined ? {} : { triggerId: state.triggerId }),
234
269
  ...(state.causalCycleId === undefined ? {} : { causalCycleId: state.causalCycleId }),
235
270
  ...(state.requestId === undefined ? {} : { requestId: state.requestId }),
271
+ ...(state.delegationFence === undefined ? {} : { delegationFence: state.delegationFence }),
272
+ ...(state.delegationWait === undefined ? {} : { delegationWait: state.delegationWait }),
236
273
  status: state.status,
237
274
  ...(state.blockReason === undefined ? {} : { blockReason: state.blockReason }),
238
275
  currentStage: state.currentStage,
@@ -32,3 +32,5 @@ export * from './domain/workflow-instance.js';
32
32
  export * from './domain/workflow-selector.js';
33
33
  export * from './application/work-completion-summary.js';
34
34
  export type { WorkflowCompileDiagnostic } from './domain/compile-diagnostics.js';
35
+ export type { DelegationArtifacts } from './contracts/delegation-ports.js';
36
+ export { DelegationPendingError } from './contracts/delegation.js';
@@ -31,3 +31,4 @@ export * from './domain/interpreter.js';
31
31
  export * from './domain/workflow-instance.js';
32
32
  export * from './domain/workflow-selector.js';
33
33
  export * from './application/work-completion-summary.js';
34
+ export { DelegationPendingError } from './contracts/delegation.js';
@@ -15,6 +15,9 @@ export const workProjection = {
15
15
  return {
16
16
  workItemId: owned.stream.id,
17
17
  objective: owned.event.payload.objective,
18
+ ...(owned.event.payload.delegation === undefined
19
+ ? {}
20
+ : { delegation: owned.event.payload.delegation }),
18
21
  state: WorkStatus.Open,
19
22
  tags: owned.event.payload.tags ?? [],
20
23
  autoApprovalGranted: false,
@@ -4,16 +4,32 @@ import { createWorkEventData } from '../contracts/event-factory.js';
4
4
  import { WorkEventType, } from '../contracts/events.js';
5
5
  import { WorkStatus } from '../contracts/vocabulary.js';
6
6
  import { WorkRepository } from './work-repository.js';
7
+ /** Delegated ancestry is derived from the validated parent, never trusted from the command. */
8
+ async function assertDelegationAncestry(repository, command) {
9
+ const delegation = command.delegation;
10
+ if (delegation === undefined)
11
+ return;
12
+ const parent = (await repository.load(delegation.parentWorkItemId)).view;
13
+ if (parent === null)
14
+ throw new Error('Delegation parent does not exist');
15
+ const ancestors = [...(parent.delegation?.ancestors ?? []), parent.workItemId];
16
+ if (ancestors.includes(command.workItemId) ||
17
+ !isDeepStrictEqual(ancestors, delegation.ancestors) ||
18
+ delegation.rootWorkItemId !== (parent.delegation?.rootWorkItemId ?? parent.workItemId))
19
+ throw new Error('Invalid delegation ancestry');
20
+ }
7
21
  export function createWorkService(journal) {
8
22
  const repository = new WorkRepository(journal);
9
23
  const change = (workItemId, context, draft, allowMissing, requireOpen) => changeWorkItem(repository, workItemId, context, draft, allowMissing, requireOpen);
10
24
  return {
11
25
  async create(command, context) {
12
26
  const loaded = await repository.load(command.workItemId);
27
+ await assertDelegationAncestry(repository, command);
13
28
  const draft = workDraft(context, {
14
29
  eventType: WorkEventType.ItemCreated,
15
30
  payload: {
16
31
  objective: command.objective,
32
+ ...(command.delegation === undefined ? {} : { delegation: command.delegation }),
17
33
  ...(command.tags === undefined ? {} : { tags: command.tags }),
18
34
  },
19
35
  });
@@ -2,6 +2,7 @@ import type { WorkItemId } from './identifiers.js';
2
2
  export interface CreateWorkItem {
3
3
  readonly workItemId: WorkItemId;
4
4
  readonly objective: string;
5
+ readonly delegation?: WorkDelegation;
5
6
  readonly tags?: readonly string[];
6
7
  }
7
8
  export interface ReviseWorkObjective {
@@ -13,3 +14,8 @@ export interface LinkWorkItems {
13
14
  readonly to: WorkItemId;
14
15
  readonly relation: 'relates-to' | 'parent-of' | 'child-of';
15
16
  }
17
+ export interface WorkDelegation {
18
+ readonly parentWorkItemId: WorkItemId;
19
+ readonly rootWorkItemId: WorkItemId;
20
+ readonly ancestors: readonly WorkItemId[];
21
+ }
@@ -1,5 +1,5 @@
1
1
  import { type EventDataUnion, type EventEnvelope, type EventUnion } from '@atolis-hq/eventing';
2
- import type { LinkWorkItems } from './commands.js';
2
+ import type { LinkWorkItems, WorkDelegation } from './commands.js';
3
3
  import { type WorkItemId } from './identifiers.js';
4
4
  import { type WorkItemStreamRef } from './streams.js';
5
5
  export declare const WorkEventType: {
@@ -20,6 +20,7 @@ export interface WorkItemLinkedPayload {
20
20
  }
21
21
  export interface WorkItemCreatedPayload {
22
22
  readonly objective: string;
23
+ readonly delegation?: WorkDelegation | undefined;
23
24
  readonly tags?: readonly string[] | undefined;
24
25
  }
25
26
  export interface WorkEventPayloads {
@@ -32,6 +32,14 @@ const reasonSchema = z.object({ reason: z.string() }).strict();
32
32
  const consentSchema = z.object({}).strict();
33
33
  const itemCreatedSchema = objectiveSchema.extend({
34
34
  tags: z.array(z.string().trim().min(1)).readonly().optional(),
35
+ delegation: z
36
+ .object({
37
+ parentWorkItemId: brandedStringSchema(workItemId),
38
+ rootWorkItemId: brandedStringSchema(workItemId),
39
+ ancestors: z.array(brandedStringSchema(workItemId)).min(1).readonly(),
40
+ })
41
+ .strict()
42
+ .optional(),
35
43
  });
36
44
  const eventSchema = eventEnvelopeSchema.extend({
37
45
  event: z.discriminatedUnion('eventType', [
@@ -1,4 +1,4 @@
1
- import type { LinkWorkItems } from './commands.js';
1
+ import type { LinkWorkItems, WorkDelegation } from './commands.js';
2
2
  import type { WorkItemId } from './identifiers.js';
3
3
  import type { WorkStatus } from './vocabulary.js';
4
4
  export type WorkState = typeof WorkStatus.Open | typeof WorkStatus.Closed | typeof WorkStatus.Cancelled;
@@ -9,6 +9,7 @@ export interface RelatedWorkItemView {
9
9
  export interface WorkItemView {
10
10
  readonly workItemId: WorkItemId;
11
11
  readonly objective: string;
12
+ readonly delegation?: WorkDelegation;
12
13
  readonly state: WorkState;
13
14
  readonly tags: readonly string[];
14
15
  readonly autoApprovalGranted: boolean;
@@ -22,6 +22,9 @@ export function foldWorkItem(events) {
22
22
  return {
23
23
  workItemId: id,
24
24
  objective: state.objective,
25
+ ...(events[0].event.payload.delegation === undefined
26
+ ? {}
27
+ : { delegation: events[0].event.payload.delegation }),
25
28
  state: state.lifecycle,
26
29
  tags: state.tags,
27
30
  autoApprovalGranted: state.autoApprovalGranted,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/wake",
3
- "version": "0.4.94",
3
+ "version": "0.4.96",
4
4
  "description": "Local autonomous agent control plane for software development",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {