reqon-dsl 0.2.0
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/.claude/settings.local.json +31 -0
- package/.claude/skills/api-integration.md +125 -0
- package/.claude/skills/database-schema.md +51 -0
- package/.claude/skills/dsl-design.md +80 -0
- package/.claude/skills/property-testing.md +143 -0
- package/.claude/skills/reqon/SKILL.md +44 -0
- package/.claude/skills/reqon/references/examples.md +206 -0
- package/.claude/skills/reqon/references/syntax.md +263 -0
- package/.claude/skills/vscode-extension.md +113 -0
- package/.github/dependabot.yml +32 -0
- package/.github/pull_request_template.md +21 -0
- package/.github/workflows/ci.yml +174 -0
- package/.github/workflows/release.yml +73 -0
- package/CLAUDE.md +72 -0
- package/CONTRIBUTING.md +161 -0
- package/README.md +235 -0
- package/TODO.md +51 -0
- package/dist/ast/index.d.ts +1 -0
- package/dist/ast/index.js +1 -0
- package/dist/ast/nodes.d.ts +237 -0
- package/dist/ast/nodes.js +12 -0
- package/dist/auth/auth.test.d.ts +1 -0
- package/dist/auth/auth.test.js +255 -0
- package/dist/auth/circuit-breaker.d.ts +115 -0
- package/dist/auth/circuit-breaker.js +267 -0
- package/dist/auth/credentials.d.ts +91 -0
- package/dist/auth/credentials.js +169 -0
- package/dist/auth/index.d.ts +5 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/oauth2-provider.d.ts +41 -0
- package/dist/auth/oauth2-provider.js +131 -0
- package/dist/auth/rate-limiter.d.ts +61 -0
- package/dist/auth/rate-limiter.js +380 -0
- package/dist/auth/token-store.d.ts +30 -0
- package/dist/auth/token-store.js +148 -0
- package/dist/auth/types.d.ts +142 -0
- package/dist/auth/types.js +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +270 -0
- package/dist/errors/errors.test.d.ts +1 -0
- package/dist/errors/errors.test.js +165 -0
- package/dist/errors/index.d.ts +83 -0
- package/dist/errors/index.js +159 -0
- package/dist/execution/execution.test.d.ts +1 -0
- package/dist/execution/execution.test.js +246 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/state.d.ts +136 -0
- package/dist/execution/state.js +82 -0
- package/dist/execution/store.d.ts +52 -0
- package/dist/execution/store.js +120 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +57 -0
- package/dist/integration.test.d.ts +1 -0
- package/dist/integration.test.js +168 -0
- package/dist/interpreter/context.d.ts +15 -0
- package/dist/interpreter/context.js +29 -0
- package/dist/interpreter/evaluator.d.ts +5 -0
- package/dist/interpreter/evaluator.js +223 -0
- package/dist/interpreter/evaluator.test.d.ts +1 -0
- package/dist/interpreter/evaluator.test.js +512 -0
- package/dist/interpreter/executor.d.ts +131 -0
- package/dist/interpreter/executor.js +663 -0
- package/dist/interpreter/fetch-handler.d.ts +43 -0
- package/dist/interpreter/fetch-handler.js +203 -0
- package/dist/interpreter/http.d.ts +57 -0
- package/dist/interpreter/http.js +210 -0
- package/dist/interpreter/http.test.d.ts +1 -0
- package/dist/interpreter/http.test.js +299 -0
- package/dist/interpreter/index.d.ts +7 -0
- package/dist/interpreter/index.js +7 -0
- package/dist/interpreter/pagination.d.ts +63 -0
- package/dist/interpreter/pagination.js +155 -0
- package/dist/interpreter/progress.test.d.ts +1 -0
- package/dist/interpreter/progress.test.js +216 -0
- package/dist/interpreter/schema-matcher.d.ts +16 -0
- package/dist/interpreter/schema-matcher.js +136 -0
- package/dist/interpreter/schema-matcher.test.d.ts +1 -0
- package/dist/interpreter/schema-matcher.test.js +122 -0
- package/dist/interpreter/signals.d.ts +57 -0
- package/dist/interpreter/signals.js +73 -0
- package/dist/interpreter/step-handlers/for-handler.d.ts +17 -0
- package/dist/interpreter/step-handlers/for-handler.js +51 -0
- package/dist/interpreter/step-handlers/index.d.ts +8 -0
- package/dist/interpreter/step-handlers/index.js +8 -0
- package/dist/interpreter/step-handlers/map-handler.d.ts +10 -0
- package/dist/interpreter/step-handlers/map-handler.js +20 -0
- package/dist/interpreter/step-handlers/match-handler.d.ts +27 -0
- package/dist/interpreter/step-handlers/match-handler.js +61 -0
- package/dist/interpreter/step-handlers/store-handler.d.ts +13 -0
- package/dist/interpreter/step-handlers/store-handler.js +66 -0
- package/dist/interpreter/step-handlers/types.d.ts +15 -0
- package/dist/interpreter/step-handlers/types.js +1 -0
- package/dist/interpreter/step-handlers/validate-handler.d.ts +10 -0
- package/dist/interpreter/step-handlers/validate-handler.js +26 -0
- package/dist/interpreter/step-handlers/webhook-handler.d.ts +36 -0
- package/dist/interpreter/step-handlers/webhook-handler.js +104 -0
- package/dist/lexer/index.d.ts +10 -0
- package/dist/lexer/index.js +12 -0
- package/dist/lexer/lexer.d.ts +24 -0
- package/dist/lexer/lexer.js +264 -0
- package/dist/lexer/lexer.test.d.ts +1 -0
- package/dist/lexer/lexer.test.js +259 -0
- package/dist/lexer/tokens.d.ts +69 -0
- package/dist/lexer/tokens.js +146 -0
- package/dist/loader/index.d.ts +36 -0
- package/dist/loader/index.js +220 -0
- package/dist/loader/loader.test.d.ts +1 -0
- package/dist/loader/loader.test.js +287 -0
- package/dist/oas/index.d.ts +4 -0
- package/dist/oas/index.js +2 -0
- package/dist/oas/loader.d.ts +21 -0
- package/dist/oas/loader.js +82 -0
- package/dist/oas/oas.test.d.ts +1 -0
- package/dist/oas/oas.test.js +218 -0
- package/dist/oas/validator.d.ts +12 -0
- package/dist/oas/validator.js +227 -0
- package/dist/parser/base.d.ts +33 -0
- package/dist/parser/base.js +97 -0
- package/dist/parser/expressions.d.ts +27 -0
- package/dist/parser/expressions.js +248 -0
- package/dist/parser/expressions.test.d.ts +1 -0
- package/dist/parser/expressions.test.js +378 -0
- package/dist/parser/index.d.ts +3 -0
- package/dist/parser/index.js +3 -0
- package/dist/parser/match.test.d.ts +1 -0
- package/dist/parser/match.test.js +254 -0
- package/dist/parser/parser.d.ts +68 -0
- package/dist/parser/parser.js +1229 -0
- package/dist/parser/parser.test.d.ts +1 -0
- package/dist/parser/parser.test.js +333 -0
- package/dist/parser/schedule.test.d.ts +1 -0
- package/dist/parser/schedule.test.js +241 -0
- package/dist/plugin.d.ts +35 -0
- package/dist/plugin.js +68 -0
- package/dist/scheduler/cron-parser.d.ts +32 -0
- package/dist/scheduler/cron-parser.js +198 -0
- package/dist/scheduler/cron-parser.test.d.ts +1 -0
- package/dist/scheduler/cron-parser.test.js +188 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +2 -0
- package/dist/scheduler/scheduler.d.ts +81 -0
- package/dist/scheduler/scheduler.js +376 -0
- package/dist/scheduler/types.d.ts +65 -0
- package/dist/scheduler/types.js +1 -0
- package/dist/stores/factory.d.ts +36 -0
- package/dist/stores/factory.js +73 -0
- package/dist/stores/file.d.ts +60 -0
- package/dist/stores/file.js +173 -0
- package/dist/stores/file.test.d.ts +1 -0
- package/dist/stores/file.test.js +165 -0
- package/dist/stores/index.d.ts +6 -0
- package/dist/stores/index.js +5 -0
- package/dist/stores/memory.d.ts +19 -0
- package/dist/stores/memory.js +51 -0
- package/dist/stores/memory.test.d.ts +1 -0
- package/dist/stores/memory.test.js +157 -0
- package/dist/stores/postgrest.d.ts +55 -0
- package/dist/stores/postgrest.js +217 -0
- package/dist/stores/stores.test.d.ts +1 -0
- package/dist/stores/stores.test.js +158 -0
- package/dist/stores/types.d.ts +31 -0
- package/dist/stores/types.js +26 -0
- package/dist/sync/index.d.ts +4 -0
- package/dist/sync/index.js +2 -0
- package/dist/sync/state.d.ts +69 -0
- package/dist/sync/state.js +66 -0
- package/dist/sync/store.d.ts +49 -0
- package/dist/sync/store.js +93 -0
- package/dist/sync/sync.test.d.ts +1 -0
- package/dist/sync/sync.test.js +221 -0
- package/dist/utils/async.d.ts +7 -0
- package/dist/utils/async.js +9 -0
- package/dist/utils/file.d.ts +38 -0
- package/dist/utils/file.js +92 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/logger.d.ts +34 -0
- package/dist/utils/logger.js +39 -0
- package/dist/utils/path.d.ts +12 -0
- package/dist/utils/path.js +41 -0
- package/dist/webhook/index.d.ts +8 -0
- package/dist/webhook/index.js +7 -0
- package/dist/webhook/server.d.ts +84 -0
- package/dist/webhook/server.js +319 -0
- package/dist/webhook/store.d.ts +67 -0
- package/dist/webhook/store.js +193 -0
- package/dist/webhook/types.d.ts +88 -0
- package/dist/webhook/types.js +6 -0
- package/docusaurus/README.md +41 -0
- package/docusaurus/docs/advanced/execution-state.md +283 -0
- package/docusaurus/docs/advanced/extending-reqon.md +388 -0
- package/docusaurus/docs/advanced/multi-file-missions.md +250 -0
- package/docusaurus/docs/advanced/parallel-execution.md +353 -0
- package/docusaurus/docs/api-reference.md +443 -0
- package/docusaurus/docs/authentication/api-key.md +339 -0
- package/docusaurus/docs/authentication/basic.md +276 -0
- package/docusaurus/docs/authentication/bearer.md +282 -0
- package/docusaurus/docs/authentication/oauth2.md +317 -0
- package/docusaurus/docs/authentication/overview.md +251 -0
- package/docusaurus/docs/cli.md +229 -0
- package/docusaurus/docs/core-concepts/actions.md +286 -0
- package/docusaurus/docs/core-concepts/missions.md +264 -0
- package/docusaurus/docs/core-concepts/schemas.md +353 -0
- package/docusaurus/docs/core-concepts/sources.md +339 -0
- package/docusaurus/docs/core-concepts/stores.md +332 -0
- package/docusaurus/docs/dsl-syntax/expressions.md +361 -0
- package/docusaurus/docs/dsl-syntax/fetch.md +293 -0
- package/docusaurus/docs/dsl-syntax/for-loops.md +324 -0
- package/docusaurus/docs/dsl-syntax/map.md +345 -0
- package/docusaurus/docs/dsl-syntax/match.md +387 -0
- package/docusaurus/docs/dsl-syntax/pipelines.md +397 -0
- package/docusaurus/docs/dsl-syntax/validate.md +401 -0
- package/docusaurus/docs/error-handling/dead-letter-queues.md +399 -0
- package/docusaurus/docs/error-handling/flow-control.md +337 -0
- package/docusaurus/docs/error-handling/retry-strategies.md +368 -0
- package/docusaurus/docs/examples.md +488 -0
- package/docusaurus/docs/getting-started.md +256 -0
- package/docusaurus/docs/http/circuit-breaker.md +401 -0
- package/docusaurus/docs/http/incremental-sync.md +394 -0
- package/docusaurus/docs/http/pagination.md +361 -0
- package/docusaurus/docs/http/rate-limiting.md +383 -0
- package/docusaurus/docs/http/requests.md +328 -0
- package/docusaurus/docs/http/retry.md +402 -0
- package/docusaurus/docs/intro.md +90 -0
- package/docusaurus/docs/openapi/loading-specs.md +305 -0
- package/docusaurus/docs/openapi/operation-calls.md +314 -0
- package/docusaurus/docs/openapi/overview.md +212 -0
- package/docusaurus/docs/openapi/response-validation.md +344 -0
- package/docusaurus/docs/scheduling/cron.md +305 -0
- package/docusaurus/docs/scheduling/daemon-mode.md +317 -0
- package/docusaurus/docs/scheduling/intervals.md +289 -0
- package/docusaurus/docs/scheduling/overview.md +231 -0
- package/docusaurus/docs/stores/custom-adapters.md +376 -0
- package/docusaurus/docs/stores/file.md +236 -0
- package/docusaurus/docs/stores/memory.md +193 -0
- package/docusaurus/docs/stores/overview.md +274 -0
- package/docusaurus/docs/stores/postgrest.md +316 -0
- package/docusaurus/docusaurus.config.ts +148 -0
- package/docusaurus/package-lock.json +18029 -0
- package/docusaurus/package.json +47 -0
- package/docusaurus/sidebars.ts +155 -0
- package/docusaurus/src/components/HomepageFeatures/index.tsx +105 -0
- package/docusaurus/src/components/HomepageFeatures/styles.module.css +12 -0
- package/docusaurus/src/css/custom.css +169 -0
- package/docusaurus/src/pages/index.module.css +48 -0
- package/docusaurus/src/pages/index.tsx +110 -0
- package/docusaurus/src/pages/markdown-page.md +7 -0
- package/docusaurus/static/.nojekyll +0 -0
- package/docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docusaurus/static/img/docusaurus.png +0 -0
- package/docusaurus/static/img/favicon.ico +0 -0
- package/docusaurus/static/img/logo.svg +10 -0
- package/docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docusaurus/tsconfig.json +8 -0
- package/examples/README.md +112 -0
- package/examples/error-handling/README.md +150 -0
- package/examples/error-handling/payment-processor.vague +287 -0
- package/examples/github-sync/README.md +74 -0
- package/examples/github-sync/fetch-issues.vague +47 -0
- package/examples/github-sync/fetch-prs.vague +40 -0
- package/examples/github-sync/mission.vague +101 -0
- package/examples/github-sync/normalize.vague +70 -0
- package/examples/jsonplaceholder/README.md +28 -0
- package/examples/jsonplaceholder/posts.vague +48 -0
- package/examples/petstore/README.md +35 -0
- package/examples/petstore/openapi.yaml +97 -0
- package/examples/petstore/sync.vague +52 -0
- package/examples/temporal-comparison/README.md +297 -0
- package/examples/temporal-comparison/reconciliation.vague +355 -0
- package/examples/temporal-comparison/temporal/activities/index.ts +8 -0
- package/examples/temporal-comparison/temporal/activities/shipstation.ts +225 -0
- package/examples/temporal-comparison/temporal/activities/shopify.ts +257 -0
- package/examples/temporal-comparison/temporal/activities/storage.ts +198 -0
- package/examples/temporal-comparison/temporal/activities/stripe.ts +169 -0
- package/examples/temporal-comparison/temporal/activities/validation.ts +205 -0
- package/examples/temporal-comparison/temporal/client/schedule.ts +218 -0
- package/examples/temporal-comparison/temporal/config/retry.ts +63 -0
- package/examples/temporal-comparison/temporal/types/index.ts +129 -0
- package/examples/temporal-comparison/temporal/workers/main.ts +130 -0
- package/examples/temporal-comparison/temporal/workflows/orderReconciliation.ts +262 -0
- package/examples/xero/README.md +88 -0
- package/examples/xero/invoices.vague +189 -0
- package/package.json +40 -0
- package/src/api-integration.test.ts +954 -0
- package/src/ast/index.ts +1 -0
- package/src/ast/nodes.ts +310 -0
- package/src/auth/auth.test.ts +326 -0
- package/src/auth/circuit-breaker.test.ts +390 -0
- package/src/auth/circuit-breaker.ts +379 -0
- package/src/auth/credentials.test.ts +273 -0
- package/src/auth/credentials.ts +246 -0
- package/src/auth/index.ts +40 -0
- package/src/auth/oauth2-provider.ts +177 -0
- package/src/auth/rate-limiter.ts +459 -0
- package/src/auth/token-store.ts +177 -0
- package/src/auth/types.ts +159 -0
- package/src/benchmark/e2e.bench.ts +288 -0
- package/src/benchmark/evaluator.bench.ts +331 -0
- package/src/benchmark/fixtures.ts +295 -0
- package/src/benchmark/index.ts +108 -0
- package/src/benchmark/lexer.bench.ts +69 -0
- package/src/benchmark/parser.bench.ts +103 -0
- package/src/benchmark/resilience.bench.ts +193 -0
- package/src/benchmark/store.bench.ts +147 -0
- package/src/benchmark/utils.ts +230 -0
- package/src/cli.ts +313 -0
- package/src/errors/errors.test.ts +234 -0
- package/src/errors/index.ts +223 -0
- package/src/execution/execution.test.ts +307 -0
- package/src/execution/index.ts +21 -0
- package/src/execution/state.ts +207 -0
- package/src/execution/store.ts +188 -0
- package/src/index.ts +169 -0
- package/src/integration.test.ts +192 -0
- package/src/interpreter/context.ts +57 -0
- package/src/interpreter/evaluator.test.ts +796 -0
- package/src/interpreter/evaluator.ts +245 -0
- package/src/interpreter/executor.ts +946 -0
- package/src/interpreter/fetch-handler.ts +302 -0
- package/src/interpreter/http.test.ts +423 -0
- package/src/interpreter/http.ts +308 -0
- package/src/interpreter/index.ts +32 -0
- package/src/interpreter/pagination.ts +207 -0
- package/src/interpreter/progress.test.ts +276 -0
- package/src/interpreter/schema-matcher.test.ts +160 -0
- package/src/interpreter/schema-matcher.ts +168 -0
- package/src/interpreter/signals.ts +73 -0
- package/src/interpreter/step-handlers/for-handler.ts +65 -0
- package/src/interpreter/step-handlers/index.ts +17 -0
- package/src/interpreter/step-handlers/map-handler.ts +24 -0
- package/src/interpreter/step-handlers/match-handler.ts +101 -0
- package/src/interpreter/step-handlers/store-handler.ts +78 -0
- package/src/interpreter/step-handlers/types.ts +17 -0
- package/src/interpreter/step-handlers/validate-handler.ts +30 -0
- package/src/interpreter/step-handlers/webhook-handler.ts +142 -0
- package/src/lexer/index.ts +18 -0
- package/src/lexer/lexer.test.ts +316 -0
- package/src/lexer/tokens.ts +179 -0
- package/src/loader/index.ts +288 -0
- package/src/loader/loader.test.ts +360 -0
- package/src/oas/index.ts +4 -0
- package/src/oas/loader.ts +126 -0
- package/src/oas/oas.test.ts +254 -0
- package/src/oas/validator.ts +299 -0
- package/src/parser/base.ts +124 -0
- package/src/parser/expressions.test.ts +525 -0
- package/src/parser/expressions.ts +314 -0
- package/src/parser/index.ts +3 -0
- package/src/parser/match.test.ts +296 -0
- package/src/parser/parser.test.ts +739 -0
- package/src/parser/parser.ts +1469 -0
- package/src/parser/schedule.test.ts +287 -0
- package/src/parser/webhook.test.ts +248 -0
- package/src/plugin.ts +83 -0
- package/src/scheduler/cron-parser.test.ts +236 -0
- package/src/scheduler/cron-parser.ts +236 -0
- package/src/scheduler/index.ts +10 -0
- package/src/scheduler/scheduler.ts +443 -0
- package/src/scheduler/types.ts +71 -0
- package/src/stores/factory.ts +104 -0
- package/src/stores/file.test.ts +276 -0
- package/src/stores/file.ts +211 -0
- package/src/stores/index.ts +6 -0
- package/src/stores/memory.test.ts +238 -0
- package/src/stores/memory.ts +63 -0
- package/src/stores/postgrest.test.ts +488 -0
- package/src/stores/postgrest.ts +263 -0
- package/src/stores/stores.test.ts +197 -0
- package/src/stores/types.ts +58 -0
- package/src/sync/index.ts +16 -0
- package/src/sync/state.ts +126 -0
- package/src/sync/store.ts +139 -0
- package/src/sync/sync.test.ts +271 -0
- package/src/utils/async.ts +10 -0
- package/src/utils/file.ts +106 -0
- package/src/utils/index.ts +14 -0
- package/src/utils/logger.ts +53 -0
- package/src/utils/path.ts +47 -0
- package/src/webhook/index.ts +15 -0
- package/src/webhook/server.test.ts +253 -0
- package/src/webhook/server.ts +389 -0
- package/src/webhook/store.ts +239 -0
- package/src/webhook/types.ts +93 -0
- package/tsconfig.json +17 -0
- package/vitest.config.ts +39 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { rmSync, existsSync } from 'node:fs';
|
|
3
|
+
import {
|
|
4
|
+
createExecutionState,
|
|
5
|
+
findResumePoint,
|
|
6
|
+
canResume,
|
|
7
|
+
getProgress,
|
|
8
|
+
getExecutionSummary,
|
|
9
|
+
type ExecutionState,
|
|
10
|
+
} from './state.js';
|
|
11
|
+
import { FileExecutionStore, MemoryExecutionStore } from './store.js';
|
|
12
|
+
|
|
13
|
+
const TEST_DIR = '.reqon-test-executions';
|
|
14
|
+
|
|
15
|
+
describe('ExecutionState', () => {
|
|
16
|
+
describe('createExecutionState', () => {
|
|
17
|
+
it('creates initial state with pending stages', () => {
|
|
18
|
+
const state = createExecutionState({
|
|
19
|
+
mission: 'TestMission',
|
|
20
|
+
stages: ['FetchData', 'ProcessData', 'StoreResults'],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(state.mission).toBe('TestMission');
|
|
24
|
+
expect(state.status).toBe('pending');
|
|
25
|
+
expect(state.stages).toHaveLength(3);
|
|
26
|
+
expect(state.stages[0].action).toBe('FetchData');
|
|
27
|
+
expect(state.stages[0].status).toBe('pending');
|
|
28
|
+
expect(state.id).toMatch(/^exec_/);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('includes metadata', () => {
|
|
32
|
+
const state = createExecutionState({
|
|
33
|
+
mission: 'Test',
|
|
34
|
+
stages: ['A'],
|
|
35
|
+
metadata: { tenant: 'acme', userId: '123' },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
expect(state.metadata).toEqual({ tenant: 'acme', userId: '123' });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('findResumePoint', () => {
|
|
43
|
+
it('returns 0 for fresh execution', () => {
|
|
44
|
+
const state = createExecutionState({
|
|
45
|
+
mission: 'Test',
|
|
46
|
+
stages: ['A', 'B', 'C'],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(findResumePoint(state)).toBe(0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('returns index of first non-completed stage', () => {
|
|
53
|
+
const state = createExecutionState({
|
|
54
|
+
mission: 'Test',
|
|
55
|
+
stages: ['A', 'B', 'C'],
|
|
56
|
+
});
|
|
57
|
+
state.stages[0].status = 'completed';
|
|
58
|
+
state.stages[1].status = 'failed';
|
|
59
|
+
|
|
60
|
+
expect(findResumePoint(state)).toBe(1);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('returns -1 when all stages complete', () => {
|
|
64
|
+
const state = createExecutionState({
|
|
65
|
+
mission: 'Test',
|
|
66
|
+
stages: ['A', 'B'],
|
|
67
|
+
});
|
|
68
|
+
state.stages[0].status = 'completed';
|
|
69
|
+
state.stages[1].status = 'completed';
|
|
70
|
+
|
|
71
|
+
expect(findResumePoint(state)).toBe(-1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('skips over skipped stages', () => {
|
|
75
|
+
const state = createExecutionState({
|
|
76
|
+
mission: 'Test',
|
|
77
|
+
stages: ['A', 'B', 'C'],
|
|
78
|
+
});
|
|
79
|
+
state.stages[0].status = 'completed';
|
|
80
|
+
state.stages[1].status = 'skipped';
|
|
81
|
+
|
|
82
|
+
expect(findResumePoint(state)).toBe(2);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('canResume', () => {
|
|
87
|
+
it('returns true for failed executions', () => {
|
|
88
|
+
const state = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
89
|
+
state.status = 'failed';
|
|
90
|
+
expect(canResume(state)).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('returns true for paused executions', () => {
|
|
94
|
+
const state = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
95
|
+
state.status = 'paused';
|
|
96
|
+
expect(canResume(state)).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('returns false for completed executions', () => {
|
|
100
|
+
const state = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
101
|
+
state.status = 'completed';
|
|
102
|
+
expect(canResume(state)).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('returns false for running executions', () => {
|
|
106
|
+
const state = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
107
|
+
state.status = 'running';
|
|
108
|
+
expect(canResume(state)).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('getProgress', () => {
|
|
113
|
+
it('returns 0 for no completed stages', () => {
|
|
114
|
+
const state = createExecutionState({
|
|
115
|
+
mission: 'Test',
|
|
116
|
+
stages: ['A', 'B', 'C', 'D'],
|
|
117
|
+
});
|
|
118
|
+
expect(getProgress(state)).toBe(0);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('returns 50 for half completed', () => {
|
|
122
|
+
const state = createExecutionState({
|
|
123
|
+
mission: 'Test',
|
|
124
|
+
stages: ['A', 'B', 'C', 'D'],
|
|
125
|
+
});
|
|
126
|
+
state.stages[0].status = 'completed';
|
|
127
|
+
state.stages[1].status = 'completed';
|
|
128
|
+
expect(getProgress(state)).toBe(50);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('returns 100 for all completed', () => {
|
|
132
|
+
const state = createExecutionState({
|
|
133
|
+
mission: 'Test',
|
|
134
|
+
stages: ['A', 'B'],
|
|
135
|
+
});
|
|
136
|
+
state.stages[0].status = 'completed';
|
|
137
|
+
state.stages[1].status = 'completed';
|
|
138
|
+
expect(getProgress(state)).toBe(100);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('counts skipped as progress', () => {
|
|
142
|
+
const state = createExecutionState({
|
|
143
|
+
mission: 'Test',
|
|
144
|
+
stages: ['A', 'B'],
|
|
145
|
+
});
|
|
146
|
+
state.stages[0].status = 'completed';
|
|
147
|
+
state.stages[1].status = 'skipped';
|
|
148
|
+
expect(getProgress(state)).toBe(100);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('getExecutionSummary', () => {
|
|
153
|
+
it('generates readable summary', () => {
|
|
154
|
+
const state = createExecutionState({
|
|
155
|
+
mission: 'SyncInvoices',
|
|
156
|
+
stages: ['Fetch', 'Process', 'Store'],
|
|
157
|
+
});
|
|
158
|
+
state.stages[0].status = 'completed';
|
|
159
|
+
state.stages[1].status = 'failed';
|
|
160
|
+
state.status = 'failed';
|
|
161
|
+
|
|
162
|
+
const summary = getExecutionSummary(state);
|
|
163
|
+
expect(summary).toContain('SyncInvoices');
|
|
164
|
+
expect(summary).toContain('failed');
|
|
165
|
+
expect(summary).toContain('1 completed');
|
|
166
|
+
expect(summary).toContain('1 failed');
|
|
167
|
+
expect(summary).toContain('1 pending');
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('MemoryExecutionStore', () => {
|
|
173
|
+
let store: MemoryExecutionStore;
|
|
174
|
+
|
|
175
|
+
beforeEach(() => {
|
|
176
|
+
store = new MemoryExecutionStore();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('saves and loads execution state', async () => {
|
|
180
|
+
const state = createExecutionState({
|
|
181
|
+
mission: 'Test',
|
|
182
|
+
stages: ['A', 'B'],
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
await store.save(state);
|
|
186
|
+
const loaded = await store.load(state.id);
|
|
187
|
+
|
|
188
|
+
expect(loaded).not.toBeNull();
|
|
189
|
+
expect(loaded!.id).toBe(state.id);
|
|
190
|
+
expect(loaded!.mission).toBe('Test');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('returns null for unknown ID', async () => {
|
|
194
|
+
const loaded = await store.load('nonexistent');
|
|
195
|
+
expect(loaded).toBeNull();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('lists by mission', async () => {
|
|
199
|
+
const state1 = createExecutionState({ mission: 'A', stages: ['X'] });
|
|
200
|
+
const state2 = createExecutionState({ mission: 'B', stages: ['X'] });
|
|
201
|
+
const state3 = createExecutionState({ mission: 'A', stages: ['X'] });
|
|
202
|
+
|
|
203
|
+
await store.save(state1);
|
|
204
|
+
await store.save(state2);
|
|
205
|
+
await store.save(state3);
|
|
206
|
+
|
|
207
|
+
const aExecutions = await store.listByMission('A');
|
|
208
|
+
expect(aExecutions).toHaveLength(2);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('finds resumable executions', async () => {
|
|
212
|
+
const completed = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
213
|
+
completed.status = 'completed';
|
|
214
|
+
|
|
215
|
+
const failed = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
216
|
+
failed.status = 'failed';
|
|
217
|
+
|
|
218
|
+
const paused = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
219
|
+
paused.status = 'paused';
|
|
220
|
+
|
|
221
|
+
await store.save(completed);
|
|
222
|
+
await store.save(failed);
|
|
223
|
+
await store.save(paused);
|
|
224
|
+
|
|
225
|
+
const resumable = await store.findResumable('Test');
|
|
226
|
+
expect(resumable).toHaveLength(2);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('deletes execution state', async () => {
|
|
230
|
+
const state = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
231
|
+
await store.save(state);
|
|
232
|
+
await store.delete(state.id);
|
|
233
|
+
|
|
234
|
+
const loaded = await store.load(state.id);
|
|
235
|
+
expect(loaded).toBeNull();
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('FileExecutionStore', () => {
|
|
240
|
+
let store: FileExecutionStore;
|
|
241
|
+
|
|
242
|
+
beforeEach(() => {
|
|
243
|
+
if (existsSync(TEST_DIR)) {
|
|
244
|
+
rmSync(TEST_DIR, { recursive: true, force: true });
|
|
245
|
+
}
|
|
246
|
+
store = new FileExecutionStore(TEST_DIR);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
afterEach(() => {
|
|
250
|
+
if (existsSync(TEST_DIR)) {
|
|
251
|
+
rmSync(TEST_DIR, { recursive: true, force: true });
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('creates directory if not exists', async () => {
|
|
256
|
+
// Trigger initialization by calling any async method
|
|
257
|
+
await store.listRecent();
|
|
258
|
+
expect(existsSync(TEST_DIR)).toBe(true);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('persists state to disk', async () => {
|
|
262
|
+
const state = createExecutionState({
|
|
263
|
+
mission: 'Persistent',
|
|
264
|
+
stages: ['A', 'B'],
|
|
265
|
+
});
|
|
266
|
+
state.stages[0].status = 'completed';
|
|
267
|
+
|
|
268
|
+
await store.save(state);
|
|
269
|
+
|
|
270
|
+
// Create new store instance
|
|
271
|
+
const newStore = new FileExecutionStore(TEST_DIR);
|
|
272
|
+
const loaded = await newStore.load(state.id);
|
|
273
|
+
|
|
274
|
+
expect(loaded).not.toBeNull();
|
|
275
|
+
expect(loaded!.stages[0].status).toBe('completed');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('preserves Date objects', async () => {
|
|
279
|
+
const state = createExecutionState({
|
|
280
|
+
mission: 'Test',
|
|
281
|
+
stages: ['A'],
|
|
282
|
+
});
|
|
283
|
+
state.stages[0].startedAt = new Date();
|
|
284
|
+
state.stages[0].completedAt = new Date();
|
|
285
|
+
|
|
286
|
+
await store.save(state);
|
|
287
|
+
const loaded = await store.load(state.id);
|
|
288
|
+
|
|
289
|
+
expect(loaded!.startedAt).toBeInstanceOf(Date);
|
|
290
|
+
expect(loaded!.stages[0].startedAt).toBeInstanceOf(Date);
|
|
291
|
+
expect(loaded!.stages[0].completedAt).toBeInstanceOf(Date);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('finds latest execution for mission', async () => {
|
|
295
|
+
const older = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
296
|
+
older.startedAt = new Date(Date.now() - 10000);
|
|
297
|
+
|
|
298
|
+
const newer = createExecutionState({ mission: 'Test', stages: ['A'] });
|
|
299
|
+
newer.startedAt = new Date();
|
|
300
|
+
|
|
301
|
+
await store.save(older);
|
|
302
|
+
await store.save(newer);
|
|
303
|
+
|
|
304
|
+
const latest = await store.findLatest('Test');
|
|
305
|
+
expect(latest!.id).toBe(newer.id);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ExecutionState,
|
|
3
|
+
ExecutionStatus,
|
|
4
|
+
StageState,
|
|
5
|
+
StageStatus,
|
|
6
|
+
Checkpoint,
|
|
7
|
+
ExecutionStateError,
|
|
8
|
+
CreateExecutionOptions,
|
|
9
|
+
} from './state.js';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
generateExecutionId,
|
|
13
|
+
createExecutionState,
|
|
14
|
+
findResumePoint,
|
|
15
|
+
canResume,
|
|
16
|
+
getProgress,
|
|
17
|
+
getExecutionSummary,
|
|
18
|
+
} from './state.js';
|
|
19
|
+
|
|
20
|
+
export type { ExecutionStore } from './store.js';
|
|
21
|
+
export { FileExecutionStore, MemoryExecutionStore } from './store.js';
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Execution State - Durable state for resumable missions
|
|
3
|
+
*
|
|
4
|
+
* Tracks progress through pipeline stages, enabling:
|
|
5
|
+
* - Resume from last successful step after failures
|
|
6
|
+
* - Idempotent re-execution
|
|
7
|
+
* - Progress visibility
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type ExecutionStatus = 'pending' | 'running' | 'completed' | 'failed' | 'paused';
|
|
11
|
+
|
|
12
|
+
export type StageStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* State for a single pipeline stage (action execution)
|
|
16
|
+
*/
|
|
17
|
+
export interface StageState {
|
|
18
|
+
/** Action name */
|
|
19
|
+
action: string;
|
|
20
|
+
/** Current status */
|
|
21
|
+
status: StageStatus;
|
|
22
|
+
/** When this stage started */
|
|
23
|
+
startedAt?: Date;
|
|
24
|
+
/** When this stage completed/failed */
|
|
25
|
+
completedAt?: Date;
|
|
26
|
+
/** Error message if failed */
|
|
27
|
+
error?: string;
|
|
28
|
+
/** Number of items processed (for actions with loops) */
|
|
29
|
+
itemsProcessed?: number;
|
|
30
|
+
/** Total items to process */
|
|
31
|
+
itemsTotal?: number;
|
|
32
|
+
/** Retry attempt number (0 = first attempt) */
|
|
33
|
+
attempt: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checkpoint within an action (for resuming mid-action)
|
|
38
|
+
*/
|
|
39
|
+
export interface Checkpoint {
|
|
40
|
+
/** Stage index in pipeline */
|
|
41
|
+
stageIndex: number;
|
|
42
|
+
/** Step index within action */
|
|
43
|
+
stepIndex: number;
|
|
44
|
+
/** For loops: current item index */
|
|
45
|
+
itemIndex?: number;
|
|
46
|
+
/** Saved context variables */
|
|
47
|
+
variables?: Record<string, unknown>;
|
|
48
|
+
/** Timestamp */
|
|
49
|
+
createdAt: Date;
|
|
50
|
+
/** Webhook wait state (for resuming webhook waits) */
|
|
51
|
+
webhookWait?: WebhookWaitState;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* State for waiting on webhook callbacks
|
|
56
|
+
*/
|
|
57
|
+
export interface WebhookWaitState {
|
|
58
|
+
/** Webhook registration ID */
|
|
59
|
+
registrationId: string;
|
|
60
|
+
/** Path for the webhook endpoint */
|
|
61
|
+
path: string;
|
|
62
|
+
/** Full webhook URL */
|
|
63
|
+
webhookUrl: string;
|
|
64
|
+
/** Number of expected events */
|
|
65
|
+
expectedEvents: number;
|
|
66
|
+
/** Number of events received so far */
|
|
67
|
+
receivedEvents: number;
|
|
68
|
+
/** When the wait started */
|
|
69
|
+
waitStartedAt: Date;
|
|
70
|
+
/** When the wait expires */
|
|
71
|
+
expiresAt: Date;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Complete execution state for a mission run
|
|
76
|
+
*/
|
|
77
|
+
export interface ExecutionState {
|
|
78
|
+
/** Unique execution ID */
|
|
79
|
+
id: string;
|
|
80
|
+
/** Mission name */
|
|
81
|
+
mission: string;
|
|
82
|
+
/** Overall status */
|
|
83
|
+
status: ExecutionStatus;
|
|
84
|
+
/** When execution started */
|
|
85
|
+
startedAt: Date;
|
|
86
|
+
/** When execution completed/failed */
|
|
87
|
+
completedAt?: Date;
|
|
88
|
+
/** Total duration in ms */
|
|
89
|
+
duration?: number;
|
|
90
|
+
/** State of each pipeline stage */
|
|
91
|
+
stages: StageState[];
|
|
92
|
+
/** Latest checkpoint for resume */
|
|
93
|
+
checkpoint?: Checkpoint;
|
|
94
|
+
/** Execution errors */
|
|
95
|
+
errors: ExecutionStateError[];
|
|
96
|
+
/** Metadata (user-provided context) */
|
|
97
|
+
metadata?: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ExecutionStateError {
|
|
101
|
+
stageIndex: number;
|
|
102
|
+
action: string;
|
|
103
|
+
step: string;
|
|
104
|
+
message: string;
|
|
105
|
+
timestamp: Date;
|
|
106
|
+
attempt: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Options for creating a new execution
|
|
111
|
+
*/
|
|
112
|
+
export interface CreateExecutionOptions {
|
|
113
|
+
/** Mission name */
|
|
114
|
+
mission: string;
|
|
115
|
+
/** Pipeline stage names (actions) */
|
|
116
|
+
stages: string[];
|
|
117
|
+
/** Optional metadata */
|
|
118
|
+
metadata?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Generate a unique execution ID
|
|
123
|
+
*/
|
|
124
|
+
export function generateExecutionId(): string {
|
|
125
|
+
const timestamp = Date.now().toString(36);
|
|
126
|
+
const random = Math.random().toString(36).substring(2, 8);
|
|
127
|
+
return `exec_${timestamp}_${random}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Create initial execution state
|
|
132
|
+
*/
|
|
133
|
+
export function createExecutionState(options: CreateExecutionOptions): ExecutionState {
|
|
134
|
+
return {
|
|
135
|
+
id: generateExecutionId(),
|
|
136
|
+
mission: options.mission,
|
|
137
|
+
status: 'pending',
|
|
138
|
+
startedAt: new Date(),
|
|
139
|
+
stages: options.stages.map((action) => ({
|
|
140
|
+
action,
|
|
141
|
+
status: 'pending',
|
|
142
|
+
attempt: 0,
|
|
143
|
+
})),
|
|
144
|
+
errors: [],
|
|
145
|
+
metadata: options.metadata,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Find the stage to resume from
|
|
151
|
+
* Returns the index of the first non-completed stage, or -1 if all complete
|
|
152
|
+
*/
|
|
153
|
+
export function findResumePoint(state: ExecutionState): number {
|
|
154
|
+
// If there's a checkpoint, use it
|
|
155
|
+
if (state.checkpoint) {
|
|
156
|
+
return state.checkpoint.stageIndex;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Otherwise, find first non-completed stage
|
|
160
|
+
for (let i = 0; i < state.stages.length; i++) {
|
|
161
|
+
const stage = state.stages[i];
|
|
162
|
+
if (stage.status !== 'completed' && stage.status !== 'skipped') {
|
|
163
|
+
return i;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return -1; // All stages complete
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Check if execution can be resumed
|
|
172
|
+
*/
|
|
173
|
+
export function canResume(state: ExecutionState): boolean {
|
|
174
|
+
return state.status === 'failed' || state.status === 'paused';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Calculate execution progress as percentage
|
|
179
|
+
*/
|
|
180
|
+
export function getProgress(state: ExecutionState): number {
|
|
181
|
+
if (state.stages.length === 0) return 100;
|
|
182
|
+
|
|
183
|
+
const completed = state.stages.filter(
|
|
184
|
+
(s) => s.status === 'completed' || s.status === 'skipped'
|
|
185
|
+
).length;
|
|
186
|
+
|
|
187
|
+
return Math.round((completed / state.stages.length) * 100);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get a summary of the execution state
|
|
192
|
+
*/
|
|
193
|
+
export function getExecutionSummary(state: ExecutionState): string {
|
|
194
|
+
const progress = getProgress(state);
|
|
195
|
+
const completed = state.stages.filter((s) => s.status === 'completed').length;
|
|
196
|
+
const failed = state.stages.filter((s) => s.status === 'failed').length;
|
|
197
|
+
const pending = state.stages.filter((s) => s.status === 'pending').length;
|
|
198
|
+
|
|
199
|
+
let summary = `${state.mission} [${state.id}]: ${state.status} (${progress}%)`;
|
|
200
|
+
summary += ` - ${completed} completed, ${failed} failed, ${pending} pending`;
|
|
201
|
+
|
|
202
|
+
if (state.duration) {
|
|
203
|
+
summary += ` - ${Math.round(state.duration / 1000)}s`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return summary;
|
|
207
|
+
}
|