@atolis-hq/wake 0.4.60 → 0.4.61
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.
- package/dist/src/bootstrap/canonical-events.d.ts +3 -0
- package/dist/src/bootstrap/canonical-events.js +32 -0
- package/dist/src/bootstrap/composition-root.d.ts +2 -2
- package/dist/src/bootstrap/composition-root.js +6 -3
- package/dist/src/bootstrap/config/root-schema.d.ts +8 -3
- package/dist/src/bootstrap/config/root-schema.js +29 -1
- package/dist/src/bootstrap/initialise.js +5 -0
- package/dist/src/bootstrap/integration-runtime.d.ts +6 -4
- package/dist/src/bootstrap/integration-runtime.js +50 -8
- package/dist/src/bootstrap/surface-cli-applications.js +1 -1
- package/dist/src/bootstrap/trigger-checkpoint-store.d.ts +9 -0
- package/dist/src/bootstrap/trigger-checkpoint-store.js +49 -0
- package/dist/src/bootstrap/version.js +1 -1
- package/dist/src/control-plane/application/control-plane-projection.js +2 -0
- package/dist/src/control-plane/application/runner-pipeline.d.ts +2 -2
- package/dist/src/control-plane/application/runner-pipeline.js +2 -2
- package/dist/src/control-plane/application/trigger-service.d.ts +35 -0
- package/dist/src/control-plane/application/trigger-service.js +118 -0
- package/dist/src/control-plane/contracts/config.d.ts +18 -8
- package/dist/src/control-plane/contracts/config.js +35 -6
- package/dist/src/control-plane/contracts/event-factory.js +2 -0
- package/dist/src/control-plane/contracts/events.d.ts +23 -0
- package/dist/src/control-plane/contracts/events.js +32 -0
- package/dist/src/control-plane/domain/cron-trigger-policy.d.ts +10 -0
- package/dist/src/control-plane/domain/{schedule-policy.js → cron-trigger-policy.js} +5 -5
- package/dist/src/control-plane/index.d.ts +2 -2
- package/dist/src/control-plane/index.js +2 -2
- package/package.json +1 -1
- package/dist/src/bootstrap/schedule-checkpoint-store.d.ts +0 -9
- package/dist/src/bootstrap/schedule-checkpoint-store.js +0 -49
- package/dist/src/control-plane/application/schedule-service.d.ts +0 -31
- package/dist/src/control-plane/application/schedule-service.js +0 -47
- package/dist/src/control-plane/domain/schedule-policy.d.ts +0 -8
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ActivityEventType, selectActivityEvent } from '../activities/index.js';
|
|
2
|
+
import { ArtifactEventType, selectArtifactEvent } from '../artifacts/index.js';
|
|
3
|
+
import { ControlEventType, selectControlEvent } from '../control-plane/index.js';
|
|
4
|
+
import { ConversationEventType, selectConversationEvent } from '../conversations/index.js';
|
|
5
|
+
import { ExecutionEventType, selectExecutionEvent } from '../execution/index.js';
|
|
6
|
+
import { GitHubEventType, selectGitHubAdapterEvent } from '../integrations/github/index.js';
|
|
7
|
+
import { DeliveryEventType, DeliveryIntentEventType, ArtifactEventType as IntegrationArtifactEventType, decodeArtifactEvent, selectDeliveryEvent, selectDeliveryIntentEvent, } from '../integrations/index.js';
|
|
8
|
+
import { OrchestrationEventType, selectOrchestrationEvent } from '../orchestration/index.js';
|
|
9
|
+
import { ResourceEventType, selectResourceEvent } from '../resources/index.js';
|
|
10
|
+
import { WorkEventType, selectWorkEvent } from '../work/index.js';
|
|
11
|
+
// Bootstrap registers the owning modules' public vocabularies and decoders together.
|
|
12
|
+
const registrations = [
|
|
13
|
+
{ types: Object.values(ActivityEventType), decode: selectActivityEvent },
|
|
14
|
+
{ types: Object.values(ArtifactEventType), decode: selectArtifactEvent },
|
|
15
|
+
{ types: Object.values(ControlEventType), decode: selectControlEvent },
|
|
16
|
+
{ types: Object.values(ConversationEventType), decode: selectConversationEvent },
|
|
17
|
+
{ types: Object.values(ExecutionEventType), decode: selectExecutionEvent },
|
|
18
|
+
{ types: Object.values(GitHubEventType), decode: selectGitHubAdapterEvent },
|
|
19
|
+
{ types: Object.values(IntegrationArtifactEventType), decode: decodeArtifactEvent },
|
|
20
|
+
{ types: Object.values(DeliveryEventType), decode: selectDeliveryEvent },
|
|
21
|
+
{ types: Object.values(DeliveryIntentEventType), decode: selectDeliveryIntentEvent },
|
|
22
|
+
{ types: Object.values(OrchestrationEventType), decode: selectOrchestrationEvent },
|
|
23
|
+
{ types: Object.values(ResourceEventType), decode: selectResourceEvent },
|
|
24
|
+
{ types: Object.values(WorkEventType), decode: selectWorkEvent },
|
|
25
|
+
];
|
|
26
|
+
export const canonicalEventTypes = new Set(registrations.flatMap(({ types }) => types));
|
|
27
|
+
export function decodeCanonicalEvent(event) {
|
|
28
|
+
const registration = registrations.find(({ types }) => types.includes(event.event.eventType));
|
|
29
|
+
if (registration === undefined)
|
|
30
|
+
throw new Error(`Unknown canonical event ${event.event.eventType}`);
|
|
31
|
+
registration.decode(event);
|
|
32
|
+
}
|
|
@@ -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
|
|
4
|
+
import { createControlPlaneService, createRunnerControlService, type ActivationScheduler, type ActivationSchedulerSubscriber, type IntakePipeline, type RunnerPipeline, type TriggerCheckpointStore } 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';
|
|
@@ -32,7 +32,7 @@ export interface CompositionRootOptions {
|
|
|
32
32
|
readonly decorateProjections?: (projections: ProjectionStore) => ProjectionStore;
|
|
33
33
|
readonly decorateCheckpoints?: (checkpoints: CheckpointStore) => CheckpointStore;
|
|
34
34
|
readonly subscriptionRunSerialiser?: ProcessorRunSerialiser;
|
|
35
|
-
readonly
|
|
35
|
+
readonly triggerCheckpoints?: TriggerCheckpointStore;
|
|
36
36
|
readonly decorateDeliveryAdapter?: (adapter: ExternalDeliveryAdapter, provider: ProviderInstance) => ExternalDeliveryAdapter;
|
|
37
37
|
readonly decorateRunner?: (runner: Runner, name: string) => Runner;
|
|
38
38
|
/** A real fake-provider adapter for deterministic composed integration evidence. */
|
|
@@ -20,8 +20,8 @@ import { resolveWakePaths } from './paths.js';
|
|
|
20
20
|
import { composePersistence } from './persistence-composition.js';
|
|
21
21
|
import { createRunnerQuotaReporter } from './runner-quota-reporter.js';
|
|
22
22
|
import { createRunnerRegistry } from './runner-registry.js';
|
|
23
|
-
import { FileScheduleCheckpointStore } from './schedule-checkpoint-store.js';
|
|
24
23
|
import { createTranscriptRetention } from './transcript-retention.js';
|
|
24
|
+
import { FileTriggerCheckpointStore } from './trigger-checkpoint-store.js';
|
|
25
25
|
import { createUpdateMaintenanceLease, } from './update-maintenance-lease.js';
|
|
26
26
|
const resourceLinkResolvers = {
|
|
27
27
|
github: resolveGitHubResourceUrl,
|
|
@@ -85,10 +85,11 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
|
|
|
85
85
|
: options.decorateRunner === undefined
|
|
86
86
|
? profileDecorator
|
|
87
87
|
: (runner, name) => profileDecorator(options.decorateRunner(runner, name), name);
|
|
88
|
+
const runners = createRunnerRegistry(config.execution, fakeScenarios, decorateRunner);
|
|
88
89
|
const execution = createExecutionService(journal, activities, config.execution, {
|
|
89
90
|
clock,
|
|
90
91
|
ids,
|
|
91
|
-
runners
|
|
92
|
+
runners,
|
|
92
93
|
mcp: createArtifactMcpProvisioner({
|
|
93
94
|
artifacts,
|
|
94
95
|
orchestration,
|
|
@@ -171,7 +172,9 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
|
|
|
171
172
|
wakeRoot,
|
|
172
173
|
subscriptionRunSerialiser,
|
|
173
174
|
processorRuntime,
|
|
174
|
-
|
|
175
|
+
activities,
|
|
176
|
+
runners,
|
|
177
|
+
triggerCheckpoints: options.triggerCheckpoints ?? new FileTriggerCheckpointStore(paths.dataRoot),
|
|
175
178
|
...(options.decorateDeliveryAdapter === undefined
|
|
176
179
|
? {}
|
|
177
180
|
: { decorateDeliveryAdapter: options.decorateDeliveryAdapter }),
|
|
@@ -338,11 +338,16 @@ export declare const rootConfigSchema: z.ZodObject<{
|
|
|
338
338
|
controlPlane: z.ZodDefault<z.ZodObject<{
|
|
339
339
|
maxDispatches: z.ZodDefault<z.ZodNumber>;
|
|
340
340
|
maxConcurrentRuns: z.ZodDefault<z.ZodNumber>;
|
|
341
|
-
|
|
341
|
+
triggers: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
342
342
|
id: z.ZodString;
|
|
343
343
|
workflow: z.ZodString;
|
|
344
|
-
|
|
345
|
-
|
|
344
|
+
on: z.ZodObject<{
|
|
345
|
+
events: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
346
|
+
cron: z.ZodOptional<z.ZodString>;
|
|
347
|
+
}, z.core.$strict>;
|
|
348
|
+
when: z.ZodOptional<z.ZodObject<{
|
|
349
|
+
capacityAvailable: z.ZodLiteral<true>;
|
|
350
|
+
}, z.core.$strict>>;
|
|
346
351
|
}, z.core.$strict>>>;
|
|
347
352
|
resident: z.ZodDefault<z.ZodObject<{
|
|
348
353
|
pollBackoffMs: z.ZodDefault<z.ZodNumber>;
|
|
@@ -8,6 +8,7 @@ import { workflowDefinitionConfigSchema, workflowSelectorConfigSchema, } from '.
|
|
|
8
8
|
import { resourcesConfigSchema } from '../../resources/index.js';
|
|
9
9
|
import { surfacesConfigSchema } from '../../surfaces/index.js';
|
|
10
10
|
import { workConfigSchema } from '../../work/index.js';
|
|
11
|
+
import { canonicalEventTypes } from '../canonical-events.js';
|
|
11
12
|
const orchestrationConfigSchema = z
|
|
12
13
|
.object({
|
|
13
14
|
workflows: z.record(z.string().trim().min(1), workflowDefinitionConfigSchema).default({}),
|
|
@@ -151,7 +152,34 @@ export const rootConfigSchema = z
|
|
|
151
152
|
surfaces: surfacesConfigSchema,
|
|
152
153
|
host: hostConfigSchema,
|
|
153
154
|
})
|
|
154
|
-
.strict()
|
|
155
|
+
.strict()
|
|
156
|
+
.superRefine((value, context) => {
|
|
157
|
+
const ids = new Set();
|
|
158
|
+
for (const [index, trigger] of value.controlPlane.triggers.entries()) {
|
|
159
|
+
const path = ['controlPlane', 'triggers', index];
|
|
160
|
+
if (ids.has(trigger.id))
|
|
161
|
+
context.addIssue({
|
|
162
|
+
code: 'custom',
|
|
163
|
+
path: [...path, 'id'],
|
|
164
|
+
message: 'Trigger ids must be unique',
|
|
165
|
+
});
|
|
166
|
+
ids.add(trigger.id);
|
|
167
|
+
if (value.orchestration.workflows[trigger.workflow] === undefined)
|
|
168
|
+
context.addIssue({
|
|
169
|
+
code: 'custom',
|
|
170
|
+
path: [...path, 'workflow'],
|
|
171
|
+
message: 'Trigger workflow is not configured',
|
|
172
|
+
});
|
|
173
|
+
for (const [eventIndex, event] of (trigger.on.events ?? []).entries()) {
|
|
174
|
+
if (!canonicalEventTypes.has(event))
|
|
175
|
+
context.addIssue({
|
|
176
|
+
code: 'custom',
|
|
177
|
+
path: [...path, 'on', 'events', eventIndex],
|
|
178
|
+
message: 'Unknown canonical event',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
155
183
|
export function parseRootConfig(input) {
|
|
156
184
|
return rootConfigSchema.parse(input);
|
|
157
185
|
}
|
|
@@ -40,6 +40,11 @@ orchestration:
|
|
|
40
40
|
matchMode: all
|
|
41
41
|
workflow: approval
|
|
42
42
|
|
|
43
|
+
# Independent workflow invocations need an explicit event and/or UTC cron source.
|
|
44
|
+
# Activity prompts define their tasks. No triggers are enabled by default.
|
|
45
|
+
controlPlane:
|
|
46
|
+
triggers: []
|
|
47
|
+
|
|
43
48
|
# External providers Wake correlates inbound work with. Empty until you
|
|
44
49
|
# configure one — see SETUP.md.
|
|
45
50
|
integrations: {}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type CheckpointStore, type EventJournal, type EventProcessor, type ProcessorRunSerialiser, type ProcessorStateStore, type ProjectionStore } from '@atolis-hq/eventing';
|
|
2
|
-
import type
|
|
3
|
-
import { type IntakePipeline, type RunnerPipeline, type
|
|
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';
|
|
4
4
|
import type { createConversationService } from '../conversations/index.js';
|
|
5
|
-
import { type createExecutionService } from '../execution/index.js';
|
|
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';
|
|
@@ -22,6 +22,8 @@ export interface IntegrationRuntime {
|
|
|
22
22
|
readonly requestImmediatePoll: (adapter: string) => void;
|
|
23
23
|
}
|
|
24
24
|
export interface IntegrationRuntimeInput {
|
|
25
|
+
readonly activities: ActivityRegistry;
|
|
26
|
+
readonly runners: RunnerRegistry;
|
|
25
27
|
readonly config: ResolvedWakeModulesConfig;
|
|
26
28
|
readonly journal: EventJournal;
|
|
27
29
|
readonly projections: ProjectionStore;
|
|
@@ -41,7 +43,7 @@ export interface IntegrationRuntimeInput {
|
|
|
41
43
|
readonly wakeRoot: string;
|
|
42
44
|
readonly subscriptionRunSerialiser: ProcessorRunSerialiser;
|
|
43
45
|
readonly processorRuntime: EventProcessorRuntime;
|
|
44
|
-
readonly
|
|
46
|
+
readonly triggerCheckpoints: TriggerCheckpointStore;
|
|
45
47
|
readonly decorateDeliveryAdapter?: (adapter: ExternalDeliveryAdapter, provider: ProviderInstance) => ExternalDeliveryAdapter;
|
|
46
48
|
readonly fakeDeliveryProvider?: DurableFakeDeliveryProvider;
|
|
47
49
|
readonly providerDefinitions: readonly ProviderDefinition[];
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { EventActorKind, } from '@atolis-hq/eventing';
|
|
2
2
|
import { withFileLock } from '@atolis-hq/eventing-filesystem';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
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
7
|
import { AgentRunPublicationReactor, ArtifactRegistrationReactor, DeliveryOutcomeReactor, DeliveryService, IntegrationStreamKind, PollService, ProviderRegistry, deliveryProjectionDefinitions, fakeProviderDefinition, } from '../integrations/index.js';
|
|
7
8
|
import { compileWorkflowSelectors, createConversationCommandReactor, createPullRequestTransitionEvidence, createResourceTransitionReactor, createWatchReactor, createWatchReconciler, selectWorkflow, workflowName, } from '../orchestration/index.js';
|
|
8
9
|
import { BuiltInResourceCapability, resourceId, } from '../resources/index.js';
|
|
10
|
+
import { decodeCanonicalEvent } from './canonical-events.js';
|
|
9
11
|
import { hydrateFakeProviderEvidence } from './fake-provider-files.js';
|
|
10
12
|
import { createRuntimeProjectionSubscriptions, } from './projection-runtime.js';
|
|
11
13
|
import { createCapabilityResourceTransitionEvidence } from './resource-transition-evidence.js';
|
|
@@ -65,13 +67,52 @@ export async function composeIntegrationRuntime(input) {
|
|
|
65
67
|
},
|
|
66
68
|
now: () => input.clock.now().toISOString(),
|
|
67
69
|
});
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
+
const runnerIneligibility = createDurableRunnerIneligibility(input.journal, () => input.clock.now().toISOString());
|
|
71
|
+
const triggers = new TriggerService({
|
|
72
|
+
journal: input.journal,
|
|
73
|
+
capacityAvailable: async (trigger) => {
|
|
74
|
+
if (await input.isPaused())
|
|
75
|
+
return false;
|
|
76
|
+
const active = (await input.execution.list()).filter((run) => isActiveRunStatus(run.status));
|
|
77
|
+
if (active.length >= input.config.controlPlane.maxConcurrentRuns)
|
|
78
|
+
return false;
|
|
79
|
+
const workflow = input.config.orchestration.workflows[trigger.workflow];
|
|
80
|
+
const stage = workflow.stages[workflow.entry ?? Object.keys(workflow.stages)[0]];
|
|
81
|
+
const pool = stage.execution?.runnerPool ?? input.config.execution.defaultRunnerPool;
|
|
82
|
+
if (input.config.execution.runnerPools[pool] === undefined)
|
|
83
|
+
return false;
|
|
84
|
+
if (input.activities.describe(activityName(stage.activity)).executionKind !==
|
|
85
|
+
ActivityExecutionKind.Agent)
|
|
86
|
+
return true;
|
|
87
|
+
try {
|
|
88
|
+
input.runners.resolve(pool, await runnerIneligibility(), { ephemeralMcp: true });
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error instanceof NoEligibleRunnerError)
|
|
93
|
+
return false;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
checkpoint: input.triggerCheckpoints,
|
|
70
98
|
ids: input.ids,
|
|
71
99
|
work: input.work,
|
|
72
100
|
orchestration: input.orchestration,
|
|
73
101
|
now: () => input.clock.now().toISOString(),
|
|
74
102
|
});
|
|
103
|
+
const triggerReactors = input.config.controlPlane.triggers.map((config) => createTriggerReactor(triggers, config, decodeCanonicalEvent));
|
|
104
|
+
// Registration starts at the current journal boundary; historical facts are not sources.
|
|
105
|
+
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 },
|
|
114
|
+
});
|
|
115
|
+
}
|
|
75
116
|
const artifacts = new ArtifactRegistrationReactor({
|
|
76
117
|
journal: input.journal,
|
|
77
118
|
resources: input.resources,
|
|
@@ -136,11 +177,11 @@ export async function composeIntegrationRuntime(input) {
|
|
|
136
177
|
});
|
|
137
178
|
const runnerPipeline = createRunnerPipeline({
|
|
138
179
|
isPaused: input.isPaused,
|
|
139
|
-
|
|
140
|
-
for (const
|
|
141
|
-
await
|
|
180
|
+
runTriggers: () => observeMemory(memoryProfile, 'runner.triggers', async () => {
|
|
181
|
+
for (const trigger of input.config.controlPlane.triggers)
|
|
182
|
+
await triggers.run(trigger, {
|
|
142
183
|
commandId: input.ids.next('command'),
|
|
143
|
-
correlationId: '
|
|
184
|
+
correlationId: 'trigger-tick',
|
|
144
185
|
occurredAt: input.clock.now().toISOString(),
|
|
145
186
|
actor: { kind: EventActorKind.System, id: ControlStreamKind.Global },
|
|
146
187
|
});
|
|
@@ -169,6 +210,7 @@ export async function composeIntegrationRuntime(input) {
|
|
|
169
210
|
projectionSubscriptions,
|
|
170
211
|
processors: [
|
|
171
212
|
...projectionSubscriptions.processors,
|
|
213
|
+
...triggerReactors,
|
|
172
214
|
watch.processor,
|
|
173
215
|
resourceTransitions.processor,
|
|
174
216
|
artifacts.processor,
|
|
@@ -37,7 +37,7 @@ export function createSurfaceCliApplications(root, api, now) {
|
|
|
37
37
|
process.stderr.write(`Wake ${label} tick failed: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
38
38
|
};
|
|
39
39
|
// Intake owns provider polling, so only its resident loop backs off
|
|
40
|
-
// exponentially when idle. The runner loop owns
|
|
40
|
+
// exponentially when idle. The runner loop owns triggers, reconciliation,
|
|
41
41
|
// and delivery; it stays on a fast cadence when there is no local work.
|
|
42
42
|
// The independent durable subscriber owns activation scheduling. Delivery
|
|
43
43
|
// can still hit an external API, so consecutive errors back off
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
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);
|
|
6
|
+
load(triggerId: string): Promise<string | null>;
|
|
7
|
+
save(triggerId: string, slot: string): Promise<void>;
|
|
8
|
+
private path;
|
|
9
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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;
|
|
8
|
+
}
|
|
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
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async save(triggerId, slot) {
|
|
23
|
+
const existing = await this.load(triggerId);
|
|
24
|
+
if (existing !== null && Date.parse(slot) < Date.parse(existing))
|
|
25
|
+
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`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -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.
|
|
127
|
+
export const wakeVersion = "v0.4.61";
|
|
128
128
|
/** The registry-installable version used by packaged sandbox images. */
|
|
129
129
|
export const wakePackageVersion = packageVersion(defaultRepoRoot) ?? wakeVersion;
|
|
@@ -4,7 +4,7 @@ export type RunnerPipelineResult = Extract<AdvanceResult, {
|
|
|
4
4
|
}>;
|
|
5
5
|
export interface RunnerPipelineStages {
|
|
6
6
|
readonly isPaused?: () => Promise<boolean>;
|
|
7
|
-
readonly
|
|
7
|
+
readonly runTriggers: () => Promise<void>;
|
|
8
8
|
readonly maintain?: () => Promise<void>;
|
|
9
9
|
readonly deliver: (signal: AbortSignal) => Promise<void>;
|
|
10
10
|
}
|
|
@@ -16,7 +16,7 @@ export interface RunnerPipeline {
|
|
|
16
16
|
run(options: AdvanceOptions, signal?: AbortSignal, beforeDelivery?: () => Promise<void>): Promise<RunnerPipelineResult>;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Runs the non-reactive internal half of a Wake tick:
|
|
19
|
+
* Runs the non-reactive internal half of a Wake tick: triggers, maintenance,
|
|
20
20
|
* and delivery. Processors are owned by the Eventing runtime. None of this touches a rate-limited external poll, so its
|
|
21
21
|
* host runs on a fast, un-backed-off cadence — see IntakePipeline for the
|
|
22
22
|
* half that does need backoff.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Runs the non-reactive internal half of a Wake tick:
|
|
2
|
+
* Runs the non-reactive internal half of a Wake tick: triggers, maintenance,
|
|
3
3
|
* and delivery. Processors are owned by the Eventing runtime. None of this touches a rate-limited external poll, so its
|
|
4
4
|
* host runs on a fast, un-backed-off cadence — see IntakePipeline for the
|
|
5
5
|
* half that does need backoff.
|
|
@@ -9,7 +9,7 @@ export function createRunnerPipeline(stages) {
|
|
|
9
9
|
const runOnce = async (options, signal, beforeDelivery) => {
|
|
10
10
|
if (await isPaused())
|
|
11
11
|
return { kind: 'paused' };
|
|
12
|
-
await stages.
|
|
12
|
+
await stages.runTriggers();
|
|
13
13
|
if (await isPaused())
|
|
14
14
|
return { kind: 'paused' };
|
|
15
15
|
await stages.maintain?.();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type CommandContext, type EventEnvelope, type EventJournal } from '@atolis-hq/eventing';
|
|
2
|
+
import type { IdGenerator } from '../../kernel/index.js';
|
|
3
|
+
import { workflowInstanceId, type StartWorkflowInstance } from '../../orchestration/index.js';
|
|
4
|
+
import { workItemId, type CreateWorkItem } from '../../work/index.js';
|
|
5
|
+
import type { TriggerConfig } from '../contracts/config.js';
|
|
6
|
+
import { type TriggerSource } from '../contracts/events.js';
|
|
7
|
+
export interface TriggerCheckpointStore {
|
|
8
|
+
load(triggerId: string): Promise<string | null>;
|
|
9
|
+
save(triggerId: string, slot: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface TriggerServiceDependencies {
|
|
12
|
+
readonly checkpoint: TriggerCheckpointStore;
|
|
13
|
+
readonly journal: EventJournal;
|
|
14
|
+
readonly ids: IdGenerator;
|
|
15
|
+
readonly work: {
|
|
16
|
+
create(command: CreateWorkItem, context: CommandContext): Promise<unknown>;
|
|
17
|
+
};
|
|
18
|
+
readonly orchestration: {
|
|
19
|
+
start(command: StartWorkflowInstance, context: CommandContext): Promise<unknown>;
|
|
20
|
+
get(id: ReturnType<typeof workflowInstanceId>): Promise<{
|
|
21
|
+
readonly workItemId: ReturnType<typeof workItemId>;
|
|
22
|
+
} | null>;
|
|
23
|
+
};
|
|
24
|
+
readonly capacityAvailable: (config: TriggerConfig) => Promise<boolean>;
|
|
25
|
+
readonly now: () => string;
|
|
26
|
+
}
|
|
27
|
+
export declare class TriggerService {
|
|
28
|
+
private readonly dependencies;
|
|
29
|
+
private readonly policy;
|
|
30
|
+
constructor(dependencies: TriggerServiceDependencies);
|
|
31
|
+
run(config: TriggerConfig, context: CommandContext): Promise<readonly string[]>;
|
|
32
|
+
start(config: TriggerConfig, source: TriggerSource, context: CommandContext): Promise<boolean>;
|
|
33
|
+
private recordInvocation;
|
|
34
|
+
}
|
|
35
|
+
export declare function createTriggerReactor(service: TriggerService, config: TriggerConfig, decode: (event: EventEnvelope) => void): import("@atolis-hq/eventing").EventProcessor;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { correlationId, defineEventProcessor, EventActorKind, EventProcessorCategory, EventProcessorReplayPolicy, EventSourceKind, WrongExpectedSequenceError, } from '@atolis-hq/eventing';
|
|
2
|
+
import { orchestrationGroupId, workflowInstanceId, workflowName, } from '../../orchestration/index.js';
|
|
3
|
+
import { workItemId } from '../../work/index.js';
|
|
4
|
+
import { createControlPlaneEventData } from '../contracts/event-factory.js';
|
|
5
|
+
import { ControlEventType, selectControlEvent, TriggerSourceKind, } from '../contracts/events.js';
|
|
6
|
+
import { controlPlaneStream, ControlStreamKind } from '../contracts/streams.js';
|
|
7
|
+
import { CronTriggerPolicy } from '../domain/cron-trigger-policy.js';
|
|
8
|
+
export class TriggerService {
|
|
9
|
+
dependencies;
|
|
10
|
+
policy = new CronTriggerPolicy();
|
|
11
|
+
constructor(dependencies) {
|
|
12
|
+
this.dependencies = dependencies;
|
|
13
|
+
}
|
|
14
|
+
async run(config, context) {
|
|
15
|
+
if (config.on.cron === undefined)
|
|
16
|
+
return [];
|
|
17
|
+
const checkpoint = await this.dependencies.checkpoint.load(config.id);
|
|
18
|
+
const slots = this.policy.elapsedSlots({ id: config.id, cron: config.on.cron }, this.dependencies.now(), checkpoint);
|
|
19
|
+
const accepted = [];
|
|
20
|
+
for (const slot of slots) {
|
|
21
|
+
if (await this.start(config, { kind: TriggerSourceKind.Cron, slot: slot.at }, context))
|
|
22
|
+
accepted.push(slot.identity);
|
|
23
|
+
// A failed gate consumes the occurrence; becoming eligible is not a source.
|
|
24
|
+
await this.dependencies.checkpoint.save(config.id, slot.at);
|
|
25
|
+
}
|
|
26
|
+
return accepted;
|
|
27
|
+
}
|
|
28
|
+
async start(config, source, context) {
|
|
29
|
+
const identity = `trigger:${config.id}:${source.kind}:${source.kind === TriggerSourceKind.Cron ? source.slot : source.eventId}`;
|
|
30
|
+
const workflow = workflowInstanceId(`workflow-${encodeURIComponent(identity)}`);
|
|
31
|
+
const invocationContext = {
|
|
32
|
+
...context,
|
|
33
|
+
commandId: `${context.commandId}:${identity}`,
|
|
34
|
+
correlationId: correlationId(identity),
|
|
35
|
+
};
|
|
36
|
+
const existing = await this.dependencies.orchestration.get(workflow);
|
|
37
|
+
if (existing === null &&
|
|
38
|
+
config.when?.capacityAvailable === true &&
|
|
39
|
+
!(await this.dependencies.capacityAvailable(config)))
|
|
40
|
+
return false;
|
|
41
|
+
const item = existing?.workItemId ?? workItemId(this.dependencies.ids.next('work'));
|
|
42
|
+
if (existing === null) {
|
|
43
|
+
await this.dependencies.work.create({ workItemId: item, objective: `Trigger ${config.id}: workflow ${config.workflow}` }, invocationContext);
|
|
44
|
+
await this.dependencies.orchestration.start({
|
|
45
|
+
workflowInstanceId: workflow,
|
|
46
|
+
workItemId: item,
|
|
47
|
+
workflowName: workflowName(config.workflow),
|
|
48
|
+
orchestrationGroupId: orchestrationGroupId(`primary:${item}`),
|
|
49
|
+
}, invocationContext);
|
|
50
|
+
}
|
|
51
|
+
await this.recordInvocation(config, source, invocationContext, item, workflow);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
async recordInvocation(config, source, context, item, workflow) {
|
|
55
|
+
const stream = controlPlaneStream();
|
|
56
|
+
const draft = createControlPlaneEventData({
|
|
57
|
+
eventId: `${workflow}:${ControlEventType.TriggerInvocationStarted}`,
|
|
58
|
+
eventType: ControlEventType.TriggerInvocationStarted,
|
|
59
|
+
occurredAt: this.dependencies.now(),
|
|
60
|
+
correlationId: context.correlationId,
|
|
61
|
+
causationId: source.kind === TriggerSourceKind.Event ? source.eventId : context.commandId,
|
|
62
|
+
actor: context.actor,
|
|
63
|
+
source: { kind: EventSourceKind.Internal, id: config.id },
|
|
64
|
+
payload: {
|
|
65
|
+
triggerId: config.id,
|
|
66
|
+
source,
|
|
67
|
+
workItemId: item,
|
|
68
|
+
workflowInstanceId: workflow,
|
|
69
|
+
workflowName: config.workflow,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
73
|
+
const events = await this.dependencies.journal.readStream(stream);
|
|
74
|
+
if (events.some((envelope) => {
|
|
75
|
+
const event = selectControlEvent(envelope);
|
|
76
|
+
return (event?.event.eventType === ControlEventType.TriggerInvocationStarted &&
|
|
77
|
+
event.event.payload.workflowInstanceId === workflow);
|
|
78
|
+
}))
|
|
79
|
+
return;
|
|
80
|
+
try {
|
|
81
|
+
await this.dependencies.journal.appendToStream(stream, events.length, [draft]);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Other triggers and runner controls share this stream. Re-read on contention.
|
|
86
|
+
if (!(error instanceof WrongExpectedSequenceError) || attempt === 7)
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export function createTriggerReactor(service, config, decode) {
|
|
93
|
+
return defineEventProcessor({
|
|
94
|
+
consumer: `reactor:control-plane.trigger:${config.id}`,
|
|
95
|
+
name: 'workflow-triggers',
|
|
96
|
+
owner: ControlStreamKind.Global,
|
|
97
|
+
category: EventProcessorCategory.Reactor,
|
|
98
|
+
replayPolicy: EventProcessorReplayPolicy.Idempotent,
|
|
99
|
+
select(event) {
|
|
100
|
+
if (!config.on.events?.includes(event.event.eventType))
|
|
101
|
+
return null;
|
|
102
|
+
decode(event);
|
|
103
|
+
return event;
|
|
104
|
+
},
|
|
105
|
+
async handle(event) {
|
|
106
|
+
await service.start(config, {
|
|
107
|
+
kind: TriggerSourceKind.Event,
|
|
108
|
+
eventId: event.event.eventId,
|
|
109
|
+
eventType: event.event.eventType,
|
|
110
|
+
}, {
|
|
111
|
+
commandId: `trigger:${config.id}:${event.event.eventId}`,
|
|
112
|
+
correlationId: correlationId(`trigger:${config.id}:${event.event.eventId}`),
|
|
113
|
+
occurredAt: event.event.occurredAt,
|
|
114
|
+
actor: { kind: EventActorKind.System, id: config.id },
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
@@ -1,27 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export interface TriggerConfig {
|
|
2
3
|
readonly id: string;
|
|
3
4
|
readonly workflow: string;
|
|
4
|
-
readonly
|
|
5
|
-
|
|
5
|
+
readonly on: {
|
|
6
|
+
readonly events?: readonly string[];
|
|
7
|
+
readonly cron?: string;
|
|
8
|
+
};
|
|
9
|
+
readonly when?: {
|
|
10
|
+
readonly capacityAvailable: true;
|
|
11
|
+
};
|
|
6
12
|
}
|
|
7
13
|
export interface ControlPlaneConfig {
|
|
8
14
|
readonly maxDispatches: number;
|
|
9
15
|
readonly maxConcurrentRuns: number;
|
|
10
|
-
readonly
|
|
16
|
+
readonly triggers: readonly TriggerConfig[];
|
|
11
17
|
readonly resident?: {
|
|
12
18
|
readonly pollBackoffMs: number;
|
|
13
19
|
readonly maxPollBackoffMs?: number;
|
|
14
20
|
};
|
|
15
21
|
}
|
|
16
|
-
import { z } from 'zod';
|
|
17
22
|
export declare const controlPlaneConfigSchema: z.ZodDefault<z.ZodObject<{
|
|
18
23
|
maxDispatches: z.ZodDefault<z.ZodNumber>;
|
|
19
24
|
maxConcurrentRuns: z.ZodDefault<z.ZodNumber>;
|
|
20
|
-
|
|
25
|
+
triggers: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
21
26
|
id: z.ZodString;
|
|
22
27
|
workflow: z.ZodString;
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
on: z.ZodObject<{
|
|
29
|
+
events: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
30
|
+
cron: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strict>;
|
|
32
|
+
when: z.ZodOptional<z.ZodObject<{
|
|
33
|
+
capacityAvailable: z.ZodLiteral<true>;
|
|
34
|
+
}, z.core.$strict>>;
|
|
25
35
|
}, z.core.$strict>>>;
|
|
26
36
|
resident: z.ZodDefault<z.ZodObject<{
|
|
27
37
|
pollBackoffMs: z.ZodDefault<z.ZodNumber>;
|
|
@@ -1,17 +1,46 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
|
|
2
|
+
import { CronTriggerPolicy } from '../domain/cron-trigger-policy.js';
|
|
3
|
+
const triggerSchema = z
|
|
3
4
|
.object({
|
|
4
|
-
id: z
|
|
5
|
+
id: z
|
|
6
|
+
.string()
|
|
7
|
+
.trim()
|
|
8
|
+
.min(1)
|
|
9
|
+
.refine((id) => !/[\\/]/.test(id), 'Trigger id must not contain path separators'),
|
|
5
10
|
workflow: z.string().trim().min(1),
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
on: z
|
|
12
|
+
.object({
|
|
13
|
+
events: z.array(z.string().trim().min(1)).min(1).optional(),
|
|
14
|
+
cron: z
|
|
15
|
+
.string()
|
|
16
|
+
.trim()
|
|
17
|
+
.min(1)
|
|
18
|
+
.superRefine((cron, context) => {
|
|
19
|
+
try {
|
|
20
|
+
new CronTriggerPolicy().elapsedSlots({ id: 'validation', cron }, '2026-01-01T00:00:00.000Z', null);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
context.addIssue({
|
|
24
|
+
code: 'custom',
|
|
25
|
+
message: error instanceof Error ? error.message : 'Invalid cron',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
.optional(),
|
|
30
|
+
})
|
|
31
|
+
.strict()
|
|
32
|
+
.refine((on) => on.events !== undefined || on.cron !== undefined, 'At least one trigger source is required'),
|
|
33
|
+
when: z
|
|
34
|
+
.object({ capacityAvailable: z.literal(true) })
|
|
35
|
+
.strict()
|
|
36
|
+
.optional(),
|
|
8
37
|
})
|
|
9
38
|
.strict();
|
|
10
39
|
export const controlPlaneConfigSchema = z
|
|
11
40
|
.object({
|
|
12
41
|
maxDispatches: z.number().int().positive().default(1),
|
|
13
42
|
maxConcurrentRuns: z.number().int().positive().default(1),
|
|
14
|
-
|
|
43
|
+
triggers: z.array(triggerSchema).default([]),
|
|
15
44
|
resident: z
|
|
16
45
|
.object({
|
|
17
46
|
// Backoff for the resident loop's own retry cadence when idle or
|
|
@@ -27,6 +56,6 @@ export const controlPlaneConfigSchema = z
|
|
|
27
56
|
.default({
|
|
28
57
|
maxDispatches: 1,
|
|
29
58
|
maxConcurrentRuns: 1,
|
|
30
|
-
|
|
59
|
+
triggers: [],
|
|
31
60
|
resident: { pollBackoffMs: 1000 },
|
|
32
61
|
});
|
|
@@ -2,6 +2,8 @@ import { createEventData } from '@atolis-hq/eventing';
|
|
|
2
2
|
import { ControlEventType } from './events.js';
|
|
3
3
|
export function createControlPlaneEventData(input) {
|
|
4
4
|
switch (input.eventType) {
|
|
5
|
+
case ControlEventType.TriggerInvocationStarted:
|
|
6
|
+
return createEventData(input);
|
|
5
7
|
case ControlEventType.DispatchPaused:
|
|
6
8
|
return createEventData(input);
|
|
7
9
|
case ControlEventType.DispatchResumed:
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type EventDataUnion, type EventEnvelope, type EventUnion } from '@atolis-hq/eventing';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
import { type ControlStreamRef } from './streams.js';
|
|
3
4
|
export declare const ControlEventType: {
|
|
5
|
+
readonly TriggerInvocationStarted: "control-plane.trigger-invocation-started";
|
|
4
6
|
readonly DispatchPaused: "control-plane.dispatch-paused";
|
|
5
7
|
readonly DispatchResumed: "control-plane.dispatch-resumed";
|
|
6
8
|
readonly RunnerPaused: "control-plane.runner-paused";
|
|
@@ -11,7 +13,27 @@ export declare const RunnerPauseCause: {
|
|
|
11
13
|
readonly Quota: "quota";
|
|
12
14
|
};
|
|
13
15
|
export declare const ControlEventNamespace: "control-plane.";
|
|
16
|
+
export declare const TriggerSourceKind: {
|
|
17
|
+
readonly Event: "event";
|
|
18
|
+
readonly Cron: "cron";
|
|
19
|
+
};
|
|
20
|
+
declare const triggerSourceSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
21
|
+
kind: z.ZodLiteral<"event">;
|
|
22
|
+
eventId: z.ZodString;
|
|
23
|
+
eventType: z.ZodString;
|
|
24
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
25
|
+
kind: z.ZodLiteral<"cron">;
|
|
26
|
+
slot: z.ZodString;
|
|
27
|
+
}, z.core.$strict>], "kind">;
|
|
28
|
+
export type TriggerSource = z.infer<typeof triggerSourceSchema>;
|
|
14
29
|
export interface ControlEventPayloads {
|
|
30
|
+
readonly [ControlEventType.TriggerInvocationStarted]: {
|
|
31
|
+
readonly triggerId: string;
|
|
32
|
+
readonly source: TriggerSource;
|
|
33
|
+
readonly workItemId: string;
|
|
34
|
+
readonly workflowInstanceId: string;
|
|
35
|
+
readonly workflowName: string;
|
|
36
|
+
};
|
|
15
37
|
readonly [ControlEventType.DispatchPaused]: {
|
|
16
38
|
readonly resumeAt: string;
|
|
17
39
|
readonly reason: string;
|
|
@@ -34,3 +56,4 @@ export type ControlEvent = EventUnion<ControlEventPayloads, ControlStreamRef>;
|
|
|
34
56
|
export type ControlEventData = EventDataUnion<ControlEventPayloads>;
|
|
35
57
|
export declare function decodeControlEvent(event: EventEnvelope): ControlEvent;
|
|
36
58
|
export declare function selectControlEvent(event: EventEnvelope): ControlEvent | null;
|
|
59
|
+
export {};
|
|
@@ -2,6 +2,7 @@ import { eventDataSchema, eventEnvelopeSchema, } from '@atolis-hq/eventing';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { ControlStreamKind } from './streams.js';
|
|
4
4
|
export const ControlEventType = {
|
|
5
|
+
TriggerInvocationStarted: 'control-plane.trigger-invocation-started',
|
|
5
6
|
DispatchPaused: 'control-plane.dispatch-paused',
|
|
6
7
|
DispatchResumed: 'control-plane.dispatch-resumed',
|
|
7
8
|
RunnerPaused: 'control-plane.runner-paused',
|
|
@@ -9,10 +10,41 @@ export const ControlEventType = {
|
|
|
9
10
|
};
|
|
10
11
|
export const RunnerPauseCause = { Manual: 'manual', Quota: 'quota' };
|
|
11
12
|
export const ControlEventNamespace = 'control-plane.';
|
|
13
|
+
export const TriggerSourceKind = { Event: 'event', Cron: 'cron' };
|
|
14
|
+
const triggerSourceSchema = z.discriminatedUnion('kind', [
|
|
15
|
+
z
|
|
16
|
+
.object({
|
|
17
|
+
kind: z.literal(TriggerSourceKind.Event),
|
|
18
|
+
eventId: z.string().min(1),
|
|
19
|
+
eventType: z.string().min(1),
|
|
20
|
+
})
|
|
21
|
+
.strict(),
|
|
22
|
+
z
|
|
23
|
+
.object({
|
|
24
|
+
kind: z.literal(TriggerSourceKind.Cron),
|
|
25
|
+
slot: z.string().datetime({ offset: true }),
|
|
26
|
+
})
|
|
27
|
+
.strict(),
|
|
28
|
+
]);
|
|
12
29
|
const streamSchema = z
|
|
13
30
|
.object({ kind: z.literal(ControlStreamKind.Global), id: z.literal('global') })
|
|
14
31
|
.strict();
|
|
15
32
|
const eventSchema = z.union([
|
|
33
|
+
eventEnvelopeSchema.extend({
|
|
34
|
+
event: eventDataSchema.extend({
|
|
35
|
+
eventType: z.literal(ControlEventType.TriggerInvocationStarted),
|
|
36
|
+
payload: z
|
|
37
|
+
.object({
|
|
38
|
+
triggerId: z.string().min(1),
|
|
39
|
+
source: triggerSourceSchema,
|
|
40
|
+
workItemId: z.string().min(1),
|
|
41
|
+
workflowInstanceId: z.string().min(1),
|
|
42
|
+
workflowName: z.string().min(1),
|
|
43
|
+
})
|
|
44
|
+
.strict(),
|
|
45
|
+
}),
|
|
46
|
+
stream: streamSchema,
|
|
47
|
+
}),
|
|
16
48
|
eventEnvelopeSchema.extend({
|
|
17
49
|
event: eventDataSchema.extend({
|
|
18
50
|
eventType: z.literal(ControlEventType.DispatchPaused),
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface CronTriggerSlot {
|
|
2
|
+
readonly identity: string;
|
|
3
|
+
readonly at: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class CronTriggerPolicy {
|
|
6
|
+
elapsedSlots(config: {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly cron: string;
|
|
9
|
+
}, now: string, checkpoint: string | null): readonly CronTriggerSlot[];
|
|
10
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CronExpressionParser } from 'cron-parser';
|
|
2
|
-
export class
|
|
2
|
+
export class CronTriggerPolicy {
|
|
3
3
|
elapsedSlots(config, now, checkpoint) {
|
|
4
4
|
const end = minute(new Date(now));
|
|
5
5
|
const start = checkpoint === null ? end : minute(new Date(new Date(checkpoint).getTime() + 60_000));
|
|
@@ -7,9 +7,9 @@ export class SchedulePolicy {
|
|
|
7
7
|
return [];
|
|
8
8
|
const fields = config.cron.trim().split(/\s+/);
|
|
9
9
|
if (fields.length !== 5)
|
|
10
|
-
throw new Error(`
|
|
10
|
+
throw new Error(`Trigger ${config.id} must use a five-field cron expression`);
|
|
11
11
|
if (fields.some(hasUnsupportedSyntax))
|
|
12
|
-
throw new Error(`
|
|
12
|
+
throw new Error(`Trigger ${config.id} uses unsupported cron syntax`);
|
|
13
13
|
const expression = CronExpressionParser.parse(config.cron, {
|
|
14
14
|
currentDate: new Date(start - 1),
|
|
15
15
|
endDate: new Date(end),
|
|
@@ -23,14 +23,14 @@ export class SchedulePolicy {
|
|
|
23
23
|
if (!matchesDays(date, dayOfMonth, dayOfWeek))
|
|
24
24
|
continue;
|
|
25
25
|
const at = date.toISOString();
|
|
26
|
-
slots.push({ identity: `
|
|
26
|
+
slots.push({ identity: `trigger:${config.id}:cron:${at}`, at });
|
|
27
27
|
}
|
|
28
28
|
return slots;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
function minute(value) {
|
|
32
32
|
if (Number.isNaN(value.getTime()))
|
|
33
|
-
throw new Error('
|
|
33
|
+
throw new Error('Trigger timestamps must be valid dates');
|
|
34
34
|
return Math.floor(value.getTime() / 60_000) * 60_000;
|
|
35
35
|
}
|
|
36
36
|
function hasUnsupportedSyntax(field) {
|
|
@@ -5,7 +5,7 @@ export type * from './application/activation-scheduler-ports.js';
|
|
|
5
5
|
export * from './application/control-plane-projection.js';
|
|
6
6
|
export * from './application/control-plane-service.js';
|
|
7
7
|
export * from './application/runner-control-service.js';
|
|
8
|
-
export * from './application/
|
|
8
|
+
export * from './application/trigger-service.js';
|
|
9
9
|
export * from './application/intake-pipeline.js';
|
|
10
10
|
export * from './application/runner-pipeline.js';
|
|
11
11
|
export * from './application/work-cancellation-policy.js';
|
|
@@ -18,7 +18,7 @@ export * from './contracts/views.js';
|
|
|
18
18
|
export * from './domain/dispatch-policy.js';
|
|
19
19
|
export * from './domain/quota-policy.js';
|
|
20
20
|
export * from './domain/runner-quota.js';
|
|
21
|
-
export * from './domain/
|
|
21
|
+
export * from './domain/cron-trigger-policy.js';
|
|
22
22
|
export * from './infrastructure/intake-host.js';
|
|
23
23
|
export * from './infrastructure/resident-host.js';
|
|
24
24
|
export * from './infrastructure/tick-host.js';
|
|
@@ -4,7 +4,7 @@ export * from './application/activation-scheduler-subscriber.js';
|
|
|
4
4
|
export * from './application/control-plane-projection.js';
|
|
5
5
|
export * from './application/control-plane-service.js';
|
|
6
6
|
export * from './application/runner-control-service.js';
|
|
7
|
-
export * from './application/
|
|
7
|
+
export * from './application/trigger-service.js';
|
|
8
8
|
export * from './application/intake-pipeline.js';
|
|
9
9
|
export * from './application/runner-pipeline.js';
|
|
10
10
|
export * from './application/work-cancellation-policy.js';
|
|
@@ -17,7 +17,7 @@ export * from './contracts/views.js';
|
|
|
17
17
|
export * from './domain/dispatch-policy.js';
|
|
18
18
|
export * from './domain/quota-policy.js';
|
|
19
19
|
export * from './domain/runner-quota.js';
|
|
20
|
-
export * from './domain/
|
|
20
|
+
export * from './domain/cron-trigger-policy.js';
|
|
21
21
|
export * from './infrastructure/intake-host.js';
|
|
22
22
|
export * from './infrastructure/resident-host.js';
|
|
23
23
|
export * from './infrastructure/tick-host.js';
|
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ScheduleCheckpointStore } from '../control-plane/index.js';
|
|
2
|
-
/** Durable timestamp cursor for ScheduleService; journal checkpoints only store numeric offsets. */
|
|
3
|
-
export declare class FileScheduleCheckpointStore implements ScheduleCheckpointStore {
|
|
4
|
-
private readonly dataRoot;
|
|
5
|
-
constructor(dataRoot: string);
|
|
6
|
-
load(scheduleId: string): Promise<string | null>;
|
|
7
|
-
save(scheduleId: string, slot: string): Promise<void>;
|
|
8
|
-
private path;
|
|
9
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { mkdir, open, readFile, rename, rm } from 'node:fs/promises';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
3
|
-
/** Durable timestamp cursor for ScheduleService; journal checkpoints only store numeric offsets. */
|
|
4
|
-
export class FileScheduleCheckpointStore {
|
|
5
|
-
dataRoot;
|
|
6
|
-
constructor(dataRoot) {
|
|
7
|
-
this.dataRoot = dataRoot;
|
|
8
|
-
}
|
|
9
|
-
async load(scheduleId) {
|
|
10
|
-
try {
|
|
11
|
-
const parsed = JSON.parse(await readFile(this.path(scheduleId), 'utf8'));
|
|
12
|
-
if (parsed.scheduleId !== scheduleId || Number.isNaN(Date.parse(parsed.slot)))
|
|
13
|
-
throw new Error(`Invalid schedule checkpoint: ${scheduleId}`);
|
|
14
|
-
return parsed.slot;
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
if (error.code === 'ENOENT')
|
|
18
|
-
return null;
|
|
19
|
-
throw error;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
async save(scheduleId, slot) {
|
|
23
|
-
const existing = await this.load(scheduleId);
|
|
24
|
-
if (existing !== null && Date.parse(slot) < Date.parse(existing))
|
|
25
|
-
throw new Error(`Schedule checkpoint regression: ${scheduleId}`);
|
|
26
|
-
const path = this.path(scheduleId);
|
|
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({ scheduleId, 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(scheduleId) {
|
|
45
|
-
if (scheduleId.length === 0 || /[\\/]/.test(scheduleId))
|
|
46
|
-
throw new Error('Schedule id must not contain path separators');
|
|
47
|
-
return join(this.dataRoot, 'schedule-checkpoints', `${encodeURIComponent(scheduleId)}.json`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { type CommandContext } from '@atolis-hq/eventing';
|
|
2
|
-
import { type IdGenerator } from '../../kernel/index.js';
|
|
3
|
-
import type { StartWorkflowInstance } from '../../orchestration/index.js';
|
|
4
|
-
import { workflowInstanceId } from '../../orchestration/index.js';
|
|
5
|
-
import type { CreateWorkItem } from '../../work/index.js';
|
|
6
|
-
import { workItemId } from '../../work/index.js';
|
|
7
|
-
import type { ScheduleConfig } from '../contracts/config.js';
|
|
8
|
-
export interface ScheduleCheckpointStore {
|
|
9
|
-
load(scheduleId: string): Promise<string | null>;
|
|
10
|
-
save(scheduleId: string, slot: string): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
export interface ScheduleServiceDependencies {
|
|
13
|
-
readonly checkpoint: ScheduleCheckpointStore;
|
|
14
|
-
readonly ids: IdGenerator;
|
|
15
|
-
readonly work: {
|
|
16
|
-
create(command: CreateWorkItem, context: CommandContext): Promise<unknown>;
|
|
17
|
-
};
|
|
18
|
-
readonly orchestration: Pick<{
|
|
19
|
-
start(command: StartWorkflowInstance, context: CommandContext): Promise<unknown>;
|
|
20
|
-
get(id: ReturnType<typeof workflowInstanceId>): Promise<{
|
|
21
|
-
readonly workItemId: ReturnType<typeof workItemId>;
|
|
22
|
-
} | null>;
|
|
23
|
-
}, 'start' | 'get'>;
|
|
24
|
-
readonly now: () => string;
|
|
25
|
-
}
|
|
26
|
-
export declare class ScheduleService {
|
|
27
|
-
private readonly dependencies;
|
|
28
|
-
private readonly policy;
|
|
29
|
-
constructor(dependencies: ScheduleServiceDependencies);
|
|
30
|
-
run(config: ScheduleConfig, context: CommandContext): Promise<readonly string[]>;
|
|
31
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { correlationId } from '@atolis-hq/eventing';
|
|
2
|
-
import { orchestrationGroupId, workflowInstanceId, workflowName, } from '../../orchestration/index.js';
|
|
3
|
-
import { workItemId } from '../../work/index.js';
|
|
4
|
-
import { SchedulePolicy } from '../domain/schedule-policy.js';
|
|
5
|
-
export class ScheduleService {
|
|
6
|
-
dependencies;
|
|
7
|
-
policy = new SchedulePolicy();
|
|
8
|
-
constructor(dependencies) {
|
|
9
|
-
this.dependencies = dependencies;
|
|
10
|
-
}
|
|
11
|
-
async run(config, context) {
|
|
12
|
-
const checkpoint = await this.dependencies.checkpoint.load(config.id);
|
|
13
|
-
const slots = this.policy.elapsedSlots(config, this.dependencies.now(), checkpoint);
|
|
14
|
-
const accepted = [];
|
|
15
|
-
for (const slot of slots) {
|
|
16
|
-
const workflow = workflowInstanceId(`workflow-${safe(slot.at)}`);
|
|
17
|
-
const slotContext = slotCommandContext(context, slot.identity);
|
|
18
|
-
const existing = await this.dependencies.orchestration.get(workflow);
|
|
19
|
-
const item = existing?.workItemId ?? workItemId(this.dependencies.ids.next('work'));
|
|
20
|
-
if (existing === null) {
|
|
21
|
-
const workCommand = { workItemId: item, objective: config.objective };
|
|
22
|
-
await this.dependencies.work.create(workCommand, slotContext);
|
|
23
|
-
}
|
|
24
|
-
const start = {
|
|
25
|
-
workflowInstanceId: workflow,
|
|
26
|
-
workItemId: item,
|
|
27
|
-
workflowName: workflowName(config.workflow),
|
|
28
|
-
orchestrationGroupId: orchestrationGroupId(`primary:${item}`),
|
|
29
|
-
};
|
|
30
|
-
await this.dependencies.orchestration.start(start, slotContext);
|
|
31
|
-
await this.dependencies.checkpoint.save(config.id, slot.at);
|
|
32
|
-
accepted.push(slot.identity);
|
|
33
|
-
}
|
|
34
|
-
return accepted;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function slotCommandContext(context, identity) {
|
|
38
|
-
const suffix = safe(identity);
|
|
39
|
-
return {
|
|
40
|
-
...context,
|
|
41
|
-
commandId: `${context.commandId}:${suffix}`,
|
|
42
|
-
correlationId: correlationId(`${context.correlationId}:${suffix}`),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
function safe(value) {
|
|
46
|
-
return value.toLowerCase().replace(/[^a-z0-9-]/g, '-');
|
|
47
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ScheduleConfig } from '../contracts/config.js';
|
|
2
|
-
export interface ScheduleSlot {
|
|
3
|
-
readonly identity: string;
|
|
4
|
-
readonly at: string;
|
|
5
|
-
}
|
|
6
|
-
export declare class SchedulePolicy {
|
|
7
|
-
elapsedSlots(config: ScheduleConfig, now: string, checkpoint: string | null): readonly ScheduleSlot[];
|
|
8
|
-
}
|