@atolis-hq/wake 0.4.101 → 0.4.103

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/activities/pr/policy.d.ts +2 -0
  2. package/dist/src/activities/pr/policy.js +9 -1
  3. package/dist/src/artifacts/application/operator-instructions-service.js +2 -2
  4. package/dist/src/bootstrap/artifact-mcp.js +3 -4
  5. package/dist/src/bootstrap/delegation-authority.js +1 -1
  6. package/dist/src/bootstrap/integration-runtime.js +1 -1
  7. package/dist/src/bootstrap/persistence-composition.js +1 -1
  8. package/dist/src/bootstrap/surface-api-delivery.d.ts +1 -1
  9. package/dist/src/bootstrap/surface-api-delivery.js +8 -4
  10. package/dist/src/bootstrap/version.js +1 -1
  11. package/dist/src/control-plane/application/activation-scheduler-ports.d.ts +2 -1
  12. package/dist/src/control-plane/application/activation-scheduler.js +2 -1
  13. package/dist/src/control-plane/application/advance-once-dispatch.d.ts +2 -4
  14. package/dist/src/control-plane/application/advance-once-dispatch.js +6 -4
  15. package/dist/src/control-plane/domain/delivery-capacity.d.ts +2 -5
  16. package/dist/src/control-plane/domain/delivery-capacity.js +3 -4
  17. package/dist/src/control-plane/domain/open-work-policy.d.ts +2 -10
  18. package/dist/src/control-plane/domain/open-work-policy.js +2 -3
  19. package/dist/src/execution/application/run-repository.d.ts +2 -2
  20. package/dist/src/execution/application/run-repository.js +3 -3
  21. package/dist/src/integrations/application/delegation-outputs.js +41 -5
  22. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.d.ts +9 -1
  23. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.d.ts.map +1 -1
  24. package/dist/src/node_modules/@atolis-hq/eventing/dist/runtime/cached-journal-view.js +62 -7
  25. package/dist/src/node_modules/@atolis-hq/eventing/dist/store/event-journal.d.ts +2 -5
  26. package/dist/src/node_modules/@atolis-hq/eventing/dist/store/event-journal.d.ts.map +1 -1
  27. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.d.ts +12 -13
  28. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.d.ts.map +1 -1
  29. package/dist/src/node_modules/@atolis-hq/eventing-filesystem/dist/file-event-journal.js +168 -109
  30. package/dist/src/orchestration/application/deferred-signals.d.ts +4 -2
  31. package/dist/src/orchestration/application/deferred-signals.js +47 -37
  32. package/dist/src/orchestration/application/delegated-work.d.ts +48 -2
  33. package/dist/src/orchestration/application/delegated-work.js +145 -27
  34. package/dist/src/orchestration/application/delegation-commands.d.ts +48 -1
  35. package/dist/src/orchestration/application/delegation-commands.js +5 -4
  36. package/dist/src/orchestration/application/delegation-gate.d.ts +16 -7
  37. package/dist/src/orchestration/application/delegation-gate.js +62 -14
  38. package/dist/src/orchestration/application/delegation-ledger.d.ts +2 -0
  39. package/dist/src/orchestration/application/delegation-ledger.js +8 -0
  40. package/dist/src/orchestration/application/delegation-prerequisites.d.ts +28 -0
  41. package/dist/src/orchestration/application/delegation-prerequisites.js +118 -0
  42. package/dist/src/orchestration/application/delegation-reservation.js +1 -1
  43. package/dist/src/orchestration/application/delegation-wait-graph.d.ts +7 -0
  44. package/dist/src/orchestration/application/delegation-wait-graph.js +122 -0
  45. package/dist/src/orchestration/application/orchestration-repository.d.ts +5 -0
  46. package/dist/src/orchestration/application/orchestration-repository.js +3 -0
  47. package/dist/src/orchestration/application/orchestration-service.d.ts +87 -0
  48. package/dist/src/orchestration/application/orchestration-service.js +13 -7
  49. package/dist/src/orchestration/application/prerequisite-readiness.d.ts +68 -0
  50. package/dist/src/orchestration/application/prerequisite-readiness.js +217 -0
  51. package/dist/src/orchestration/contracts/delegation-ports.d.ts +2 -1
  52. package/dist/src/orchestration/contracts/delegation.d.ts +88 -0
  53. package/dist/src/orchestration/contracts/delegation.js +36 -1
  54. package/dist/src/orchestration/contracts/event-decoder.js +8 -1
  55. package/dist/src/orchestration/contracts/event-factory.js +5 -4
  56. package/dist/src/orchestration/contracts/events.d.ts +13 -1
  57. package/dist/src/orchestration/contracts/events.js +3 -0
  58. package/dist/src/orchestration/contracts/vocabulary.d.ts +27 -0
  59. package/dist/src/orchestration/contracts/vocabulary.js +25 -0
  60. package/dist/src/orchestration/domain/workflow-instance-events.js +3 -0
  61. package/dist/src/orchestration/index.d.ts +2 -2
  62. package/dist/src/orchestration/index.js +1 -1
  63. package/dist/src/surfaces/mcp/delegation.js +18 -10
  64. package/dist/src/work/application/work-index.d.ts +21 -0
  65. package/dist/src/work/application/work-index.js +73 -0
  66. package/dist/src/work/application/work-repository.d.ts +2 -2
  67. package/dist/src/work/application/work-repository.js +2 -2
  68. package/dist/src/work/application/work-service.d.ts +5 -2
  69. package/dist/src/work/application/work-service.js +5 -2
  70. package/dist/src/work/{application → domain}/delivery-availability.d.ts +4 -7
  71. package/dist/src/work/{application → domain}/delivery-availability.js +3 -3
  72. package/dist/src/work/index.d.ts +2 -1
  73. package/dist/src/work/index.js +2 -1
  74. package/package.json +2 -1
@@ -1,19 +1,29 @@
1
- import { readDeliveryAvailability } from '../../work/index.js';
2
1
  import { DelegationPendingError } from '../contracts/delegation.js';
3
2
  /** One admission policy for ordinary advancement while a committed cohort owns the parent. */
4
3
  export class DelegationGate {
5
4
  repository;
6
5
  work;
7
- constructor(repository, work) {
6
+ prerequisiteReasons;
7
+ constructor(repository, work, prerequisiteReasons) {
8
8
  this.repository = repository;
9
9
  this.work = work;
10
+ this.prerequisiteReasons = prerequisiteReasons;
10
11
  }
11
- async unavailable(view) {
12
- // Standalone work keeps existing freeze semantics; only delegated trees and fenced parents are held.
13
- const item = await this.work.get(view.workItemId, { fresh: true });
14
- if (item?.delegation === undefined && view.delegationFence === undefined)
12
+ /** Standalone work keeps existing freeze semantics; only delegated trees and fenced parents are held. */
13
+ heldBy(snapshot, view) {
14
+ if (snapshot.invalid(view.workItemId))
15
+ return true;
16
+ if (snapshot.get(view.workItemId)?.delegation === undefined &&
17
+ view.delegationFence === undefined)
15
18
  return false;
16
- return (await readDeliveryAvailability(view.workItemId, this.work)).reason !== undefined;
19
+ return snapshot.availability(view.workItemId).reason !== undefined;
20
+ }
21
+ /** Commit-boundary check for one instance; batches use `filterAvailable`. */
22
+ async unavailable(view) {
23
+ const snapshot = await this.work.deliverySnapshot();
24
+ return (this.heldBy(snapshot, view) ||
25
+ (await this.prerequisiteReasons?.([view.workItemId], snapshot))?.get(view.workItemId)
26
+ ?.reason !== undefined);
17
27
  }
18
28
  allows(view) {
19
29
  return view.delegationFence === undefined;
@@ -22,24 +32,62 @@ export class DelegationGate {
22
32
  const view = (await this.repository.load(id)).view;
23
33
  return view !== null && (!this.allows(view) || (await this.unavailable(view)));
24
34
  }
35
+ /** The subset of `ids` whose workflow is fenced or held, against one Work synchronisation. */
36
+ async fencedAmong(ids) {
37
+ if (ids.length === 0)
38
+ return new Set();
39
+ const requested = new Set(ids);
40
+ const views = (await this.repository.list()).flatMap(({ view }) => view !== null && requested.has(view.workflowInstanceId)
41
+ ? [[view.workflowInstanceId, view]]
42
+ : []);
43
+ const snapshot = views.some(([, view]) => this.allows(view))
44
+ ? await this.work.deliverySnapshot()
45
+ : undefined;
46
+ const reasons = await this.prerequisiteReasons?.(views.filter(([, view]) => this.allows(view)).map(([, view]) => view.workItemId), snapshot);
47
+ return new Set(views
48
+ .filter(([, view]) => !this.allows(view) ||
49
+ this.heldBy(snapshot, view) ||
50
+ reasons?.get(view.workItemId)?.reason !== undefined)
51
+ .map(([id]) => id));
52
+ }
25
53
  async run(id, operation) {
26
54
  if (await this.fenced(id))
27
55
  throw new DelegationPendingError(id);
28
56
  return operation();
29
57
  }
30
- async outcome(id, activationId, operation, held) {
58
+ async outcome(id, activationId, operation, held, historical) {
31
59
  const view = (await this.repository.load(id)).view;
32
- if (view !== null && (await this.unavailable(view)))
60
+ if (view === null)
61
+ return operation();
62
+ const snapshot = await this.work.deliverySnapshot();
63
+ if (this.heldBy(snapshot, view))
64
+ throw new DelegationPendingError(id);
65
+ const admission = (await this.prerequisiteReasons?.([view.workItemId]))?.get(view.workItemId);
66
+ if (admission?.abandoned === true)
67
+ return historical(view);
68
+ if (admission?.reason !== undefined)
33
69
  throw new DelegationPendingError(id);
34
- if (view === null || this.allows(view))
70
+ if (this.allows(view))
35
71
  return operation();
36
72
  if (view.delegationFence?.activationId !== activationId)
37
73
  throw new DelegationPendingError(id);
38
74
  return held();
39
75
  }
40
- async availableInstances() {
41
- const entries = await this.repository.list();
42
- const available = await Promise.all(entries.map(async ({ view }) => view !== null && this.allows(view) && !(await this.unavailable(view))));
43
- return entries.filter((_, index) => available[index]);
76
+ /**
77
+ * Admits the items whose workflow may advance, against one synchronisation
78
+ * with Work however many items there are. Nothing is read when none are
79
+ * unfenced, so a quiet tick costs no storage round trip.
80
+ */
81
+ async filterAvailable(items, viewOf) {
82
+ const unfenced = items.filter((item) => {
83
+ const view = viewOf(item);
84
+ return view !== null && this.allows(view);
85
+ });
86
+ if (unfenced.length === 0)
87
+ return [];
88
+ const snapshot = await this.work.deliverySnapshot();
89
+ const reasons = await this.prerequisiteReasons?.(unfenced.map((item) => viewOf(item).workItemId), snapshot);
90
+ return unfenced.filter((item) => !this.heldBy(snapshot, viewOf(item)) &&
91
+ reasons?.get(viewOf(item).workItemId)?.reason === undefined);
44
92
  }
45
93
  }
@@ -25,4 +25,6 @@ export declare function isJoined(events: readonly WorkflowOrchestrationEvent[],
25
25
  export declare function isSettled(events: readonly WorkflowOrchestrationEvent[], reservation: DelegationReservation): boolean;
26
26
  export declare function selectedResults(events: readonly WorkflowOrchestrationEvent[], reservation: DelegationReservation): readonly DelegationResult[];
27
27
  export declare function reservedEvent(events: readonly WorkflowOrchestrationEvent[], reservation: DelegationReservation): WorkflowOrchestrationEvent;
28
+ /** Terminal membership is indexed once for a reconciliation pass, independent of history size. */
29
+ export declare function unsettledReservations(events: readonly WorkflowOrchestrationEvent[]): readonly DelegationReservation[];
28
30
  export {};
@@ -77,3 +77,11 @@ export function reservedEvent(events, reservation) {
77
77
  throw new Error('Missing delegation reservation');
78
78
  return found;
79
79
  }
80
+ /** Terminal membership is indexed once for a reconciliation pass, independent of history size. */
81
+ export function unsettledReservations(events) {
82
+ const settled = new Set(events.flatMap(({ event, stream }) => event.eventType === OrchestrationEventType.DelegationJoined ||
83
+ event.eventType === OrchestrationEventType.DelegationFailed
84
+ ? [JSON.stringify([stream.id, event.payload.cohortId])]
85
+ : []));
86
+ return reservationsOf(events).filter((reservation) => !settled.has(JSON.stringify([reservation.parentWorkflowInstanceId, reservation.cohortId])));
87
+ }
@@ -0,0 +1,28 @@
1
+ import type { CommandContext } from '@atolis-hq/eventing';
2
+ import type { DeliverySnapshot, WorkService } from '../../work/index.js';
3
+ import type { DelegationArtifacts, DelegationRepositories } from '../contracts/delegation-ports.js';
4
+ import type { DelegationInputs, DelegationReservation } from '../contracts/delegation.js';
5
+ import type { DelegationLedger } from './delegation-ledger.js';
6
+ import type { OrchestrationRepository } from './orchestration-repository.js';
7
+ import { PrerequisiteReadiness, type PrerequisiteAdmission } from './prerequisite-readiness.js';
8
+ type Child = DelegationReservation['children'][number];
9
+ /** Immutable consumption receipts and live eligibility; never chooses a repair. */
10
+ export declare class DelegationPrerequisites {
11
+ private readonly repository;
12
+ private readonly ledger;
13
+ private readonly artifacts;
14
+ private readonly repositories;
15
+ private readonly work;
16
+ constructor(repository: OrchestrationRepository, ledger: DelegationLedger, artifacts: DelegationArtifacts, repositories: DelegationRepositories | undefined, work: WorkService);
17
+ inputs(id: string): Promise<DelegationInputs | undefined>;
18
+ read(workSnapshot?: DeliverySnapshot): Promise<PrerequisiteReadiness>;
19
+ reasons(ids: readonly string[], workSnapshot?: DeliverySnapshot): Promise<ReadonlyMap<string, PrerequisiteAdmission>>;
20
+ reason(id: string): Promise<string | undefined>;
21
+ prepare(reservation: DelegationReservation, child: Child, context: CommandContext, readiness?: PrerequisiteReadiness): Promise<boolean>;
22
+ private resolveBase;
23
+ private grant;
24
+ check(reservation: DelegationReservation, child: Child, context: CommandContext, readiness?: PrerequisiteReadiness): Promise<boolean>;
25
+ private block;
26
+ private diagnostic;
27
+ }
28
+ export {};
@@ -0,0 +1,118 @@
1
+ import { OrchestrationEventType } from '../contracts/events.js';
2
+ import { PrerequisiteBlockCode } from '../contracts/vocabulary.js';
3
+ import { PrerequisiteReadiness } from './prerequisite-readiness.js';
4
+ /** Immutable consumption receipts and live eligibility; never chooses a repair. */
5
+ export class DelegationPrerequisites {
6
+ repository;
7
+ ledger;
8
+ artifacts;
9
+ repositories;
10
+ work;
11
+ constructor(repository, ledger, artifacts, repositories, work) {
12
+ this.repository = repository;
13
+ this.ledger = ledger;
14
+ this.artifacts = artifacts;
15
+ this.repositories = repositories;
16
+ this.work = work;
17
+ }
18
+ async inputs(id) {
19
+ return (await this.ledger.events()).flatMap(({ event }) => event.eventType === OrchestrationEventType.DelegationInputsResolved &&
20
+ event.payload.childWorkItemId === id
21
+ ? [event.payload]
22
+ : [])[0];
23
+ }
24
+ async read(workSnapshot) {
25
+ const snapshot = await this.repository.snapshot();
26
+ return new PrerequisiteReadiness(snapshot, workSnapshot ?? (await this.work.deliverySnapshot()), this.artifacts, this.repositories);
27
+ }
28
+ async reasons(ids, workSnapshot) {
29
+ if (ids.length === 0)
30
+ return new Map();
31
+ const read = await this.read(workSnapshot);
32
+ return new Map(await Promise.all(ids.map(async (id) => [id, { reason: await read.reason(id), abandoned: read.abandoned(id) }])));
33
+ }
34
+ async reason(id) {
35
+ return (await this.read()).reason(id);
36
+ }
37
+ async prepare(reservation, child, context, readiness) {
38
+ if ((child.prerequisites?.length ?? 0) === 0)
39
+ return true;
40
+ const read = readiness ?? (await this.read());
41
+ const stored = read.inputs(child.workItemId);
42
+ if (stored !== undefined) {
43
+ await this.grant(reservation, child, stored, context);
44
+ return this.check(reservation, child, context, read);
45
+ }
46
+ const consumed = await read.select(child, new Set([child.workItemId]));
47
+ if (!Array.isArray(consumed)) {
48
+ await this.diagnostic(reservation, child, consumed.reason, context);
49
+ if (consumed.code === PrerequisiteBlockCode.Changed ||
50
+ consumed.code === PrerequisiteBlockCode.Failed)
51
+ await this.block(reservation, child, consumed.reason, context);
52
+ return false;
53
+ }
54
+ const repository = await this.resolveBase(reservation, child, consumed);
55
+ await this.ledger.appendOnce(reservation.parentWorkflowInstanceId, context, `inputs:${child.grantId}`, {
56
+ eventType: OrchestrationEventType.DelegationInputsResolved,
57
+ payload: {
58
+ childWorkItemId: child.workItemId,
59
+ consumed,
60
+ ...(repository === undefined ? {} : { repository }),
61
+ },
62
+ });
63
+ const pinned = await this.inputs(child.workItemId);
64
+ if (pinned === undefined)
65
+ throw new Error('Missing committed prerequisite inputs');
66
+ await this.grant(reservation, child, pinned, context);
67
+ return true;
68
+ }
69
+ async resolveBase(reservation, child, consumed) {
70
+ const index = child.prerequisites?.findIndex((edge) => edge.useAsBase) ?? -1;
71
+ if (index < 0)
72
+ return child.repository;
73
+ const reference = consumed[index].reference;
74
+ if (!('revision' in reference) ||
75
+ child.repository === undefined ||
76
+ this.repositories === undefined)
77
+ throw new Error('Upstream Git base resolver unavailable');
78
+ const parent = (await this.repository.loadRequired(reservation.parentWorkflowInstanceId)).view;
79
+ return this.repositories.resolve(parent, {
80
+ resourceId: child.repository.resourceId,
81
+ sourceRef: reference.branch,
82
+ sourceRevision: reference.revision,
83
+ pullRequestBase: reference.branch,
84
+ });
85
+ }
86
+ async grant(reservation, child, inputs, context) {
87
+ const references = inputs.consumed.flatMap(({ reference }) => 'revisionId' in reference ? [reference] : []);
88
+ await this.artifacts.registerChild({
89
+ delegationId: `${child.grantId}:prerequisites`,
90
+ parentWorkItemId: reservation.parentWorkItemId,
91
+ childWorkItemId: child.workItemId,
92
+ childReferences: references,
93
+ }, context);
94
+ }
95
+ async check(reservation, child, context, readiness) {
96
+ const read = readiness ?? (await this.read());
97
+ const blocked = await read.block(child.workItemId);
98
+ if (blocked === undefined)
99
+ return true;
100
+ await this.diagnostic(reservation, child, blocked.reason, context);
101
+ if (blocked.code === PrerequisiteBlockCode.Changed ||
102
+ blocked.code === PrerequisiteBlockCode.Failed)
103
+ await this.block(reservation, child, blocked.reason, context);
104
+ return false;
105
+ }
106
+ async block(reservation, child, reason, context) {
107
+ await this.ledger.appendOnce(reservation.parentWorkflowInstanceId, context, 'child-blocked:' + child.grantId, {
108
+ eventType: OrchestrationEventType.DelegationChildBlocked,
109
+ payload: { childWorkItemId: child.workItemId, reason },
110
+ });
111
+ }
112
+ async diagnostic(reservation, child, reason, context) {
113
+ await this.ledger.appendOnce(reservation.parentWorkflowInstanceId, context, `prerequisite:${child.grantId}:${PrerequisiteBlockCode.Blocked}`, {
114
+ eventType: OrchestrationEventType.DelegationPrerequisiteBlocked,
115
+ payload: { childWorkItemId: child.workItemId, reason },
116
+ });
117
+ }
118
+ }
@@ -53,7 +53,7 @@ export function matchesAssignments(reservation, assignments) {
53
53
  reservation.children.find((child) => child.assignmentId === assignment.assignmentId)
54
54
  ?.repository?.sourceRevision !== assignment.repository.sourceRevision))
55
55
  return false;
56
- return isDeepStrictEqual(reservation.children.map(({ workItemId: _w, workflowInstanceId: _i, grantId: _g, ...assignment }) => ({
56
+ return isDeepStrictEqual(reservation.children.map(({ workItemId: _w, workflowInstanceId: _i, grantId: _g, prerequisiteWorkItemIds: _p, ...assignment }) => ({
57
57
  ...assignment,
58
58
  ...(assignment.repository === undefined
59
59
  ? {}
@@ -0,0 +1,7 @@
1
+ import { type DelegationReservation } from '../contracts/delegation.js';
2
+ import { type DelegationLedger } from './delegation-ledger.js';
3
+ import type { WorkflowDefinitionRegistry } from './workflow-definition-registry.js';
4
+ type Child = DelegationReservation['children'][number];
5
+ /** Resolve local keys once under the root reservation's sequence guard. */
6
+ export declare function resolvePrerequisites(candidate: DelegationReservation, existing: readonly DelegationReservation[], ledger: DelegationLedger, definitions: WorkflowDefinitionRegistry, mergeAvailable: (target: Child, output: string) => Promise<boolean>): Promise<DelegationReservation>;
7
+ export {};
@@ -0,0 +1,122 @@
1
+ import { ActivityResourceRole, BuiltInActivityName, pullRequestMergeInputSchema, } from '../../activities/index.js';
2
+ import { DelegationConflictError, } from '../contracts/delegation.js';
3
+ import { DelegationConflictCode as Code, DelegationPrerequisiteCondition as Condition, } from '../contracts/vocabulary.js';
4
+ import { isSettled } from './delegation-ledger.js';
5
+ /** Resolve local keys once under the root reservation's sequence guard. */
6
+ export async function resolvePrerequisites(candidate, existing, ledger, definitions, mergeAvailable) {
7
+ const all = [...existing, candidate];
8
+ const resolved = {
9
+ ...candidate,
10
+ children: candidate.children.map((child) => resolveChild(candidate, child, all)),
11
+ };
12
+ const graph = new Map();
13
+ const add = (from, to) => graph.set(from, [...(graph.get(from) ?? []), to]);
14
+ const events = await ledger.events();
15
+ const reservations = [...existing, resolved];
16
+ for (const reservation of reservations) {
17
+ const unsettled = reservation === resolved || !isSettled(events, reservation);
18
+ for (const child of reservation.children) {
19
+ if (unsettled)
20
+ add(reservation.parentWorkItemId, child.workItemId);
21
+ for (const id of child.prerequisiteWorkItemIds ?? [])
22
+ add(child.workItemId, id);
23
+ const waits = await mergeWaits(child, reservations, definitions, mergeAvailable);
24
+ for (const [from, to] of waits)
25
+ if (unsettled)
26
+ add(from, to);
27
+ }
28
+ }
29
+ assertAcyclic(graph);
30
+ return resolved;
31
+ }
32
+ function resolveChild(candidate, child, all) {
33
+ if (child.prerequisites === undefined)
34
+ return child;
35
+ if (child.prerequisites.filter((edge) => edge.useAsBase).length > 1)
36
+ reject(Code.MultipleBases, 'Select at most one upstream Git base');
37
+ return {
38
+ ...child,
39
+ prerequisiteWorkItemIds: child.prerequisites.map((edge) => {
40
+ const cohort = all.find((entry) => entry.cohortId === (edge.cohortId ?? candidate.cohortId));
41
+ const target = cohort?.children.find((entry) => entry.assignmentId === edge.assignmentId);
42
+ if (target === undefined)
43
+ reject(Code.MissingTarget, 'Prerequisite target is outside this delivery or does not exist');
44
+ if (target.workItemId === child.workItemId)
45
+ reject(Code.SelfDependency, 'A child cannot depend on itself');
46
+ validateOutput(child, target, edge);
47
+ return target.workItemId;
48
+ }),
49
+ };
50
+ }
51
+ function validateOutput(child, target, edge) {
52
+ const output = target.outputs.find((entry) => entry.name === edge.output);
53
+ if (output === undefined)
54
+ reject(Code.MissingOutput, 'Prerequisite must select a declared named output');
55
+ if ((edge.condition === Condition.AcceptedOutput && !('path' in output)) ||
56
+ (requiresChange(edge) && !('kind' in output)))
57
+ reject(Code.OutputKind, 'Prerequisite condition does not match the named output kind');
58
+ if (edge.useAsBase)
59
+ validateBase(child, target, edge);
60
+ if (child.repository?.resourceId !== target.repository?.resourceId && 'kind' in output)
61
+ reject(Code.CrossRepositoryChange, 'Cross-repository prerequisites must select accepted context outputs');
62
+ }
63
+ function validateBase(child, target, edge) {
64
+ if (edge.condition === Condition.AcceptedOutput ||
65
+ child.repository === undefined ||
66
+ target.repository?.resourceId !== child.repository.resourceId)
67
+ reject(Code.CrossRepositoryBase, 'Git bases require a same-repository change prerequisite');
68
+ }
69
+ async function mergeWaits(child, all, definitions, available) {
70
+ const waits = [];
71
+ for (const [index, edge] of (child.prerequisites ?? []).entries()) {
72
+ if (edge.condition !== Condition.MergedChange)
73
+ continue;
74
+ const upstream = all.find((entry) => entry.children.some((target) => target.workItemId === child.prerequisiteWorkItemIds?.[index]));
75
+ const target = upstream?.children.find((entry) => entry.workItemId === child.prerequisiteWorkItemIds?.[index]);
76
+ if (upstream === undefined || target === undefined || (await available(target, edge.output)))
77
+ continue;
78
+ const workflow = await definitions.resolve({
79
+ workflowName: target.workflowName,
80
+ workflowDefinitionFingerprint: target.workflowFingerprint,
81
+ }, { journalFallback: true });
82
+ // A delivery-ready child's parent must join before it can merge the child's output.
83
+ if (!hasOwnMergeLane(workflow))
84
+ waits.push([target.workItemId, upstream.parentWorkItemId]);
85
+ }
86
+ return waits;
87
+ }
88
+ function assertAcyclic(graph) {
89
+ const visiting = new Set();
90
+ const done = new Set();
91
+ function visit(node) {
92
+ if (visiting.has(node))
93
+ reject(Code.WaitCycle, 'Prerequisites create an explicit or implicit join/merge wait cycle');
94
+ if (done.has(node))
95
+ return;
96
+ visiting.add(node);
97
+ for (const target of graph.get(node) ?? [])
98
+ visit(target);
99
+ visiting.delete(node);
100
+ done.add(node);
101
+ }
102
+ for (const node of graph.keys())
103
+ visit(node);
104
+ }
105
+ function reject(code, message) {
106
+ throw new DelegationConflictError(code, message);
107
+ }
108
+ function requiresChange(edge) {
109
+ return (edge.condition === Condition.PushedChange ||
110
+ edge.condition === Condition.MergedChange ||
111
+ edge.useAsBase === true);
112
+ }
113
+ function hasOwnMergeLane(workflow) {
114
+ return Object.values(workflow.stages).some((stage) => mergesPrimary(stage.activity, stage.with) ||
115
+ Object.values(stage.on).some((route) => route.activities?.some((activity) => mergesPrimary(activity.use, activity.with))));
116
+ }
117
+ function mergesPrimary(activity, input) {
118
+ if (activity !== BuiltInActivityName.PullRequestMerge)
119
+ return false;
120
+ const parsed = pullRequestMergeInputSchema.safeParse(input);
121
+ return parsed.success && parsed.data.target === ActivityResourceRole.Primary;
122
+ }
@@ -13,6 +13,11 @@ export declare class OrchestrationRepository {
13
13
  sequence: number;
14
14
  view: WorkflowInstanceView | null;
15
15
  }>;
16
+ snapshot(): Promise<{
17
+ instances: readonly ListedInstance[];
18
+ events: readonly WorkflowOrchestrationEvent[];
19
+ inherited: ReadonlyMap<string, readonly WorkflowOrchestrationEvent[]>;
20
+ }>;
16
21
  events(): Promise<readonly WorkflowOrchestrationEvent[]>;
17
22
  history(id: string): Promise<{
18
23
  sequence: number;
@@ -36,6 +36,9 @@ export class OrchestrationRepository {
36
36
  });
37
37
  return { sequence, view: foldWorkflowInstance([...own, ...inherited]) };
38
38
  }
39
+ snapshot() {
40
+ return this.listed.get();
41
+ }
39
42
  events() {
40
43
  return this.listed.get().then((snapshot) => snapshot.events);
41
44
  }
@@ -17,6 +17,7 @@ export declare class OrchestrationService {
17
17
  private readonly delegationCommands;
18
18
  private readonly delegationGate;
19
19
  private readonly delegatedWork;
20
+ private readonly prerequisiteReasons;
20
21
  private readonly repository;
21
22
  private readonly deferredSignals;
22
23
  private readonly startWorkflow;
@@ -73,13 +74,22 @@ export declare class OrchestrationService {
73
74
  workflowInstanceId: WorkflowInstanceId;
74
75
  grantId: string;
75
76
  repositoryResourceIds?: string[] | undefined;
77
+ prerequisites?: {
78
+ assignmentId: string;
79
+ output: string;
80
+ condition: "accepted-output" | "pushed-change" | "delivery-ready" | "merged-change";
81
+ cohortId?: string | undefined;
82
+ useAsBase?: boolean | undefined;
83
+ }[] | undefined;
76
84
  repository?: {
77
85
  resourceId: string;
78
86
  sourceRef: string;
79
87
  pullRequestBase: string;
80
88
  sourceRevision: string;
81
89
  } | undefined;
90
+ prerequisiteWorkItemIds?: WorkItemId[] | undefined;
82
91
  }[];
92
+ planRevision?: number | undefined;
83
93
  maxConcurrentRuns?: number | undefined;
84
94
  }>;
85
95
  inspectDelegation(caller: DelegationCaller, context: CommandContext): Promise<{
@@ -91,10 +101,39 @@ export declare class OrchestrationService {
91
101
  repositories?: string[] | undefined;
92
102
  } | null;
93
103
  repositories: string[];
104
+ planRevision: number;
94
105
  tree: {
95
106
  settled: boolean;
107
+ failureReason: string | undefined;
96
108
  children: {
97
109
  work: WorkItemView | null;
110
+ prerequisiteBlocker: string | undefined;
111
+ consumedInputs: {
112
+ childWorkItemId: WorkItemId;
113
+ consumed: {
114
+ name: string;
115
+ reference: {
116
+ workItemId: WorkItemId;
117
+ revisionId: string;
118
+ } | {
119
+ workItemId: WorkItemId;
120
+ resourceId: string;
121
+ repositoryResourceId: string;
122
+ revision: string;
123
+ branch: string;
124
+ pullRequestBase: string;
125
+ runId: string;
126
+ baseRevision?: string | undefined;
127
+ merged?: true | undefined;
128
+ };
129
+ }[];
130
+ repository?: {
131
+ resourceId: string;
132
+ sourceRef: string;
133
+ pullRequestBase: string;
134
+ sourceRevision: string;
135
+ } | undefined;
136
+ } | undefined;
98
137
  workflow: WorkflowInstanceView | null;
99
138
  assignmentId: string;
100
139
  objective: string;
@@ -118,12 +157,20 @@ export declare class OrchestrationService {
118
157
  workflowInstanceId: WorkflowInstanceId;
119
158
  grantId: string;
120
159
  repositoryResourceIds?: string[] | undefined;
160
+ prerequisites?: {
161
+ assignmentId: string;
162
+ output: string;
163
+ condition: "accepted-output" | "pushed-change" | "delivery-ready" | "merged-change";
164
+ cohortId?: string | undefined;
165
+ useAsBase?: boolean | undefined;
166
+ }[] | undefined;
121
167
  repository?: {
122
168
  resourceId: string;
123
169
  sourceRef: string;
124
170
  pullRequestBase: string;
125
171
  sourceRevision: string;
126
172
  } | undefined;
173
+ prerequisiteWorkItemIds?: WorkItemId[] | undefined;
127
174
  }[];
128
175
  cohortId: string;
129
176
  parentWorkflowInstanceId: WorkflowInstanceId;
@@ -134,13 +181,42 @@ export declare class OrchestrationService {
134
181
  ancestors: WorkItemId[];
135
182
  maxDepth: number;
136
183
  maxDescendants: number;
184
+ planRevision?: number | undefined;
137
185
  maxConcurrentRuns?: number | undefined;
138
186
  }[];
139
187
  }>;
140
188
  deliveryTree(id: WorkItemId): Promise<{
141
189
  settled: boolean;
190
+ failureReason: string | undefined;
142
191
  children: {
143
192
  work: WorkItemView | null;
193
+ prerequisiteBlocker: string | undefined;
194
+ consumedInputs: {
195
+ childWorkItemId: WorkItemId;
196
+ consumed: {
197
+ name: string;
198
+ reference: {
199
+ workItemId: WorkItemId;
200
+ revisionId: string;
201
+ } | {
202
+ workItemId: WorkItemId;
203
+ resourceId: string;
204
+ repositoryResourceId: string;
205
+ revision: string;
206
+ branch: string;
207
+ pullRequestBase: string;
208
+ runId: string;
209
+ baseRevision?: string | undefined;
210
+ merged?: true | undefined;
211
+ };
212
+ }[];
213
+ repository?: {
214
+ resourceId: string;
215
+ sourceRef: string;
216
+ pullRequestBase: string;
217
+ sourceRevision: string;
218
+ } | undefined;
219
+ } | undefined;
144
220
  workflow: WorkflowInstanceView | null;
145
221
  assignmentId: string;
146
222
  objective: string;
@@ -164,12 +240,20 @@ export declare class OrchestrationService {
164
240
  workflowInstanceId: WorkflowInstanceId;
165
241
  grantId: string;
166
242
  repositoryResourceIds?: string[] | undefined;
243
+ prerequisites?: {
244
+ assignmentId: string;
245
+ output: string;
246
+ condition: "accepted-output" | "pushed-change" | "delivery-ready" | "merged-change";
247
+ cohortId?: string | undefined;
248
+ useAsBase?: boolean | undefined;
249
+ }[] | undefined;
167
250
  repository?: {
168
251
  resourceId: string;
169
252
  sourceRef: string;
170
253
  pullRequestBase: string;
171
254
  sourceRevision: string;
172
255
  } | undefined;
256
+ prerequisiteWorkItemIds?: WorkItemId[] | undefined;
173
257
  }[];
174
258
  cohortId: string;
175
259
  parentWorkflowInstanceId: WorkflowInstanceId;
@@ -180,6 +264,7 @@ export declare class OrchestrationService {
180
264
  ancestors: WorkItemId[];
181
265
  maxDepth: number;
182
266
  maxDescendants: number;
267
+ planRevision?: number | undefined;
183
268
  maxConcurrentRuns?: number | undefined;
184
269
  }[]>;
185
270
  deliveryRunLimit(id: WorkItemId): Promise<number | undefined>;
@@ -200,6 +285,8 @@ export declare class OrchestrationService {
200
285
  branch: string;
201
286
  pullRequestBase: string;
202
287
  runId: string;
288
+ baseRevision?: string | undefined;
289
+ merged?: true | undefined;
203
290
  };
204
291
  }[];
205
292
  }[]>;