@leed-ai/git-gateway-worker 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +49 -2
- package/dist/audit/audit-recorder.d.ts +2 -0
- package/dist/audit/audit-recorder.js +53 -0
- package/dist/audit/audit-recorder.js.map +1 -0
- package/dist/audit/push-report.d.ts +13 -0
- package/dist/audit/push-report.js +66 -0
- package/dist/audit/push-report.js.map +1 -0
- package/dist/audit-recorder-K1Ns3tfC.d.ts +31 -0
- package/dist/auth/authenticate.d.ts +14 -0
- package/dist/auth/authenticate.js +59 -0
- package/dist/auth/authenticate.js.map +1 -0
- package/dist/auth/authorize.d.ts +12 -0
- package/dist/auth/authorize.js +57 -0
- package/dist/auth/authorize.js.map +1 -0
- package/dist/auth/read-token-auth.d.ts +60 -0
- package/dist/auth/read-token-auth.js +83 -0
- package/dist/auth/read-token-auth.js.map +1 -0
- package/dist/cache/decision-cache.d.ts +2 -0
- package/dist/cache/decision-cache.js +45 -0
- package/dist/cache/decision-cache.js.map +1 -0
- package/dist/commit-workflow-schemas-BWlg7bZR.d.ts +95 -0
- package/dist/decision-cache-87U32iSj.d.ts +24 -0
- package/dist/gateway-app.d.ts +69 -0
- package/dist/gateway-app.js +80 -0
- package/dist/gateway-app.js.map +1 -0
- package/dist/gateway-context-Boh9I0mk.d.ts +54 -0
- package/dist/gateway-env-DnsBU4Pq.d.ts +40 -0
- package/dist/gateway-worker.d.ts +13 -0
- package/dist/gateway-worker.js +22 -0
- package/dist/gateway-worker.js.map +1 -0
- package/dist/git-route-Spxi09Rm.d.ts +22 -0
- package/dist/health.d.ts +6 -0
- package/dist/health.js +16 -0
- package/dist/health.js.map +1 -0
- package/dist/logger-BZ1zyKLb.d.ts +27 -0
- package/dist/logging/logger.d.ts +2 -0
- package/dist/logging/logger.js +15 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/on-push-DTREOB__.d.ts +62 -0
- package/dist/policy/branch-policy.d.ts +24 -0
- package/dist/policy/branch-policy.js +125 -0
- package/dist/policy/branch-policy.js.map +1 -0
- package/dist/policy/enforce-branch-policy.d.ts +30 -0
- package/dist/policy/enforce-branch-policy.js +214 -0
- package/dist/policy/enforce-branch-policy.js.map +1 -0
- package/dist/policy/fast-forward.d.ts +32 -0
- package/dist/policy/fast-forward.js +95 -0
- package/dist/policy/fast-forward.js.map +1 -0
- package/dist/policy/ref-pattern.d.ts +7 -0
- package/dist/policy/ref-pattern.js +25 -0
- package/dist/policy/ref-pattern.js.map +1 -0
- package/dist/policy/rule-branch-policy.d.ts +17 -0
- package/dist/policy/rule-branch-policy.js +69 -0
- package/dist/policy/rule-branch-policy.js.map +1 -0
- package/dist/proxy/defer.d.ts +8 -0
- package/dist/proxy/defer.js +15 -0
- package/dist/proxy/defer.js.map +1 -0
- package/dist/proxy/forward-read.d.ts +11 -0
- package/dist/proxy/forward-read.js +53 -0
- package/dist/proxy/forward-read.js.map +1 -0
- package/dist/proxy/gateway-deps.d.ts +39 -0
- package/dist/proxy/gateway-deps.js +0 -0
- package/dist/proxy/gateway-env.d.ts +2 -0
- package/dist/proxy/gateway-env.js +0 -0
- package/dist/proxy/git-responses.d.ts +25 -0
- package/dist/proxy/git-responses.js +83 -0
- package/dist/proxy/git-responses.js.map +1 -0
- package/dist/proxy/git-route.d.ts +2 -0
- package/dist/proxy/git-route.js +34 -0
- package/dist/proxy/git-route.js.map +1 -0
- package/dist/proxy/push-events.d.ts +28 -0
- package/dist/proxy/push-events.js +65 -0
- package/dist/proxy/push-events.js.map +1 -0
- package/dist/proxy/push-guard.d.ts +43 -0
- package/dist/proxy/push-guard.js +81 -0
- package/dist/proxy/push-guard.js.map +1 -0
- package/dist/proxy/push-options.d.ts +16 -0
- package/dist/proxy/push-options.js +33 -0
- package/dist/proxy/push-options.js.map +1 -0
- package/dist/proxy/receive-pack.d.ts +17 -0
- package/dist/proxy/receive-pack.js +244 -0
- package/dist/proxy/receive-pack.js.map +1 -0
- package/dist/proxy/request-log.d.ts +13 -0
- package/dist/proxy/request-log.js +41 -0
- package/dist/proxy/request-log.js.map +1 -0
- package/dist/proxy/resolve-route.d.ts +9 -0
- package/dist/proxy/resolve-route.js +22 -0
- package/dist/proxy/resolve-route.js.map +1 -0
- package/dist/proxy/upstream-call.d.ts +23 -0
- package/dist/proxy/upstream-call.js +63 -0
- package/dist/proxy/upstream-call.js.map +1 -0
- package/dist/proxy/upstream.d.ts +2 -0
- package/dist/proxy/upstream.js +68 -0
- package/dist/proxy/upstream.js.map +1 -0
- package/dist/push-event-f-5TsFUY.d.ts +15 -0
- package/dist/push-policy/changed-paths.d.ts +26 -0
- package/dist/push-policy/changed-paths.js +117 -0
- package/dist/push-policy/changed-paths.js.map +1 -0
- package/dist/push-policy/pushed-objects.d.ts +30 -0
- package/dist/push-policy/pushed-objects.js +76 -0
- package/dist/push-policy/pushed-objects.js.map +1 -0
- package/dist/push-policy/run-push-policy.d.ts +35 -0
- package/dist/push-policy/run-push-policy.js +77 -0
- package/dist/push-policy/run-push-policy.js.map +1 -0
- package/dist/push-policy/tree-diff.d.ts +21 -0
- package/dist/push-policy/tree-diff.js +78 -0
- package/dist/push-policy/tree-diff.js.map +1 -0
- package/dist/push-policy/uncheckable.d.ts +14 -0
- package/dist/push-policy/uncheckable.js +25 -0
- package/dist/push-policy/uncheckable.js.map +1 -0
- package/dist/rpc/branch-methods.d.ts +16 -0
- package/dist/rpc/branch-methods.js +43 -0
- package/dist/rpc/branch-methods.js.map +1 -0
- package/dist/rpc/commit-attempt.d.ts +27 -0
- package/dist/rpc/commit-attempt.js +85 -0
- package/dist/rpc/commit-attempt.js.map +1 -0
- package/dist/rpc/commit-graph.d.ts +20 -0
- package/dist/rpc/commit-graph.js +38 -0
- package/dist/rpc/commit-graph.js.map +1 -0
- package/dist/rpc/commit-method.d.ts +13 -0
- package/dist/rpc/commit-method.js +30 -0
- package/dist/rpc/commit-method.js.map +1 -0
- package/dist/rpc/commit-source.d.ts +12 -0
- package/dist/rpc/commit-source.js +9 -0
- package/dist/rpc/commit-source.js.map +1 -0
- package/dist/rpc/gateway-context.d.ts +2 -0
- package/dist/rpc/gateway-context.js +30 -0
- package/dist/rpc/gateway-context.js.map +1 -0
- package/dist/rpc/gateway-rpc.d.ts +10 -0
- package/dist/rpc/gateway-rpc.js +48 -0
- package/dist/rpc/gateway-rpc.js.map +1 -0
- package/dist/rpc/gateway-service.d.ts +24 -0
- package/dist/rpc/gateway-service.js +63 -0
- package/dist/rpc/gateway-service.js.map +1 -0
- package/dist/rpc/history-page.d.ts +35 -0
- package/dist/rpc/history-page.js +122 -0
- package/dist/rpc/history-page.js.map +1 -0
- package/dist/rpc/lifecycle-methods.d.ts +12 -0
- package/dist/rpc/lifecycle-methods.js +39 -0
- package/dist/rpc/lifecycle-methods.js.map +1 -0
- package/dist/rpc/paint-down.d.ts +17 -0
- package/dist/rpc/paint-down.js +96 -0
- package/dist/rpc/paint-down.js.map +1 -0
- package/dist/rpc/promote-attempt.d.ts +21 -0
- package/dist/rpc/promote-attempt.js +55 -0
- package/dist/rpc/promote-attempt.js.map +1 -0
- package/dist/rpc/push-ref.d.ts +21 -0
- package/dist/rpc/push-ref.js +47 -0
- package/dist/rpc/push-ref.js.map +1 -0
- package/dist/rpc/read-methods.d.ts +17 -0
- package/dist/rpc/read-methods.js +94 -0
- package/dist/rpc/read-methods.js.map +1 -0
- package/dist/rpc/read-token.d.ts +39 -0
- package/dist/rpc/read-token.js +107 -0
- package/dist/rpc/read-token.js.map +1 -0
- package/dist/rpc/repo-access.d.ts +36 -0
- package/dist/rpc/repo-access.js +75 -0
- package/dist/rpc/repo-access.js.map +1 -0
- package/dist/rpc/sync-attempt.d.ts +23 -0
- package/dist/rpc/sync-attempt.js +105 -0
- package/dist/rpc/sync-attempt.js.map +1 -0
- package/dist/sync-workflow-schemas-DCNmNE5-.d.ts +57 -0
- package/dist/upstream-DvqMCj5N.d.ts +15 -0
- package/dist/workflow-starter-BqhwX6oA.d.ts +45 -0
- package/dist/workflows/commit-workflow-schemas.d.ts +2 -0
- package/dist/workflows/commit-workflow-schemas.js +84 -0
- package/dist/workflows/commit-workflow-schemas.js.map +1 -0
- package/dist/workflows/commit-workflow.d.ts +16 -0
- package/dist/workflows/commit-workflow.js +22 -0
- package/dist/workflows/commit-workflow.js.map +1 -0
- package/dist/workflows/on-push.d.ts +2 -0
- package/dist/workflows/on-push.js +90 -0
- package/dist/workflows/on-push.js.map +1 -0
- package/dist/workflows/push-event.d.ts +2 -0
- package/dist/workflows/push-event.js +28 -0
- package/dist/workflows/push-event.js.map +1 -0
- package/dist/workflows/run-commit-workflow.d.ts +15 -0
- package/dist/workflows/run-commit-workflow.js +49 -0
- package/dist/workflows/run-commit-workflow.js.map +1 -0
- package/dist/workflows/run-sync-workflow.d.ts +14 -0
- package/dist/workflows/run-sync-workflow.js +67 -0
- package/dist/workflows/run-sync-workflow.js.map +1 -0
- package/dist/workflows/step-errors.d.ts +15 -0
- package/dist/workflows/step-errors.js +24 -0
- package/dist/workflows/step-errors.js.map +1 -0
- package/dist/workflows/step-runner.d.ts +31 -0
- package/dist/workflows/step-runner.js +6 -0
- package/dist/workflows/step-runner.js.map +1 -0
- package/dist/workflows/sync-branch-workflow.d.ts +15 -0
- package/dist/workflows/sync-branch-workflow.js +21 -0
- package/dist/workflows/sync-branch-workflow.js.map +1 -0
- package/dist/workflows/sync-workflow-schemas.d.ts +2 -0
- package/dist/workflows/sync-workflow-schemas.js +104 -0
- package/dist/workflows/sync-workflow-schemas.js.map +1 -0
- package/dist/workflows/workflow-instance-id.d.ts +12 -0
- package/dist/workflows/workflow-instance-id.js +33 -0
- package/dist/workflows/workflow-instance-id.js.map +1 -0
- package/dist/workflows/workflow-runtime.d.ts +7 -0
- package/dist/workflows/workflow-runtime.js +11 -0
- package/dist/workflows/workflow-runtime.js.map +1 -0
- package/dist/workflows/workflow-starter.d.ts +2 -0
- package/dist/workflows/workflow-starter.js +40 -0
- package/dist/workflows/workflow-starter.js.map +1 -0
- package/package.json +381 -4
- package/src/audit/audit-recorder.ts +74 -0
- package/src/audit/push-report.ts +60 -0
- package/src/auth/authenticate.ts +68 -0
- package/src/auth/authorize.ts +57 -0
- package/src/auth/read-token-auth.ts +119 -0
- package/src/cache/decision-cache.ts +57 -0
- package/src/gateway-app.ts +147 -0
- package/src/gateway-worker.ts +32 -0
- package/src/health.ts +12 -0
- package/src/logging/logger.ts +40 -0
- package/src/policy/branch-policy.ts +119 -0
- package/src/policy/enforce-branch-policy.ts +213 -0
- package/src/policy/fast-forward.ts +136 -0
- package/src/policy/ref-pattern.ts +24 -0
- package/src/policy/rule-branch-policy.ts +76 -0
- package/src/proxy/defer.ts +18 -0
- package/src/proxy/forward-read.ts +52 -0
- package/src/proxy/gateway-deps.ts +43 -0
- package/src/proxy/gateway-env.ts +43 -0
- package/src/proxy/git-responses.ts +89 -0
- package/src/proxy/git-route.ts +49 -0
- package/src/proxy/push-events.ts +91 -0
- package/src/proxy/push-guard.ts +109 -0
- package/src/proxy/push-options.ts +38 -0
- package/src/proxy/receive-pack.ts +257 -0
- package/src/proxy/request-log.ts +44 -0
- package/src/proxy/resolve-route.ts +24 -0
- package/src/proxy/upstream-call.ts +71 -0
- package/src/proxy/upstream.ts +76 -0
- package/src/push-policy/changed-paths.ts +143 -0
- package/src/push-policy/pushed-objects.ts +88 -0
- package/src/push-policy/run-push-policy.ts +99 -0
- package/src/push-policy/tree-diff.ts +107 -0
- package/src/push-policy/uncheckable.ts +27 -0
- package/src/rpc/branch-methods.ts +57 -0
- package/src/rpc/commit-attempt.ts +87 -0
- package/src/rpc/commit-graph.ts +44 -0
- package/src/rpc/commit-method.ts +36 -0
- package/src/rpc/commit-source.ts +15 -0
- package/src/rpc/gateway-context.ts +77 -0
- package/src/rpc/gateway-rpc.ts +65 -0
- package/src/rpc/gateway-service.ts +85 -0
- package/src/rpc/history-page.ts +163 -0
- package/src/rpc/lifecycle-methods.ts +50 -0
- package/src/rpc/paint-down.ts +120 -0
- package/src/rpc/promote-attempt.ts +52 -0
- package/src/rpc/push-ref.ts +54 -0
- package/src/rpc/read-methods.ts +111 -0
- package/src/rpc/read-token.ts +135 -0
- package/src/rpc/repo-access.ts +112 -0
- package/src/rpc/sync-attempt.ts +84 -0
- package/src/workflows/commit-workflow-schemas.ts +95 -0
- package/src/workflows/commit-workflow.ts +39 -0
- package/src/workflows/on-push.ts +128 -0
- package/src/workflows/push-event.ts +41 -0
- package/src/workflows/run-commit-workflow.ts +45 -0
- package/src/workflows/run-sync-workflow.ts +52 -0
- package/src/workflows/step-errors.ts +26 -0
- package/src/workflows/step-runner.ts +36 -0
- package/src/workflows/sync-branch-workflow.ts +40 -0
- package/src/workflows/sync-workflow-schemas.ts +122 -0
- package/src/workflows/workflow-instance-id.ts +26 -0
- package/src/workflows/workflow-runtime.ts +13 -0
- package/src/workflows/workflow-starter.ts +69 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { RpcOutput, RpcParsedInput } from '@leed-ai/git-gateway-contract/git-gateway';
|
|
3
|
+
import { gatewayError } from '@leed-ai/git-gateway-contract/gateway-error';
|
|
4
|
+
import { storageUnavailable } from '@leed-ai/git-gateway-contract/storage/storage-errors';
|
|
5
|
+
import { mergeTrees } from '@leed-ai/git-gateway-core/merge/merge-trees';
|
|
6
|
+
import { createCommit } from '@leed-ai/git-gateway-core/objects/commit-object';
|
|
7
|
+
import type { GitObject } from '@leed-ai/git-gateway-core/schemas/git-object';
|
|
8
|
+
|
|
9
|
+
import { CommitGraph } from './commit-graph.ts';
|
|
10
|
+
import type { GatewayContext } from './gateway-context.ts';
|
|
11
|
+
import { pushRefUpdate } from './push-ref.ts';
|
|
12
|
+
import { branchHead, branchRef, requireBranchHead, requireCommit, treeReader } from './repo-access.ts';
|
|
13
|
+
|
|
14
|
+
/** syncBranch() input after parsing. */
|
|
15
|
+
export type SyncParams = RpcParsedInput<'syncBranch'>;
|
|
16
|
+
|
|
17
|
+
/** The outcome of one sync attempt. */
|
|
18
|
+
export type SyncAttempt =
|
|
19
|
+
| { readonly kind: 'done'; readonly output: RpcOutput<'syncBranch'> }
|
|
20
|
+
/** The push was refused because `into` moved; `intoHead` is the head that was built on. */
|
|
21
|
+
| { readonly kind: 'stale'; readonly intoHead: string };
|
|
22
|
+
|
|
23
|
+
/** A commit message as git stores it: ending in exactly one newline. */
|
|
24
|
+
const asStoredMessage = (message: string): string => {
|
|
25
|
+
// A scan from the end, not /\n+$/: that regex retries from every newline run (polynomial time).
|
|
26
|
+
let end = message.length;
|
|
27
|
+
while (end > 0 && message.charCodeAt(end - 1) === 10) end -= 1;
|
|
28
|
+
return `${message.slice(0, end)}\n`;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One attempt at syncBranch(): `into` already contains `from` → up_to_date; `into` has nothing of
|
|
33
|
+
* its own → fast-forward; otherwise a file-level three-way merge against the merge base (the
|
|
34
|
+
* reference's own merge-base rule, found by a paint-down walk that reads only the history since it), written as a merge commit whose
|
|
35
|
+
* parents are `into` then `from`, or a conflict listing every path, with nothing written. Every
|
|
36
|
+
* push names the head of `into` it was built on, so nothing is overwritten or forced.
|
|
37
|
+
*/
|
|
38
|
+
export async function attemptSync(context: GatewayContext, input: SyncParams): Promise<SyncAttempt> {
|
|
39
|
+
const { repo, from, into } = input;
|
|
40
|
+
const fromHead = await requireBranchHead(context, repo, from);
|
|
41
|
+
const intoHead = await requireBranchHead(context, repo, into);
|
|
42
|
+
// One paint-down walk answers all three questions: `from` is contained in `into` exactly when
|
|
43
|
+
// it is their merge base, `into` in `from` exactly when `into` is.
|
|
44
|
+
const base = await new CommitGraph(context, repo).mergeBase(intoHead, fromHead);
|
|
45
|
+
if (base === fromHead) {
|
|
46
|
+
return { kind: 'done', output: { status: 'up_to_date', head: intoHead } };
|
|
47
|
+
}
|
|
48
|
+
const refUpdate = (newSha: string) => ({ ref: branchRef(into), oldSha: intoHead, newSha });
|
|
49
|
+
/** Pushes `into` to `newSha`; false when `into` moved first (a refusal for any other reason throws). */
|
|
50
|
+
const push = async (newSha: string, objects: readonly GitObject[]): Promise<boolean> => {
|
|
51
|
+
const pushed = await pushRefUpdate(context, repo, refUpdate(newSha), objects);
|
|
52
|
+
if (pushed.status === 'ok') return true;
|
|
53
|
+
if ((await branchHead(context, repo, into)) === intoHead) {
|
|
54
|
+
throw storageUnavailable(`push to ${branchRef(into)}`, pushed.reason);
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
};
|
|
58
|
+
if (base === intoHead) {
|
|
59
|
+
if (!(await push(fromHead, []))) return { kind: 'stale', intoHead };
|
|
60
|
+
return { kind: 'done', output: { status: 'fast_forward', refUpdate: refUpdate(fromHead) } };
|
|
61
|
+
}
|
|
62
|
+
if (base === null) {
|
|
63
|
+
throw gatewayError('unrelated_histories', `${from} and ${into} share no history`, { from, into });
|
|
64
|
+
}
|
|
65
|
+
const treeOf = async (sha: string): Promise<string> => (await requireCommit(context, repo, sha)).tree;
|
|
66
|
+
const merged = await mergeTrees(
|
|
67
|
+
{ base: await treeOf(base), ours: await treeOf(intoHead), theirs: await treeOf(fromHead) },
|
|
68
|
+
treeReader(context, repo),
|
|
69
|
+
);
|
|
70
|
+
if (merged.status === 'conflict') {
|
|
71
|
+
return { kind: 'done', output: { status: 'conflict', paths: merged.paths, base, fromHead, intoHead } };
|
|
72
|
+
}
|
|
73
|
+
const identity = input.author ?? context.config.serviceIdentity;
|
|
74
|
+
const signature = { ...identity, date: context.now().toISOString() };
|
|
75
|
+
const { object, commit } = await createCommit({
|
|
76
|
+
tree: merged.tree,
|
|
77
|
+
parents: [intoHead, fromHead],
|
|
78
|
+
author: signature,
|
|
79
|
+
committer: signature,
|
|
80
|
+
message: asStoredMessage(input.message ?? `Merge branch '${from}' into ${into}`),
|
|
81
|
+
});
|
|
82
|
+
if (!(await push(commit.sha, [...merged.objects, object]))) return { kind: 'stale', intoHead };
|
|
83
|
+
return { kind: 'done', output: { status: 'merged', commit, refUpdate: refUpdate(commit.sha) } };
|
|
84
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { CommitInput } from '@leed-ai/git-gateway-contract/rpc/commit';
|
|
3
|
+
import {
|
|
4
|
+
EXAMPLE_IDENTITY,
|
|
5
|
+
EXAMPLE_SHA_A,
|
|
6
|
+
EXAMPLE_SHA_B,
|
|
7
|
+
} from '@leed-ai/git-gateway-contract/schemas/examples';
|
|
8
|
+
import { Sha } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
|
|
11
|
+
/** What a CommitWorkflow instance is started with: the commit() input, unchanged. */
|
|
12
|
+
export const CommitWorkflowParams = CommitInput.meta({
|
|
13
|
+
id: 'CommitWorkflowParams',
|
|
14
|
+
title: 'CommitWorkflow params',
|
|
15
|
+
description:
|
|
16
|
+
'The params a CommitWorkflow instance is started with: exactly the commit() input whose inline retries ran out, idempotency key included, so the workflow writes the same commit at most once. Params may carry file content; step results never do.',
|
|
17
|
+
examples: [
|
|
18
|
+
{
|
|
19
|
+
repo: 'acme-raw-content',
|
|
20
|
+
branch: 'staging',
|
|
21
|
+
message: 'Update about page',
|
|
22
|
+
author: EXAMPLE_IDENTITY,
|
|
23
|
+
actions: [{ action: 'update', path: 'content/about.md', content: '# About us\n', encoding: 'text' }],
|
|
24
|
+
idempotencyKey: 'cms:publish:2f9c1e',
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** CommitWorkflow params after parsing (defaults applied). */
|
|
30
|
+
export type CommitWorkflowParams = z.output<typeof CommitWorkflowParams>;
|
|
31
|
+
|
|
32
|
+
/** The workflow wrote the commit. */
|
|
33
|
+
export const CommitStepCommitted = z
|
|
34
|
+
.object({
|
|
35
|
+
status: z.literal('committed').describe('The workflow wrote a new commit and moved the branch to it.'),
|
|
36
|
+
sha: Sha.describe('Id of the commit written.'),
|
|
37
|
+
})
|
|
38
|
+
.strict()
|
|
39
|
+
.meta({
|
|
40
|
+
id: 'CommitStepCommitted',
|
|
41
|
+
title: 'commit step: committed',
|
|
42
|
+
description: "The CommitWorkflow's commit step wrote the commit. Carries the commit id only.",
|
|
43
|
+
examples: [{ status: 'committed', sha: EXAMPLE_SHA_B }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** A commit with the same idempotency key was already on the branch. */
|
|
47
|
+
export const CommitStepReplayed = z
|
|
48
|
+
.object({
|
|
49
|
+
status: z
|
|
50
|
+
.literal('replayed')
|
|
51
|
+
.describe(
|
|
52
|
+
'A commit carrying the same idempotency key was already in the branch history (an earlier attempt, or a step that ran before a crash); nothing was written.',
|
|
53
|
+
),
|
|
54
|
+
sha: Sha.describe('Id of the earlier commit carrying the key.'),
|
|
55
|
+
})
|
|
56
|
+
.strict()
|
|
57
|
+
.meta({
|
|
58
|
+
id: 'CommitStepReplayed',
|
|
59
|
+
title: 'commit step: replayed',
|
|
60
|
+
description:
|
|
61
|
+
"The CommitWorkflow's commit step found the commit already written, so a replayed step never commits twice.",
|
|
62
|
+
examples: [{ status: 'replayed', sha: EXAMPLE_SHA_B }],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/** The actions changed nothing. */
|
|
66
|
+
export const CommitStepNoop = z
|
|
67
|
+
.object({
|
|
68
|
+
status: z.literal('noop').describe('The actions left the tree unchanged; nothing was written.'),
|
|
69
|
+
head: Sha.nullable().describe('The branch head, unchanged; null when the branch does not exist.'),
|
|
70
|
+
})
|
|
71
|
+
.strict()
|
|
72
|
+
.meta({
|
|
73
|
+
id: 'CommitStepNoop',
|
|
74
|
+
title: 'commit step: no-op',
|
|
75
|
+
description: "The CommitWorkflow's commit step applied the actions to the head and nothing changed.",
|
|
76
|
+
examples: [{ status: 'noop', head: EXAMPLE_SHA_A }],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* What the CommitWorkflow's commit step stores and the instance returns. Ids and a status only:
|
|
81
|
+
* never file content, a message or a credential, because step results are persisted by the
|
|
82
|
+
* Workflows engine and shown in its dashboard.
|
|
83
|
+
*/
|
|
84
|
+
export const CommitStepResult = z
|
|
85
|
+
.discriminatedUnion('status', [CommitStepCommitted, CommitStepReplayed, CommitStepNoop])
|
|
86
|
+
.meta({
|
|
87
|
+
id: 'CommitStepResult',
|
|
88
|
+
title: 'CommitWorkflow result',
|
|
89
|
+
description:
|
|
90
|
+
"What the CommitWorkflow's commit step stores and the instance returns, discriminated by `status`: commit ids only, never file content or a credential.",
|
|
91
|
+
examples: [{ status: 'committed', sha: EXAMPLE_SHA_B }],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/** A CommitWorkflow result, inferred from {@link CommitStepResult}. */
|
|
95
|
+
export type CommitStepResult = z.infer<typeof CommitStepResult>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep } from 'cloudflare:workers';
|
|
3
|
+
|
|
4
|
+
import { gatewayContext } from '../rpc/gateway-context.ts';
|
|
5
|
+
import type { GatewayOptionsResolver } from '../rpc/gateway-service.ts';
|
|
6
|
+
import type { CommitStepResult, CommitWorkflowParams } from './commit-workflow-schemas.ts';
|
|
7
|
+
import { runCommitWorkflow } from './run-commit-workflow.ts';
|
|
8
|
+
import { nonRetryable, stepRunnerOf } from './workflow-runtime.ts';
|
|
9
|
+
|
|
10
|
+
/** The CommitWorkflow class {@link createCommitWorkflow} returns. */
|
|
11
|
+
export type CommitWorkflowClass<Env> = new (
|
|
12
|
+
ctx: ExecutionContext,
|
|
13
|
+
env: Env,
|
|
14
|
+
) => WorkflowEntrypoint<Env, CommitWorkflowParams> & {
|
|
15
|
+
run(event: Readonly<WorkflowEvent<CommitWorkflowParams>>, step: WorkflowStep): Promise<CommitStepResult>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The CommitWorkflow a deployment exports and binds (as the `commitWorkflow` of its options), to
|
|
20
|
+
* finish commits whose inline retries ran out. Built from the same options resolver as the RPC
|
|
21
|
+
* entrypoint, so both write through the same backend and configuration:
|
|
22
|
+
*
|
|
23
|
+
* export const CommitWorkflow = createCommitWorkflow(gatewayOptions);
|
|
24
|
+
*/
|
|
25
|
+
export function createCommitWorkflow<Env>(resolve: GatewayOptionsResolver<Env>): CommitWorkflowClass<Env> {
|
|
26
|
+
return class CommitWorkflow extends WorkflowEntrypoint<Env, CommitWorkflowParams> {
|
|
27
|
+
override run(
|
|
28
|
+
event: Readonly<WorkflowEvent<CommitWorkflowParams>>,
|
|
29
|
+
step: WorkflowStep,
|
|
30
|
+
): Promise<CommitStepResult> {
|
|
31
|
+
return runCommitWorkflow(
|
|
32
|
+
gatewayContext(resolve(this.env)),
|
|
33
|
+
event.payload,
|
|
34
|
+
stepRunnerOf(step),
|
|
35
|
+
nonRetryable,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { invalidInput } from '@leed-ai/git-gateway-contract/gateway-error';
|
|
3
|
+
import type { SyncRule } from '@leed-ai/git-gateway-contract/schemas/gateway-config';
|
|
4
|
+
import { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
5
|
+
|
|
6
|
+
import { PushEvent } from './push-event.ts';
|
|
7
|
+
import type { SyncBranchWorkflowParams } from './sync-workflow-schemas.ts';
|
|
8
|
+
import { syncWorkflowId } from './workflow-instance-id.ts';
|
|
9
|
+
import { startWorkflowOnce, type WorkflowStarter } from './workflow-starter.ts';
|
|
10
|
+
|
|
11
|
+
/** Something a deployment runs after every accepted push. Rejecting asks for the event again. */
|
|
12
|
+
export type OnPushHook = (event: PushEvent) => Promise<void>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The onPush hook for the configured sync rules: for every rule whose `from` branch the push
|
|
16
|
+
* moved (not deleted), start a SyncBranchWorkflow syncing `into` from it. The instance id is
|
|
17
|
+
* derived from repository, both branches and the new head, so a redelivered event starts nothing
|
|
18
|
+
* new.
|
|
19
|
+
*/
|
|
20
|
+
export function syncRuleHook(
|
|
21
|
+
syncRules: readonly SyncRule[],
|
|
22
|
+
syncWorkflow: WorkflowStarter<SyncBranchWorkflowParams>,
|
|
23
|
+
): OnPushHook {
|
|
24
|
+
return async (event) => {
|
|
25
|
+
for (const rule of syncRules) {
|
|
26
|
+
const update = event.updates.findLast(
|
|
27
|
+
(candidate) => candidate.ref === `refs/heads/${rule.from}` && candidate.newSha !== ZERO_SHA,
|
|
28
|
+
);
|
|
29
|
+
if (update === undefined) continue;
|
|
30
|
+
const params = { repo: event.repo, from: rule.from, into: rule.into, fromHead: update.newSha };
|
|
31
|
+
const id = await syncWorkflowId(event.repo, rule.from, rule.into, update.newSha);
|
|
32
|
+
await startWorkflowOnce(syncWorkflow, id, params);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Delivers one push event to every hook. The event is parsed with {@link PushEvent} first
|
|
39
|
+
* (invalid_input when it does not match). Every hook runs even when another fails; if any failed,
|
|
40
|
+
* an AggregateError of their errors is thrown afterwards. Hooks must therefore be idempotent, as
|
|
41
|
+
* {@link syncRuleHook} is.
|
|
42
|
+
*/
|
|
43
|
+
export async function dispatchPushEvent(event: unknown, hooks: readonly OnPushHook[]): Promise<void> {
|
|
44
|
+
const parsed = PushEvent.safeParse(event);
|
|
45
|
+
if (!parsed.success) throw invalidInput(parsed.error);
|
|
46
|
+
const results = await Promise.allSettled(hooks.map((hook) => hook(parsed.data)));
|
|
47
|
+
const failures = results.flatMap((result) => (result.status === 'rejected' ? [result.reason] : []));
|
|
48
|
+
if (failures.length > 0) throw new AggregateError(failures, `${failures.length} onPush hook(s) failed`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What {@link composeOnPush} builds the deployment's onPush hook from. */
|
|
52
|
+
export interface OnPushOptions {
|
|
53
|
+
/** The configured sync rules (GatewayConfig.syncRules). */
|
|
54
|
+
readonly syncRules: readonly SyncRule[];
|
|
55
|
+
/** The SyncBranchWorkflow binding the sync rules start instances of; required when there are rules. */
|
|
56
|
+
readonly syncWorkflow?: WorkflowStarter<SyncBranchWorkflowParams> | undefined;
|
|
57
|
+
/** The deployment's own hook, run beside the sync rules. */
|
|
58
|
+
readonly onPush?: OnPushHook | undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The one onPush hook a gateway runs after every accepted push: {@link syncRuleHook} for the
|
|
63
|
+
* configured sync rules and the deployment's own hook, each run even when the other fails (see
|
|
64
|
+
* {@link dispatchPushEvent}); a lone hook is returned as it is. Undefined when there is nothing
|
|
65
|
+
* to run. Throws a TypeError when sync rules are configured without a SyncBranchWorkflow binding,
|
|
66
|
+
* which would leave them dead.
|
|
67
|
+
*/
|
|
68
|
+
export function composeOnPush(options: OnPushOptions): OnPushHook | undefined {
|
|
69
|
+
const hooks: OnPushHook[] = [];
|
|
70
|
+
if (options.syncRules.length > 0) {
|
|
71
|
+
if (options.syncWorkflow === undefined) {
|
|
72
|
+
throw new TypeError(
|
|
73
|
+
'GatewayConfig.syncRules are set but no SyncBranchWorkflow binding (syncWorkflow) was given to start them',
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
hooks.push(syncRuleHook(options.syncRules, options.syncWorkflow));
|
|
77
|
+
}
|
|
78
|
+
if (options.onPush !== undefined) hooks.push(options.onPush);
|
|
79
|
+
if (hooks.length <= 1) return hooks[0];
|
|
80
|
+
return async (event) => dispatchPushEvent(event, hooks);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The part of a Queues message the consumer uses. */
|
|
84
|
+
export interface PushEventMessage {
|
|
85
|
+
readonly body: unknown;
|
|
86
|
+
ack(): void;
|
|
87
|
+
retry(): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** What {@link consumePushEvents} did with a batch. */
|
|
91
|
+
export interface PushBatchOutcome {
|
|
92
|
+
/** Events every hook handled. */
|
|
93
|
+
readonly delivered: number;
|
|
94
|
+
/** Messages that are not push events; acknowledged, since redelivery cannot fix them. */
|
|
95
|
+
readonly dropped: number;
|
|
96
|
+
/** Events some hook failed on; handed back to the queue for redelivery. */
|
|
97
|
+
readonly retried: number;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A Queues consumer for push events: each message is dispatched to the hooks, then acknowledged,
|
|
102
|
+
* or retried when a hook failed. Messages that do not parse as a {@link PushEvent} are
|
|
103
|
+
* acknowledged and counted as dropped.
|
|
104
|
+
*/
|
|
105
|
+
export async function consumePushEvents(
|
|
106
|
+
batch: { readonly messages: readonly PushEventMessage[] },
|
|
107
|
+
hooks: readonly OnPushHook[],
|
|
108
|
+
): Promise<PushBatchOutcome> {
|
|
109
|
+
let delivered = 0;
|
|
110
|
+
let dropped = 0;
|
|
111
|
+
let retried = 0;
|
|
112
|
+
for (const message of batch.messages) {
|
|
113
|
+
if (!PushEvent.safeParse(message.body).success) {
|
|
114
|
+
message.ack();
|
|
115
|
+
dropped += 1;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
await dispatchPushEvent(message.body, hooks);
|
|
120
|
+
message.ack();
|
|
121
|
+
delivered += 1;
|
|
122
|
+
} catch {
|
|
123
|
+
message.retry();
|
|
124
|
+
retried += 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { delivered, dropped, retried };
|
|
128
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import {
|
|
3
|
+
EXAMPLE_SHA_A,
|
|
4
|
+
EXAMPLE_SHA_B,
|
|
5
|
+
EXAMPLE_TIMESTAMP,
|
|
6
|
+
} from '@leed-ai/git-gateway-contract/schemas/examples';
|
|
7
|
+
import { Timestamp } from '@leed-ai/git-gateway-contract/schemas/identity';
|
|
8
|
+
import { RepoName } from '@leed-ai/git-gateway-contract/schemas/names';
|
|
9
|
+
import { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
|
|
12
|
+
/** A push the gateway accepted, as onPush hooks receive it. */
|
|
13
|
+
export const PushEvent = z
|
|
14
|
+
.object({
|
|
15
|
+
repo: RepoName.describe('Repository that was pushed to.'),
|
|
16
|
+
updates: z
|
|
17
|
+
.array(RefUpdate)
|
|
18
|
+
.min(1)
|
|
19
|
+
.max(1000)
|
|
20
|
+
.describe(
|
|
21
|
+
'Every ref the push moved, in the order the push named them; 1–1000. A created ref has an all-zero oldSha, a deleted ref an all-zero newSha.',
|
|
22
|
+
),
|
|
23
|
+
pushedAt: Timestamp.describe('When the storage host accepted the push.'),
|
|
24
|
+
})
|
|
25
|
+
.strict()
|
|
26
|
+
.meta({
|
|
27
|
+
id: 'PushEvent',
|
|
28
|
+
title: 'Push event',
|
|
29
|
+
description:
|
|
30
|
+
'A push the gateway accepted (through the smart-HTTP proxy or an RPC write), delivered to onPush hooks directly or through a Queue. Carries ref ids only: no content, no credential, no principal secret.',
|
|
31
|
+
examples: [
|
|
32
|
+
{
|
|
33
|
+
repo: 'acme-raw-content',
|
|
34
|
+
updates: [{ ref: 'refs/heads/main', oldSha: EXAMPLE_SHA_A, newSha: EXAMPLE_SHA_B }],
|
|
35
|
+
pushedAt: EXAMPLE_TIMESTAMP,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** A push event, inferred from {@link PushEvent}. */
|
|
41
|
+
export type PushEvent = z.infer<typeof PushEvent>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { invalidInput } from '@leed-ai/git-gateway-contract/gateway-error';
|
|
3
|
+
|
|
4
|
+
import { attemptCommit } from '../rpc/commit-attempt.ts';
|
|
5
|
+
import type { GatewayContext } from '../rpc/gateway-context.ts';
|
|
6
|
+
import { CommitStepResult, CommitWorkflowParams } from './commit-workflow-schemas.ts';
|
|
7
|
+
import { BranchMovedError, classifyStepError } from './step-errors.ts';
|
|
8
|
+
import { keepError, type FatalError, type StepConfig, type StepRunner } from './step-runner.ts';
|
|
9
|
+
|
|
10
|
+
/** Retries of the commit step: contention clears in seconds, so back off from 2 s, ten times. */
|
|
11
|
+
export const COMMIT_STEP_CONFIG: StepConfig = {
|
|
12
|
+
retries: { limit: 10, delay: '2 seconds', backoff: 'exponential' },
|
|
13
|
+
timeout: '2 minutes',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The body of CommitWorkflow: one durable step that makes one commit attempt with the
|
|
18
|
+
* idempotency key the RPC call carried. A lost race throws so the engine retries the step with
|
|
19
|
+
* backoff; a broken rule stops the instance. Because every attempt first looks for a commit
|
|
20
|
+
* carrying the key, a step that pushed and then crashed before its result was stored finds its
|
|
21
|
+
* own commit on replay and reports it as replayed: exactly one commit, however often the step
|
|
22
|
+
* runs. The stored result holds ids only (see {@link CommitStepResult}).
|
|
23
|
+
*/
|
|
24
|
+
export async function runCommitWorkflow(
|
|
25
|
+
context: GatewayContext,
|
|
26
|
+
payload: unknown,
|
|
27
|
+
step: StepRunner,
|
|
28
|
+
fatal: FatalError = keepError,
|
|
29
|
+
): Promise<CommitStepResult> {
|
|
30
|
+
const parsed = CommitWorkflowParams.safeParse(payload);
|
|
31
|
+
if (!parsed.success) throw fatal(invalidInput(parsed.error));
|
|
32
|
+
const params = parsed.data;
|
|
33
|
+
const stored = await step.do('commit', COMMIT_STEP_CONFIG, async (): Promise<CommitStepResult> => {
|
|
34
|
+
try {
|
|
35
|
+
const outcome = await attemptCommit(context, params);
|
|
36
|
+
if (outcome.kind === 'stale') throw new BranchMovedError(params.branch);
|
|
37
|
+
const { output } = outcome;
|
|
38
|
+
if (output.status === 'noop') return CommitStepResult.parse({ status: 'noop', head: output.head });
|
|
39
|
+
return CommitStepResult.parse({ status: output.status, sha: output.commit.sha });
|
|
40
|
+
} catch (thrown) {
|
|
41
|
+
throw classifyStepError(thrown, fatal);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return CommitStepResult.parse(stored);
|
|
45
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { RpcOutput } from '@leed-ai/git-gateway-contract/git-gateway';
|
|
3
|
+
import { invalidInput } from '@leed-ai/git-gateway-contract/gateway-error';
|
|
4
|
+
|
|
5
|
+
import type { GatewayContext } from '../rpc/gateway-context.ts';
|
|
6
|
+
import { attemptSync } from '../rpc/sync-attempt.ts';
|
|
7
|
+
import { BranchMovedError, classifyStepError } from './step-errors.ts';
|
|
8
|
+
import { keepError, type FatalError, type StepConfig, type StepRunner } from './step-runner.ts';
|
|
9
|
+
import { SyncBranchWorkflowParams, SyncStepResult } from './sync-workflow-schemas.ts';
|
|
10
|
+
|
|
11
|
+
/** Retries of the sync step: back off from 5 s, eight times. */
|
|
12
|
+
export const SYNC_STEP_CONFIG: StepConfig = {
|
|
13
|
+
retries: { limit: 8, delay: '5 seconds', backoff: 'exponential' },
|
|
14
|
+
timeout: '5 minutes',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The stored form of a syncBranch() result: ids and paths only. */
|
|
18
|
+
function summarize(output: RpcOutput<'syncBranch'>): SyncStepResult {
|
|
19
|
+
if (output.status === 'up_to_date') return { status: 'up_to_date', head: output.head };
|
|
20
|
+
if (output.status === 'fast_forward') return { status: 'fast_forward', head: output.refUpdate.newSha };
|
|
21
|
+
if (output.status === 'merged') return { status: 'merged', head: output.commit.sha };
|
|
22
|
+
const { paths, base, fromHead, intoHead } = output;
|
|
23
|
+
return { status: 'conflict', paths, base, fromHead, intoHead };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The body of SyncBranchWorkflow: one durable step that syncs `into` with `from` (see
|
|
28
|
+
* syncBranch). `into` moving under the step throws so the engine retries with backoff; a missing
|
|
29
|
+
* branch or unrelated histories stop the instance; a conflict completes it with the conflicting
|
|
30
|
+
* paths. Replaying a step that merged and then crashed finds `into` already containing `from` and
|
|
31
|
+
* reports up_to_date, so a sync never writes two merge commits.
|
|
32
|
+
*/
|
|
33
|
+
export async function runSyncWorkflow(
|
|
34
|
+
context: GatewayContext,
|
|
35
|
+
payload: unknown,
|
|
36
|
+
step: StepRunner,
|
|
37
|
+
fatal: FatalError = keepError,
|
|
38
|
+
): Promise<SyncStepResult> {
|
|
39
|
+
const parsed = SyncBranchWorkflowParams.safeParse(payload);
|
|
40
|
+
if (!parsed.success) throw fatal(invalidInput(parsed.error));
|
|
41
|
+
const { repo, from, into } = parsed.data;
|
|
42
|
+
const stored = await step.do('sync', SYNC_STEP_CONFIG, async (): Promise<SyncStepResult> => {
|
|
43
|
+
try {
|
|
44
|
+
const outcome = await attemptSync(context, { repo, from, into });
|
|
45
|
+
if (outcome.kind === 'stale') throw new BranchMovedError(into);
|
|
46
|
+
return SyncStepResult.parse(summarize(outcome.output));
|
|
47
|
+
} catch (thrown) {
|
|
48
|
+
throw classifyStepError(thrown, fatal);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return SyncStepResult.parse(stored);
|
|
52
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { readGatewayError } from '@leed-ai/git-gateway-contract/gateway-error';
|
|
3
|
+
|
|
4
|
+
import type { FatalError } from './step-runner.ts';
|
|
5
|
+
|
|
6
|
+
/** Thrown inside a step when the branch moved under it, so the engine retries the step with backoff. */
|
|
7
|
+
export class BranchMovedError extends Error {
|
|
8
|
+
override readonly name = 'BranchMovedError';
|
|
9
|
+
|
|
10
|
+
constructor(branch: string) {
|
|
11
|
+
super(`Branch ${branch} moved during the step; retrying`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sorts an error thrown inside a step: a GatewayError that retrying cannot fix (any code but
|
|
17
|
+
* storage_unavailable — a rule broken by the actions, a missing branch, a stale expected head) is
|
|
18
|
+
* turned into a fatal error so the instance stops at once, keeping the GatewayError's message (and
|
|
19
|
+
* so its body). Everything else — storage failures, a moved branch, the unexpected — is rethrown
|
|
20
|
+
* as is for the engine to retry.
|
|
21
|
+
*/
|
|
22
|
+
export function classifyStepError(thrown: unknown, fatal: FatalError): unknown {
|
|
23
|
+
const body = readGatewayError(thrown);
|
|
24
|
+
if (body === null || body.code === 'storage_unavailable' || !(thrown instanceof Error)) return thrown;
|
|
25
|
+
return fatal(thrown);
|
|
26
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { CommitStepResult } from './commit-workflow-schemas.ts';
|
|
3
|
+
import type { SyncStepResult } from './sync-workflow-schemas.ts';
|
|
4
|
+
|
|
5
|
+
/** Retry and timeout settings of one Workflow step, in the shape `WorkflowStep.do` takes. */
|
|
6
|
+
export interface StepConfig {
|
|
7
|
+
readonly retries: {
|
|
8
|
+
readonly limit: number;
|
|
9
|
+
readonly delay: `${number} ${'second' | 'minute'}${'s' | ''}`;
|
|
10
|
+
readonly backoff: 'constant' | 'linear' | 'exponential';
|
|
11
|
+
};
|
|
12
|
+
readonly timeout: `${number} ${'second' | 'minute'}${'s' | ''}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** What a gateway Workflow step stores: ids, statuses and paths, never content or a credential. */
|
|
16
|
+
export type StepResult = CommitStepResult | SyncStepResult;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The part of a Workflows `WorkflowStep` the gateway's workflows use: run a named, durable step.
|
|
20
|
+
* The engine stores what the callback returns and, when the instance replays after a crash or a
|
|
21
|
+
* retry, hands back the stored value instead of running a finished step again. A step whose
|
|
22
|
+
* result was never stored (the isolate died mid-step) runs again, so every step is idempotent.
|
|
23
|
+
* The stored value comes back as `unknown`: callers parse it with its schema.
|
|
24
|
+
*/
|
|
25
|
+
export interface StepRunner {
|
|
26
|
+
do(name: string, config: StepConfig, callback: () => Promise<StepResult>): Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Turns an error a step cannot recover from into the error that stops the instance without
|
|
31
|
+
* retries (in a Worker, `NonRetryableError` from `cloudflare:workflows`).
|
|
32
|
+
*/
|
|
33
|
+
export type FatalError = (error: Error) => Error;
|
|
34
|
+
|
|
35
|
+
/** The default {@link FatalError}: the error unchanged (for runners without a non-retryable error). */
|
|
36
|
+
export const keepError: FatalError = (error) => error;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep } from 'cloudflare:workers';
|
|
3
|
+
|
|
4
|
+
import { gatewayContext } from '../rpc/gateway-context.ts';
|
|
5
|
+
import type { GatewayOptionsResolver } from '../rpc/gateway-service.ts';
|
|
6
|
+
import { runSyncWorkflow } from './run-sync-workflow.ts';
|
|
7
|
+
import { nonRetryable, stepRunnerOf } from './workflow-runtime.ts';
|
|
8
|
+
import type { SyncBranchWorkflowParams, SyncStepResult } from './sync-workflow-schemas.ts';
|
|
9
|
+
|
|
10
|
+
/** The SyncBranchWorkflow class {@link createSyncBranchWorkflow} returns. */
|
|
11
|
+
export type SyncBranchWorkflowClass<Env> = new (
|
|
12
|
+
ctx: ExecutionContext,
|
|
13
|
+
env: Env,
|
|
14
|
+
) => WorkflowEntrypoint<Env, SyncBranchWorkflowParams> & {
|
|
15
|
+
run(event: Readonly<WorkflowEvent<SyncBranchWorkflowParams>>, step: WorkflowStep): Promise<SyncStepResult>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The SyncBranchWorkflow a deployment exports and binds, which onPush sync rules start (see
|
|
20
|
+
* `syncRuleHook`), e.g. to sync staging from main after every push to main:
|
|
21
|
+
*
|
|
22
|
+
* export const SyncBranchWorkflow = createSyncBranchWorkflow(gatewayOptions);
|
|
23
|
+
*/
|
|
24
|
+
export function createSyncBranchWorkflow<Env>(
|
|
25
|
+
resolve: GatewayOptionsResolver<Env>,
|
|
26
|
+
): SyncBranchWorkflowClass<Env> {
|
|
27
|
+
return class SyncBranchWorkflow extends WorkflowEntrypoint<Env, SyncBranchWorkflowParams> {
|
|
28
|
+
override run(
|
|
29
|
+
event: Readonly<WorkflowEvent<SyncBranchWorkflowParams>>,
|
|
30
|
+
step: WorkflowStep,
|
|
31
|
+
): Promise<SyncStepResult> {
|
|
32
|
+
return runSyncWorkflow(
|
|
33
|
+
gatewayContext(resolve(this.env)),
|
|
34
|
+
event.payload,
|
|
35
|
+
stepRunnerOf(step),
|
|
36
|
+
nonRetryable,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|