@atolis-hq/wake 0.4.96 → 0.4.97
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/activities/agent/agent-activity.js +12 -1
- package/dist/src/activities/agent/agent-result.d.ts +1 -0
- package/dist/src/activities/agent/agent-result.js +1 -0
- package/dist/src/activities/contracts/activity.d.ts +7 -0
- package/dist/src/artifacts/application/delegation-artifacts.d.ts +5 -2
- package/dist/src/artifacts/application/delegation-artifacts.js +5 -1
- package/dist/src/bootstrap/composition-root.d.ts +6 -1
- package/dist/src/bootstrap/composition-root.js +40 -15
- package/dist/src/bootstrap/version.js +1 -1
- package/dist/src/control-plane/application/activation-scheduler-ports.d.ts +3 -1
- package/dist/src/control-plane/application/advance-once-dispatch.d.ts +1 -1
- package/dist/src/control-plane/application/advance-once-dispatch.js +4 -2
- package/dist/src/control-plane/application/dispatch-resources.d.ts +4 -0
- package/dist/src/control-plane/application/dispatch-resources.js +13 -0
- package/dist/src/execution/application/execution-service.js +27 -5
- package/dist/src/execution/application/execution-validation.d.ts +6 -1
- package/dist/src/execution/application/execution-validation.js +29 -11
- package/dist/src/execution/application/repository-assignments.d.ts +11 -0
- package/dist/src/execution/application/repository-assignments.js +18 -0
- package/dist/src/execution/application/run-lifecycle.d.ts +1 -0
- package/dist/src/execution/application/run-lifecycle.js +30 -10
- package/dist/src/execution/contracts/commands.d.ts +2 -0
- package/dist/src/execution/contracts/events.d.ts +20 -0
- package/dist/src/execution/contracts/events.js +9 -0
- package/dist/src/execution/contracts/repository-assignment.d.ts +17 -0
- package/dist/src/execution/contracts/repository-assignment.js +9 -0
- package/dist/src/execution/contracts/views.d.ts +5 -0
- package/dist/src/execution/contracts/workspace.d.ts +6 -0
- package/dist/src/execution/domain/run.js +9 -0
- package/dist/src/execution/index.d.ts +3 -0
- package/dist/src/execution/index.js +3 -0
- package/dist/src/execution/infrastructure/workspace/git-source.d.ts +6 -0
- package/dist/src/execution/infrastructure/workspace/git-source.js +35 -0
- package/dist/src/execution/infrastructure/workspace/git-workspace.d.ts +5 -0
- package/dist/src/execution/infrastructure/workspace/git-workspace.js +50 -3
- package/dist/src/integrations/application/artifact-registration-reactor.d.ts +6 -0
- package/dist/src/integrations/application/artifact-registration-reactor.js +35 -11
- package/dist/src/integrations/application/artifact-source.d.ts +9 -0
- package/dist/src/integrations/application/artifact-source.js +12 -0
- package/dist/src/integrations/application/delegation-outputs.d.ts +15 -0
- package/dist/src/integrations/application/delegation-outputs.js +118 -0
- package/dist/src/integrations/contracts/provider.d.ts +3 -0
- package/dist/src/integrations/fake/external-source.d.ts +1 -0
- package/dist/src/integrations/fake/inbound-translator.js +1 -0
- package/dist/src/integrations/fake/provider.d.ts +1 -0
- package/dist/src/integrations/fake/provider.js +11 -2
- package/dist/src/integrations/github/provider.js +7 -1
- package/dist/src/integrations/index.d.ts +1 -0
- package/dist/src/integrations/index.js +1 -0
- package/dist/src/orchestration/application/delegated-work.d.ts +10 -1
- package/dist/src/orchestration/application/delegated-work.js +41 -5
- package/dist/src/orchestration/application/delegation-reservation.d.ts +2 -2
- package/dist/src/orchestration/application/delegation-reservation.js +34 -11
- package/dist/src/orchestration/application/orchestration-service.d.ts +16 -1
- package/dist/src/orchestration/application/orchestration-service.js +4 -0
- package/dist/src/orchestration/contracts/delegation-ports.d.ts +6 -1
- package/dist/src/orchestration/contracts/delegation.d.ts +52 -7
- package/dist/src/orchestration/contracts/delegation.js +47 -2
- package/dist/src/orchestration/contracts/vocabulary.d.ts +3 -0
- package/dist/src/orchestration/contracts/vocabulary.js +1 -0
- package/dist/src/orchestration/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { reportedArtifactSchema } from '../../activities/index.js';
|
|
3
|
+
import { RunStatus, WorkspaceMode } from '../../execution/index.js';
|
|
4
|
+
import { BuiltInResourceKind, ResourceCorrelationProvenance, ResourceCorrelationRole, resourceId, } from '../../resources/index.js';
|
|
5
|
+
import { ArtifactVerificationResult } from '../contracts/artifact-vocabulary.js';
|
|
6
|
+
const reportsSchema = z.object({ reportedArtifacts: z.array(reportedArtifactSchema) });
|
|
7
|
+
/** Select exact provider-verified results owned by the producing child. */
|
|
8
|
+
export function createDelegationOutputs(input) {
|
|
9
|
+
return {
|
|
10
|
+
...input.artifacts,
|
|
11
|
+
async qualifyingOutputs(child, outputs) {
|
|
12
|
+
const artifacts = outputs.filter((output) => 'path' in output);
|
|
13
|
+
const references = artifacts.length === 0 ? [] : await input.artifacts.qualifyingOutputs(child, artifacts);
|
|
14
|
+
if (references === null)
|
|
15
|
+
return null;
|
|
16
|
+
for (const output of outputs) {
|
|
17
|
+
if (!('kind' in output))
|
|
18
|
+
continue;
|
|
19
|
+
const change = await qualifyingChange(input, child, output);
|
|
20
|
+
if (change === null)
|
|
21
|
+
return null;
|
|
22
|
+
references.push(change);
|
|
23
|
+
}
|
|
24
|
+
return references;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function reportedChange(runs, name) {
|
|
29
|
+
return runs
|
|
30
|
+
.flatMap((run) => {
|
|
31
|
+
const data = reportsSchema.safeParse(run.outcome?.data);
|
|
32
|
+
return data.success
|
|
33
|
+
? data.data.reportedArtifacts
|
|
34
|
+
.filter((entry) => entry.name === name && entry.kind === BuiltInResourceKind.PullRequest)
|
|
35
|
+
.map((report) => ({ run, report }))
|
|
36
|
+
: [];
|
|
37
|
+
})
|
|
38
|
+
.at(-1);
|
|
39
|
+
}
|
|
40
|
+
async function verifiedChange(input, report, source) {
|
|
41
|
+
const { assignment, branch, revision } = source;
|
|
42
|
+
const repository = await input.resources.get(resourceId(assignment.resourceId));
|
|
43
|
+
const provider = (await input.providers()).find((entry) => entry.adapter === report.externalKey.adapter);
|
|
44
|
+
if (repository === null || provider === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const verified = await provider.verifyArtifact(BuiltInResourceKind.PullRequest, report.externalKey, {
|
|
47
|
+
repository: repository.externalKey,
|
|
48
|
+
workspaceBranch: branch,
|
|
49
|
+
workspaceRevision: revision,
|
|
50
|
+
pullRequestBase: assignment.pullRequestBase,
|
|
51
|
+
});
|
|
52
|
+
if (verified === ArtifactVerificationResult.Ambiguous)
|
|
53
|
+
throw new Error('Child PR verification is ambiguous; reconcile provider evidence before selecting the output');
|
|
54
|
+
if (typeof verified === 'string')
|
|
55
|
+
return null;
|
|
56
|
+
const resource = await input.resources.findByExternalKey(verified.externalKey);
|
|
57
|
+
if (resource === null ||
|
|
58
|
+
resource.kind !== BuiltInResourceKind.PullRequest ||
|
|
59
|
+
verified.revision !== revision ||
|
|
60
|
+
resource.primaryCorrelationConflict !== undefined)
|
|
61
|
+
return null;
|
|
62
|
+
return resource;
|
|
63
|
+
}
|
|
64
|
+
async function qualifyingChange(input, child, output) {
|
|
65
|
+
const runs = (await input.runs()).filter((run) => run.workflowInstanceId === child.workflowInstanceId &&
|
|
66
|
+
run.status === RunStatus.Succeeded &&
|
|
67
|
+
child.acceptedOutcomes.includes(run.activationId));
|
|
68
|
+
const latest = reportedChange(runs, output.name);
|
|
69
|
+
if (latest === undefined)
|
|
70
|
+
return null;
|
|
71
|
+
// Judge the PR against the latest implementation head so a revision run need not
|
|
72
|
+
// repeat the report; the provider must confirm the PR now points there.
|
|
73
|
+
const run = runs.filter((entry) => entry.workspace?.mode === WorkspaceMode.Branch).at(-1);
|
|
74
|
+
const source = run === undefined ? undefined : runSource(run);
|
|
75
|
+
if (run === undefined || source === undefined)
|
|
76
|
+
return null;
|
|
77
|
+
const { assignment, branch, revision } = source;
|
|
78
|
+
const resource = await verifiedChange(input, latest.report, source);
|
|
79
|
+
if (resource === null)
|
|
80
|
+
return null;
|
|
81
|
+
if (!(await hasOutputAuthority(input, child, output, resource.resourceId, revision)))
|
|
82
|
+
return null;
|
|
83
|
+
return {
|
|
84
|
+
name: output.name,
|
|
85
|
+
reference: {
|
|
86
|
+
workItemId: child.workItemId,
|
|
87
|
+
resourceId: resource.resourceId,
|
|
88
|
+
repositoryResourceId: assignment.resourceId,
|
|
89
|
+
revision,
|
|
90
|
+
branch,
|
|
91
|
+
pullRequestBase: assignment.pullRequestBase,
|
|
92
|
+
runId: run.runId,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function hasOutputAuthority(input, child, output, id, revision) {
|
|
97
|
+
const owner = await input.resources.primaryCorrelation(id);
|
|
98
|
+
if (owner?.workItemId !== child.workItemId ||
|
|
99
|
+
owner.role !== ResourceCorrelationRole.Primary ||
|
|
100
|
+
owner.provenance !== ResourceCorrelationProvenance.AgentReported)
|
|
101
|
+
return false;
|
|
102
|
+
const authority = await input.pullRequests.decideAuthority(child.workItemId, {
|
|
103
|
+
target: { resourceId: id },
|
|
104
|
+
requireAcceptedReview: output.requireReview ?? true,
|
|
105
|
+
requireChecks: output.requireChecks ?? true,
|
|
106
|
+
});
|
|
107
|
+
if (!authority.allowed || authority.revision !== revision)
|
|
108
|
+
return false;
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
function runSource(run) {
|
|
112
|
+
const assignment = run.repositoryAssignment;
|
|
113
|
+
const branch = run.workspace?.branch;
|
|
114
|
+
const revision = run.workspaceRevision;
|
|
115
|
+
if (assignment === undefined || branch === undefined || revision === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
return { assignment, branch, revision };
|
|
118
|
+
}
|
|
@@ -76,6 +76,9 @@ export interface ProviderInstance {
|
|
|
76
76
|
};
|
|
77
77
|
verifyArtifact(kind: ResourceKind, externalKey: ExternalResourceKey, context: {
|
|
78
78
|
readonly workspaceBranch: string;
|
|
79
|
+
readonly repository?: ExternalResourceKey;
|
|
80
|
+
readonly workspaceRevision?: string;
|
|
81
|
+
readonly pullRequestBase?: string;
|
|
79
82
|
}): Promise<VerifiedArtifact | ArtifactVerificationResult>;
|
|
80
83
|
readonly checkConnectivity?: () => Promise<void>;
|
|
81
84
|
readonly health?: () => readonly AdapterHealthCheck[];
|
|
@@ -12,6 +12,7 @@ export interface FakeWorkEvidence {
|
|
|
12
12
|
readonly tags?: readonly string[] | undefined;
|
|
13
13
|
readonly kind?: 'issue' | 'pull-request' | undefined;
|
|
14
14
|
readonly revision?: string | undefined;
|
|
15
|
+
readonly baseBranch?: string | undefined;
|
|
15
16
|
readonly baseRevision?: string | undefined;
|
|
16
17
|
readonly checks?: typeof PullRequestCheckState.Unknown | typeof PullRequestCheckState.Pending | typeof PullRequestCheckState.Passing | typeof PullRequestCheckState.Failing | undefined;
|
|
17
18
|
readonly acceptedReview?: boolean | undefined;
|
|
@@ -14,6 +14,7 @@ const evidenceSchema = z
|
|
|
14
14
|
kind: z.enum(['issue', 'pull-request']).optional(),
|
|
15
15
|
revision: z.string().min(1).optional(),
|
|
16
16
|
branch: z.string().min(1).optional(),
|
|
17
|
+
baseBranch: z.string().min(1).optional(),
|
|
17
18
|
baseRevision: z.string().min(1).optional(),
|
|
18
19
|
checks: z.enum(['unknown', 'pending', 'passing', 'failing']).optional(),
|
|
19
20
|
acceptedReview: z.boolean().optional(),
|
|
@@ -11,6 +11,7 @@ declare const configSchema: z.ZodObject<{
|
|
|
11
11
|
}>>;
|
|
12
12
|
revision: z.ZodOptional<z.ZodString>;
|
|
13
13
|
branch: z.ZodOptional<z.ZodString>;
|
|
14
|
+
baseBranch: z.ZodOptional<z.ZodString>;
|
|
14
15
|
baseRevision: z.ZodOptional<z.ZodString>;
|
|
15
16
|
checks: z.ZodOptional<z.ZodEnum<{
|
|
16
17
|
unknown: "unknown";
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { PullRequestCheckState, ReviewActorKind } from '../../activities/index.js';
|
|
3
|
-
import { BuiltInResourceCapability } from '../../resources/index.js';
|
|
3
|
+
import { BuiltInResourceCapability, BuiltInResourceKind } from '../../resources/index.js';
|
|
4
|
+
import { matchesArtifactSource } from '../application/artifact-source.js';
|
|
4
5
|
import { ArtifactVerificationResult } from '../contracts/artifact-vocabulary.js';
|
|
5
6
|
import { DurableFakeDeliveryProvider } from './durable-delivery-provider.js';
|
|
6
7
|
import { FakeEventType, FakeExternalEventSource } from './external-source.js';
|
|
@@ -16,6 +17,7 @@ const configSchema = z
|
|
|
16
17
|
kind: z.enum(['issue', 'pull-request']).optional(),
|
|
17
18
|
revision: z.string().min(1).optional(),
|
|
18
19
|
branch: z.string().min(1).optional(),
|
|
20
|
+
baseBranch: z.string().min(1).optional(),
|
|
19
21
|
baseRevision: z.string().min(1).optional(),
|
|
20
22
|
checks: z
|
|
21
23
|
.enum([
|
|
@@ -69,10 +71,17 @@ export const fakeProviderDefinition = {
|
|
|
69
71
|
...(config.effectsFile === undefined ? {} : { effectsFile: config.effectsFile }),
|
|
70
72
|
}),
|
|
71
73
|
verifyArtifact: async (kind, externalKey, context) => {
|
|
74
|
+
if (kind !== BuiltInResourceKind.PullRequest)
|
|
75
|
+
return ArtifactVerificationResult.NotFound;
|
|
72
76
|
const evidence = config.events.find((event) => event.key === externalKey.key);
|
|
73
77
|
if (evidence === undefined || (evidence.kind ?? 'issue') !== 'pull-request')
|
|
74
78
|
return ArtifactVerificationResult.NotFound;
|
|
75
|
-
if (
|
|
79
|
+
if (!matchesArtifactSource(externalKey, {
|
|
80
|
+
repository: externalKey.key.split('#')[0],
|
|
81
|
+
branch: evidence.branch ?? '',
|
|
82
|
+
revision: evidence.revision,
|
|
83
|
+
base: evidence.baseBranch,
|
|
84
|
+
}, context))
|
|
76
85
|
return ArtifactVerificationResult.NotFound;
|
|
77
86
|
return {
|
|
78
87
|
kind,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BuiltInActivityName, PullRequestState } from '../../activities/index.js';
|
|
2
2
|
import { ConversationSurfaceCapability } from '../../orchestration/index.js';
|
|
3
3
|
import { BuiltInResourceCapability, BuiltInResourceKind, ResourceCorrelationRole, resourceId, resourceKind, } from '../../resources/index.js';
|
|
4
|
+
import { matchesArtifactSource } from '../application/artifact-source.js';
|
|
4
5
|
import { ArtifactVerificationResult } from '../contracts/artifact-vocabulary.js';
|
|
5
6
|
import { AdapterHealthStatus, } from '../contracts/provider.js';
|
|
6
7
|
import { InboundTranslator } from './application/inbound-translator.js';
|
|
@@ -176,7 +177,12 @@ export const gitHubProviderDefinition = {
|
|
|
176
177
|
return ArtifactVerificationResult.NotFound;
|
|
177
178
|
try {
|
|
178
179
|
const pullRequest = await client.getPullRequest(parsed.owner, parsed.repo, parsed.number);
|
|
179
|
-
if (
|
|
180
|
+
if (!matchesArtifactSource(externalKey, {
|
|
181
|
+
repository: parsed.owner + '/' + parsed.repo,
|
|
182
|
+
branch: pullRequest.head.ref,
|
|
183
|
+
revision: pullRequest.head.sha,
|
|
184
|
+
base: pullRequest.base.ref,
|
|
185
|
+
}, context))
|
|
180
186
|
return ArtifactVerificationResult.NotFound;
|
|
181
187
|
return {
|
|
182
188
|
kind,
|
|
@@ -32,3 +32,4 @@ export * from './fake/external-sink.js';
|
|
|
32
32
|
export * from './fake/external-source.js';
|
|
33
33
|
export * from './fake/provider.js';
|
|
34
34
|
export * from './application/conversation-command-feedback.js';
|
|
35
|
+
export { createDelegationOutputs } from './application/delegation-outputs.js';
|
|
@@ -32,3 +32,4 @@ export * from './fake/external-sink.js';
|
|
|
32
32
|
export * from './fake/external-source.js';
|
|
33
33
|
export * from './fake/provider.js';
|
|
34
34
|
export * from './application/conversation-command-feedback.js';
|
|
35
|
+
export { createDelegationOutputs } from './application/delegation-outputs.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type CommandContext } from '@atolis-hq/eventing';
|
|
2
2
|
import { type WorkService } from '../../work/index.js';
|
|
3
|
-
import type { DelegationArtifacts } from '../contracts/delegation-ports.js';
|
|
3
|
+
import type { DelegationArtifacts, DelegationRepositories } from '../contracts/delegation-ports.js';
|
|
4
4
|
import type { DelegatedAssignment, DelegationCaller, DelegationLimits, DelegationReservation, DelegationResult } from '../contracts/delegation.js';
|
|
5
5
|
import { type WorkflowInstanceId } from '../contracts/identifiers.js';
|
|
6
6
|
import type { WorkflowInstanceView } from '../contracts/views.js';
|
|
@@ -20,14 +20,23 @@ export declare class DelegatedWork {
|
|
|
20
20
|
private readonly workflows;
|
|
21
21
|
private readonly ledger;
|
|
22
22
|
private readonly artifacts;
|
|
23
|
+
private readonly repositories;
|
|
23
24
|
private readonly runHasEnded;
|
|
24
25
|
constructor(repository: OrchestrationRepository, work: WorkService, claims: CoordinationClaims, definitions: WorkflowDefinitionRegistry, workflows: StartWorkflow, dependencies: {
|
|
25
26
|
readonly artifacts: DelegationArtifacts;
|
|
27
|
+
readonly repositories?: DelegationRepositories | undefined;
|
|
26
28
|
readonly runHasEnded: (runId: string) => Promise<boolean>;
|
|
27
29
|
});
|
|
30
|
+
repositoryFor(workItemId: string): Promise<{
|
|
31
|
+
resourceId: string;
|
|
32
|
+
sourceRef: string;
|
|
33
|
+
pullRequestBase: string;
|
|
34
|
+
sourceRevision: string;
|
|
35
|
+
} | undefined>;
|
|
28
36
|
contextWorkItems(parent: WorkflowInstanceView): Promise<ReadonlySet<string>>;
|
|
29
37
|
results(id: WorkflowInstanceId, cohortId: string): Promise<readonly DelegationResult[]>;
|
|
30
38
|
reserve(caller: DelegationCaller, cohortId: string, assignments: readonly DelegatedAssignment[], context: CommandContext, limits?: DelegationLimits): Promise<DelegationReservation>;
|
|
39
|
+
private pinAssignments;
|
|
31
40
|
reconcile(context: CommandContext): Promise<void>;
|
|
32
41
|
private capacityReservations;
|
|
33
42
|
private parentIdentity;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable max-lines */
|
|
2
2
|
import { WrongExpectedSequenceError } from '@atolis-hq/eventing';
|
|
3
3
|
import { activationId } from '../../activities/index.js';
|
|
4
|
+
import { WorkspaceMode } from '../../execution/index.js';
|
|
4
5
|
import { WorkStatus } from '../../work/index.js';
|
|
5
6
|
import { OrchestrationEventType } from '../contracts/events.js';
|
|
6
7
|
import { orchestrationGroupId, workflowInstanceId, } from '../contracts/identifiers.js';
|
|
@@ -8,7 +9,7 @@ import { WorkflowStatus } from '../contracts/vocabulary.js';
|
|
|
8
9
|
import { nextOrdinal } from '../domain/decision-events.js';
|
|
9
10
|
import { isSuccessfulWorkflowCompletion } from '../domain/workflow-completion.js';
|
|
10
11
|
import { delegationDraft, delegationEventId, DelegationLedger, isJoined, isSettled, reservationsOf, reservedEvent, selectedResults, } from './delegation-ledger.js';
|
|
11
|
-
import { assertWithinRootLimits, buildReservation,
|
|
12
|
+
import { assertWithinRootLimits, buildReservation, matchesAssignments, } from './delegation-reservation.js';
|
|
12
13
|
const defaultLimits = { maxDepth: 1, maxDescendants: 8 };
|
|
13
14
|
/**
|
|
14
15
|
* Durable delegation of independent child WorkItems. Reservation is the single commit point;
|
|
@@ -22,6 +23,7 @@ export class DelegatedWork {
|
|
|
22
23
|
workflows;
|
|
23
24
|
ledger;
|
|
24
25
|
artifacts;
|
|
26
|
+
repositories;
|
|
25
27
|
runHasEnded;
|
|
26
28
|
constructor(repository, work, claims, definitions, workflows, dependencies) {
|
|
27
29
|
this.repository = repository;
|
|
@@ -31,8 +33,14 @@ export class DelegatedWork {
|
|
|
31
33
|
this.workflows = workflows;
|
|
32
34
|
this.ledger = new DelegationLedger(repository);
|
|
33
35
|
this.artifacts = dependencies.artifacts;
|
|
36
|
+
this.repositories = dependencies.repositories;
|
|
34
37
|
this.runHasEnded = dependencies.runHasEnded;
|
|
35
38
|
}
|
|
39
|
+
async repositoryFor(workItemId) {
|
|
40
|
+
return (await this.ledger.reservations())
|
|
41
|
+
.flatMap((entry) => entry.children)
|
|
42
|
+
.find((child) => child.workItemId === workItemId)?.repository;
|
|
43
|
+
}
|
|
36
44
|
async contextWorkItems(parent) {
|
|
37
45
|
const item = await this.work.get(parent.workItemId);
|
|
38
46
|
const allowed = new Set([parent.workItemId, ...(item?.delegation?.ancestors ?? [])]);
|
|
@@ -55,7 +63,14 @@ export class DelegatedWork {
|
|
|
55
63
|
const artifacts = this.artifacts;
|
|
56
64
|
const parent = (await this.repository.loadRequired(caller.workflowInstanceId)).view;
|
|
57
65
|
const identity = await this.parentIdentity(parent);
|
|
58
|
-
|
|
66
|
+
const prior = (await this.ledger.reservations()).find((entry) => entry.rootWorkItemId === identity.parent.rootWorkItemId && entry.cohortId === cohortId);
|
|
67
|
+
if (prior !== undefined) {
|
|
68
|
+
assertReplay(prior, caller, assignments);
|
|
69
|
+
await this.reconcile(context);
|
|
70
|
+
return prior;
|
|
71
|
+
}
|
|
72
|
+
const pinned = await this.pinAssignments(parent, assignments);
|
|
73
|
+
let reservation = buildReservation(identity.parent, caller, cohortId, pinned, limits);
|
|
59
74
|
for (let attempt = 0; attempt < 8; attempt++) {
|
|
60
75
|
const { events: rootFacts, sequence } = await this.repository.history(identity.root);
|
|
61
76
|
const events = await this.ledger.events();
|
|
@@ -64,11 +79,10 @@ export class DelegatedWork {
|
|
|
64
79
|
maxDepth: all[0]?.maxDepth ?? limits.maxDepth,
|
|
65
80
|
maxDescendants: all[0]?.maxDescendants ?? limits.maxDescendants,
|
|
66
81
|
};
|
|
67
|
-
reservation = buildReservation(identity.parent, caller, cohortId,
|
|
82
|
+
reservation = buildReservation(identity.parent, caller, cohortId, pinned, rootLimits);
|
|
68
83
|
const prior = all.find((entry) => entry.cohortId === cohortId);
|
|
69
84
|
if (prior !== undefined) {
|
|
70
|
-
|
|
71
|
-
throw new Error('Delegation replay conflict');
|
|
85
|
+
assertReplay(prior, caller, assignments);
|
|
72
86
|
await this.reconcile(context);
|
|
73
87
|
return prior;
|
|
74
88
|
}
|
|
@@ -82,6 +96,18 @@ export class DelegatedWork {
|
|
|
82
96
|
}
|
|
83
97
|
throw new Error('Delegation reservation contention exceeded retry limit');
|
|
84
98
|
}
|
|
99
|
+
async pinAssignments(parent, assignments) {
|
|
100
|
+
return Promise.all(assignments.map(async (assignment) => {
|
|
101
|
+
if (assignment.repository === undefined)
|
|
102
|
+
return assignment;
|
|
103
|
+
if (this.repositories === undefined)
|
|
104
|
+
throw new Error('Delegation repository resolver unavailable');
|
|
105
|
+
return {
|
|
106
|
+
...assignment,
|
|
107
|
+
repository: await this.repositories.resolve(parent, assignment.repository),
|
|
108
|
+
};
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
85
111
|
async reconcile(context) {
|
|
86
112
|
const all = await this.ledger.reservations();
|
|
87
113
|
if (all.length === 0)
|
|
@@ -174,6 +200,10 @@ export class DelegatedWork {
|
|
|
174
200
|
if (current.fingerprint !== child.workflowFingerprint)
|
|
175
201
|
throw new Error('Delegation workflow fingerprint must match current configuration');
|
|
176
202
|
const definition = current.definition;
|
|
203
|
+
if (child.repository === undefined &&
|
|
204
|
+
Object.values(definition.stages).some((stage) => stage.execution?.workspace === WorkspaceMode.Branch ||
|
|
205
|
+
stage.execution?.workspace === WorkspaceMode.ReadOnly))
|
|
206
|
+
throw new Error('Workspace-using child workflows require an explicit repository assignment');
|
|
177
207
|
await this.definitions.register(child.workflowName, child.workflowFingerprint, definition, context);
|
|
178
208
|
}
|
|
179
209
|
/** Appends the reservation only if the root stream is unchanged since `events` was read. */
|
|
@@ -418,3 +448,9 @@ export class DelegatedWork {
|
|
|
418
448
|
}
|
|
419
449
|
}
|
|
420
450
|
}
|
|
451
|
+
function assertReplay(prior, caller, assignments) {
|
|
452
|
+
if (prior.parentWorkflowInstanceId !== caller.workflowInstanceId ||
|
|
453
|
+
prior.activationId !== caller.activationId ||
|
|
454
|
+
!matchesAssignments(prior, assignments))
|
|
455
|
+
throw new Error('Delegation replay conflict');
|
|
456
|
+
}
|
|
@@ -9,7 +9,7 @@ export interface ReservationParent {
|
|
|
9
9
|
}
|
|
10
10
|
/** Child identities derive from root, cohort and assignment so replays name the same children. */
|
|
11
11
|
export declare function buildReservation(parent: ReservationParent, caller: DelegationCaller, cohortId: string, assignments: readonly DelegatedAssignment[], limits: DelegationLimits): DelegationReservation;
|
|
12
|
-
/** Identities are minted once and then committed, so replays compare the requested content only. */
|
|
13
|
-
export declare function sameDelegation(a: DelegationReservation, b: DelegationReservation): boolean;
|
|
14
12
|
/** Bounds are fixed by the first reservation under a root so nesting cannot widen them. */
|
|
15
13
|
export declare function assertWithinRootLimits(existing: readonly DelegationReservation[], reservation: DelegationReservation, limits: DelegationLimits): void;
|
|
14
|
+
/** A replay of a branch request retains the originally resolved commit. */
|
|
15
|
+
export declare function matchesAssignments(reservation: DelegationReservation, assignments: readonly DelegatedAssignment[]): boolean;
|
|
@@ -3,6 +3,7 @@ import { UlidIdGenerator } from '../../kernel/index.js';
|
|
|
3
3
|
import { workItemId } from '../../work/index.js';
|
|
4
4
|
import { delegatedAssignmentSchema, delegationReservationSchema, } from '../contracts/delegation.js';
|
|
5
5
|
import { workflowInstanceId } from '../contracts/identifiers.js';
|
|
6
|
+
import { DelegationOutputKind } from '../contracts/vocabulary.js';
|
|
6
7
|
const ids = new UlidIdGenerator();
|
|
7
8
|
/** Child identities derive from root, cohort and assignment so replays name the same children. */
|
|
8
9
|
export function buildReservation(parent, caller, cohortId, assignments, limits) {
|
|
@@ -10,6 +11,11 @@ export function buildReservation(parent, caller, cohortId, assignments, limits)
|
|
|
10
11
|
if (new Set(parsed.map((assignment) => assignment.assignmentId)).size !== parsed.length)
|
|
11
12
|
throw new Error('Duplicate assignment identity');
|
|
12
13
|
for (const assignment of parsed) {
|
|
14
|
+
if (assignment.outputs.filter((output) => 'kind' in output && output.kind === DelegationOutputKind.Change).length > 1)
|
|
15
|
+
throw new Error('One change output per child is currently supported');
|
|
16
|
+
if (assignment.outputs.some((output) => 'kind' in output && output.kind === DelegationOutputKind.Change) &&
|
|
17
|
+
assignment.repository === undefined)
|
|
18
|
+
throw new Error('Change outputs require an explicit repository assignment');
|
|
13
19
|
if (new Set(assignment.outputs.map((output) => output.name)).size !== assignment.outputs.length)
|
|
14
20
|
throw new Error('Duplicate named output');
|
|
15
21
|
}
|
|
@@ -33,17 +39,6 @@ export function buildReservation(parent, caller, cohortId, assignments, limits)
|
|
|
33
39
|
}),
|
|
34
40
|
});
|
|
35
41
|
}
|
|
36
|
-
/** Identities are minted once and then committed, so replays compare the requested content only. */
|
|
37
|
-
export function sameDelegation(a, b) {
|
|
38
|
-
const content = (reservation) => ({
|
|
39
|
-
...reservation,
|
|
40
|
-
runId: undefined,
|
|
41
|
-
maxDepth: undefined,
|
|
42
|
-
maxDescendants: undefined,
|
|
43
|
-
children: reservation.children.map(({ workItemId: _w, workflowInstanceId: _i, grantId: _g, ...assignment }) => assignment),
|
|
44
|
-
});
|
|
45
|
-
return isDeepStrictEqual(content(a), content(b));
|
|
46
|
-
}
|
|
47
42
|
/** Bounds are fixed by the first reservation under a root so nesting cannot widen them. */
|
|
48
43
|
export function assertWithinRootLimits(existing, reservation, limits) {
|
|
49
44
|
const rootLimits = existing[0] ?? limits;
|
|
@@ -52,3 +47,31 @@ export function assertWithinRootLimits(existing, reservation, limits) {
|
|
|
52
47
|
reserved + reservation.children.length > rootLimits.maxDescendants)
|
|
53
48
|
throw new Error('Delegation root reservation limit exceeded');
|
|
54
49
|
}
|
|
50
|
+
/** A replay of a branch request retains the originally resolved commit. */
|
|
51
|
+
export function matchesAssignments(reservation, assignments) {
|
|
52
|
+
if (assignments.some((assignment) => assignment.repository?.sourceRevision !== undefined &&
|
|
53
|
+
reservation.children.find((child) => child.assignmentId === assignment.assignmentId)
|
|
54
|
+
?.repository?.sourceRevision !== assignment.repository.sourceRevision))
|
|
55
|
+
return false;
|
|
56
|
+
return isDeepStrictEqual(reservation.children.map(({ workItemId: _w, workflowInstanceId: _i, grantId: _g, ...assignment }) => ({
|
|
57
|
+
...assignment,
|
|
58
|
+
...(assignment.repository === undefined
|
|
59
|
+
? {}
|
|
60
|
+
: {
|
|
61
|
+
repository: {
|
|
62
|
+
...assignment.repository,
|
|
63
|
+
sourceRevision: assignments.find((entry) => entry.assignmentId === assignment.assignmentId)?.repository?.sourceRevision,
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
})), assignments.map((assignment) => ({
|
|
67
|
+
...delegatedAssignmentSchema.parse(assignment),
|
|
68
|
+
...(assignment.repository === undefined
|
|
69
|
+
? {}
|
|
70
|
+
: {
|
|
71
|
+
repository: {
|
|
72
|
+
...assignment.repository,
|
|
73
|
+
sourceRevision: assignment.repository.sourceRevision,
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
})));
|
|
77
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DelegationArtifacts } from '../contracts/delegation-ports.js';
|
|
1
|
+
import type { DelegationArtifacts, DelegationRepositories } from '../contracts/delegation-ports.js';
|
|
2
2
|
import type { ResourceTransitionEvidence } from './resource-transition-evidence.js';
|
|
3
3
|
import type { CommandContext, EventJournal, ProjectionStore } from '@atolis-hq/eventing';
|
|
4
4
|
import { type ActivationId, type ActivityOutcome } from '../../activities/index.js';
|
|
@@ -30,6 +30,12 @@ export declare class OrchestrationService {
|
|
|
30
30
|
private readonly definitions;
|
|
31
31
|
private readonly work;
|
|
32
32
|
constructor(journal: EventJournal, work: WorkService, definitions: Readonly<Record<string, CompiledWorkflow>>, projections?: ProjectionStore, isAgentActivity?: AgentActivityClassifier, delegation?: DelegationDependenciesFactory);
|
|
33
|
+
repositoryAssignment(workItemId: string): Promise<{
|
|
34
|
+
resourceId: string;
|
|
35
|
+
sourceRef: string;
|
|
36
|
+
pullRequestBase: string;
|
|
37
|
+
sourceRevision: string;
|
|
38
|
+
} | undefined>;
|
|
33
39
|
isActivationAccepted(id: string, item?: string): Promise<boolean>;
|
|
34
40
|
joinedDelegationResults(id: WorkflowInstanceId, cohortId: string): Promise<readonly {
|
|
35
41
|
cohortId: string;
|
|
@@ -39,6 +45,14 @@ export declare class OrchestrationService {
|
|
|
39
45
|
reference: {
|
|
40
46
|
workItemId: WorkItemId;
|
|
41
47
|
revisionId: string;
|
|
48
|
+
} | {
|
|
49
|
+
workItemId: WorkItemId;
|
|
50
|
+
resourceId: string;
|
|
51
|
+
repositoryResourceId: string;
|
|
52
|
+
revision: string;
|
|
53
|
+
branch: string;
|
|
54
|
+
pullRequestBase: string;
|
|
55
|
+
runId: string;
|
|
42
56
|
};
|
|
43
57
|
}[];
|
|
44
58
|
}[]>;
|
|
@@ -124,6 +138,7 @@ type DelegationDependenciesFactory = (queries: {
|
|
|
124
138
|
isActivationAccepted: (activationId: string, workItemId?: string) => Promise<boolean>;
|
|
125
139
|
}) => {
|
|
126
140
|
artifacts: DelegationArtifacts;
|
|
141
|
+
repositories?: DelegationRepositories;
|
|
127
142
|
runHasEnded: (runId: string) => Promise<boolean>;
|
|
128
143
|
};
|
|
129
144
|
export {};
|
|
@@ -54,6 +54,7 @@ export class OrchestrationService {
|
|
|
54
54
|
this.startWorkflow = new StartWorkflow(repository, this.claims, work, this.definitions);
|
|
55
55
|
this.delegatedWork = new DelegatedWork(repository, work, this.claims, this.definitions, this.startWorkflow, {
|
|
56
56
|
artifacts: dependencies?.artifacts ?? unavailableArtifacts,
|
|
57
|
+
repositories: dependencies?.repositories,
|
|
57
58
|
runHasEnded: dependencies?.runHasEnded ?? unavailableRunCompletion,
|
|
58
59
|
});
|
|
59
60
|
this.advanceWorkflow = new AdvanceWorkflow(repository, this.startWorkflow, (command, context) => this.acceptOutcome(command, context), isAgentActivity);
|
|
@@ -66,6 +67,9 @@ export class OrchestrationService {
|
|
|
66
67
|
await this.deferredSignals.reconcile(context);
|
|
67
68
|
});
|
|
68
69
|
}
|
|
70
|
+
repositoryAssignment(workItemId) {
|
|
71
|
+
return this.delegatedWork.repositoryFor(workItemId);
|
|
72
|
+
}
|
|
69
73
|
async isActivationAccepted(id, item) {
|
|
70
74
|
return (await this.repository.list()).some(({ view }) => view !== null &&
|
|
71
75
|
(item === undefined || view.workItemId === item) &&
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CommandContext } from '@atolis-hq/eventing';
|
|
2
|
-
import type {
|
|
2
|
+
import type { RepositoryAssignment } from '../../execution/index.js';
|
|
3
|
+
import type { DelegatedAssignment, DelegationReservation, DelegationResult, DelegationRevisionReference, RepositoryAssignmentRequest } from './delegation.js';
|
|
3
4
|
import type { WorkflowInstanceView } from './views.js';
|
|
4
5
|
/** Trusted Bootstrap adapters; no provider, Run or session authority is supplied by assignments. */
|
|
5
6
|
export interface DelegationArtifacts {
|
|
@@ -12,3 +13,7 @@ export interface DelegationArtifacts {
|
|
|
12
13
|
}, context: CommandContext): Promise<void>;
|
|
13
14
|
qualifyingOutputs(child: WorkflowInstanceView, outputs: DelegatedAssignment['outputs']): Promise<DelegationResult['outputs'] | null>;
|
|
14
15
|
}
|
|
16
|
+
/** Resolves only repositories already authorized by the parent's resource scope. */
|
|
17
|
+
export interface DelegationRepositories {
|
|
18
|
+
resolve(parent: WorkflowInstanceView, request: RepositoryAssignmentRequest): Promise<RepositoryAssignment>;
|
|
19
|
+
}
|
|
@@ -6,6 +6,13 @@ export declare const delegationRevisionReferenceSchema: z.ZodObject<{
|
|
|
6
6
|
revisionId: z.ZodString;
|
|
7
7
|
}, z.core.$strict>;
|
|
8
8
|
export type DelegationRevisionReference = z.infer<typeof delegationRevisionReferenceSchema>;
|
|
9
|
+
export declare const repositoryAssignmentRequestSchema: z.ZodObject<{
|
|
10
|
+
resourceId: z.ZodString;
|
|
11
|
+
sourceRef: z.ZodString;
|
|
12
|
+
sourceRevision: z.ZodOptional<z.ZodString>;
|
|
13
|
+
pullRequestBase: z.ZodString;
|
|
14
|
+
}, z.core.$strict>;
|
|
15
|
+
export type RepositoryAssignmentRequest = z.infer<typeof repositoryAssignmentRequestSchema>;
|
|
9
16
|
export declare const delegatedAssignmentSchema: z.ZodObject<{
|
|
10
17
|
assignmentId: z.ZodString;
|
|
11
18
|
objective: z.ZodString;
|
|
@@ -16,10 +23,21 @@ export declare const delegatedAssignmentSchema: z.ZodObject<{
|
|
|
16
23
|
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
17
24
|
revisionId: z.ZodString;
|
|
18
25
|
}, z.core.$strict>>;
|
|
19
|
-
|
|
26
|
+
repository: z.ZodOptional<z.ZodObject<{
|
|
27
|
+
resourceId: z.ZodString;
|
|
28
|
+
sourceRef: z.ZodString;
|
|
29
|
+
sourceRevision: z.ZodOptional<z.ZodString>;
|
|
30
|
+
pullRequestBase: z.ZodString;
|
|
31
|
+
}, z.core.$strict>>;
|
|
32
|
+
outputs: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
20
33
|
name: z.ZodString;
|
|
21
34
|
path: z.ZodString;
|
|
22
|
-
}, z.core.$strict
|
|
35
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
36
|
+
name: z.ZodString;
|
|
37
|
+
kind: z.ZodLiteral<"change">;
|
|
38
|
+
requireReview: z.ZodOptional<z.ZodBoolean>;
|
|
39
|
+
requireChecks: z.ZodOptional<z.ZodBoolean>;
|
|
40
|
+
}, z.core.$strict>]>>;
|
|
23
41
|
}, z.core.$strict>;
|
|
24
42
|
export type DelegatedAssignment = z.infer<typeof delegatedAssignmentSchema>;
|
|
25
43
|
export declare const delegationReservationSchema: z.ZodObject<{
|
|
@@ -42,9 +60,20 @@ export declare const delegationReservationSchema: z.ZodObject<{
|
|
|
42
60
|
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
43
61
|
revisionId: z.ZodString;
|
|
44
62
|
}, z.core.$strict>>;
|
|
45
|
-
outputs: z.ZodArray<z.ZodObject<{
|
|
63
|
+
outputs: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
46
64
|
name: z.ZodString;
|
|
47
65
|
path: z.ZodString;
|
|
66
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
67
|
+
name: z.ZodString;
|
|
68
|
+
kind: z.ZodLiteral<"change">;
|
|
69
|
+
requireReview: z.ZodOptional<z.ZodBoolean>;
|
|
70
|
+
requireChecks: z.ZodOptional<z.ZodBoolean>;
|
|
71
|
+
}, z.core.$strict>]>>;
|
|
72
|
+
repository: z.ZodOptional<z.ZodObject<{
|
|
73
|
+
resourceId: z.ZodString;
|
|
74
|
+
sourceRef: z.ZodString;
|
|
75
|
+
pullRequestBase: z.ZodString;
|
|
76
|
+
sourceRevision: z.ZodString;
|
|
48
77
|
}, z.core.$strict>>;
|
|
49
78
|
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
50
79
|
workflowInstanceId: z.ZodPipe<z.ZodString, z.ZodTransform<import("./identifiers.js").WorkflowInstanceId, string>>;
|
|
@@ -57,10 +86,18 @@ export declare const delegationResultSchema: z.ZodObject<{
|
|
|
57
86
|
childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
58
87
|
outputs: z.ZodArray<z.ZodObject<{
|
|
59
88
|
name: z.ZodString;
|
|
60
|
-
reference: z.ZodObject<{
|
|
89
|
+
reference: z.ZodUnion<readonly [z.ZodObject<{
|
|
61
90
|
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
62
91
|
revisionId: z.ZodString;
|
|
63
|
-
}, z.core.$strict
|
|
92
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
93
|
+
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
94
|
+
resourceId: z.ZodString;
|
|
95
|
+
repositoryResourceId: z.ZodString;
|
|
96
|
+
revision: z.ZodString;
|
|
97
|
+
branch: z.ZodString;
|
|
98
|
+
pullRequestBase: z.ZodString;
|
|
99
|
+
runId: z.ZodString;
|
|
100
|
+
}, z.core.$strict>]>;
|
|
64
101
|
}, z.core.$strict>>;
|
|
65
102
|
}, z.core.$strict>;
|
|
66
103
|
export type DelegationResult = z.infer<typeof delegationResultSchema>;
|
|
@@ -75,10 +112,18 @@ export declare const delegationJoinSchema: z.ZodObject<{
|
|
|
75
112
|
childWorkItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
76
113
|
outputs: z.ZodArray<z.ZodObject<{
|
|
77
114
|
name: z.ZodString;
|
|
78
|
-
reference: z.ZodObject<{
|
|
115
|
+
reference: z.ZodUnion<readonly [z.ZodObject<{
|
|
79
116
|
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
80
117
|
revisionId: z.ZodString;
|
|
81
|
-
}, z.core.$strict
|
|
118
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
119
|
+
workItemId: z.ZodPipe<z.ZodString, z.ZodTransform<import("../../work/index.js").WorkItemId, string>>;
|
|
120
|
+
resourceId: z.ZodString;
|
|
121
|
+
repositoryResourceId: z.ZodString;
|
|
122
|
+
revision: z.ZodString;
|
|
123
|
+
branch: z.ZodString;
|
|
124
|
+
pullRequestBase: z.ZodString;
|
|
125
|
+
runId: z.ZodString;
|
|
126
|
+
}, z.core.$strict>]>;
|
|
82
127
|
}, z.core.$strict>>;
|
|
83
128
|
}, z.core.$strict>>;
|
|
84
129
|
}, z.core.$strict>;
|