@atolis-hq/wake 0.4.99 → 0.4.101

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 (62) hide show
  1. package/dist/src/activities/agent/agent-activity.js +17 -2
  2. package/dist/src/bootstrap/artifact-mcp.d.ts +1 -0
  3. package/dist/src/bootstrap/artifact-mcp.js +27 -1
  4. package/dist/src/bootstrap/composition-root.js +6 -1
  5. package/dist/src/bootstrap/config/root-schema.d.ts +14 -0
  6. package/dist/src/bootstrap/delegation-authority.d.ts +10 -0
  7. package/dist/src/bootstrap/delegation-authority.js +52 -0
  8. package/dist/src/bootstrap/integration-runtime.js +4 -2
  9. package/dist/src/bootstrap/surface-api-delivery.d.ts +4 -0
  10. package/dist/src/bootstrap/surface-api-delivery.js +35 -0
  11. package/dist/src/bootstrap/surface-api-work-applications.js +2 -0
  12. package/dist/src/bootstrap/version.js +1 -1
  13. package/dist/src/control-plane/application/activation-scheduler-ports.d.ts +3 -1
  14. package/dist/src/control-plane/application/advance-once-dispatch.d.ts +2 -11
  15. package/dist/src/control-plane/application/advance-once-dispatch.js +11 -24
  16. package/dist/src/control-plane/application/trigger-recovery.js +2 -1
  17. package/dist/src/control-plane/application/trigger-workflow.d.ts +2 -1
  18. package/dist/src/control-plane/application/work-cancellation-policy.d.ts +1 -0
  19. package/dist/src/control-plane/application/work-cancellation-policy.js +6 -1
  20. package/dist/src/control-plane/domain/delivery-capacity.d.ts +12 -0
  21. package/dist/src/control-plane/domain/delivery-capacity.js +41 -0
  22. package/dist/src/control-plane/domain/dispatch-policy.d.ts +1 -0
  23. package/dist/src/control-plane/domain/dispatch-policy.js +6 -0
  24. package/dist/src/control-plane/domain/open-work-policy.d.ts +3 -0
  25. package/dist/src/control-plane/domain/open-work-policy.js +12 -8
  26. package/dist/src/execution/application/repository-assignments.d.ts +1 -1
  27. package/dist/src/execution/application/repository-assignments.js +7 -4
  28. package/dist/src/execution/application/run-repository.d.ts +2 -2
  29. package/dist/src/execution/application/run-repository.js +3 -3
  30. package/dist/src/orchestration/application/delegated-work.d.ts +52 -0
  31. package/dist/src/orchestration/application/delegated-work.js +61 -2
  32. package/dist/src/orchestration/application/delegation-commands.d.ts +119 -0
  33. package/dist/src/orchestration/application/delegation-commands.js +100 -0
  34. package/dist/src/orchestration/application/delegation-gate.d.ts +4 -1
  35. package/dist/src/orchestration/application/delegation-gate.js +17 -3
  36. package/dist/src/orchestration/application/orchestration-service.d.ts +150 -2
  37. package/dist/src/orchestration/application/orchestration-service.js +45 -4
  38. package/dist/src/orchestration/contracts/config.d.ts +16 -0
  39. package/dist/src/orchestration/contracts/config.js +10 -0
  40. package/dist/src/orchestration/contracts/delegation-ports.d.ts +10 -1
  41. package/dist/src/orchestration/contracts/delegation.d.ts +34 -0
  42. package/dist/src/orchestration/contracts/delegation.js +8 -0
  43. package/dist/src/orchestration/domain/compiler.js +7 -0
  44. package/dist/src/orchestration/index.d.ts +3 -0
  45. package/dist/src/orchestration/index.js +1 -0
  46. package/dist/src/surfaces/api/contracts/work.d.ts +14 -0
  47. package/dist/src/surfaces/index.d.ts +1 -0
  48. package/dist/src/surfaces/index.js +1 -0
  49. package/dist/src/surfaces/mcp/delegation.d.ts +9 -0
  50. package/dist/src/surfaces/mcp/delegation.js +25 -0
  51. package/dist/src/surfaces/web-assets/assets/index-C5R-aWzH.js +1192 -0
  52. package/dist/src/surfaces/web-assets/index.html +1 -1
  53. package/dist/src/work/application/delivery-availability.d.ts +17 -0
  54. package/dist/src/work/application/delivery-availability.js +20 -0
  55. package/dist/src/work/application/work-repository.d.ts +2 -2
  56. package/dist/src/work/application/work-repository.js +2 -2
  57. package/dist/src/work/application/work-service.d.ts +2 -2
  58. package/dist/src/work/application/work-service.js +2 -2
  59. package/dist/src/work/index.d.ts +1 -0
  60. package/dist/src/work/index.js +1 -0
  61. package/package.json +1 -1
  62. package/dist/src/surfaces/web-assets/assets/index-s9CWgq-s.js +0 -1192
@@ -1,4 +1,4 @@
1
- import { type EventJournal } from '@atolis-hq/eventing';
1
+ import { type EventJournal, type JournalReadOptions } from '@atolis-hq/eventing';
2
2
  import type { ActivationId } from '../../activities/index.js';
3
3
  import { type RunExecutionEvent, type RunExecutionEventData } from '../contracts/events.js';
4
4
  import { type RunId } from '../contracts/identifiers.js';
@@ -8,7 +8,7 @@ export declare class RunRepository {
8
8
  private readonly journal;
9
9
  private readonly runs;
10
10
  constructor(journal: EventJournal);
11
- load(runId: RunId): Promise<{
11
+ load(runId: RunId, options?: JournalReadOptions): Promise<{
12
12
  readonly sequence: number;
13
13
  readonly events?: readonly RunExecutionEvent[];
14
14
  readonly view: ReturnType<typeof foldRun>;
@@ -1,4 +1,4 @@
1
- import { cachedJournalView } from '@atolis-hq/eventing';
1
+ import { cachedJournalView, } from '@atolis-hq/eventing';
2
2
  import { decodeRunExecutionEvent, } from '../contracts/events.js';
3
3
  import { runId } from '../contracts/identifiers.js';
4
4
  import { isRunStream, runStream } from '../contracts/streams.js';
@@ -10,8 +10,8 @@ export class RunRepository {
10
10
  this.journal = journal;
11
11
  this.runs = cachedJournalView(journal, deriveRuns);
12
12
  }
13
- async load(runId) {
14
- const events = await this.journal.readStream(runStream(runId));
13
+ async load(runId, options) {
14
+ const events = await this.journal.readStream(runStream(runId), options);
15
15
  const decoded = events.map(decodeRunExecutionEvent);
16
16
  return { sequence: events.length, events: decoded, view: foldRun(decoded) };
17
17
  }
@@ -1,3 +1,5 @@
1
+ import type { WorkItemId } from '../../work/index.js';
2
+ import type { DelegationAuthority } from '../contracts/delegation-ports.js';
1
3
  import { type CommandContext } from '@atolis-hq/eventing';
2
4
  import { type WorkService } from '../../work/index.js';
3
5
  import type { DelegationArtifacts, DelegationRepositories } from '../contracts/delegation-ports.js';
@@ -18,6 +20,7 @@ export declare class DelegatedWork {
18
20
  private readonly claims;
19
21
  private readonly definitions;
20
22
  private readonly workflows;
23
+ private readonly dependencies;
21
24
  private readonly ledger;
22
25
  private readonly artifacts;
23
26
  private readonly repositories;
@@ -26,7 +29,55 @@ export declare class DelegatedWork {
26
29
  readonly artifacts: DelegationArtifacts;
27
30
  readonly repositories?: DelegationRepositories | undefined;
28
31
  readonly runHasEnded: (runId: string) => Promise<boolean>;
32
+ readonly authority?: DelegationAuthority | undefined;
29
33
  });
34
+ inspect(id: WorkItemId): Promise<{
35
+ settled: boolean;
36
+ children: {
37
+ work: import("../../work/index.js").WorkItemView | null;
38
+ workflow: WorkflowInstanceView | null;
39
+ assignmentId: string;
40
+ objective: string;
41
+ criteria: string[];
42
+ workflowName: import("../contracts/identifiers.js").WorkflowName;
43
+ workflowFingerprint: string;
44
+ context: {
45
+ workItemId: WorkItemId;
46
+ revisionId: string;
47
+ }[];
48
+ outputs: ({
49
+ name: string;
50
+ path: string;
51
+ } | {
52
+ name: string;
53
+ kind: "change";
54
+ requireReview?: boolean | undefined;
55
+ requireChecks?: boolean | undefined;
56
+ })[];
57
+ workItemId: WorkItemId;
58
+ workflowInstanceId: WorkflowInstanceId;
59
+ grantId: string;
60
+ repositoryResourceIds?: string[] | undefined;
61
+ repository?: {
62
+ resourceId: string;
63
+ sourceRef: string;
64
+ pullRequestBase: string;
65
+ sourceRevision: string;
66
+ } | undefined;
67
+ }[];
68
+ cohortId: string;
69
+ parentWorkflowInstanceId: WorkflowInstanceId;
70
+ parentWorkItemId: WorkItemId;
71
+ rootWorkItemId: WorkItemId;
72
+ activationId: import("../../activities/index.js").ActivationId;
73
+ runId: string;
74
+ ancestors: WorkItemId[];
75
+ maxDepth: number;
76
+ maxDescendants: number;
77
+ maxConcurrentRuns?: number | undefined;
78
+ }[]>;
79
+ runLimit(id: WorkItemId): Promise<number | undefined>;
80
+ repositoryScope(workItemId: string): Promise<string[] | undefined>;
30
81
  repositoryFor(workItemId: string): Promise<{
31
82
  resourceId: string;
32
83
  sourceRef: string;
@@ -55,6 +106,7 @@ export declare class DelegatedWork {
55
106
  private childWorkTerminated;
56
107
  private missingOutputs;
57
108
  private endedRun;
109
+ private canJoin;
58
110
  /** Join and continuation commit together, so a duplicate trigger cannot resume the parent twice. */
59
111
  private join;
60
112
  }
@@ -2,7 +2,7 @@
2
2
  import { WrongExpectedSequenceError } from '@atolis-hq/eventing';
3
3
  import { activationId } from '../../activities/index.js';
4
4
  import { WorkspaceMode } from '../../execution/index.js';
5
- import { WorkStatus } from '../../work/index.js';
5
+ import { readDeliveryAvailability, WorkStatus } from '../../work/index.js';
6
6
  import { OrchestrationEventType } from '../contracts/events.js';
7
7
  import { orchestrationGroupId, workflowInstanceId, } from '../contracts/identifiers.js';
8
8
  import { WorkflowStatus } from '../contracts/vocabulary.js';
@@ -21,6 +21,7 @@ export class DelegatedWork {
21
21
  claims;
22
22
  definitions;
23
23
  workflows;
24
+ dependencies;
24
25
  ledger;
25
26
  artifacts;
26
27
  repositories;
@@ -31,11 +32,50 @@ export class DelegatedWork {
31
32
  this.claims = claims;
32
33
  this.definitions = definitions;
33
34
  this.workflows = workflows;
35
+ this.dependencies = dependencies;
34
36
  this.ledger = new DelegationLedger(repository);
35
37
  this.artifacts = dependencies.artifacts;
36
38
  this.repositories = dependencies.repositories;
37
39
  this.runHasEnded = dependencies.runHasEnded;
38
40
  }
41
+ async inspect(id) {
42
+ const item = await this.work.get(id);
43
+ const root = item?.delegation?.rootWorkItemId ?? id;
44
+ const events = await this.ledger.events();
45
+ const reservations = reservationsOf(events).filter((entry) => entry.rootWorkItemId === root);
46
+ return Promise.all(reservations.map(async (reservation) => ({
47
+ ...reservation,
48
+ settled: isSettled(events, reservation),
49
+ children: await Promise.all(reservation.children.map(async (child) => ({
50
+ ...child,
51
+ work: await this.work.get(child.workItemId),
52
+ workflow: (await this.repository.load(child.workflowInstanceId)).view,
53
+ }))),
54
+ })));
55
+ }
56
+ async runLimit(id) {
57
+ const item = await this.work.get(id);
58
+ const root = item?.delegation?.rootWorkItemId ?? id;
59
+ const reserved = (await this.ledger.reservations()).find((entry) => entry.rootWorkItemId === root)?.maxConcurrentRuns;
60
+ if (reserved !== undefined)
61
+ return reserved;
62
+ const claim = await this.claims.primaryWorkflowInstanceId(root);
63
+ if (claim === undefined)
64
+ return undefined;
65
+ const view = (await this.repository.load(workflowInstanceId(claim))).view;
66
+ if (view === null)
67
+ return undefined;
68
+ return (await this.definitions.resolve(view)).delegation?.maxConcurrentRuns;
69
+ }
70
+ async repositoryScope(workItemId) {
71
+ const child = (await this.ledger.reservations())
72
+ .flatMap((entry) => entry.children)
73
+ .find((entry) => entry.workItemId === workItemId);
74
+ if (child === undefined)
75
+ return undefined;
76
+ return (child.repositoryResourceIds ??
77
+ (child.repository === undefined ? [] : [child.repository.resourceId]));
78
+ }
39
79
  async repositoryFor(workItemId) {
40
80
  return (await this.ledger.reservations())
41
81
  .flatMap((entry) => entry.children)
@@ -76,6 +116,7 @@ export class DelegatedWork {
76
116
  const events = await this.ledger.events();
77
117
  const all = reservationsOf(rootFacts).filter((entry) => entry.rootWorkItemId === reservation.rootWorkItemId);
78
118
  const rootLimits = {
119
+ ...rootRunLimit(all, limits),
79
120
  maxDepth: all[0]?.maxDepth ?? limits.maxDepth,
80
121
  maxDescendants: all[0]?.maxDescendants ?? limits.maxDescendants,
81
122
  };
@@ -161,6 +202,9 @@ export class DelegatedWork {
161
202
  return capacity;
162
203
  }
163
204
  async parentIdentity(parent) {
205
+ const availability = await readDeliveryAvailability(parent.workItemId, this.work);
206
+ if (availability.reason !== undefined)
207
+ throw new Error(availability.reason);
164
208
  const item = await this.work.get(parent.workItemId);
165
209
  if (item === null || item.state !== WorkStatus.Open || item.frozen || item.deleted)
166
210
  throw new Error('Delegation requires open, unfrozen work');
@@ -181,6 +225,9 @@ export class DelegatedWork {
181
225
  };
182
226
  }
183
227
  async assertReservable(caller, existing, reservation, limits) {
228
+ const availability = await readDeliveryAvailability(reservation.parentWorkItemId, this.work);
229
+ if (availability.reason !== undefined)
230
+ throw new Error(availability.reason);
184
231
  const current = (await this.repository.loadRequired(caller.workflowInstanceId)).view;
185
232
  if (current.pendingActivation?.activationId !== caller.activationId ||
186
233
  current.status !== WorkflowStatus.Active)
@@ -223,6 +270,8 @@ export class DelegatedWork {
223
270
  async reconcileReservation(artifacts, reservation, context) {
224
271
  if (isSettled(await this.ledger.events(), reservation))
225
272
  return;
273
+ if ((await readDeliveryAvailability(reservation.parentWorkItemId, this.work)).reason !== undefined)
274
+ return;
226
275
  const parent = (await this.repository.loadRequired(reservation.parentWorkflowInstanceId)).view;
227
276
  if (parent.status !== WorkflowStatus.Active && parent.status !== WorkflowStatus.Waiting) {
228
277
  await this.fail(reservation, 'Delegating parent is no longer active or waiting', context);
@@ -337,6 +386,8 @@ export class DelegatedWork {
337
386
  }
338
387
  /** Starts the pinned definition, then records the result only once the child qualifies. */
339
388
  async advanceChild(artifacts, root, reservation, child, context) {
389
+ if ((await readDeliveryAvailability(reservation.parentWorkItemId, this.work)).reason !== undefined)
390
+ return;
340
391
  const existing = await this.repository.load(child.workflowInstanceId);
341
392
  if (await this.childWorkTerminated(child.workItemId, existing.view)) {
342
393
  await this.fail(reservation, 'Delegated child work is no longer open', context);
@@ -402,9 +453,13 @@ export class DelegatedWork {
402
453
  endedRun(id) {
403
454
  return this.runHasEnded(id);
404
455
  }
456
+ async canJoin(reservation) {
457
+ return ((await this.endedRun(reservation.runId)) &&
458
+ (await readDeliveryAvailability(reservation.parentWorkItemId, this.work)).reason === undefined);
459
+ }
405
460
  /** Join and continuation commit together, so a duplicate trigger cannot resume the parent twice. */
406
461
  async join(reservation, results, context) {
407
- if (!(await this.endedRun(reservation.runId)))
462
+ if (!(await this.canJoin(reservation)))
408
463
  return;
409
464
  const parentId = reservation.parentWorkflowInstanceId;
410
465
  for (;;) {
@@ -448,6 +503,10 @@ export class DelegatedWork {
448
503
  }
449
504
  }
450
505
  }
506
+ function rootRunLimit(reservations, limits) {
507
+ const maxConcurrentRuns = reservations[0]?.maxConcurrentRuns ?? limits.maxConcurrentRuns;
508
+ return maxConcurrentRuns === undefined ? {} : { maxConcurrentRuns };
509
+ }
451
510
  function assertReplay(prior, caller, assignments) {
452
511
  if (prior.parentWorkflowInstanceId !== caller.workflowInstanceId ||
453
512
  prior.activationId !== caller.activationId ||
@@ -0,0 +1,119 @@
1
+ import type { CommandContext } from '@atolis-hq/eventing';
2
+ import { type WorkService } from '../../work/index.js';
3
+ import type { DelegationAuthority } from '../contracts/delegation-ports.js';
4
+ import { type DelegationCaller, type DelegationSubmission } from '../contracts/delegation.js';
5
+ import type { WorkflowInstanceView } from '../contracts/views.js';
6
+ import type { DelegatedWork } from './delegated-work.js';
7
+ import type { OrchestrationRepository } from './orchestration-repository.js';
8
+ import type { StartWorkflow } from './start-workflow.js';
9
+ import type { WorkflowDefinitionRegistry } from './workflow-definition-registry.js';
10
+ /** Agent inputs describe work; authority comes from the current Run and workflow. */
11
+ export declare class DelegationCommands {
12
+ private readonly repository;
13
+ private readonly work;
14
+ private readonly workflows;
15
+ private readonly definitions;
16
+ private readonly delegated;
17
+ private readonly authority;
18
+ constructor(repository: OrchestrationRepository, work: WorkService, workflows: StartWorkflow, definitions: WorkflowDefinitionRegistry, delegated: DelegatedWork, authority: DelegationAuthority);
19
+ submit(caller: DelegationCaller, input: DelegationSubmission, context: CommandContext): Promise<{
20
+ cohortId: string;
21
+ parentWorkflowInstanceId: import("../index.js").WorkflowInstanceId;
22
+ parentWorkItemId: import("../../work/index.js").WorkItemId;
23
+ rootWorkItemId: import("../../work/index.js").WorkItemId;
24
+ activationId: import("../../activities/index.js").ActivationId;
25
+ runId: string;
26
+ ancestors: import("../../work/index.js").WorkItemId[];
27
+ maxDepth: number;
28
+ maxDescendants: number;
29
+ children: {
30
+ assignmentId: string;
31
+ objective: string;
32
+ criteria: string[];
33
+ workflowName: import("../index.js").WorkflowName;
34
+ workflowFingerprint: string;
35
+ context: {
36
+ workItemId: import("../../work/index.js").WorkItemId;
37
+ revisionId: string;
38
+ }[];
39
+ outputs: ({
40
+ name: string;
41
+ path: string;
42
+ } | {
43
+ name: string;
44
+ kind: "change";
45
+ requireReview?: boolean | undefined;
46
+ requireChecks?: boolean | undefined;
47
+ })[];
48
+ workItemId: import("../../work/index.js").WorkItemId;
49
+ workflowInstanceId: import("../index.js").WorkflowInstanceId;
50
+ grantId: string;
51
+ repositoryResourceIds?: string[] | undefined;
52
+ repository?: {
53
+ resourceId: string;
54
+ sourceRef: string;
55
+ pullRequestBase: string;
56
+ sourceRevision: string;
57
+ } | undefined;
58
+ }[];
59
+ maxConcurrentRuns?: number | undefined;
60
+ }>;
61
+ inspect(caller: DelegationCaller, context: CommandContext): Promise<{
62
+ policy: {
63
+ workflows: string[];
64
+ maxDepth: number;
65
+ maxDescendants: number;
66
+ maxConcurrentRuns: number;
67
+ repositories?: string[] | undefined;
68
+ } | null;
69
+ repositories: string[];
70
+ tree: {
71
+ settled: boolean;
72
+ children: {
73
+ work: import("../../work/index.js").WorkItemView | null;
74
+ workflow: WorkflowInstanceView | null;
75
+ assignmentId: string;
76
+ objective: string;
77
+ criteria: string[];
78
+ workflowName: import("../index.js").WorkflowName;
79
+ workflowFingerprint: string;
80
+ context: {
81
+ workItemId: import("../../work/index.js").WorkItemId;
82
+ revisionId: string;
83
+ }[];
84
+ outputs: ({
85
+ name: string;
86
+ path: string;
87
+ } | {
88
+ name: string;
89
+ kind: "change";
90
+ requireReview?: boolean | undefined;
91
+ requireChecks?: boolean | undefined;
92
+ })[];
93
+ workItemId: import("../../work/index.js").WorkItemId;
94
+ workflowInstanceId: import("../index.js").WorkflowInstanceId;
95
+ grantId: string;
96
+ repositoryResourceIds?: string[] | undefined;
97
+ repository?: {
98
+ resourceId: string;
99
+ sourceRef: string;
100
+ pullRequestBase: string;
101
+ sourceRevision: string;
102
+ } | undefined;
103
+ }[];
104
+ cohortId: string;
105
+ parentWorkflowInstanceId: import("../index.js").WorkflowInstanceId;
106
+ parentWorkItemId: import("../../work/index.js").WorkItemId;
107
+ rootWorkItemId: import("../../work/index.js").WorkItemId;
108
+ activationId: import("../../activities/index.js").ActivationId;
109
+ runId: string;
110
+ ancestors: import("../../work/index.js").WorkItemId[];
111
+ maxDepth: number;
112
+ maxDescendants: number;
113
+ maxConcurrentRuns?: number | undefined;
114
+ }[];
115
+ }>;
116
+ /** Effective repository scope: the parent's inherited scope bounded by parent and root policy. */
117
+ private scopeFor;
118
+ private rootFor;
119
+ }
@@ -0,0 +1,100 @@
1
+ import { readDeliveryAvailability } from '../../work/index.js';
2
+ import { delegationSubmissionSchema, } from '../contracts/delegation.js';
3
+ import { WorkflowStatus } from '../contracts/vocabulary.js';
4
+ /** Agent inputs describe work; authority comes from the current Run and workflow. */
5
+ export class DelegationCommands {
6
+ repository;
7
+ work;
8
+ workflows;
9
+ definitions;
10
+ delegated;
11
+ authority;
12
+ constructor(repository, work, workflows, definitions, delegated, authority) {
13
+ this.repository = repository;
14
+ this.work = work;
15
+ this.workflows = workflows;
16
+ this.definitions = definitions;
17
+ this.delegated = delegated;
18
+ this.authority = authority;
19
+ }
20
+ async submit(caller, input, context) {
21
+ const command = delegationSubmissionSchema.parse(input);
22
+ const parent = (await this.repository.loadRequired(caller.workflowInstanceId)).view;
23
+ await this.authority.assertCaller(caller, parent);
24
+ if ((parent.pendingActivation?.activationId ?? parent.delegationFence?.activationId) !==
25
+ caller.activationId ||
26
+ (parent.status !== WorkflowStatus.Active && parent.status !== WorkflowStatus.Waiting))
27
+ throw new Error('Delegation requires the current active activation');
28
+ const availability = await readDeliveryAvailability(parent.workItemId, this.work);
29
+ if (availability.reason !== undefined)
30
+ throw new Error(availability.reason);
31
+ const policy = (await this.workflows.definitionFor(parent)).delegation;
32
+ if (policy === undefined)
33
+ throw new Error('Workflow delegation is disabled');
34
+ const { rootPolicy, permitted } = await this.scopeFor(parent, policy, context);
35
+ const assignments = command.assignments.map((assignment) => {
36
+ if (!policy.workflows.includes(assignment.workflowName) ||
37
+ rootPolicy === undefined ||
38
+ !rootPolicy.workflows.includes(assignment.workflowName))
39
+ throw new Error('Delegation workflow is not allowed');
40
+ // The permitted set only bounds explicit requests; it never selects a checkout target.
41
+ const repositories = assignment.repositoryResourceIds ?? permitted;
42
+ const requested = [
43
+ ...repositories,
44
+ ...(assignment.repository === undefined ? [] : [assignment.repository.resourceId]),
45
+ ];
46
+ if (assignment.repository !== undefined &&
47
+ !repositories.includes(assignment.repository.resourceId))
48
+ throw new Error('Delegation repository authority exceeded');
49
+ if (requested.some((id) => !permitted.includes(id) ||
50
+ (policy.repositories !== undefined && !policy.repositories.includes(id)) ||
51
+ (rootPolicy.repositories !== undefined && !rootPolicy.repositories.includes(id))))
52
+ throw new Error('Delegation repository authority exceeded');
53
+ return {
54
+ ...assignment,
55
+ repositoryResourceIds: repositories,
56
+ workflowFingerprint: this.definitions.currentDefinition(assignment.workflowName)
57
+ .fingerprint,
58
+ };
59
+ });
60
+ return this.delegated.reserve(caller, command.cohortId, assignments, context, {
61
+ maxDepth: policy.maxDepth,
62
+ maxDescendants: policy.maxDescendants,
63
+ maxConcurrentRuns: policy.maxConcurrentRuns,
64
+ });
65
+ }
66
+ async inspect(caller, context) {
67
+ const parent = (await this.repository.loadRequired(caller.workflowInstanceId)).view;
68
+ await this.authority.assertCaller(caller, parent);
69
+ const policy = (await this.workflows.definitionFor(parent)).delegation;
70
+ return {
71
+ policy: policy ?? null,
72
+ // Resource IDs an explicit repository selection may name; empty when none is authorized.
73
+ repositories: policy === undefined ? [] : (await this.scopeFor(parent, policy, context)).permitted,
74
+ tree: await this.delegated.inspect(parent.workItemId),
75
+ };
76
+ }
77
+ /** Effective repository scope: the parent's inherited scope bounded by parent and root policy. */
78
+ async scopeFor(parent, policy, context) {
79
+ const root = await this.rootFor(parent);
80
+ const rootPolicy = (await this.workflows.definitionFor(root)).delegation;
81
+ await this.authority.bindRepositoryScope?.(root, context);
82
+ const rootRepositories = await this.authority.repositories(root);
83
+ const inherited = ((await this.delegated.repositoryScope(parent.workItemId)) ??
84
+ (await this.authority.repositories(parent))).filter((id) => rootRepositories.includes(id));
85
+ const permitted = inherited.filter((id) => (policy.repositories === undefined || policy.repositories.includes(id)) &&
86
+ (rootPolicy?.repositories === undefined || rootPolicy.repositories.includes(id)));
87
+ return { rootPolicy, permitted };
88
+ }
89
+ async rootFor(parent) {
90
+ const item = await this.work.get(parent.workItemId);
91
+ if (item?.delegation === undefined)
92
+ return parent;
93
+ const root = (await this.repository.list()).find(({ view }) => view !== null &&
94
+ view.workItemId === item.delegation?.rootWorkItemId &&
95
+ view.parentWorkflowInstanceId === undefined)?.view;
96
+ if (root == null)
97
+ throw new Error('Delegation root workflow is unavailable');
98
+ return root;
99
+ }
100
+ }
@@ -1,11 +1,14 @@
1
1
  import type { ActivationId } from '../../activities/index.js';
2
+ import { type WorkService } from '../../work/index.js';
2
3
  import type { WorkflowInstanceId } from '../contracts/identifiers.js';
3
4
  import type { WorkflowInstanceView } from '../contracts/views.js';
4
5
  import type { OrchestrationRepository } from './orchestration-repository.js';
5
6
  /** One admission policy for ordinary advancement while a committed cohort owns the parent. */
6
7
  export declare class DelegationGate {
7
8
  private readonly repository;
8
- constructor(repository: OrchestrationRepository);
9
+ private readonly work;
10
+ constructor(repository: OrchestrationRepository, work: WorkService);
11
+ unavailable(view: WorkflowInstanceView): Promise<boolean>;
9
12
  allows(view: WorkflowInstanceView): boolean;
10
13
  fenced(id: WorkflowInstanceId): Promise<boolean>;
11
14
  run<T>(id: WorkflowInstanceId, operation: () => Promise<T>): Promise<T>;
@@ -1,16 +1,26 @@
1
+ import { readDeliveryAvailability } from '../../work/index.js';
1
2
  import { DelegationPendingError } from '../contracts/delegation.js';
2
3
  /** One admission policy for ordinary advancement while a committed cohort owns the parent. */
3
4
  export class DelegationGate {
4
5
  repository;
5
- constructor(repository) {
6
+ work;
7
+ constructor(repository, work) {
6
8
  this.repository = repository;
9
+ this.work = work;
10
+ }
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)
15
+ return false;
16
+ return (await readDeliveryAvailability(view.workItemId, this.work)).reason !== undefined;
7
17
  }
8
18
  allows(view) {
9
19
  return view.delegationFence === undefined;
10
20
  }
11
21
  async fenced(id) {
12
22
  const view = (await this.repository.load(id)).view;
13
- return view !== null && !this.allows(view);
23
+ return view !== null && (!this.allows(view) || (await this.unavailable(view)));
14
24
  }
15
25
  async run(id, operation) {
16
26
  if (await this.fenced(id))
@@ -19,6 +29,8 @@ export class DelegationGate {
19
29
  }
20
30
  async outcome(id, activationId, operation, held) {
21
31
  const view = (await this.repository.load(id)).view;
32
+ if (view !== null && (await this.unavailable(view)))
33
+ throw new DelegationPendingError(id);
22
34
  if (view === null || this.allows(view))
23
35
  return operation();
24
36
  if (view.delegationFence?.activationId !== activationId)
@@ -26,6 +38,8 @@ export class DelegationGate {
26
38
  return held();
27
39
  }
28
40
  async availableInstances() {
29
- return (await this.repository.list()).filter(({ view }) => view !== null && this.allows(view));
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]);
30
44
  }
31
45
  }