@toolproof-core/lib 1.0.48 → 1.0.50
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/utils/mutableStrategyOverlay.d.ts +2 -8
- package/dist/utils/mutableStrategyOverlay.js +17 -18
- package/dist/utils/stepCreation.js +4 -10
- package/dist/utils/stepParallelization.js +13 -13
- package/dist/utils/strategyCanonicalization.d.ts +2 -2
- package/dist/utils/strategyCanonicalization.js +28 -33
- package/dist/utils/strategyStateResolution.js +1 -1
- package/package.json +1 -1
- package/src/utils/mutableStrategyOverlay.ts +18 -25
- package/src/utils/stepCreation.ts +4 -10
- package/src/utils/stepParallelization.ts +13 -13
- package/src/utils/strategyCanonicalization.ts +30 -35
- package/src/utils/strategyStateResolution.ts +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -4,12 +4,10 @@ export type MutableBranchStepKey = `BRANCH_STEP-${string}`;
|
|
|
4
4
|
export type MutableWhileStepKey = `WHILE_STEP-${string}`;
|
|
5
5
|
export type MutableForStepKey = `FOR_STEP-${string}`;
|
|
6
6
|
export type MutableLoopStepKey = MutableWhileStepKey | MutableForStepKey;
|
|
7
|
-
export type
|
|
8
|
-
export type MutableStepKey = MutableToolStepKey | MutableMacroStepKey;
|
|
7
|
+
export type MutableStepKey = MutableToolStepKey | MutableBranchStepKey | MutableLoopStepKey;
|
|
9
8
|
export type MutableMaterializedResource = MaterializedResource;
|
|
10
9
|
export type MutableToolStepRoleAddress = Omit<ToolStepRoleAddress, 'toolStepPath'> & {
|
|
11
10
|
stepKey: MutableToolStepKey;
|
|
12
|
-
toolStepKey: MutableToolStepKey;
|
|
13
11
|
};
|
|
14
12
|
export type MutableInternalInputPotential = Omit<InternalInputPotential, 'toolStepRoleAddress'> & {
|
|
15
13
|
toolStepRoleAddress: MutableToolStepRoleAddress;
|
|
@@ -20,7 +18,6 @@ export type MutableStrategyStateEntryByRoleName = Partial<Record<RoleName, Mutab
|
|
|
20
18
|
export type MutableStrategyState = Partial<Record<MutableToolStepKey, MutableStrategyStateEntryByRoleName>>;
|
|
21
19
|
export type MutableToolStep = ToolStep & {
|
|
22
20
|
stepKey: MutableToolStepKey;
|
|
23
|
-
toolStepKey: MutableToolStepKey;
|
|
24
21
|
};
|
|
25
22
|
export type MutableCase = Omit<Case, 'when' | 'what'> & {
|
|
26
23
|
when: MutableToolStep;
|
|
@@ -28,17 +25,14 @@ export type MutableCase = Omit<Case, 'when' | 'what'> & {
|
|
|
28
25
|
};
|
|
29
26
|
export type MutableBranchStep = Omit<BranchStep, 'cases'> & {
|
|
30
27
|
stepKey: MutableBranchStepKey;
|
|
31
|
-
macroStepKey: MutableBranchStepKey;
|
|
32
28
|
cases: [MutableCase, ...MutableCase[]];
|
|
33
29
|
};
|
|
34
30
|
export type MutableWhileStep = Omit<WhileStep, 'case'> & {
|
|
35
31
|
stepKey: MutableWhileStepKey;
|
|
36
|
-
macroStepKey: MutableWhileStepKey;
|
|
37
32
|
case: MutableCase;
|
|
38
33
|
};
|
|
39
34
|
export type MutableForStep = Omit<ForStep, 'case'> & {
|
|
40
35
|
stepKey: MutableForStepKey;
|
|
41
|
-
macroStepKey: MutableForStepKey;
|
|
42
36
|
case: MutableCase;
|
|
43
37
|
};
|
|
44
38
|
export type MutableStep = MutableToolStep | MutableBranchStep | MutableWhileStep | MutableForStep;
|
|
@@ -48,7 +42,7 @@ export type MutableStrategy = Omit<Strategy, 'stepsByThreadIndex' | 'strategySta
|
|
|
48
42
|
};
|
|
49
43
|
export declare function getAuthoringSteps(strategy: MutableStrategy): MutableStep[];
|
|
50
44
|
export declare function setAuthoringSteps(strategy: MutableStrategy, steps: MutableStep[]): MutableStrategy;
|
|
51
|
-
export declare function getStrategyStateEntryByRoleName(strategyState: MutableStrategyState,
|
|
45
|
+
export declare function getStrategyStateEntryByRoleName(strategyState: MutableStrategyState, stepKey: MutableToolStepKey): MutableStrategyStateEntryByRoleName | undefined;
|
|
52
46
|
export declare function getStrategyStateEntry(strategyState: MutableStrategyState, toolStepRoleAddress: MutableToolStepRoleAddress): MutableStrategyStateEntry | undefined;
|
|
53
47
|
export declare function setStrategyStateEntry(strategyState: MutableStrategyState, toolStepRoleAddress: MutableToolStepRoleAddress, entry: MutableStrategyStateEntry): MutableStrategyState;
|
|
54
48
|
export declare function clearInputBinding(strategyState: MutableStrategyState, toolStepRoleAddress: MutableToolStepRoleAddress): MutableStrategyState;
|
|
@@ -10,25 +10,25 @@ export function setAuthoringSteps(strategy, steps) {
|
|
|
10
10
|
stepsByThreadIndex: nextStepsByThreadIndex,
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
|
-
export function getStrategyStateEntryByRoleName(strategyState,
|
|
14
|
-
return strategyState[
|
|
13
|
+
export function getStrategyStateEntryByRoleName(strategyState, stepKey) {
|
|
14
|
+
return strategyState[stepKey];
|
|
15
15
|
}
|
|
16
16
|
export function getStrategyStateEntry(strategyState, toolStepRoleAddress) {
|
|
17
|
-
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.
|
|
17
|
+
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.stepKey);
|
|
18
18
|
return inputEntryByRoleName?.[toolStepRoleAddress.roleName];
|
|
19
19
|
}
|
|
20
20
|
export function setStrategyStateEntry(strategyState, toolStepRoleAddress, entry) {
|
|
21
|
-
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.
|
|
21
|
+
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.stepKey) ?? {};
|
|
22
22
|
return {
|
|
23
23
|
...strategyState,
|
|
24
|
-
[toolStepRoleAddress.
|
|
24
|
+
[toolStepRoleAddress.stepKey]: {
|
|
25
25
|
...inputEntryByRoleName,
|
|
26
26
|
[toolStepRoleAddress.roleName]: entry,
|
|
27
27
|
},
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
export function clearInputBinding(strategyState, toolStepRoleAddress) {
|
|
31
|
-
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.
|
|
31
|
+
const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.stepKey);
|
|
32
32
|
if (!inputEntryByRoleName || !(toolStepRoleAddress.roleName in inputEntryByRoleName)) {
|
|
33
33
|
return strategyState;
|
|
34
34
|
}
|
|
@@ -36,12 +36,12 @@ export function clearInputBinding(strategyState, toolStepRoleAddress) {
|
|
|
36
36
|
delete nextInputEntryByRoleName[toolStepRoleAddress.roleName];
|
|
37
37
|
if (Object.keys(nextInputEntryByRoleName).length === 0) {
|
|
38
38
|
const nextStrategyState = { ...strategyState };
|
|
39
|
-
delete nextStrategyState[toolStepRoleAddress.
|
|
39
|
+
delete nextStrategyState[toolStepRoleAddress.stepKey];
|
|
40
40
|
return nextStrategyState;
|
|
41
41
|
}
|
|
42
42
|
return {
|
|
43
43
|
...strategyState,
|
|
44
|
-
[toolStepRoleAddress.
|
|
44
|
+
[toolStepRoleAddress.stepKey]: nextInputEntryByRoleName,
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
export function bindMaterializedResource(strategyState, target, resource) {
|
|
@@ -58,13 +58,13 @@ function getOutputRoleValue(tool, roleName) {
|
|
|
58
58
|
return tool.roleSpec.outputRoleValueByName[roleName];
|
|
59
59
|
}
|
|
60
60
|
export function bindInternalInputPotential(strategyState, targetAddress, sourceAddress, context) {
|
|
61
|
-
const sourceToolStep = context.toolStepByKey.get(sourceAddress.
|
|
61
|
+
const sourceToolStep = context.toolStepByKey.get(sourceAddress.stepKey);
|
|
62
62
|
if (!sourceToolStep) {
|
|
63
|
-
throw new Error(`Source tool step not found for '${sourceAddress.
|
|
63
|
+
throw new Error(`Source tool step not found for '${sourceAddress.stepKey}'.`);
|
|
64
64
|
}
|
|
65
|
-
const targetToolStep = context.toolStepByKey.get(targetAddress.
|
|
65
|
+
const targetToolStep = context.toolStepByKey.get(targetAddress.stepKey);
|
|
66
66
|
if (!targetToolStep) {
|
|
67
|
-
throw new Error(`Target tool step not found for '${targetAddress.
|
|
67
|
+
throw new Error(`Target tool step not found for '${targetAddress.stepKey}'.`);
|
|
68
68
|
}
|
|
69
69
|
const sourceTool = context.toolMap.get(sourceToolStep.toolHandle);
|
|
70
70
|
if (!sourceTool) {
|
|
@@ -76,11 +76,11 @@ export function bindInternalInputPotential(strategyState, targetAddress, sourceA
|
|
|
76
76
|
}
|
|
77
77
|
const targetInputRoleValue = getInputRoleValue(targetTool, targetAddress.roleName);
|
|
78
78
|
if (!targetInputRoleValue) {
|
|
79
|
-
throw new Error(`Target input role not found for (${targetAddress.
|
|
79
|
+
throw new Error(`Target input role not found for (${targetAddress.stepKey}, ${targetAddress.roleName}).`);
|
|
80
80
|
}
|
|
81
81
|
const sourceOutputRoleValue = getOutputRoleValue(sourceTool, sourceAddress.roleName);
|
|
82
82
|
const sourceInputRoleValue = getInputRoleValue(sourceTool, sourceAddress.roleName);
|
|
83
|
-
const sourceInputEntryByRoleName = strategyState[sourceAddress.
|
|
83
|
+
const sourceInputEntryByRoleName = strategyState[sourceAddress.stepKey];
|
|
84
84
|
const sourceInputEntry = sourceInputEntryByRoleName?.[sourceAddress.roleName];
|
|
85
85
|
const sourceRoleType = sourceOutputRoleValue
|
|
86
86
|
? extractResourceTypeHandleFromRoleValue(sourceOutputRoleValue)
|
|
@@ -90,20 +90,19 @@ export function bindInternalInputPotential(strategyState, targetAddress, sourceA
|
|
|
90
90
|
const targetRoleType = extractResourceTypeHandleFromRoleValue(targetInputRoleValue);
|
|
91
91
|
if (!sourceOutputRoleValue && sourceInputRoleValue) {
|
|
92
92
|
if (!sourceInputEntry) {
|
|
93
|
-
throw new Error(`Source input role is not bound for (${sourceAddress.
|
|
93
|
+
throw new Error(`Source input role is not bound for (${sourceAddress.stepKey}, ${sourceAddress.roleName}).`); // ATTENTION: not an ontological invariant, but it might help catch some mistakes early on.
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
if (!sourceOutputRoleValue && !sourceInputRoleValue) {
|
|
97
|
-
throw new Error(`Source role not found for (${sourceAddress.
|
|
97
|
+
throw new Error(`Source role not found for (${sourceAddress.stepKey}, ${sourceAddress.roleName}).`);
|
|
98
98
|
}
|
|
99
99
|
if (!sourceRoleType || !targetRoleType || sourceRoleType !== targetRoleType) {
|
|
100
|
-
throw new Error(`Reference type mismatch between source (${sourceAddress.
|
|
100
|
+
throw new Error(`Reference type mismatch between source (${sourceAddress.stepKey}, ${sourceAddress.roleName}) and target (${targetAddress.stepKey}, ${targetAddress.roleName}).`);
|
|
101
101
|
}
|
|
102
102
|
return setStrategyStateEntry(strategyState, targetAddress, {
|
|
103
103
|
strategyStateEntryKind: 'internalInputPotential',
|
|
104
104
|
toolStepRoleAddress: {
|
|
105
105
|
stepKey: sourceAddress.stepKey,
|
|
106
|
-
toolStepKey: sourceAddress.toolStepKey,
|
|
107
106
|
roleName: sourceAddress.roleName,
|
|
108
107
|
},
|
|
109
108
|
});
|
|
@@ -15,7 +15,6 @@ function getRoleBindingSpec(tool) {
|
|
|
15
15
|
export function createToolStepFromTool(tool) {
|
|
16
16
|
return {
|
|
17
17
|
stepKey: generateIdentifier('ToolStepKey'),
|
|
18
|
-
toolStepKey: generateIdentifier('ToolStepKey'),
|
|
19
18
|
stepKind: CONSTANTS.Enums.StepKind.tool,
|
|
20
19
|
toolHandle: tool.handle,
|
|
21
20
|
roleBindingSpec: getRoleBindingSpec(tool),
|
|
@@ -34,14 +33,12 @@ export function createLoopStepFromCase(stepCase, stepKind) {
|
|
|
34
33
|
if (stepKind === CONSTANTS.Enums.StepKind.for) {
|
|
35
34
|
return {
|
|
36
35
|
stepKey: generateIdentifier('ForStepKey'),
|
|
37
|
-
macroStepKey: generateIdentifier('ForStepKey'),
|
|
38
36
|
stepKind: CONSTANTS.Enums.StepKind.for,
|
|
39
37
|
case: stepCase,
|
|
40
38
|
};
|
|
41
39
|
}
|
|
42
40
|
return {
|
|
43
41
|
stepKey: generateIdentifier('WhileStepKey'),
|
|
44
|
-
macroStepKey: generateIdentifier('WhileStepKey'),
|
|
45
42
|
stepKind: CONSTANTS.Enums.StepKind.while,
|
|
46
43
|
case: stepCase,
|
|
47
44
|
};
|
|
@@ -50,7 +47,6 @@ export function createBranchStepFromCases(cases) {
|
|
|
50
47
|
assertNonEmpty(cases, 'createBranchStepFromCases requires at least one case');
|
|
51
48
|
return {
|
|
52
49
|
stepKey: generateIdentifier('BranchStepKey'),
|
|
53
|
-
macroStepKey: generateIdentifier('BranchStepKey'),
|
|
54
50
|
stepKind: CONSTANTS.Enums.StepKind.branch,
|
|
55
51
|
cases,
|
|
56
52
|
};
|
|
@@ -58,16 +54,15 @@ export function createBranchStepFromCases(cases) {
|
|
|
58
54
|
export function cloneForStep(forStep) {
|
|
59
55
|
return {
|
|
60
56
|
stepKey: generateIdentifier('ForStepKey'),
|
|
61
|
-
macroStepKey: generateIdentifier('ForStepKey'),
|
|
62
57
|
stepKind: CONSTANTS.Enums.StepKind.for,
|
|
63
58
|
case: {
|
|
64
59
|
what: {
|
|
65
60
|
...forStep.case.what,
|
|
66
|
-
|
|
61
|
+
stepKey: generateIdentifier('ToolStepKey'),
|
|
67
62
|
},
|
|
68
63
|
when: {
|
|
69
64
|
...forStep.case.when,
|
|
70
|
-
|
|
65
|
+
stepKey: generateIdentifier('ToolStepKey'),
|
|
71
66
|
},
|
|
72
67
|
},
|
|
73
68
|
};
|
|
@@ -75,16 +70,15 @@ export function cloneForStep(forStep) {
|
|
|
75
70
|
export function cloneWhileStep(whileStep) {
|
|
76
71
|
return {
|
|
77
72
|
stepKey: generateIdentifier('WhileStepKey'),
|
|
78
|
-
macroStepKey: generateIdentifier('WhileStepKey'),
|
|
79
73
|
stepKind: CONSTANTS.Enums.StepKind.while,
|
|
80
74
|
case: {
|
|
81
75
|
what: {
|
|
82
76
|
...whileStep.case.what,
|
|
83
|
-
|
|
77
|
+
stepKey: generateIdentifier('ToolStepKey'),
|
|
84
78
|
},
|
|
85
79
|
when: {
|
|
86
80
|
...whileStep.case.when,
|
|
87
|
-
|
|
81
|
+
stepKey: generateIdentifier('ToolStepKey'),
|
|
88
82
|
},
|
|
89
83
|
},
|
|
90
84
|
};
|
|
@@ -4,17 +4,17 @@ export function getIndependentThreads(steps, strategyState) {
|
|
|
4
4
|
const step = steps[ownerIndex];
|
|
5
5
|
return `steps[${ownerIndex}] stepKind=${step?.stepKind ?? 'unknown'}`;
|
|
6
6
|
};
|
|
7
|
-
const
|
|
7
|
+
const stepKeyToOwner = new Map();
|
|
8
8
|
const toolStepByKey = new Map();
|
|
9
9
|
const addToolStep = (toolStep, ownerIndex) => {
|
|
10
10
|
if (!toolStep)
|
|
11
11
|
return;
|
|
12
|
-
const existingOwner =
|
|
12
|
+
const existingOwner = stepKeyToOwner.get(toolStep.stepKey);
|
|
13
13
|
if (existingOwner !== undefined) {
|
|
14
|
-
throw new Error(`Duplicate
|
|
14
|
+
throw new Error(`Duplicate stepKey '${toolStep.stepKey}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`);
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
toolStepByKey.set(toolStep.
|
|
16
|
+
stepKeyToOwner.set(toolStep.stepKey, ownerIndex);
|
|
17
|
+
toolStepByKey.set(toolStep.stepKey, toolStep);
|
|
18
18
|
};
|
|
19
19
|
steps.forEach((step, ownerIndex) => {
|
|
20
20
|
for (const toolStep of extractToolStepsFromStep(step, { branchCaseOrder: 'what-when' })) {
|
|
@@ -32,22 +32,22 @@ export function getIndependentThreads(steps, strategyState) {
|
|
|
32
32
|
for (let index = 0; index < steps.length; index++) {
|
|
33
33
|
ensureOwner(index);
|
|
34
34
|
}
|
|
35
|
-
for (const [
|
|
35
|
+
for (const [stepKey, ownerIndex] of stepKeyToOwner) {
|
|
36
36
|
ensureOwner(ownerIndex);
|
|
37
|
-
const toolStep = toolStepByKey.get(
|
|
37
|
+
const toolStep = toolStepByKey.get(stepKey);
|
|
38
38
|
const inputBindings = toolStep?.roleBindingSpec?.inputBindings ?? [];
|
|
39
|
-
const bucket = strategyState[
|
|
39
|
+
const bucket = strategyState[stepKey];
|
|
40
40
|
for (const inputRoleId of inputBindings) {
|
|
41
41
|
const entry = bucket?.[inputRoleId];
|
|
42
42
|
if (!entry || entry.strategyStateEntryKind !== 'internalInputPotential')
|
|
43
43
|
continue;
|
|
44
|
-
const
|
|
45
|
-
if (typeof
|
|
46
|
-
throw new Error(`Unresolvable internalInputPotential in toolStep '${
|
|
44
|
+
const producerStepKey = entry.toolStepRoleAddress?.stepKey;
|
|
45
|
+
if (typeof producerStepKey !== 'string') {
|
|
46
|
+
throw new Error(`Unresolvable internalInputPotential in toolStep '${stepKey}' (${getOwnerLabel(ownerIndex)}): missing toolStepRoleAddress.stepKey for role '${inputRoleId}'`);
|
|
47
47
|
}
|
|
48
|
-
const producerOwner =
|
|
48
|
+
const producerOwner = stepKeyToOwner.get(producerStepKey);
|
|
49
49
|
if (producerOwner === undefined) {
|
|
50
|
-
throw new Error(`Unresolvable internalInputPotential in toolStep '${
|
|
50
|
+
throw new Error(`Unresolvable internalInputPotential in toolStep '${stepKey}' (${getOwnerLabel(ownerIndex)}): source stepKey '${producerStepKey}' not found in strategy steps`);
|
|
51
51
|
}
|
|
52
52
|
ensureOwner(producerOwner);
|
|
53
53
|
ownerAdj.get(ownerIndex)?.add(producerOwner);
|
|
@@ -2,5 +2,5 @@ import type { Strategy, ToolStepPath } from '@toolproof-core/genesis';
|
|
|
2
2
|
import type { MutableStrategy, MutableStrategyStateEntryByRoleName, MutableToolStepKey } from './mutableStrategyOverlay.js';
|
|
3
3
|
export declare function strategyToMutableStrategy(strategy: Strategy): MutableStrategy;
|
|
4
4
|
export declare function mutableStrategyToStrategy(mutableStrategy: MutableStrategy): Strategy;
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
5
|
+
export declare function mutableStepKeyToToolStepPath(mutableStrategy: MutableStrategy, stepKey: MutableToolStepKey): ToolStepPath;
|
|
6
|
+
export declare function projectMutableStrategyStateEntriesByStepKeyToCanonicalDelta(mutableStrategy: MutableStrategy, stepKey: MutableToolStepKey, entries: MutableStrategyStateEntryByRoleName): Strategy['strategyState'];
|
|
@@ -2,8 +2,8 @@ import { CONSTANTS } from '../lookups/lookups.js';
|
|
|
2
2
|
import { generateIdentifier } from './identifierGeneration.js';
|
|
3
3
|
function stripMutableCase(stepCase) {
|
|
4
4
|
const { when, what, ...rest } = stepCase;
|
|
5
|
-
const { stepKey: _whenStepKey,
|
|
6
|
-
const { stepKey: _whatStepKey,
|
|
5
|
+
const { stepKey: _whenStepKey, ...canonicalWhen } = when;
|
|
6
|
+
const { stepKey: _whatStepKey, ...canonicalWhat } = what;
|
|
7
7
|
return {
|
|
8
8
|
...rest,
|
|
9
9
|
when: canonicalWhen,
|
|
@@ -15,18 +15,18 @@ function buildExecutionPathMap(strategy) {
|
|
|
15
15
|
const registerStep = (step, pathPrefix) => {
|
|
16
16
|
switch (step.stepKind) {
|
|
17
17
|
case CONSTANTS.Enums.StepKind.tool:
|
|
18
|
-
executionPathByKey.set(step.
|
|
18
|
+
executionPathByKey.set(step.stepKey, `${pathPrefix}/self`);
|
|
19
19
|
return;
|
|
20
20
|
case CONSTANTS.Enums.StepKind.branch:
|
|
21
21
|
step.cases.forEach((stepCase, caseIndex) => {
|
|
22
|
-
executionPathByKey.set(stepCase.when.
|
|
23
|
-
executionPathByKey.set(stepCase.what.
|
|
22
|
+
executionPathByKey.set(stepCase.when.stepKey, `${pathPrefix}/cases/${caseIndex}/when`);
|
|
23
|
+
executionPathByKey.set(stepCase.what.stepKey, `${pathPrefix}/cases/${caseIndex}/what`);
|
|
24
24
|
});
|
|
25
25
|
return;
|
|
26
26
|
case CONSTANTS.Enums.StepKind.while:
|
|
27
27
|
case CONSTANTS.Enums.StepKind.for:
|
|
28
|
-
executionPathByKey.set(step.case.when.
|
|
29
|
-
executionPathByKey.set(step.case.what.
|
|
28
|
+
executionPathByKey.set(step.case.when.stepKey, `${pathPrefix}/case/when`);
|
|
29
|
+
executionPathByKey.set(step.case.what.stepKey, `${pathPrefix}/case/what`);
|
|
30
30
|
return;
|
|
31
31
|
default:
|
|
32
32
|
throw new Error('Unsupported step kind while projecting strategy.');
|
|
@@ -43,9 +43,9 @@ function toCanonicalStrategyStateEntry(entry, executionPathByKey) {
|
|
|
43
43
|
if (entry.strategyStateEntryKind !== 'internalInputPotential') {
|
|
44
44
|
return entry;
|
|
45
45
|
}
|
|
46
|
-
const sourceToolStepPath = executionPathByKey.get(entry.toolStepRoleAddress.
|
|
46
|
+
const sourceToolStepPath = executionPathByKey.get(entry.toolStepRoleAddress.stepKey);
|
|
47
47
|
if (!sourceToolStepPath) {
|
|
48
|
-
throw new Error(`Cannot project strategy state: no execution path found for
|
|
48
|
+
throw new Error(`Cannot project strategy state: no execution path found for stepKey '${entry.toolStepRoleAddress.stepKey}'.`);
|
|
49
49
|
}
|
|
50
50
|
return {
|
|
51
51
|
strategyStateEntryKind: 'internalInputPotential',
|
|
@@ -58,11 +58,11 @@ function toCanonicalStrategyStateEntry(entry, executionPathByKey) {
|
|
|
58
58
|
function toCanonicalStep(step) {
|
|
59
59
|
switch (step.stepKind) {
|
|
60
60
|
case CONSTANTS.Enums.StepKind.tool: {
|
|
61
|
-
const { stepKey: _stepKey,
|
|
61
|
+
const { stepKey: _stepKey, ...canonicalToolStep } = step;
|
|
62
62
|
return canonicalToolStep;
|
|
63
63
|
}
|
|
64
64
|
case CONSTANTS.Enums.StepKind.branch: {
|
|
65
|
-
const { stepKey: _stepKey,
|
|
65
|
+
const { stepKey: _stepKey, cases, ...rest } = step;
|
|
66
66
|
return {
|
|
67
67
|
...rest,
|
|
68
68
|
cases: cases.map(stripMutableCase),
|
|
@@ -70,7 +70,7 @@ function toCanonicalStep(step) {
|
|
|
70
70
|
}
|
|
71
71
|
case CONSTANTS.Enums.StepKind.while:
|
|
72
72
|
case CONSTANTS.Enums.StepKind.for: {
|
|
73
|
-
const { stepKey: _stepKey,
|
|
73
|
+
const { stepKey: _stepKey, case: stepCase, ...rest } = step;
|
|
74
74
|
return {
|
|
75
75
|
...rest,
|
|
76
76
|
case: stripMutableCase(stepCase),
|
|
@@ -92,7 +92,6 @@ function toMutableStrategyStateEntry(entry, executionKeyByPath) {
|
|
|
92
92
|
strategyStateEntryKind: 'internalInputPotential',
|
|
93
93
|
toolStepRoleAddress: {
|
|
94
94
|
stepKey: sourceToolStepKey,
|
|
95
|
-
toolStepKey: sourceToolStepKey,
|
|
96
95
|
roleName: entry.toolStepRoleAddress.roleName,
|
|
97
96
|
},
|
|
98
97
|
};
|
|
@@ -102,7 +101,6 @@ function toMutableToolStep(step) {
|
|
|
102
101
|
return {
|
|
103
102
|
...step,
|
|
104
103
|
stepKey: key,
|
|
105
|
-
toolStepKey: key,
|
|
106
104
|
};
|
|
107
105
|
}
|
|
108
106
|
function toMutableCase(stepCase) {
|
|
@@ -119,7 +117,6 @@ function toMutableBranchStep(step) {
|
|
|
119
117
|
return {
|
|
120
118
|
...rest,
|
|
121
119
|
stepKey: key,
|
|
122
|
-
macroStepKey: key,
|
|
123
120
|
cases: cases.map(toMutableCase),
|
|
124
121
|
};
|
|
125
122
|
}
|
|
@@ -129,7 +126,6 @@ function toMutableWhileStep(step) {
|
|
|
129
126
|
return {
|
|
130
127
|
...rest,
|
|
131
128
|
stepKey: key,
|
|
132
|
-
macroStepKey: key,
|
|
133
129
|
case: toMutableCase(stepCase),
|
|
134
130
|
};
|
|
135
131
|
}
|
|
@@ -139,7 +135,6 @@ function toMutableForStep(step) {
|
|
|
139
135
|
return {
|
|
140
136
|
...rest,
|
|
141
137
|
stepKey: key,
|
|
142
|
-
macroStepKey: key,
|
|
143
138
|
case: toMutableCase(stepCase),
|
|
144
139
|
};
|
|
145
140
|
}
|
|
@@ -160,8 +155,8 @@ function toMutableStep(step) {
|
|
|
160
155
|
function buildExecutionKeyMap(strategy) {
|
|
161
156
|
const executionPathByKey = buildExecutionPathMap(strategy);
|
|
162
157
|
const executionKeyByPath = new Map();
|
|
163
|
-
for (const [
|
|
164
|
-
executionKeyByPath.set(toolStepPath,
|
|
158
|
+
for (const [stepKey, toolStepPath] of executionPathByKey.entries()) {
|
|
159
|
+
executionKeyByPath.set(toolStepPath, stepKey);
|
|
165
160
|
}
|
|
166
161
|
return executionKeyByPath;
|
|
167
162
|
}
|
|
@@ -175,11 +170,11 @@ export function strategyToMutableStrategy(strategy) {
|
|
|
175
170
|
const executionKeyByPath = buildExecutionKeyMap(seededMutableStrategy);
|
|
176
171
|
const strategyState = {};
|
|
177
172
|
for (const [toolStepPath, inputEntryByRoleName] of Object.entries(strategy.strategyState)) {
|
|
178
|
-
const
|
|
179
|
-
if (!
|
|
173
|
+
const stepKey = executionKeyByPath.get(toolStepPath);
|
|
174
|
+
if (!stepKey) {
|
|
180
175
|
throw new Error(`Cannot deproject strategy state: no mutable key found for toolStepPath '${toolStepPath}'.`);
|
|
181
176
|
}
|
|
182
|
-
strategyState[
|
|
177
|
+
strategyState[stepKey] = Object.fromEntries(Object.entries(inputEntryByRoleName).map(([roleName, entry]) => {
|
|
183
178
|
if (entry === undefined) {
|
|
184
179
|
throw new Error(`Cannot deproject strategy state: role '${roleName}' on toolStepPath '${toolStepPath}' is explicitly undefined.`);
|
|
185
180
|
}
|
|
@@ -198,17 +193,17 @@ export function strategyToMutableStrategy(strategy) {
|
|
|
198
193
|
export function mutableStrategyToStrategy(mutableStrategy) {
|
|
199
194
|
const executionPathByKey = buildExecutionPathMap(mutableStrategy);
|
|
200
195
|
const strategyState = {};
|
|
201
|
-
for (const [
|
|
196
|
+
for (const [stepKey, inputEntryByRoleName] of Object.entries(mutableStrategy.strategyState)) {
|
|
202
197
|
if (!inputEntryByRoleName) {
|
|
203
198
|
continue;
|
|
204
199
|
}
|
|
205
|
-
const toolStepPath = executionPathByKey.get(
|
|
200
|
+
const toolStepPath = executionPathByKey.get(stepKey);
|
|
206
201
|
if (!toolStepPath) {
|
|
207
|
-
throw new Error(`Cannot project strategy state: no execution path found for
|
|
202
|
+
throw new Error(`Cannot project strategy state: no execution path found for stepKey '${stepKey}'.`);
|
|
208
203
|
}
|
|
209
204
|
strategyState[toolStepPath] = Object.fromEntries(Object.entries(inputEntryByRoleName).map(([roleName, entry]) => {
|
|
210
205
|
if (entry === undefined) {
|
|
211
|
-
throw new Error(`Cannot project strategy state: role '${roleName}' on
|
|
206
|
+
throw new Error(`Cannot project strategy state: role '${roleName}' on stepKey '${stepKey}' is explicitly undefined.`);
|
|
212
207
|
}
|
|
213
208
|
return [
|
|
214
209
|
roleName,
|
|
@@ -222,25 +217,25 @@ export function mutableStrategyToStrategy(mutableStrategy) {
|
|
|
222
217
|
strategyState,
|
|
223
218
|
};
|
|
224
219
|
}
|
|
225
|
-
export function
|
|
220
|
+
export function mutableStepKeyToToolStepPath(mutableStrategy, stepKey) {
|
|
226
221
|
const executionPathByKey = buildExecutionPathMap(mutableStrategy);
|
|
227
|
-
const toolStepPath = executionPathByKey.get(
|
|
222
|
+
const toolStepPath = executionPathByKey.get(stepKey);
|
|
228
223
|
if (!toolStepPath) {
|
|
229
|
-
throw new Error(`Cannot project toolStepPath: no execution path found for
|
|
224
|
+
throw new Error(`Cannot project toolStepPath: no execution path found for stepKey '${stepKey}'.`);
|
|
230
225
|
}
|
|
231
226
|
return toolStepPath;
|
|
232
227
|
}
|
|
233
|
-
export function
|
|
228
|
+
export function projectMutableStrategyStateEntriesByStepKeyToCanonicalDelta(mutableStrategy, stepKey, entries) {
|
|
234
229
|
const executionPathByKey = buildExecutionPathMap(mutableStrategy);
|
|
235
|
-
const toolStepPath = executionPathByKey.get(
|
|
230
|
+
const toolStepPath = executionPathByKey.get(stepKey);
|
|
236
231
|
if (!toolStepPath) {
|
|
237
|
-
throw new Error(`Cannot project strategy state delta: no execution path found for
|
|
232
|
+
throw new Error(`Cannot project strategy state delta: no execution path found for stepKey '${stepKey}'.`);
|
|
238
233
|
}
|
|
239
234
|
const canonicalEntries = Object.fromEntries(Object.entries(entries)
|
|
240
235
|
.filter(([, entry]) => entry !== undefined)
|
|
241
236
|
.map(([roleName, entry]) => {
|
|
242
237
|
if (entry === undefined) {
|
|
243
|
-
throw new Error(`Cannot project strategy state delta: role '${roleName}' on
|
|
238
|
+
throw new Error(`Cannot project strategy state delta: role '${roleName}' on stepKey '${stepKey}' is explicitly undefined.`);
|
|
244
239
|
}
|
|
245
240
|
return [
|
|
246
241
|
roleName,
|
|
@@ -6,7 +6,7 @@ export function resolveStrategyStateChain(strategyState, start, opts) {
|
|
|
6
6
|
let current = start;
|
|
7
7
|
for (let depth = 0; depth <= maxDepth; depth++) {
|
|
8
8
|
path.push(current);
|
|
9
|
-
const visitKey = `${current.
|
|
9
|
+
const visitKey = `${current.stepKey}::${current.roleName}`;
|
|
10
10
|
if (visited.has(visitKey)) {
|
|
11
11
|
return { status: 'unresolved', reason: 'cycle', path };
|
|
12
12
|
}
|