@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,6 +1,8 @@
1
1
  import { ActivityEventType } from '../contracts/events.js';
2
2
  import type { PullRequestAuthorityDecision, PullRequestAuthorityInput, PullRequestAuthorityOptions, PullRequestResourceView, PullRequestView } from './contracts.js';
3
3
  export declare function decidePullRequestAuthority(input: PullRequestAuthorityInput, options?: PullRequestAuthorityOptions): PullRequestAuthorityDecision;
4
+ /** Qualifies an existing output without authorizing another approval or merge. */
5
+ export declare function decidePullRequestOutputAuthority(input: PullRequestAuthorityInput, options: PullRequestAuthorityOptions): PullRequestAuthorityDecision;
4
6
  export declare function createPullRequestMergeDenial(decision: PullRequestAuthorityDecision): {
5
7
  readonly eventType: typeof ActivityEventType.PrMergeDenied;
6
8
  readonly payload: {
@@ -8,6 +8,13 @@ export function decidePullRequestAuthority(input, options = {
8
8
  requireAcceptedReview: true,
9
9
  requireChecks: true,
10
10
  }) {
11
+ return decideAuthority(input, options, false);
12
+ }
13
+ /** Qualifies an existing output without authorizing another approval or merge. */
14
+ export function decidePullRequestOutputAuthority(input, options) {
15
+ return decideAuthority(input, options, true);
16
+ }
17
+ function decideAuthority(input, options, allowMerged) {
11
18
  if (input.work === null)
12
19
  return denied(PullRequestDenialCode.MissingResource);
13
20
  const resource = selectResource(input, input.work.workItemId, options.target);
@@ -16,7 +23,8 @@ export function decidePullRequestAuthority(input, options = {
16
23
  const pullRequest = selectPullRequest(input, resource, input.work.workItemId);
17
24
  if (isDenial(pullRequest))
18
25
  return pullRequest;
19
- if (pullRequest.state !== PullRequestState.Open)
26
+ if (pullRequest.state !== PullRequestState.Open &&
27
+ !(allowMerged && pullRequest.state === PullRequestState.Merged))
20
28
  return denied(PullRequestDenialCode.Closed);
21
29
  const authority = finalizeAuthority(input, resource, pullRequest, options);
22
30
  if (authority !== null)
@@ -46,7 +46,7 @@ export class OperatorInstructionsService {
46
46
  if (bytes.byteLength > this.config.maxWriteBytes)
47
47
  throw new Error(`Operator instructions exceed ${this.config.maxWriteBytes} byte limit`);
48
48
  const stream = artifactContextStream(input.contextScope);
49
- const events = await this.journal.readStream(stream, { fresh: true });
49
+ const events = await this.journal.readStream(stream);
50
50
  const revisions = this.selectRevisions(events);
51
51
  const existing = revisions.find((item) => item.revisionId === input.revisionId);
52
52
  if (existing !== undefined) {
@@ -88,7 +88,7 @@ export class OperatorInstructionsService {
88
88
  }
89
89
  async revisions(contextScope) {
90
90
  artifactSharedContextNameSchema.parse(contextScope);
91
- return this.selectRevisions(await this.journal.readStream(artifactContextStream(contextScope), { fresh: true }));
91
+ return this.selectRevisions(await this.journal.readStream(artifactContextStream(contextScope)));
92
92
  }
93
93
  selectRevisions(events) {
94
94
  return events.flatMap((envelope) => {
@@ -84,9 +84,8 @@ export async function createArtifactMcpServerForSession(root, sessions, path, to
84
84
  await sessions.read(path, token);
85
85
  if (root.journal === undefined)
86
86
  throw new Error('Delegation journal is unavailable');
87
- const run = (await new RunRepository(root.journal).load(executionRunId(descriptor.runId), {
88
- fresh: true,
89
- })).view;
87
+ const run = (await new RunRepository(root.journal).load(executionRunId(descriptor.runId)))
88
+ .view;
90
89
  if (run === null || run.activationId !== descriptor.activationId)
91
90
  throw new Error('Delegation Run is unavailable');
92
91
  const parent = await root.orchestration.get(workflowInstanceId(run.workflowInstanceId));
@@ -146,7 +145,7 @@ function operatorInstructionsPrompt(instructions) {
146
145
  async function instructionsForRun(input, runId, contextScope) {
147
146
  const prior = input.journal === undefined
148
147
  ? undefined
149
- : (await input.journal.readStream(runStream(executionRunId(runId)), { fresh: true }))
148
+ : (await input.journal.readStream(runStream(executionRunId(runId))))
150
149
  .flatMap((envelope) => {
151
150
  const event = selectRunExecutionEvent(envelope);
152
151
  return event?.event.eventType === ExecutionEventType.RunOperatorInstructionsCaptured
@@ -13,7 +13,7 @@ export function createDelegationAuthority(journal, resources, activities, bindin
13
13
  };
14
14
  return {
15
15
  async assertCaller(caller, parent) {
16
- const run = (await runs.load(runId(caller.runId), { fresh: true })).view;
16
+ const run = (await runs.load(runId(caller.runId))).view;
17
17
  if (run === null ||
18
18
  !isActiveRunStatus(run.status) ||
19
19
  run.cancellation !== undefined ||
@@ -134,7 +134,7 @@ export async function composeIntegrationRuntime(input) {
134
134
  async function openWorkCapacityAvailable() {
135
135
  const limit = input.config.controlPlane.maxOpenWorkItems ?? null;
136
136
  return (limit === null ||
137
- hasOpenWorkCapacity(await readOpenWorkItemIds(await input.orchestration.listAll(), input.work), limit));
137
+ hasOpenWorkCapacity(readOpenWorkItemIds(await input.orchestration.listAll(), await input.work.deliverySnapshot()), limit));
138
138
  }
139
139
  const triggerReactors = input.config.controlPlane.triggers.map((config) => createTriggerReactor(triggers, config, decodeCanonicalEvent));
140
140
  // Registration starts at the current journal boundary; historical facts are not sources.
@@ -10,7 +10,7 @@ function serializeJournalAppends(journal) {
10
10
  appendTail = appended.then(() => undefined, () => undefined);
11
11
  return appended;
12
12
  },
13
- readStream: (stream, options) => journal.readStream(stream, options),
13
+ readStream: (stream) => journal.readStream(stream),
14
14
  ...(journal.scanAll === undefined
15
15
  ? {}
16
16
  : { scanAll: (signal) => journal.scanAll(signal) }),
@@ -1,4 +1,4 @@
1
1
  import { type WorkDetailResponse } from '../surfaces/index.js';
2
- import { type WorkItemView } from '../work/index.js';
2
+ import type { WorkItemView } from '../work/index.js';
3
3
  import type { CompositionRoot } from './composition-root.js';
4
4
  export declare function presentDelivery(root: CompositionRoot, work: WorkItemView): Promise<Pick<WorkDetailResponse, 'delegation'>>;
@@ -1,11 +1,11 @@
1
1
  import { presentResource, toWorkItemKey } from '../surfaces/index.js';
2
- import { readDeliveryAvailability } from '../work/index.js';
3
2
  export async function presentDelivery(root, work) {
4
3
  if (root.orchestration?.deliveryTree === undefined)
5
4
  return {};
6
5
  const cohorts = await root.orchestration.deliveryTree(work.workItemId);
7
6
  if (cohorts.length === 0 && work.delegation === undefined)
8
7
  return {};
8
+ const delivery = await root.work.deliverySnapshot();
9
9
  return {
10
10
  delegation: {
11
11
  rootWorkItemKey: toWorkItemKey(work.delegation?.rootWorkItemId ?? work.workItemId),
@@ -13,7 +13,7 @@ export async function presentDelivery(root, work) {
13
13
  ? {}
14
14
  : { parentWorkItemKey: toWorkItemKey(work.delegation.parentWorkItemId) }),
15
15
  children: await Promise.all(cohorts.flatMap((cohort) => cohort.children.map(async (child) => {
16
- const availability = await readDeliveryAvailability(child.workItemId, root.work);
16
+ const availability = delivery.availability(child.workItemId);
17
17
  const resources = await Promise.all((await root.resources.correlationsForWork(child.workItemId)).map((entry) => root.resources.get(entry.resourceId)));
18
18
  return {
19
19
  workItemKey: toWorkItemKey(child.workItemId),
@@ -22,8 +22,12 @@ export async function presentDelivery(root, work) {
22
22
  workflowName: child.workflowName,
23
23
  status: child.workflow?.status ?? 'registration-pending',
24
24
  cohortId: cohort.cohortId,
25
- ...((availability.reason ?? child.workflow?.blockReason)
26
- ? { blockReason: (availability.reason ?? child.workflow?.blockReason) }
25
+ ...((availability.reason ?? child.prerequisiteBlocker ?? child.workflow?.blockReason)
26
+ ? {
27
+ blockReason: (availability.reason ??
28
+ child.prerequisiteBlocker ??
29
+ child.workflow?.blockReason),
30
+ }
27
31
  : {}),
28
32
  resources: resources
29
33
  .filter((entry) => entry !== null)
@@ -124,6 +124,6 @@ export function resolveWakeVersion(options = {}) {
124
124
  return `g${headHash.slice(0, 7)}`;
125
125
  return '0.1.0-dev';
126
126
  }
127
- export const wakeVersion = "v0.4.101";
127
+ export const wakeVersion = "v0.4.103";
128
128
  /** The registry-installable version used by packaged sandbox images. */
129
129
  export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
@@ -3,7 +3,7 @@ import type { ExecutionFailureCode, RepositoryAssignment, RunView, WorkspaceReco
3
3
  import type { IdGenerator } from '../../kernel/index.js';
4
4
  import type { ActivityActivationView, PendingActivationPriority, WorkflowInstanceView } from '../../orchestration/index.js';
5
5
  import type { ResourceService } from '../../resources/index.js';
6
- import type { WorkDelegation, WorkItemId, WorkStatus } from '../../work/index.js';
6
+ import type { DeliverySnapshot, WorkDelegation, WorkItemId, WorkStatus } from '../../work/index.js';
7
7
  import type { DispatchPolicy } from '../domain/dispatch-policy.js';
8
8
  export interface OrchestrationPort {
9
9
  reconcileChildCompletions(context: CommandContext): Promise<void>;
@@ -64,6 +64,7 @@ export interface ActivationSchedulerDependencies {
64
64
  readonly frozen?: boolean;
65
65
  readonly deleted?: boolean;
66
66
  } | null>;
67
+ deliverySnapshot(): Promise<DeliverySnapshot>;
67
68
  };
68
69
  readonly runnerIneligibility?: () => Promise<ReadonlySet<string>>;
69
70
  readonly isDispatchPaused?: () => Promise<boolean>;
@@ -105,11 +105,12 @@ export function createActivationScheduler(orchestration, execution, resources, c
105
105
  return { kind: 'paused' };
106
106
  }
107
107
  const rawPending = await orchestration.listPendingActivations(options.workItemId);
108
+ const delivery = rawPending.length === 0 ? undefined : await dependencies.work?.deliverySnapshot();
108
109
  const pending = (await Promise.all(rawPending.map(async (candidate) => {
109
110
  if (candidate.activation.notBefore !== undefined &&
110
111
  Date.parse(candidate.activation.notBefore) > clock.now().getTime())
111
112
  return null;
112
- const work = await dependencies.work?.get(candidate.workflow.workItemId);
113
+ const work = delivery?.get(candidate.workflow.workItemId);
113
114
  return work?.frozen || work?.deleted ? null : candidate;
114
115
  }))).filter((candidate) => candidate !== null);
115
116
  const blocked = ((await orchestration.listAll?.()) ?? []).flatMap((workflow) => workflow.status === WorkflowStatus.Blocked &&
@@ -1,9 +1,9 @@
1
1
  import type { CommandContext } from '@atolis-hq/eventing';
2
2
  import type { ActivityActivationView, PendingActivationPriority, WorkflowInstanceView } from '../../orchestration/index.js';
3
3
  import { type ResourceService } from '../../resources/index.js';
4
+ import type { WorkService } from '../../work/index.js';
4
5
  import type { AdvanceResult } from '../contracts/views.js';
5
6
  import { type DispatchPolicy } from '../domain/dispatch-policy.js';
6
- import { type WorkAvailability } from '../domain/open-work-policy.js';
7
7
  import type { ExecutionPort, OrchestrationPort } from './activation-scheduler-ports.js';
8
8
  export interface DispatchLoopContext {
9
9
  readonly orchestration: OrchestrationPort;
@@ -13,9 +13,7 @@ export interface DispatchLoopContext {
13
13
  readonly maxConcurrentRuns: number;
14
14
  readonly maxDispatches: number;
15
15
  readonly maxOpenWorkItems: number | null;
16
- readonly work: {
17
- get(id: string): Promise<WorkAvailability | null>;
18
- } | undefined;
16
+ readonly work: Pick<WorkService, 'deliverySnapshot'> | undefined;
19
17
  readonly runnerIneligibility: () => Promise<ReadonlySet<string>>;
20
18
  readonly isDispatchPaused: () => Promise<boolean>;
21
19
  readonly commandContext: (cause: string) => CommandContext;
@@ -4,7 +4,7 @@ import { isRunnerQuotaOutcome, reportUnappliedRunnerQuotaRetry, runnerQuotaMessa
4
4
  import { ActivationClaimConflictError, isActiveRunStatus, NoEligibleRunnerError, RunStatus, WorkspaceMode, } from '../../execution/index.js';
5
5
  import { DelegationPendingError, WorkflowStatus } from '../../orchestration/index.js';
6
6
  import { neutralDispatchPriority } from '../domain/dispatch-policy.js';
7
- import { canDispatchWorkItem, readOpenWorkItemIds, } from '../domain/open-work-policy.js';
7
+ import { canDispatchWorkItem, readOpenWorkItemIds } from '../domain/open-work-policy.js';
8
8
  import { isExecutionFailureTerminal } from './execution-reconciliation.js';
9
9
  export async function advancementWasDurablyApplied(orchestration, workflowInstanceId, activationId) {
10
10
  const current = await orchestration.get?.(workflowInstanceId);
@@ -43,13 +43,15 @@ export async function runDispatchLoop(pending, ctx) {
43
43
  break;
44
44
  }
45
45
  const inventory = (await ctx.orchestration.listAll?.()) ?? pending.map((item) => item.workflow);
46
- const capacity = await deliveryCapacity(inventory, allRuns, ctx.work, ctx.orchestration.deliveryRunLimit?.bind(ctx.orchestration));
46
+ // One synchronisation with Work per pass; every item is then answered from memory.
47
+ const delivery = await ctx.work?.deliverySnapshot();
48
+ const capacity = await deliveryCapacity(inventory, allRuns, delivery, ctx.orchestration.deliveryRunLimit?.bind(ctx.orchestration));
47
49
  // Legacy test ports may omit listAll; production supplies the journal-backed inventory.
48
50
  const open = ctx.maxOpenWorkItems === null
49
51
  ? new Set()
50
- : await readOpenWorkItemIds(inventory.map((workflow) => startedWorkflowIds.has(workflow.workflowInstanceId)
52
+ : readOpenWorkItemIds(inventory.map((workflow) => startedWorkflowIds.has(workflow.workflowInstanceId)
51
53
  ? { ...workflow, hasStartedActivation: true }
52
- : workflow), ctx.work);
54
+ : workflow), delivery);
53
55
  const selectedCandidate = ctx.dispatchPolicy.select(pending
54
56
  .filter((item) => !isolatedIds.has(item.workflow.workflowInstanceId) &&
55
57
  !capacity.unavailable.has(item.workflow.workItemId) &&
@@ -1,11 +1,8 @@
1
1
  import type { RunView } from '../../execution/index.js';
2
2
  import { type WorkflowInstanceView } from '../../orchestration/index.js';
3
- import { type WorkItemId } from '../../work/index.js';
4
- import type { WorkAvailability } from './open-work-policy.js';
3
+ import type { DeliverySnapshot, WorkItemId } from '../../work/index.js';
5
4
  /** A waiting workflow is not a Run; every actual Run, including watches, counts. */
6
- export declare function deliveryCapacity(workflows: readonly WorkflowInstanceView[], runs: readonly RunView[], work: {
7
- get(id: string): Promise<WorkAvailability | null>;
8
- } | undefined, runLimit: ((id: WorkItemId) => Promise<number | undefined>) | undefined): Promise<{
5
+ export declare function deliveryCapacity(workflows: readonly WorkflowInstanceView[], runs: readonly RunView[], work: DeliverySnapshot | undefined, runLimit: ((id: WorkItemId) => Promise<number | undefined>) | undefined): Promise<{
9
6
  roots: ReadonlyMap<string, string>;
10
7
  unavailable: ReadonlySet<string>;
11
8
  full: ReadonlySet<string>;
@@ -1,6 +1,5 @@
1
1
  import { isActiveRunStatus } from '../../execution/index.js';
2
2
  import { WorkflowStatus } from '../../orchestration/index.js';
3
- import { readDeliveryAvailability } from '../../work/index.js';
4
3
  /** A waiting workflow is not a Run; every actual Run, including watches, counts. */
5
4
  export async function deliveryCapacity(workflows, runs, work, runLimit) {
6
5
  const activeWorkflowIds = new Set(runs.filter((run) => isActiveRunStatus(run.status)).map((run) => run.workflowInstanceId));
@@ -10,12 +9,12 @@ export async function deliveryCapacity(workflows, runs, work, runLimit) {
10
9
  workflow.status !== WorkflowStatus.Superseded));
11
10
  const roots = new Map();
12
11
  const unavailable = new Set();
13
- await Promise.all([...new Set(live.map((workflow) => workflow.workItemId))].map(async (id) => {
14
- const delivery = work === undefined ? { rootWorkItemId: id } : await readDeliveryAvailability(id, work);
12
+ for (const id of new Set(live.map((workflow) => workflow.workItemId))) {
13
+ const delivery = work === undefined ? { rootWorkItemId: id } : work.availability(id);
15
14
  roots.set(id, delivery.rootWorkItemId);
16
15
  if (delivery.reason !== undefined)
17
16
  unavailable.add(id);
18
- }));
17
+ }
19
18
  const full = new Set();
20
19
  // A root without an active Run cannot be at a positive limit.
21
20
  await Promise.all([...activeRunCounts(live, runs, roots)].map(async ([root, count]) => {
@@ -1,15 +1,7 @@
1
1
  import { type WorkflowInstanceView } from '../../orchestration/index.js';
2
- import type { WorkDelegation } from '../../work/index.js';
3
- export interface WorkAvailability {
4
- readonly state?: string;
5
- readonly delegation?: WorkDelegation;
6
- readonly frozen?: boolean;
7
- readonly deleted?: boolean;
8
- }
2
+ import type { DeliverySnapshot } from '../../work/index.js';
9
3
  /** Distinct started items with any nonterminal workflow, including waiting children. */
10
4
  export declare function deriveOpenWorkItemIds(workflows: readonly WorkflowInstanceView[], unavailable?: ReadonlySet<string>): ReadonlySet<string>;
11
5
  export declare function hasOpenWorkCapacity(open: ReadonlySet<string>, limit: number | null): boolean;
12
6
  export declare function canDispatchWorkItem(workItemId: string, open: ReadonlySet<string>, limit: number | null): boolean;
13
- export declare function readOpenWorkItemIds(workflows: readonly WorkflowInstanceView[], work?: {
14
- get(id: string): Promise<WorkAvailability | null>;
15
- }): Promise<ReadonlySet<string>>;
7
+ export declare function readOpenWorkItemIds(workflows: readonly WorkflowInstanceView[], work?: DeliverySnapshot): ReadonlySet<string>;
@@ -1,5 +1,4 @@
1
1
  import { ActivityActivationStatus, WorkflowStatus, } from '../../orchestration/index.js';
2
- import { readDeliveryAvailability } from '../../work/index.js';
3
2
  /** Distinct started items with any nonterminal workflow, including waiting children. */
4
3
  export function deriveOpenWorkItemIds(workflows, unavailable = new Set()) {
5
4
  const started = new Set();
@@ -24,14 +23,14 @@ export function hasOpenWorkCapacity(open, limit) {
24
23
  export function canDispatchWorkItem(workItemId, open, limit) {
25
24
  return open.has(workItemId) || hasOpenWorkCapacity(open, limit);
26
25
  }
27
- export async function readOpenWorkItemIds(workflows, work) {
26
+ export function readOpenWorkItemIds(workflows, work) {
28
27
  const roots = new Set();
29
28
  for (const id of deriveOpenWorkItemIds(workflows)) {
30
29
  if (work === undefined) {
31
30
  roots.add(id);
32
31
  continue;
33
32
  }
34
- const delivery = await readDeliveryAvailability(id, work);
33
+ const delivery = work.availability(id);
35
34
  if (delivery.reason === undefined)
36
35
  roots.add(delivery.rootWorkItemId);
37
36
  }
@@ -1,4 +1,4 @@
1
- import { type EventJournal, type JournalReadOptions } from '@atolis-hq/eventing';
1
+ import { type EventJournal } 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, options?: JournalReadOptions): Promise<{
11
+ load(runId: RunId): 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, options) {
14
- const events = await this.journal.readStream(runStream(runId), options);
13
+ async load(runId) {
14
+ const events = await this.journal.readStream(runStream(runId));
15
15
  const decoded = events.map(decodeRunExecutionEvent);
16
16
  return { sequence: events.length, events: decoded, view: foldRun(decoded) };
17
17
  }
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
- import { reportedArtifactSchema } from '../../activities/index.js';
2
+ import { decidePullRequestOutputAuthority, PullRequestState, reportedArtifactSchema, } from '../../activities/index.js';
3
3
  import { RunStatus, WorkspaceMode } from '../../execution/index.js';
4
+ import { DelegationPrerequisiteCondition as Condition } from '../../orchestration/index.js';
4
5
  import { BuiltInResourceKind, ResourceCorrelationProvenance, ResourceCorrelationRole, resourceId, } from '../../resources/index.js';
5
6
  import { ArtifactVerificationResult } from '../contracts/artifact-vocabulary.js';
6
7
  const reportsSchema = z.object({ reportedArtifacts: z.array(reportedArtifactSchema) });
@@ -8,6 +9,15 @@ const reportsSchema = z.object({ reportedArtifacts: z.array(reportedArtifactSche
8
9
  export function createDelegationOutputs(input) {
9
10
  return {
10
11
  ...input.artifacts,
12
+ async prerequisiteOutput(child, output, condition) {
13
+ if ('path' in output &&
14
+ (condition === Condition.AcceptedOutput || condition === Condition.DeliveryReady)) {
15
+ return (await input.artifacts.qualifyingOutputs(child, [output]))?.[0] ?? null;
16
+ }
17
+ if (!('kind' in output))
18
+ return null;
19
+ return qualifyingChange(input, child, output, condition);
20
+ },
11
21
  async qualifyingOutputs(child, outputs) {
12
22
  const artifacts = outputs.filter((output) => 'path' in output);
13
23
  const references = artifacts.length === 0 ? [] : await input.artifacts.qualifyingOutputs(child, artifacts);
@@ -61,7 +71,7 @@ async function verifiedChange(input, report, source) {
61
71
  return null;
62
72
  return resource;
63
73
  }
64
- async function qualifyingChange(input, child, output) {
74
+ async function qualifyingChange(input, child, output, condition) {
65
75
  const runs = (await input.runs()).filter((run) => run.workflowInstanceId === child.workflowInstanceId &&
66
76
  run.status === RunStatus.Succeeded &&
67
77
  child.acceptedOutcomes.includes(run.activationId));
@@ -78,7 +88,8 @@ async function qualifyingChange(input, child, output) {
78
88
  const resource = await verifiedChange(input, latest.report, source);
79
89
  if (resource === null)
80
90
  return null;
81
- if (!(await hasOutputAuthority(input, child, output, resource.resourceId, revision)))
91
+ const observed = await qualifyingObservation(input, child, output, resource.resourceId, revision, condition);
92
+ if (observed === null)
82
93
  return null;
83
94
  return {
84
95
  name: output.name,
@@ -90,20 +101,45 @@ async function qualifyingChange(input, child, output) {
90
101
  branch,
91
102
  pullRequestBase: assignment.pullRequestBase,
92
103
  runId: run.runId,
104
+ ...(condition === undefined ? {} : { baseRevision: observed.baseRevision }),
105
+ ...(observed.state === PullRequestState.Merged ? { merged: true } : {}),
93
106
  },
94
107
  };
95
108
  }
109
+ async function qualifyingObservation(input, child, output, id, revision, condition) {
110
+ const owner = await input.resources.primaryCorrelation(id);
111
+ if (owner?.workItemId !== child.workItemId ||
112
+ owner.role !== ResourceCorrelationRole.Primary ||
113
+ owner.provenance !== ResourceCorrelationProvenance.AgentReported)
114
+ return null;
115
+ const observed = await input.pullRequests.get(id);
116
+ if (observed === null || observed.headRevision !== revision)
117
+ return null;
118
+ if (condition === Condition.MergedChange) {
119
+ if (observed.state !== PullRequestState.Merged)
120
+ return null;
121
+ }
122
+ else if (!(await hasOutputAuthority(input, child, condition === Condition.PushedChange
123
+ ? { ...output, requireReview: false, requireChecks: false }
124
+ : output, id, revision)))
125
+ return null;
126
+ return observed;
127
+ }
96
128
  async function hasOutputAuthority(input, child, output, id, revision) {
97
129
  const owner = await input.resources.primaryCorrelation(id);
98
130
  if (owner?.workItemId !== child.workItemId ||
99
131
  owner.role !== ResourceCorrelationRole.Primary ||
100
132
  owner.provenance !== ResourceCorrelationProvenance.AgentReported)
101
133
  return false;
102
- const authority = await input.pullRequests.decideAuthority(child.workItemId, {
134
+ const options = {
103
135
  target: { resourceId: id },
104
136
  requireAcceptedReview: output.requireReview ?? true,
105
137
  requireChecks: output.requireChecks ?? true,
106
- });
138
+ };
139
+ const observed = await input.pullRequests.get(id);
140
+ const authority = observed?.state === PullRequestState.Merged
141
+ ? decidePullRequestOutputAuthority(await input.pullRequests.authorityInput(child.workItemId), options)
142
+ : await input.pullRequests.decideAuthority(child.workItemId, options);
107
143
  if (!authority.allowed || authority.revision !== revision)
108
144
  return false;
109
145
  return true;
@@ -4,5 +4,13 @@ export declare const JOURNAL_CHANGE_FALLBACK_MS = 30000;
4
4
  export interface CachedJournalView<Value> {
5
5
  get(): Promise<Value>;
6
6
  }
7
- export declare function cachedJournalView<Value>(journal: Pick<EventJournal, 'readAll' | 'latestGlobalPosition'>, derive: (events: readonly EventEnvelope[]) => Value | Promise<Value>, _now?: () => number): CachedJournalView<Value>;
7
+ type ViewJournal = Pick<EventJournal, 'readAll' | 'latestGlobalPosition'>;
8
+ export declare function cachedJournalView<Value>(journal: ViewJournal, derive: (events: readonly EventEnvelope[]) => Value | Promise<Value>, _now?: () => number): CachedJournalView<Value>;
9
+ export interface JournalFold<Value> {
10
+ readonly initial: Value;
11
+ /** Returns the value after `events`, which follow everything already folded. Must not mutate `value`. */
12
+ extend(value: Value, events: readonly EventEnvelope[]): Value;
13
+ }
14
+ export declare function foldedJournalView<Value>(journal: ViewJournal, fold: JournalFold<Value>): CachedJournalView<Value>;
15
+ export {};
8
16
  //# sourceMappingURL=cached-journal-view.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cached-journal-view.d.ts","sourceRoot":"","sources":["../../src/runtime/cached-journal-view.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAE9D,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAEjD,MAAM,WAAW,iBAAiB,CAAC,KAAK;IACtC,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;CACvB;AAUD,wBAAgB,iBAAiB,CAAC,KAAK,EACrC,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,SAAS,GAAG,sBAAsB,CAAC,EAC/D,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,aAAa,EAAE,KAAK,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,EACpE,IAAI,GAAE,MAAM,MAAiB,GAC5B,iBAAiB,CAAC,KAAK,CAAC,CAa1B"}
1
+ {"version":3,"file":"cached-journal-view.d.ts","sourceRoot":"","sources":["../../src/runtime/cached-journal-view.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAE9D,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAEjD,MAAM,WAAW,iBAAiB,CAAC,KAAK;IACtC,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;CACvB;AAED,KAAK,WAAW,GAAG,IAAI,CAAC,YAAY,EAAE,SAAS,GAAG,sBAAsB,CAAC,CAAC;AAe1E,wBAAgB,iBAAiB,CAAC,KAAK,EACrC,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,aAAa,EAAE,KAAK,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,EACpE,IAAI,GAAE,MAAM,MAAiB,GAC5B,iBAAiB,CAAC,KAAK,CAAC,CAsB1B;AAED,MAAM,WAAW,WAAW,CAAC,KAAK;IAChC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IACxB,yGAAyG;IACzG,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,aAAa,EAAE,GAAG,KAAK,CAAC;CAC/D;AAMD,wBAAgB,iBAAiB,CAAC,KAAK,EACrC,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,GACvB,iBAAiB,CAAC,KAAK,CAAC,CA+B1B"}
@@ -7,18 +7,73 @@ export const JOURNAL_CHANGE_FALLBACK_MS = 30_000;
7
7
  // incremental batch. Depends only on the EventJournal port, not on any
8
8
  // concrete persistence implementation, so it lives in Eventing rather than
9
9
  // Persistence; domain and adapter modules may call it directly.
10
+ //
11
+ // The cached value is keyed by the last position actually read, never by the
12
+ // position sampled beforehand, so a value can never be held under a position
13
+ // newer than the events it was derived from. Callers that find the journal
14
+ // unchanged since a derivation already running share it.
10
15
  export function cachedJournalView(journal, derive, _now = Date.now) {
11
16
  let cache;
17
+ let inFlight;
12
18
  return {
13
19
  async get() {
14
- const position = await journal.latestGlobalPosition();
15
- if (cache !== undefined && cache.position === position)
20
+ const sampled = await journal.latestGlobalPosition();
21
+ if (cache !== undefined && cache.position >= sampled)
16
22
  return cache.value;
17
- const value = await derive(await journal.readAll(0));
18
- // Keep the position sampled before the read. An append racing the read
19
- // must force another derivation rather than being hidden by the cache.
20
- cache = { position, value };
21
- return value;
23
+ if (inFlight?.sampled === sampled)
24
+ return inFlight.run;
25
+ const run = (async () => {
26
+ const events = await journal.readAll(0);
27
+ const value = await derive(events);
28
+ const position = events.at(-1)?.globalPosition ?? 0;
29
+ // An older derivation finishing late must not replace a newer one.
30
+ if (cache === undefined || position >= cache.position)
31
+ cache = { position, value };
32
+ return value;
33
+ })().finally(() => {
34
+ if (inFlight?.run === run)
35
+ inFlight = undefined;
36
+ });
37
+ inFlight = { sampled, run };
38
+ return run;
39
+ },
40
+ };
41
+ }
42
+ // An incremental counterpart of cachedJournalView for views that can be
43
+ // extended event by event: each journal move folds only the new events instead
44
+ // of re-deriving history. Folds are serialised, so every event is applied once
45
+ // and in order however many callers ask at the same time.
46
+ export function foldedJournalView(journal, fold) {
47
+ let state = {
48
+ position: 0,
49
+ value: fold.initial,
50
+ };
51
+ let running;
52
+ return {
53
+ async get() {
54
+ for (;;) {
55
+ const sampled = await journal.latestGlobalPosition();
56
+ if (state.position >= sampled)
57
+ return state.value;
58
+ if (running !== undefined) {
59
+ await running.catch(() => undefined);
60
+ continue;
61
+ }
62
+ const run = (async () => {
63
+ const events = await journal.readAll(state.position);
64
+ if (events.length > 0)
65
+ state = {
66
+ position: events.at(-1).globalPosition,
67
+ value: fold.extend(state.value, events),
68
+ };
69
+ return state.value;
70
+ })().finally(() => {
71
+ if (running === run)
72
+ running = undefined;
73
+ });
74
+ running = run;
75
+ return run;
76
+ }
22
77
  },
23
78
  };
24
79
  }
@@ -2,13 +2,10 @@ import type { EventData, EventEnvelope, StreamRef } from '../contracts/events.js
2
2
  import type { JournalChangeSignal } from '../subscriptions/journal-change-signal.js';
3
3
  export declare class WrongExpectedSequenceError extends Error {
4
4
  }
5
- export interface JournalReadOptions {
6
- /** Observe committed appends from other hosts without using a bounded freshness cache. */
7
- readonly fresh?: boolean;
8
- }
9
5
  export interface EventJournal {
10
6
  appendToStream(stream: StreamRef, expectedSequence: number, events: readonly EventData[]): Promise<readonly EventEnvelope[]>;
11
- readStream(stream: StreamRef, options?: JournalReadOptions): Promise<readonly EventEnvelope[]>;
7
+ /** Every read observes all appends committed before the call, including by other processes. */
8
+ readStream(stream: StreamRef): Promise<readonly EventEnvelope[]>;
12
9
  /** Optional bounded, cancellable snapshot reader for adapters with a streaming source. */
13
10
  scanAll?(signal?: AbortSignal): AsyncIterable<EventEnvelope>;
14
11
  readAll(afterGlobalPosition: number, limit?: number): Promise<readonly EventEnvelope[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"event-journal.d.ts","sourceRoot":"","sources":["../../src/store/event-journal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2CAA2C,CAAC;AAErF,qBAAa,0BAA2B,SAAQ,KAAK;CAAG;AAExD,MAAM,WAAW,kBAAkB;IACjC,0FAA0F;IAC1F,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,YAAY;IAC3B,cAAc,CACZ,MAAM,EAAE,SAAS,EACjB,gBAAgB,EAAE,MAAM,EACxB,MAAM,EAAE,SAAS,SAAS,EAAE,GAC3B,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACrC,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IAC/F,0FAA0F;IAC1F,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC;IAC7D,OAAO,CAAC,mBAAmB,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACxF,UAAU,CAAC,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IAI9F,oBAAoB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,kBAAkB,CAChB,mBAAmB,EAAE,MAAM,EAC3B,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAAC;IAGjB,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAAC;CAC5C"}
1
+ {"version":3,"file":"event-journal.d.ts","sourceRoot":"","sources":["../../src/store/event-journal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2CAA2C,CAAC;AAErF,qBAAa,0BAA2B,SAAQ,KAAK;CAAG;AAExD,MAAM,WAAW,YAAY;IAC3B,cAAc,CACZ,MAAM,EAAE,SAAS,EACjB,gBAAgB,EAAE,MAAM,EACxB,MAAM,EAAE,SAAS,SAAS,EAAE,GAC3B,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACrC,+FAA+F;IAC/F,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACjE,0FAA0F;IAC1F,OAAO,CAAC,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC;IAC7D,OAAO,CAAC,mBAAmB,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IACxF,UAAU,CAAC,CAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,aAAa,EAAE,CAAC,CAAC;IAI9F,oBAAoB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,kBAAkB,CAChB,mBAAmB,EAAE,MAAM,EAC3B,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAAC;IAGjB,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAAC;CAC5C"}