@atolis-hq/wake 0.4.61 → 0.4.62

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 (39) hide show
  1. package/dist/src/bootstrap/composition-root.d.ts +4 -1
  2. package/dist/src/bootstrap/composition-root.js +5 -2
  3. package/dist/src/bootstrap/integration-runtime.d.ts +4 -2
  4. package/dist/src/bootstrap/integration-runtime.js +74 -13
  5. package/dist/src/bootstrap/trigger-checkpoint-store.d.ts +5 -5
  6. package/dist/src/bootstrap/trigger-checkpoint-store.js +15 -40
  7. package/dist/src/bootstrap/version.js +1 -1
  8. package/dist/src/control-plane/application/control-plane-projection.js +11 -1
  9. package/dist/src/control-plane/application/runner-pipeline.js +0 -2
  10. package/dist/src/control-plane/application/trigger-facts.d.ts +11 -0
  11. package/dist/src/control-plane/application/trigger-facts.js +38 -0
  12. package/dist/src/control-plane/application/trigger-invocation-projection.d.ts +4 -0
  13. package/dist/src/control-plane/application/trigger-invocation-projection.js +32 -0
  14. package/dist/src/control-plane/application/trigger-projection.d.ts +24 -0
  15. package/dist/src/control-plane/application/trigger-projection.js +98 -0
  16. package/dist/src/control-plane/application/trigger-reactor.d.ts +4 -0
  17. package/dist/src/control-plane/application/trigger-reactor.js +30 -0
  18. package/dist/src/control-plane/application/trigger-recovery.d.ts +10 -0
  19. package/dist/src/control-plane/application/trigger-recovery.js +61 -0
  20. package/dist/src/control-plane/application/trigger-rules.d.ts +19 -0
  21. package/dist/src/control-plane/application/trigger-rules.js +38 -0
  22. package/dist/src/control-plane/application/trigger-service.d.ts +34 -20
  23. package/dist/src/control-plane/application/trigger-service.js +182 -102
  24. package/dist/src/control-plane/application/trigger-source-cursor.d.ts +16 -0
  25. package/dist/src/control-plane/application/trigger-source-cursor.js +35 -0
  26. package/dist/src/control-plane/application/trigger-state-reader.d.ts +13 -0
  27. package/dist/src/control-plane/application/trigger-state-reader.js +34 -0
  28. package/dist/src/control-plane/application/trigger-workflow.d.ts +17 -0
  29. package/dist/src/control-plane/application/trigger-workflow.js +18 -0
  30. package/dist/src/control-plane/contracts/event-factory.js +10 -0
  31. package/dist/src/control-plane/contracts/events.d.ts +50 -3
  32. package/dist/src/control-plane/contracts/events.js +100 -4
  33. package/dist/src/control-plane/contracts/streams.d.ts +9 -2
  34. package/dist/src/control-plane/contracts/streams.js +13 -1
  35. package/dist/src/control-plane/domain/cron-trigger-policy.d.ts +1 -1
  36. package/dist/src/control-plane/domain/cron-trigger-policy.js +3 -1
  37. package/dist/src/control-plane/index.d.ts +3 -0
  38. package/dist/src/control-plane/index.js +3 -0
  39. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import { type CheckpointStore, type EventJournal, type ProcessorRunSerialiser, type ProcessorStateStore, type ProjectionStore } from '@atolis-hq/eventing';
2
2
  import { createPullRequestService, type ActivityRegistry } from '../activities/index.js';
3
3
  import { createArtifactService } from '../artifacts/index.js';
4
- import { createControlPlaneService, createRunnerControlService, type ActivationScheduler, type ActivationSchedulerSubscriber, type IntakePipeline, type RunnerPipeline, type TriggerCheckpointStore } from '../control-plane/index.js';
4
+ import { createControlPlaneService, createRunnerControlService, type ActivationScheduler, type ActivationSchedulerSubscriber, type IntakePipeline, type RunnerPipeline, type TriggerCheckpointStore, type TriggerConfig, type TriggerService } from '../control-plane/index.js';
5
5
  import { createConversationService } from '../conversations/index.js';
6
6
  import { RecoveryService, TranscriptStore, createExecutionService, type FakeScenarioResolver, type Runner } from '../execution/index.js';
7
7
  import type { DeliveryService, DurableFakeDeliveryProvider, ExternalDeliveryAdapter, ProviderCompositionFailure, ProviderInstance } from '../integrations/index.js';
@@ -33,12 +33,15 @@ export interface CompositionRootOptions {
33
33
  readonly decorateCheckpoints?: (checkpoints: CheckpointStore) => CheckpointStore;
34
34
  readonly subscriptionRunSerialiser?: ProcessorRunSerialiser;
35
35
  readonly triggerCheckpoints?: TriggerCheckpointStore;
36
+ /** Trusted context binding supplies a shared scope identity; absent bindings use the trigger ID. */
37
+ readonly triggerCoordinationKey?: (config: TriggerConfig) => string;
36
38
  readonly decorateDeliveryAdapter?: (adapter: ExternalDeliveryAdapter, provider: ProviderInstance) => ExternalDeliveryAdapter;
37
39
  readonly decorateRunner?: (runner: Runner, name: string) => Runner;
38
40
  /** A real fake-provider adapter for deterministic composed integration evidence. */
39
41
  readonly fakeDeliveryProvider?: DurableFakeDeliveryProvider;
40
42
  }
41
43
  export interface CompositionRoot {
44
+ readonly triggers: TriggerService;
42
45
  readonly config: ResolvedWakeModulesConfig;
43
46
  readonly fakeScenarios: FakeScenarioResolver;
44
47
  readonly paths: WakePaths;
@@ -21,7 +21,7 @@ import { composePersistence } from './persistence-composition.js';
21
21
  import { createRunnerQuotaReporter } from './runner-quota-reporter.js';
22
22
  import { createRunnerRegistry } from './runner-registry.js';
23
23
  import { createTranscriptRetention } from './transcript-retention.js';
24
- import { FileTriggerCheckpointStore } from './trigger-checkpoint-store.js';
24
+ import { ProcessorTriggerCheckpointStore } from './trigger-checkpoint-store.js';
25
25
  import { createUpdateMaintenanceLease, } from './update-maintenance-lease.js';
26
26
  const resourceLinkResolvers = {
27
27
  github: resolveGitHubResourceUrl,
@@ -174,7 +174,10 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
174
174
  processorRuntime,
175
175
  activities,
176
176
  runners,
177
- triggerCheckpoints: options.triggerCheckpoints ?? new FileTriggerCheckpointStore(paths.dataRoot),
177
+ triggerCheckpoints: options.triggerCheckpoints ?? new ProcessorTriggerCheckpointStore(processorState),
178
+ ...(options.triggerCoordinationKey === undefined
179
+ ? {}
180
+ : { triggerCoordinationKey: options.triggerCoordinationKey }),
178
181
  ...(options.decorateDeliveryAdapter === undefined
179
182
  ? {}
180
183
  : { decorateDeliveryAdapter: options.decorateDeliveryAdapter }),
@@ -1,17 +1,18 @@
1
1
  import { type CheckpointStore, type EventJournal, type EventProcessor, type ProcessorRunSerialiser, type ProcessorStateStore, type ProjectionStore } from '@atolis-hq/eventing';
2
2
  import { type ActivityRegistry, type createPullRequestService } from '../activities/index.js';
3
- import { type IntakePipeline, type RunnerPipeline, type TriggerCheckpointStore, type createControlPlaneService } from '../control-plane/index.js';
3
+ import { TriggerService, type IntakePipeline, type RunnerPipeline, type TriggerCheckpointStore, type TriggerConfig, type createControlPlaneService } from '../control-plane/index.js';
4
4
  import type { createConversationService } from '../conversations/index.js';
5
5
  import { type RunnerRegistry, type createExecutionService } from '../execution/index.js';
6
6
  import { DeliveryService, type DurableFakeDeliveryProvider, type ExternalDeliveryAdapter, type ProviderCompositionFailure, type ProviderDefinition, type ProviderInstance } from '../integrations/index.js';
7
7
  import { type Clock, type UlidIdGenerator } from '../kernel/index.js';
8
8
  import { type createOrchestrationService } from '../orchestration/index.js';
9
9
  import { type createResourceLookup, type createResourceService } from '../resources/index.js';
10
- import type { createWorkService } from '../work/index.js';
10
+ import { type createWorkService } from '../work/index.js';
11
11
  import type { ResolvedWakeModulesConfig } from './config/load-config.js';
12
12
  import type { EventProcessorRuntime } from './event-processor-runtime.js';
13
13
  import { type RuntimeProjectionSubscriptions } from './projection-runtime.js';
14
14
  export interface IntegrationRuntime {
15
+ readonly triggers: TriggerService;
15
16
  readonly projectionSubscriptions: RuntimeProjectionSubscriptions;
16
17
  readonly processors: readonly EventProcessor[];
17
18
  readonly providers: readonly ProviderInstance[];
@@ -44,6 +45,7 @@ export interface IntegrationRuntimeInput {
44
45
  readonly subscriptionRunSerialiser: ProcessorRunSerialiser;
45
46
  readonly processorRuntime: EventProcessorRuntime;
46
47
  readonly triggerCheckpoints: TriggerCheckpointStore;
48
+ readonly triggerCoordinationKey?: (config: TriggerConfig) => string;
47
49
  readonly decorateDeliveryAdapter?: (adapter: ExternalDeliveryAdapter, provider: ProviderInstance) => ExternalDeliveryAdapter;
48
50
  readonly fakeDeliveryProvider?: DurableFakeDeliveryProvider;
49
51
  readonly providerDefinitions: readonly ProviderDefinition[];
@@ -1,12 +1,14 @@
1
- import { EventActorKind, } from '@atolis-hq/eventing';
1
+ import { EventActorKind, correlationId, } from '@atolis-hq/eventing';
2
2
  import { withFileLock } from '@atolis-hq/eventing-filesystem';
3
3
  import { join } from 'node:path';
4
+ import { z } from 'zod';
4
5
  import { ActivityExecutionKind, activityName, } from '../activities/index.js';
5
- import { ControlStreamKind, TriggerService, createDurableRunnerIneligibility, createIntakePipeline, createRunnerPipeline, createTriggerReactor, createWorkCancellationPolicy, } from '../control-plane/index.js';
6
- import { NoEligibleRunnerError, RunRepository, createRuntimeMemoryProfile, isActiveRunStatus, } from '../execution/index.js';
6
+ import { ControlEventType, ControlStreamKind, TriggerService, createDurableRunnerIneligibility, createIntakePipeline, createRunnerPipeline, createTriggerReactor, createWorkCancellationPolicy, selectControlEvent, triggerInvocationProjection, triggerInvocationStream, } from '../control-plane/index.js';
7
+ import { ExecutionStreamKind, NoEligibleRunnerError, RunRepository, createRuntimeMemoryProfile, decodeActivationExecutionEvent, isActiveRunStatus, isRunStream, runId, } from '../execution/index.js';
7
8
  import { AgentRunPublicationReactor, ArtifactRegistrationReactor, DeliveryOutcomeReactor, DeliveryService, IntegrationStreamKind, PollService, ProviderRegistry, deliveryProjectionDefinitions, fakeProviderDefinition, } from '../integrations/index.js';
8
- import { compileWorkflowSelectors, createConversationCommandReactor, createPullRequestTransitionEvidence, createResourceTransitionReactor, createWatchReactor, createWatchReconciler, selectWorkflow, workflowName, } from '../orchestration/index.js';
9
+ import { compileWorkflowSelectors, createConversationCommandReactor, createPullRequestTransitionEvidence, createResourceTransitionReactor, createWatchReactor, createWatchReconciler, isWorkflowInstanceStream, selectWorkflow, workflowInstanceId, workflowName, } from '../orchestration/index.js';
9
10
  import { BuiltInResourceCapability, resourceId, } from '../resources/index.js';
11
+ import { isWorkItemStream } from '../work/index.js';
10
12
  import { decodeCanonicalEvent } from './canonical-events.js';
11
13
  import { hydrateFakeProviderEvidence } from './fake-provider-files.js';
12
14
  import { createRuntimeProjectionSubscriptions, } from './projection-runtime.js';
@@ -68,7 +70,51 @@ export async function composeIntegrationRuntime(input) {
68
70
  now: () => input.clock.now().toISOString(),
69
71
  });
70
72
  const runnerIneligibility = createDurableRunnerIneligibility(input.journal, () => input.clock.now().toISOString());
73
+ const triggerRuns = new RunRepository(input.journal);
71
74
  const triggers = new TriggerService({
75
+ state: input.processorState,
76
+ history: async () => {
77
+ await projectionSubscriptions.catchUp(triggerInvocationProjection);
78
+ return (await input.projections.list(triggerInvocationProjection.name)).flatMap(({ value }) => (value === null ? [] : [value]));
79
+ },
80
+ invocationScope: async (item) => {
81
+ const claim = (await input.journal.readStream(triggerInvocationStream(item)))
82
+ .map(selectControlEvent)
83
+ .find((event) => event?.event.eventType === ControlEventType.TriggerInvocationClaimed);
84
+ return claim?.event.eventType === ControlEventType.TriggerInvocationClaimed
85
+ ? claim.event.payload.coordinationKey
86
+ : null;
87
+ },
88
+ ...(input.triggerCoordinationKey === undefined
89
+ ? {}
90
+ : { coordinationKey: input.triggerCoordinationKey }),
91
+ serialise: input.subscriptionRunSerialiser,
92
+ configurations: () => input.config.controlPlane.triggers,
93
+ isDispatchPaused: input.isPaused,
94
+ sourceWorkItem: async (event) => {
95
+ if (isWorkItemStream(event.stream))
96
+ return event.stream.id;
97
+ let workflowId;
98
+ if (isWorkflowInstanceStream(event.stream))
99
+ workflowId = event.stream.id;
100
+ if (isRunStream(event.stream))
101
+ workflowId = (await triggerRuns.load(runId(event.stream.id))).view?.workflowInstanceId;
102
+ if (event.stream.kind === ExecutionStreamKind.Activation)
103
+ workflowId = (await triggerRuns.load(runId(decodeActivationExecutionEvent(event).event.payload.runId))).view?.workflowInstanceId;
104
+ if (workflowId === undefined)
105
+ return null;
106
+ const seen = new Set();
107
+ while (!seen.has(workflowId)) {
108
+ seen.add(workflowId);
109
+ const instance = await input.orchestration.get(workflowInstanceId(workflowId));
110
+ if (instance === null)
111
+ return null;
112
+ if (instance.parentWorkflowInstanceId === undefined)
113
+ return instance.workItemId;
114
+ workflowId = instance.parentWorkflowInstanceId;
115
+ }
116
+ throw new Error('Cyclic workflow provenance');
117
+ },
72
118
  journal: input.journal,
73
119
  capacityAvailable: async (trigger) => {
74
120
  if (await input.isPaused())
@@ -102,15 +148,23 @@ export async function composeIntegrationRuntime(input) {
102
148
  });
103
149
  const triggerReactors = input.config.controlPlane.triggers.map((config) => createTriggerReactor(triggers, config, decodeCanonicalEvent));
104
150
  // Registration starts at the current journal boundary; historical facts are not sources.
151
+ const registrationSchema = z.object({ position: z.number().int().nonnegative() }).strict();
105
152
  for (const reactor of triggerReactors) {
106
- if ((await input.processorState.read(reactor.consumer, 'registration')) !== null)
107
- continue;
108
- const position = await input.journal.latestGlobalPosition();
109
- await input.checkpoints.save(reactor.consumer, position);
110
- await input.processorState.write({
111
- consumer: reactor.consumer,
112
- key: 'registration',
113
- value: { position },
153
+ await input.subscriptionRunSerialiser(reactor.consumer, new AbortController().signal, async () => {
154
+ const stored = await input.processorState.read(reactor.consumer, 'registration');
155
+ const position = stored === null
156
+ ? await input.journal.latestGlobalPosition()
157
+ : registrationSchema.parse(stored.value).position;
158
+ if (stored === null)
159
+ await input.processorState.write({
160
+ consumer: reactor.consumer,
161
+ key: 'registration',
162
+ value: { position },
163
+ });
164
+ // The registration boundary is written first so an interrupted checkpoint save
165
+ // can recover that boundary without skipping sources observed in the meantime.
166
+ if ((await input.checkpoints.load(reactor.consumer)) < position)
167
+ await input.checkpoints.save(reactor.consumer, position);
114
168
  });
115
169
  }
116
170
  const artifacts = new ArtifactRegistrationReactor({
@@ -178,10 +232,16 @@ export async function composeIntegrationRuntime(input) {
178
232
  const runnerPipeline = createRunnerPipeline({
179
233
  isPaused: input.isPaused,
180
234
  runTriggers: () => observeMemory(memoryProfile, 'runner.triggers', async () => {
235
+ await triggers.reconcile({
236
+ commandId: input.ids.next('command'),
237
+ correlationId: correlationId('trigger-recovery'),
238
+ occurredAt: input.clock.now().toISOString(),
239
+ actor: { kind: EventActorKind.System, id: ControlStreamKind.Global },
240
+ });
181
241
  for (const trigger of input.config.controlPlane.triggers)
182
242
  await triggers.run(trigger, {
183
243
  commandId: input.ids.next('command'),
184
- correlationId: 'trigger-tick',
244
+ correlationId: correlationId('trigger-tick'),
185
245
  occurredAt: input.clock.now().toISOString(),
186
246
  actor: { kind: EventActorKind.System, id: ControlStreamKind.Global },
187
247
  });
@@ -207,6 +267,7 @@ export async function composeIntegrationRuntime(input) {
207
267
  });
208
268
  const requestImmediatePoll = createImmediatePollRequester(providers, input.journal, input.wakeRoot);
209
269
  return {
270
+ triggers,
210
271
  projectionSubscriptions,
211
272
  processors: [
212
273
  ...projectionSubscriptions.processors,
@@ -1,9 +1,9 @@
1
+ import type { ProcessorStateStore } from '@atolis-hq/eventing';
1
2
  import type { TriggerCheckpointStore } from '../control-plane/index.js';
2
- /** Durable timestamp cursor for TriggerService; journal checkpoints only store numeric offsets. */
3
- export declare class FileTriggerCheckpointStore implements TriggerCheckpointStore {
4
- private readonly dataRoot;
5
- constructor(dataRoot: string);
3
+ /** Cron watermarks are processor recovery state, separate from rebuildable projections. */
4
+ export declare class ProcessorTriggerCheckpointStore implements TriggerCheckpointStore {
5
+ private readonly state;
6
+ constructor(state: ProcessorStateStore);
6
7
  load(triggerId: string): Promise<string | null>;
7
8
  save(triggerId: string, slot: string): Promise<void>;
8
- private path;
9
9
  }
@@ -1,49 +1,24 @@
1
- import { mkdir, open, readFile, rename, rm } from 'node:fs/promises';
2
- import { dirname, join } from 'node:path';
3
- /** Durable timestamp cursor for TriggerService; journal checkpoints only store numeric offsets. */
4
- export class FileTriggerCheckpointStore {
5
- dataRoot;
6
- constructor(dataRoot) {
7
- this.dataRoot = dataRoot;
1
+ import { z } from 'zod';
2
+ const cursorSchema = z.object({ slot: z.string().datetime({ offset: true }) }).strict();
3
+ /** Cron watermarks are processor recovery state, separate from rebuildable projections. */
4
+ export class ProcessorTriggerCheckpointStore {
5
+ state;
6
+ constructor(state) {
7
+ this.state = state;
8
8
  }
9
9
  async load(triggerId) {
10
- try {
11
- const parsed = JSON.parse(await readFile(this.path(triggerId), 'utf8'));
12
- if (parsed.triggerId !== triggerId || Number.isNaN(Date.parse(parsed.slot)))
13
- throw new Error(`Invalid trigger checkpoint: ${triggerId}`);
14
- return parsed.slot;
15
- }
16
- catch (error) {
17
- if (error.code === 'ENOENT')
18
- return null;
19
- throw error;
20
- }
10
+ const stored = await this.state.read(`reactor:control-plane.trigger:${triggerId}`, 'cron');
11
+ return stored === null ? null : cursorSchema.parse(stored.value).slot;
21
12
  }
22
13
  async save(triggerId, slot) {
14
+ const value = cursorSchema.parse({ slot });
23
15
  const existing = await this.load(triggerId);
24
16
  if (existing !== null && Date.parse(slot) < Date.parse(existing))
25
17
  throw new Error(`Trigger checkpoint regression: ${triggerId}`);
26
- const path = this.path(triggerId);
27
- await mkdir(dirname(path), { recursive: true });
28
- const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;
29
- try {
30
- const handle = await open(temporary, 'wx');
31
- try {
32
- await handle.writeFile(`${JSON.stringify({ triggerId, slot })}\n`, 'utf8');
33
- await handle.sync();
34
- }
35
- finally {
36
- await handle.close();
37
- }
38
- await rename(temporary, path);
39
- }
40
- finally {
41
- await rm(temporary, { force: true });
42
- }
43
- }
44
- path(triggerId) {
45
- if (triggerId.length === 0 || /[\\/]/.test(triggerId))
46
- throw new Error('Trigger id must not contain path separators');
47
- return join(this.dataRoot, 'trigger-checkpoints', `${encodeURIComponent(triggerId)}.json`);
18
+ await this.state.write({
19
+ consumer: `reactor:control-plane.trigger:${triggerId}`,
20
+ key: 'cron',
21
+ value,
22
+ });
48
23
  }
49
24
  }
@@ -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.61";
127
+ export const wakeVersion = "v0.4.62";
128
128
  /** The registry-installable version used by packaged sandbox images. */
129
129
  export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
@@ -1,5 +1,7 @@
1
1
  import { ControlEventType, selectControlEvent } from '../contracts/events.js';
2
2
  import { ControlStreamKind, controlPlaneStream } from '../contracts/streams.js';
3
+ import { triggerInvocationProjection } from './trigger-invocation-projection.js';
4
+ import { triggerProjection } from './trigger-projection.js';
3
5
  export function ineligibleRunners(view, now) {
4
6
  const current = Date.parse(now);
5
7
  return new Set(Object.entries(view.runnerPauses)
@@ -24,7 +26,8 @@ export function createDurableRunnerIneligibility(journal, now) {
24
26
  export const controlPlaneProjection = {
25
27
  name: ControlStreamKind.Global,
26
28
  select(event) {
27
- return selectControlEvent(event) === null ? null : { key: 'global' };
29
+ const control = selectControlEvent(event);
30
+ return control?.stream.kind === ControlStreamKind.Global ? { key: 'global' } : null;
28
31
  },
29
32
  initial: () => ({ dispatchPausedUntil: null, runnerPauses: {} }),
30
33
  project(previous, envelope) {
@@ -32,6 +35,11 @@ export const controlPlaneProjection = {
32
35
  if (event === null)
33
36
  return previous;
34
37
  switch (event.event.eventType) {
38
+ case ControlEventType.TriggerRequestRecorded:
39
+ case ControlEventType.TriggerInvocationClaimed:
40
+ case ControlEventType.TriggerPaused:
41
+ case ControlEventType.TriggerResumed:
42
+ case ControlEventType.TriggerInvocationFinished:
35
43
  case ControlEventType.TriggerInvocationStarted:
36
44
  return previous;
37
45
  case ControlEventType.DispatchPaused:
@@ -63,4 +71,6 @@ function withoutDispatchPause(previous) {
63
71
  }
64
72
  export const controlPlaneProjectionDefinitions = [
65
73
  controlPlaneProjection,
74
+ triggerProjection,
75
+ triggerInvocationProjection,
66
76
  ];
@@ -7,8 +7,6 @@
7
7
  export function createRunnerPipeline(stages) {
8
8
  const isPaused = async () => (await stages.isPaused?.()) ?? false;
9
9
  const runOnce = async (options, signal, beforeDelivery) => {
10
- if (await isPaused())
11
- return { kind: 'paused' };
12
10
  await stages.runTriggers();
13
11
  if (await isPaused())
14
12
  return { kind: 'paused' };
@@ -0,0 +1,11 @@
1
+ import { type CommandContext, type EventJournal } from '@atolis-hq/eventing';
2
+ import { type ControlEventPayloads, type TriggerSource } from '../contracts/events.js';
3
+ export type TriggerFactInput = {
4
+ [Type in keyof ControlEventPayloads]: {
5
+ readonly eventType: Type;
6
+ readonly payload: ControlEventPayloads[Type];
7
+ };
8
+ }[keyof ControlEventPayloads];
9
+ export declare function sourceIdentity(source: TriggerSource): string;
10
+ /** Invocation/command appends check event IDs; source appends use the caught-up scope sequence. */
11
+ export declare function appendTriggerFact(journal: EventJournal, occurredAt: string, input: TriggerFactInput, context: CommandContext, eventId: string, sequence?: number): Promise<void>;
@@ -0,0 +1,38 @@
1
+ import { EventSourceKind, WrongExpectedSequenceError, } from '@atolis-hq/eventing';
2
+ import { createControlPlaneEventData } from '../contracts/event-factory.js';
3
+ import { TriggerSourceKind, } from '../contracts/events.js';
4
+ import { triggerControlStream, triggerInvocationStream } from '../contracts/streams.js';
5
+ export function sourceIdentity(source) {
6
+ return `${source.kind}:${source.kind === TriggerSourceKind.Cron ? source.slot : source.eventId}`;
7
+ }
8
+ /** Invocation/command appends check event IDs; source appends use the caught-up scope sequence. */
9
+ export async function appendTriggerFact(journal, occurredAt, input, context, eventId, sequence) {
10
+ if (!('coordinationKey' in input.payload))
11
+ throw new Error('Expected trigger fact');
12
+ const stream = 'workItemId' in input.payload
13
+ ? triggerInvocationStream(input.payload.workItemId)
14
+ : triggerControlStream(input.payload.coordinationKey);
15
+ const draft = createControlPlaneEventData({
16
+ ...input,
17
+ eventId,
18
+ occurredAt,
19
+ correlationId: context.correlationId,
20
+ causationId: context.commandId,
21
+ actor: context.actor,
22
+ source: { kind: EventSourceKind.Internal, id: stream.kind },
23
+ });
24
+ for (let attempt = 0; attempt < 8; attempt += 1) {
25
+ const events = sequence === undefined ? await journal.readStream(stream) : null;
26
+ if (events?.some((envelope) => envelope.event.eventId === eventId))
27
+ return;
28
+ try {
29
+ await journal.appendToStream(stream, sequence ?? events.length, [draft]);
30
+ return;
31
+ }
32
+ catch (error) {
33
+ if (!(error instanceof WrongExpectedSequenceError) || attempt === 7)
34
+ throw error;
35
+ sequence = undefined;
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,4 @@
1
+ import type { ProjectionDefinition } from '@atolis-hq/eventing';
2
+ import type { TriggerInvocationView } from './trigger-projection.js';
3
+ /** One history document per top-level WorkItem, independent of operational scope state. */
4
+ export declare const triggerInvocationProjection: ProjectionDefinition<TriggerInvocationView | null>;
@@ -0,0 +1,32 @@
1
+ import { ControlEventType, selectControlEvent } from '../contracts/events.js';
2
+ import { ControlStreamKind } from '../contracts/streams.js';
3
+ /** One history document per top-level WorkItem, independent of operational scope state. */
4
+ export const triggerInvocationProjection = {
5
+ name: ControlStreamKind.TriggerInvocation,
6
+ select(envelope) {
7
+ const event = selectControlEvent(envelope);
8
+ return event?.stream.kind === ControlStreamKind.TriggerInvocation
9
+ ? { key: event.stream.id }
10
+ : null;
11
+ },
12
+ initial: () => null,
13
+ project(previous, envelope) {
14
+ const event = selectControlEvent(envelope);
15
+ if (event === null)
16
+ return previous;
17
+ switch (event.event.eventType) {
18
+ case ControlEventType.TriggerInvocationClaimed:
19
+ return { claim: event.event.payload, claimedAt: event.event.occurredAt };
20
+ case ControlEventType.TriggerInvocationStarted:
21
+ if (previous === null)
22
+ throw new Error('Trigger start without invocation claim');
23
+ return { ...previous, startedAt: event.event.occurredAt };
24
+ case ControlEventType.TriggerInvocationFinished:
25
+ if (previous === null)
26
+ throw new Error('Trigger result without invocation claim');
27
+ return { ...previous, result: event.event.payload, finishedAt: event.event.occurredAt };
28
+ default:
29
+ return previous;
30
+ }
31
+ },
32
+ };
@@ -0,0 +1,24 @@
1
+ import type { ProjectionDefinition } from '@atolis-hq/eventing';
2
+ import { ControlEventType, type ControlEventPayloads, type TriggerSource } from '../contracts/events.js';
3
+ export declare const triggerProjectionName = "control-plane-triggers";
4
+ export declare const maximumTriggerSources = 64;
5
+ export interface TriggerInvocationView {
6
+ readonly claim: ControlEventPayloads[typeof ControlEventType.TriggerInvocationClaimed];
7
+ readonly claimedAt: string;
8
+ readonly startedAt?: string;
9
+ readonly result?: ControlEventPayloads[typeof ControlEventType.TriggerInvocationFinished];
10
+ readonly finishedAt?: string;
11
+ readonly failureOutcomeCount?: number;
12
+ }
13
+ export interface TriggerView {
14
+ readonly lastStarts: Readonly<Record<string, string>>;
15
+ readonly pauses: Readonly<Record<string, string>>;
16
+ /** Latest recorded request per trigger, rather than an occurrence history. */
17
+ readonly requests: readonly ControlEventPayloads[typeof ControlEventType.TriggerRequestRecorded][];
18
+ readonly coalesced: Readonly<Record<string, ControlEventPayloads[typeof ControlEventType.TriggerRequestRecorded] & {
19
+ readonly sources: readonly TriggerSource[];
20
+ }>>;
21
+ /** Operational projections contain only active claims; explicit service inspection includes history. */
22
+ readonly invocations: readonly TriggerInvocationView[];
23
+ }
24
+ export declare const triggerProjection: ProjectionDefinition<TriggerView>;
@@ -0,0 +1,98 @@
1
+ import { ControlEventType, TriggerRequestDisposition, selectControlEvent, } from '../contracts/events.js';
2
+ export const triggerProjectionName = 'control-plane-triggers';
3
+ export const maximumTriggerSources = 64;
4
+ export const triggerProjection = {
5
+ name: triggerProjectionName,
6
+ select(envelope) {
7
+ const event = selectControlEvent(envelope);
8
+ return event !== null && 'coordinationKey' in event.event.payload
9
+ ? { key: event.event.payload.coordinationKey }
10
+ : null;
11
+ },
12
+ initial: () => ({ lastStarts: {}, pauses: {}, requests: [], coalesced: {}, invocations: [] }),
13
+ project(previous, envelope) {
14
+ const event = selectControlEvent(envelope);
15
+ if (event === null)
16
+ return previous;
17
+ switch (event.event.eventType) {
18
+ case ControlEventType.TriggerRequestRecorded: {
19
+ const request = event.event.payload;
20
+ return {
21
+ ...previous,
22
+ requests: [
23
+ ...previous.requests.filter((last) => last.triggerId !== request.triggerId),
24
+ request,
25
+ ],
26
+ coalesced: request.disposition === TriggerRequestDisposition.Coalesced
27
+ ? {
28
+ ...previous.coalesced,
29
+ [request.coordinationKey]: {
30
+ ...request,
31
+ sources: [
32
+ ...(previous.coalesced[request.coordinationKey]?.sources ?? []),
33
+ request.source,
34
+ ].slice(0, maximumTriggerSources),
35
+ },
36
+ }
37
+ : withoutCoalesced(previous.coalesced, request),
38
+ };
39
+ }
40
+ case ControlEventType.TriggerInvocationClaimed: {
41
+ const claim = event.event.payload;
42
+ const { [claim.coordinationKey]: _, ...coalesced } = previous.coalesced;
43
+ return {
44
+ ...previous,
45
+ coalesced,
46
+ invocations: [...previous.invocations, { claim, claimedAt: event.event.occurredAt }],
47
+ };
48
+ }
49
+ case ControlEventType.TriggerInvocationStarted: {
50
+ const payload = event.event.payload;
51
+ return {
52
+ ...previous,
53
+ lastStarts: { ...previous.lastStarts, [payload.triggerId]: event.event.occurredAt },
54
+ invocations: previous.invocations.map((invocation) => invocation.claim.workflowInstanceId === payload.workflowInstanceId
55
+ ? { ...invocation, startedAt: event.event.occurredAt }
56
+ : invocation),
57
+ };
58
+ }
59
+ case ControlEventType.TriggerInvocationFinished: {
60
+ const payload = event.event.payload;
61
+ return {
62
+ ...previous,
63
+ invocations: previous.invocations.filter((invocation) => invocation.claim.workflowInstanceId !== payload.workflowInstanceId),
64
+ };
65
+ }
66
+ case ControlEventType.TriggerPaused: {
67
+ const payload = event.event.payload;
68
+ return {
69
+ ...previous,
70
+ pauses: { ...previous.pauses, [payload.triggerId]: payload.reason },
71
+ invocations: pausedInvocations(previous, payload),
72
+ coalesced: Object.fromEntries(Object.entries(previous.coalesced).filter(([key]) => key !== payload.coordinationKey)),
73
+ };
74
+ }
75
+ case ControlEventType.TriggerResumed: {
76
+ const { [event.event.payload.triggerId]: _, ...pauses } = previous.pauses;
77
+ return { ...previous, pauses };
78
+ }
79
+ default:
80
+ return previous;
81
+ }
82
+ },
83
+ };
84
+ function withoutCoalesced(coalesced, request) {
85
+ const key = request.coordinationKey;
86
+ // An ineligible new occurrence does not discard another trigger's coalesced request.
87
+ if (coalesced[key]?.triggerId !== request.triggerId ||
88
+ JSON.stringify(coalesced[key]?.source) !== JSON.stringify(request.source))
89
+ return coalesced;
90
+ const { [key]: _, ...rest } = coalesced;
91
+ return rest;
92
+ }
93
+ function pausedInvocations(previous, payload) {
94
+ return previous.invocations.map((invocation) => payload.workflowInstanceId === invocation.claim.workflowInstanceId &&
95
+ payload.outcomeCount !== undefined
96
+ ? { ...invocation, failureOutcomeCount: payload.outcomeCount }
97
+ : invocation);
98
+ }
@@ -0,0 +1,4 @@
1
+ import { type EventEnvelope } from '@atolis-hq/eventing';
2
+ import type { TriggerConfig } from '../contracts/config.js';
3
+ import type { TriggerService } from './trigger-service.js';
4
+ export declare function createTriggerReactor(service: TriggerService, config: TriggerConfig, decode: (event: EventEnvelope) => void): import("@atolis-hq/eventing").EventProcessor;
@@ -0,0 +1,30 @@
1
+ import { correlationId, defineEventProcessor, EventActorKind, EventProcessorCategory, EventProcessorReplayPolicy, } from '@atolis-hq/eventing';
2
+ import { TriggerSourceKind } from '../contracts/events.js';
3
+ import { ControlStreamKind } from '../contracts/streams.js';
4
+ export function createTriggerReactor(service, config, decode) {
5
+ return defineEventProcessor({
6
+ consumer: `reactor:control-plane.trigger:${config.id}`,
7
+ name: 'workflow-triggers',
8
+ owner: ControlStreamKind.Global,
9
+ category: EventProcessorCategory.Reactor,
10
+ replayPolicy: EventProcessorReplayPolicy.Idempotent,
11
+ select(event) {
12
+ if (!config.on.events?.includes(event.event.eventType))
13
+ return null;
14
+ decode(event);
15
+ return event;
16
+ },
17
+ async handle(event) {
18
+ await service.start(config, {
19
+ kind: TriggerSourceKind.Event,
20
+ eventId: event.event.eventId,
21
+ eventType: event.event.eventType,
22
+ }, {
23
+ commandId: `trigger:${config.id}:${event.event.eventId}`,
24
+ correlationId: correlationId(`trigger:${config.id}:${event.event.eventId}`),
25
+ occurredAt: event.event.occurredAt,
26
+ actor: { kind: EventActorKind.System, id: config.id },
27
+ }, event);
28
+ },
29
+ });
30
+ }
@@ -0,0 +1,10 @@
1
+ import { type CommandContext } from '@atolis-hq/eventing';
2
+ import type { TriggerFactInput } from './trigger-facts.js';
3
+ import type { TriggerInvocationView } from './trigger-projection.js';
4
+ import { type TriggerWorkflowPorts } from './trigger-workflow.js';
5
+ interface TriggerRecoveryPorts extends TriggerWorkflowPorts {
6
+ readonly emit: (input: TriggerFactInput, context: CommandContext, eventId: string) => Promise<void>;
7
+ }
8
+ /** Blocked invocations remain recoverable through operator retry or clarification. */
9
+ export declare function recoverTriggerInvocation(dependencies: TriggerRecoveryPorts, invocation: TriggerInvocationView, context: CommandContext): Promise<void>;
10
+ export {};