@toolproof-core/lib 1.0.46 → 1.0.48

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.
@@ -16,97 +16,87 @@ import type {
16
16
  } from '@toolproof-core/genesis';
17
17
  import { extractResourceTypeHandleFromRoleValue } from './strategyExtraction.js';
18
18
 
19
- export type MutableToolStepKey = string;
20
- export type MutableBranchStepKey = string;
21
- export type MutableWhileStepKey = string;
22
- export type MutableForStepKey = string;
19
+ export type MutableToolStepKey = `TOOL_STEP-${string}`;
20
+ export type MutableBranchStepKey = `BRANCH_STEP-${string}`;
21
+ export type MutableWhileStepKey = `WHILE_STEP-${string}`;
22
+ export type MutableForStepKey = `FOR_STEP-${string}`;
23
23
  export type MutableLoopStepKey = MutableWhileStepKey | MutableForStepKey;
24
24
  export type MutableMacroStepKey = MutableBranchStepKey | MutableLoopStepKey;
25
+ export type MutableStepKey = MutableToolStepKey | MutableMacroStepKey;
25
26
 
26
27
  export type MutableMaterializedResource = MaterializedResource;
27
28
 
28
- export type MutableToolStepRoleAddress<TKey extends string = MutableToolStepKey> =
29
+ export type MutableToolStepRoleAddress =
29
30
  Omit<ToolStepRoleAddress, 'toolStepPath'> & {
30
- toolStepKey: TKey;
31
+ stepKey: MutableToolStepKey;
32
+ toolStepKey: MutableToolStepKey;
31
33
  };
32
34
 
33
- export type MutableInternalInputPotential<TKey extends string = MutableToolStepKey> =
35
+ export type MutableInternalInputPotential =
34
36
  Omit<InternalInputPotential, 'toolStepRoleAddress'> & {
35
- toolStepRoleAddress: MutableToolStepRoleAddress<TKey>;
37
+ toolStepRoleAddress: MutableToolStepRoleAddress;
36
38
  };
37
39
 
38
40
  export type MutableExternalInputPotential = ExternalInputPotential;
39
41
 
40
- export type MutableStrategyStateEntry<TKey extends string = MutableToolStepKey> =
42
+ export type MutableStrategyStateEntry =
41
43
  | MutableMaterializedResource
42
- | MutableInternalInputPotential<TKey>
44
+ | MutableInternalInputPotential
43
45
  | MutableExternalInputPotential;
44
46
 
45
- export type MutableStrategyStateEntryByRoleName<
46
- TKey extends string = MutableToolStepKey,
47
- > = Partial<Record<RoleName, MutableStrategyStateEntry<TKey>>>;
47
+ export type MutableStrategyStateEntryByRoleName = Partial<Record<RoleName, MutableStrategyStateEntry>>;
48
48
 
49
- export type MutableStrategyState<
50
- TKey extends string = MutableToolStepKey,
51
- > = Partial<Record<TKey, MutableStrategyStateEntryByRoleName<TKey>>>;
49
+ export type MutableStrategyState = Partial<Record<MutableToolStepKey, MutableStrategyStateEntryByRoleName>>;
52
50
 
53
- export type MutableToolStep<TKey extends string = MutableToolStepKey> = ToolStep & {
54
- toolStepKey: TKey;
51
+ export type MutableToolStep = ToolStep & {
52
+ stepKey: MutableToolStepKey;
53
+ toolStepKey: MutableToolStepKey;
55
54
  };
56
55
 
57
- export type MutableCase<TKey extends string = MutableToolStepKey> = Omit<Case, 'when' | 'what'> & {
58
- when: MutableToolStep<TKey>;
59
- what: MutableToolStep<TKey>;
56
+ export type MutableCase = Omit<Case, 'when' | 'what'> & {
57
+ when: MutableToolStep;
58
+ what: MutableToolStep;
60
59
  };
61
60
 
62
- export type MutableBranchStep<TKey extends string = MutableToolStepKey> = Omit<BranchStep, 'cases'> & {
61
+ export type MutableBranchStep = Omit<BranchStep, 'cases'> & {
62
+ stepKey: MutableBranchStepKey;
63
63
  macroStepKey: MutableBranchStepKey;
64
- cases: [MutableCase<TKey>, ...MutableCase<TKey>[]];
64
+ cases: [MutableCase, ...MutableCase[]];
65
65
  };
66
66
 
67
- export type MutableWhileStep<TKey extends string = MutableToolStepKey> = Omit<WhileStep, 'case'> & {
67
+ export type MutableWhileStep = Omit<WhileStep, 'case'> & {
68
+ stepKey: MutableWhileStepKey;
68
69
  macroStepKey: MutableWhileStepKey;
69
- case: MutableCase<TKey>;
70
+ case: MutableCase;
70
71
  };
71
72
 
72
- export type MutableForStep<TKey extends string = MutableToolStepKey> = Omit<ForStep, 'case'> & {
73
+ export type MutableForStep = Omit<ForStep, 'case'> & {
74
+ stepKey: MutableForStepKey;
73
75
  macroStepKey: MutableForStepKey;
74
- case: MutableCase<TKey>;
76
+ case: MutableCase;
75
77
  };
76
78
 
77
- export type MutableStep<TKey extends string = MutableToolStepKey> =
78
- | MutableToolStep<TKey>
79
- | MutableBranchStep<TKey>
80
- | MutableWhileStep<TKey>
81
- | MutableForStep<TKey>;
82
-
83
- export type MutableStrategy<
84
- TKey extends string = MutableToolStepKey,
85
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
86
- TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
87
- > = Omit<Strategy, 'stepsByThreadIndex' | 'strategyState'> & {
88
- stepsByThreadIndex: TStep[][];
89
- strategyState: TState;
79
+ export type MutableStep =
80
+ | MutableToolStep
81
+ | MutableBranchStep
82
+ | MutableWhileStep
83
+ | MutableForStep;
84
+
85
+ export type MutableStrategy = Omit<Strategy, 'stepsByThreadIndex' | 'strategyState'> & {
86
+ stepsByThreadIndex: MutableStep[][];
87
+ strategyState: MutableStrategyState;
90
88
  };
91
89
 
92
- export function getAuthoringSteps<
93
- TKey extends string = MutableToolStepKey,
94
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
95
- TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
96
- >(
97
- strategy: MutableStrategy<TKey, TStep, TState>,
98
- ): TStep[] {
90
+ export function getAuthoringSteps(
91
+ strategy: MutableStrategy,
92
+ ): MutableStep[] {
99
93
  return strategy.stepsByThreadIndex[0] ?? [];
100
94
  }
101
95
 
102
- export function setAuthoringSteps< // ATTENTION
103
- TKey extends string = MutableToolStepKey,
104
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
105
- TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
106
- >(
107
- strategy: MutableStrategy<TKey, TStep, TState>,
108
- steps: TStep[],
109
- ): MutableStrategy<TKey, TStep, TState> {
96
+ export function setAuthoringSteps(
97
+ strategy: MutableStrategy,
98
+ steps: MutableStep[],
99
+ ): MutableStrategy {
110
100
  const nextStepsByThreadIndex = [...strategy.stepsByThreadIndex];
111
101
  nextStepsByThreadIndex[0] = steps;
112
102
 
@@ -116,26 +106,26 @@ export function setAuthoringSteps< // ATTENTION
116
106
  };
117
107
  }
118
108
 
119
- export function getStrategyStateEntryByRoleName<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
120
- strategyState: TStrategyState,
121
- toolStepKey: TKey,
122
- ): MutableStrategyStateEntryByRoleName<TKey> | undefined {
109
+ export function getStrategyStateEntryByRoleName(
110
+ strategyState: MutableStrategyState,
111
+ toolStepKey: MutableToolStepKey,
112
+ ): MutableStrategyStateEntryByRoleName | undefined {
123
113
  return strategyState[toolStepKey];
124
114
  }
125
115
 
126
- export function getStrategyStateEntry<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
127
- strategyState: TStrategyState,
128
- toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
129
- ): MutableStrategyStateEntry<TKey> | undefined {
116
+ export function getStrategyStateEntry(
117
+ strategyState: MutableStrategyState,
118
+ toolStepRoleAddress: MutableToolStepRoleAddress,
119
+ ): MutableStrategyStateEntry | undefined {
130
120
  const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.toolStepKey);
131
121
  return inputEntryByRoleName?.[toolStepRoleAddress.roleName];
132
122
  }
133
123
 
134
- export function setStrategyStateEntry<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
135
- strategyState: TStrategyState,
136
- toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
137
- entry: MutableStrategyStateEntry<TKey>,
138
- ): TStrategyState {
124
+ export function setStrategyStateEntry(
125
+ strategyState: MutableStrategyState,
126
+ toolStepRoleAddress: MutableToolStepRoleAddress,
127
+ entry: MutableStrategyStateEntry,
128
+ ): MutableStrategyState {
139
129
  const inputEntryByRoleName = getStrategyStateEntryByRoleName(strategyState, toolStepRoleAddress.toolStepKey) ?? {};
140
130
 
141
131
  return {
@@ -147,13 +137,10 @@ export function setStrategyStateEntry<TKey extends string, TStrategyState extend
147
137
  };
148
138
  }
149
139
 
150
- export function clearInputBinding<
151
- TKey extends string,
152
- TStrategyState extends MutableStrategyState<TKey>
153
- >(
154
- strategyState: TStrategyState,
155
- toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
156
- ): TStrategyState {
140
+ export function clearInputBinding(
141
+ strategyState: MutableStrategyState,
142
+ toolStepRoleAddress: MutableToolStepRoleAddress,
143
+ ): MutableStrategyState {
157
144
  const inputEntryByRoleName =
158
145
  getStrategyStateEntryByRoleName(
159
146
  strategyState,
@@ -170,7 +157,7 @@ export function clearInputBinding<
170
157
  if (Object.keys(nextInputEntryByRoleName).length === 0) {
171
158
  const nextStrategyState = { ...strategyState };
172
159
  delete nextStrategyState[toolStepRoleAddress.toolStepKey];
173
- return nextStrategyState as TStrategyState;
160
+ return nextStrategyState;
174
161
  }
175
162
 
176
163
  return {
@@ -179,11 +166,11 @@ export function clearInputBinding<
179
166
  };
180
167
  }
181
168
 
182
- export function bindMaterializedResource<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
183
- strategyState: TStrategyState,
184
- target: MutableToolStepRoleAddress<TKey>,
169
+ export function bindMaterializedResource(
170
+ strategyState: MutableStrategyState,
171
+ target: MutableToolStepRoleAddress,
185
172
  resource: Resource,
186
- ): TStrategyState { // ATTENTION: misleading name since it also materializes the Resource
173
+ ): MutableStrategyState { // ATTENTION: misleading name since it also materializes the Resource
187
174
  const mutableMaterializedResource: MutableMaterializedResource = {
188
175
  ...resource,
189
176
  strategyStateEntryKind: 'materializedResource',
@@ -200,15 +187,15 @@ function getOutputRoleValue(tool: Tool, roleName: RoleName) {
200
187
  return tool.roleSpec.outputRoleValueByName[roleName];
201
188
  }
202
189
 
203
- export function bindInternalInputPotential<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
204
- strategyState: TStrategyState,
205
- targetAddress: MutableToolStepRoleAddress<TKey>,
206
- sourceAddress: MutableToolStepRoleAddress<TKey>,
190
+ export function bindInternalInputPotential(
191
+ strategyState: MutableStrategyState,
192
+ targetAddress: MutableToolStepRoleAddress,
193
+ sourceAddress: MutableToolStepRoleAddress,
207
194
  context: {
208
- toolStepByKey: ReadonlyMap<TKey, MutableToolStep<TKey>>;
195
+ toolStepByKey: ReadonlyMap<MutableToolStepKey, MutableToolStep>;
209
196
  toolMap: ReadonlyMap<ToolHandle, Tool>;
210
197
  }
211
- ): TStrategyState {
198
+ ): MutableStrategyState {
212
199
 
213
200
  const sourceToolStep = context.toolStepByKey.get(sourceAddress.toolStepKey);
214
201
  if (!sourceToolStep) {
@@ -268,16 +255,17 @@ export function bindInternalInputPotential<TKey extends string, TStrategyState e
268
255
  return setStrategyStateEntry(strategyState, targetAddress, {
269
256
  strategyStateEntryKind: 'internalInputPotential',
270
257
  toolStepRoleAddress: {
258
+ stepKey: sourceAddress.stepKey,
271
259
  toolStepKey: sourceAddress.toolStepKey,
272
260
  roleName: sourceAddress.roleName,
273
261
  },
274
262
  });
275
263
  }
276
264
 
277
- export function bindExternalInputPotential<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
278
- strategyState: TStrategyState,
279
- target: MutableToolStepRoleAddress<TKey>,
280
- ): TStrategyState {
265
+ export function bindExternalInputPotential(
266
+ strategyState: MutableStrategyState,
267
+ target: MutableToolStepRoleAddress,
268
+ ): MutableStrategyState {
281
269
  const mutableExternalInputPotential: MutableExternalInputPotential = {
282
270
  strategyStateEntryKind: 'externalInputPotential',
283
271
  };
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  Tool,
3
+ WhenInputRoleBindingSpec,
3
4
  } from '@toolproof-core/genesis';
4
5
  import type {
5
6
  MutableToolStep,
@@ -30,6 +31,7 @@ export function createToolStepFromTool(
30
31
  tool: Tool,
31
32
  ): MutableToolStep {
32
33
  return {
34
+ stepKey: generateIdentifier('ToolStepKey'),
33
35
  toolStepKey: generateIdentifier('ToolStepKey'),
34
36
  stepKind: CONSTANTS.Enums.StepKind.tool,
35
37
  toolHandle: tool.handle,
@@ -37,6 +39,16 @@ export function createToolStepFromTool(
37
39
  };
38
40
  }
39
41
 
42
+ export function createLoopSlotWhenToolStepFromTool(
43
+ tool: Tool,
44
+ whenInputRoleBindingSpec: WhenInputRoleBindingSpec,
45
+ ): MutableToolStep {
46
+ return {
47
+ ...createToolStepFromTool(tool),
48
+ whenInputRoleBindingSpec,
49
+ };
50
+ }
51
+
40
52
  export function createCaseFromToolStepPair(
41
53
  what: MutableToolStep,
42
54
  when: MutableToolStep,
@@ -50,6 +62,7 @@ export function createLoopStepFromCase(
50
62
  ): MutableForStep | MutableWhileStep {
51
63
  if (stepKind === CONSTANTS.Enums.StepKind.for) {
52
64
  return {
65
+ stepKey: generateIdentifier('ForStepKey'),
53
66
  macroStepKey: generateIdentifier('ForStepKey'),
54
67
  stepKind: CONSTANTS.Enums.StepKind.for,
55
68
  case: stepCase,
@@ -57,6 +70,7 @@ export function createLoopStepFromCase(
57
70
  }
58
71
 
59
72
  return {
73
+ stepKey: generateIdentifier('WhileStepKey'),
60
74
  macroStepKey: generateIdentifier('WhileStepKey'),
61
75
  stepKind: CONSTANTS.Enums.StepKind.while,
62
76
  case: stepCase,
@@ -69,6 +83,7 @@ export function createBranchStepFromCases(
69
83
  assertNonEmpty(cases, 'createBranchStepFromCases requires at least one case');
70
84
 
71
85
  return {
86
+ stepKey: generateIdentifier('BranchStepKey'),
72
87
  macroStepKey: generateIdentifier('BranchStepKey'),
73
88
  stepKind: CONSTANTS.Enums.StepKind.branch,
74
89
  cases,
@@ -79,6 +94,7 @@ export function cloneForStep(
79
94
  forStep: MutableForStep,
80
95
  ): MutableForStep {
81
96
  return {
97
+ stepKey: generateIdentifier('ForStepKey'),
82
98
  macroStepKey: generateIdentifier('ForStepKey'),
83
99
  stepKind: CONSTANTS.Enums.StepKind.for,
84
100
  case: {
@@ -98,6 +114,7 @@ export function cloneWhileStep(
98
114
  whileStep: MutableWhileStep,
99
115
  ): MutableWhileStep {
100
116
  return {
117
+ stepKey: generateIdentifier('WhileStepKey'),
101
118
  macroStepKey: generateIdentifier('WhileStepKey'),
102
119
  stepKind: CONSTANTS.Enums.StepKind.while,
103
120
  case: {
@@ -1,63 +1,56 @@
1
1
  import { CONSTANTS } from '../lookups/lookups.js';
2
2
  import type {
3
- MutableBranchStep,
4
- MutableForStep,
5
3
  MutableStep,
6
4
  MutableToolStep,
7
- MutableToolStepKey,
8
- MutableWhileStep,
9
5
  } from './mutableStrategyOverlay.js';
10
6
 
11
- export type StepExpansionOrder = {
7
+ export type StepExpansionOrder = { // ATTENTION: redundant?
12
8
  branchCaseOrder?: 'when-what' | 'what-when';
13
9
  loopCaseOrder?: 'what-when' | 'when-what';
14
10
  };
15
11
 
16
- export function extractToolStepsFromStep<
17
- TKey extends string = MutableToolStepKey,
18
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
19
- >(
20
- step: TStep,
12
+ export function extractToolStepsFromStep(
13
+ step: MutableStep,
21
14
  order: StepExpansionOrder = {},
22
- ): MutableToolStep<TKey>[] {
15
+ ): MutableToolStep[] {
23
16
  const {
24
17
  branchCaseOrder = 'when-what',
25
18
  loopCaseOrder = 'what-when',
26
19
  } = order;
27
20
 
28
21
  if (step.stepKind === CONSTANTS.Enums.StepKind.tool) {
29
- return [step as MutableToolStep<TKey>];
22
+ return [step];
30
23
  }
31
24
 
32
25
  if (step.stepKind === CONSTANTS.Enums.StepKind.branch) {
33
- const branch = step as MutableBranchStep<TKey>;
34
- const cases = branch.cases ?? [];
26
+ const branchStep = step;
27
+ const cases = branchStep.cases ?? [];
35
28
  return cases.flatMap((stepCase) => {
36
29
  if (branchCaseOrder === 'what-when') {
37
30
  return [stepCase.what, stepCase.when];
38
31
  }
39
32
 
40
33
  return [stepCase.when, stepCase.what];
41
- }).filter(Boolean) as MutableToolStep<TKey>[];
34
+ });
42
35
  }
43
36
 
44
37
  if (step.stepKind === CONSTANTS.Enums.StepKind.while) {
45
- const loop = step as MutableWhileStep<TKey>;
38
+ const whileStep = step;
46
39
  if (loopCaseOrder === 'when-what') {
47
- return [loop.case.when, loop.case.what].filter(Boolean) as MutableToolStep<TKey>[];
40
+ return [whileStep.case.when, whileStep.case.what];
48
41
  }
49
42
 
50
- return [loop.case.what, loop.case.when].filter(Boolean) as MutableToolStep<TKey>[];
43
+ return [whileStep.case.what, whileStep.case.when];
51
44
  }
52
45
 
53
46
  if (step.stepKind === CONSTANTS.Enums.StepKind.for) {
54
- const loop = step as MutableForStep<TKey>;
47
+ const forStep = step;
55
48
  if (loopCaseOrder === 'when-what') {
56
- return [loop.case.when, loop.case.what].filter(Boolean) as MutableToolStep<TKey>[];
49
+ return [forStep.case.when, forStep.case.what];
57
50
  }
58
51
 
59
- return [loop.case.what, loop.case.when].filter(Boolean) as MutableToolStep<TKey>[];
52
+ return [forStep.case.what, forStep.case.when];
60
53
  }
61
54
 
62
- return [] as MutableToolStep<TKey>[];
55
+ return [];
63
56
  }
@@ -6,13 +6,10 @@ import type {
6
6
  } from './mutableStrategyOverlay.js';
7
7
  import { extractToolStepsFromStep } from './stepExpansion.js';
8
8
 
9
- export function getIndependentThreads<
10
- TKey extends string = MutableToolStepKey,
11
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
12
- >(
13
- steps: TStep[],
14
- strategyState: MutableStrategyState<TKey>,
15
- ): TStep[][] {
9
+ export function getIndependentThreads(
10
+ steps: MutableStep[],
11
+ strategyState: MutableStrategyState,
12
+ ): MutableStep[][] {
16
13
  type OwnerIndex = number;
17
14
 
18
15
  const getOwnerLabel = (ownerIndex: OwnerIndex) => {
@@ -20,11 +17,11 @@ export function getIndependentThreads<
20
17
  return `steps[${ownerIndex}] stepKind=${step?.stepKind ?? 'unknown'}`;
21
18
  };
22
19
 
23
- const toolStepKeyToOwner = new Map<TKey, OwnerIndex>();
24
- const toolStepByKey = new Map<TKey, MutableToolStep<TKey>>();
20
+ const toolStepKeyToOwner = new Map<MutableToolStepKey, OwnerIndex>();
21
+ const toolStepByKey = new Map<MutableToolStepKey, MutableToolStep>();
25
22
 
26
23
  const addToolStep = (
27
- toolStep: MutableToolStep<TKey> | undefined,
24
+ toolStep: MutableToolStep | undefined,
28
25
  ownerIndex: OwnerIndex,
29
26
  ) => {
30
27
  if (!toolStep) return;
@@ -41,7 +38,7 @@ export function getIndependentThreads<
41
38
  };
42
39
 
43
40
  steps.forEach((step, ownerIndex) => {
44
- for (const toolStep of extractToolStepsFromStep<TKey, TStep>(step, { branchCaseOrder: 'what-when' })) {
41
+ for (const toolStep of extractToolStepsFromStep(step, { branchCaseOrder: 'what-when' })) {
45
42
  addToolStep(toolStep, ownerIndex);
46
43
  }
47
44
  });
@@ -76,7 +73,7 @@ export function getIndependentThreads<
76
73
  );
77
74
  }
78
75
 
79
- const producerOwner = toolStepKeyToOwner.get(producerToolStepKey as TKey);
76
+ const producerOwner = toolStepKeyToOwner.get(producerToolStepKey);
80
77
  if (producerOwner === undefined) {
81
78
  throw new Error(
82
79
  `Unresolvable internalInputPotential in toolStep '${toolStepKey}' (${getOwnerLabel(ownerIndex)}): source toolStepKey '${producerToolStepKey}' not found in strategy steps`
@@ -4,8 +4,8 @@ import { toExecutionMutableStrategy } from './strategyThreading.js';
4
4
  import { mutableStrategyToStrategy } from './strategyCanonicalization.js';
5
5
 
6
6
 
7
- export function composeStrategyForExecution<TKey extends string>(
8
- mutableStrategy: MutableStrategy<TKey>,
7
+ export function composeStrategyForExecution(
8
+ mutableStrategy: MutableStrategy,
9
9
  ): Strategy {
10
10
  const executionStrategy =
11
11
  toExecutionMutableStrategy(mutableStrategy);
@@ -23,6 +23,7 @@ import type {
23
23
  MutableStep,
24
24
  MutableStrategyStateEntryByRoleName,
25
25
  MutableStrategyStateEntry,
26
+ MutableToolStep,
26
27
  MutableToolStepKey,
27
28
  MutableWhileStep,
28
29
  } from './mutableStrategyOverlay.js';
@@ -30,8 +31,8 @@ import { generateIdentifier } from './identifierGeneration.js';
30
31
 
31
32
  function stripMutableCase(stepCase: MutableCase): Case {
32
33
  const { when, what, ...rest } = stepCase;
33
- const { toolStepKey: _whenToolStepKey, ...canonicalWhen } = when;
34
- const { toolStepKey: _whatToolStepKey, ...canonicalWhat } = what;
34
+ const { stepKey: _whenStepKey, toolStepKey: _whenToolStepKey, ...canonicalWhen } = when;
35
+ const { stepKey: _whatStepKey, toolStepKey: _whatToolStepKey, ...canonicalWhat } = what;
35
36
 
36
37
  return {
37
38
  ...rest,
@@ -98,11 +99,11 @@ function toCanonicalStrategyStateEntry(
98
99
  function toCanonicalStep(step: MutableStep): Step {
99
100
  switch (step.stepKind) {
100
101
  case CONSTANTS.Enums.StepKind.tool: {
101
- const { toolStepKey: _toolStepKey, ...canonicalToolStep } = step;
102
+ const { stepKey: _stepKey, toolStepKey: _toolStepKey, ...canonicalToolStep } = step;
102
103
  return canonicalToolStep;
103
104
  }
104
105
  case CONSTANTS.Enums.StepKind.branch: {
105
- const { macroStepKey: _macroStepKey, cases, ...rest } = step;
106
+ const { stepKey: _stepKey, macroStepKey: _macroStepKey, cases, ...rest } = step;
106
107
  return {
107
108
  ...rest,
108
109
  cases: cases.map(stripMutableCase) as [Case, ...Case[]],
@@ -110,7 +111,7 @@ function toCanonicalStep(step: MutableStep): Step {
110
111
  }
111
112
  case CONSTANTS.Enums.StepKind.while:
112
113
  case CONSTANTS.Enums.StepKind.for: {
113
- const { macroStepKey: _macroStepKey, case: stepCase, ...rest } = step;
114
+ const { stepKey: _stepKey, macroStepKey: _macroStepKey, case: stepCase, ...rest } = step;
114
115
  return {
115
116
  ...rest,
116
117
  case: stripMutableCase(stepCase),
@@ -137,16 +138,19 @@ function toMutableStrategyStateEntry(
137
138
  return {
138
139
  strategyStateEntryKind: 'internalInputPotential',
139
140
  toolStepRoleAddress: {
141
+ stepKey: sourceToolStepKey,
140
142
  toolStepKey: sourceToolStepKey,
141
143
  roleName: entry.toolStepRoleAddress.roleName,
142
144
  },
143
145
  } satisfies MutableInternalInputPotential;
144
146
  }
145
147
 
146
- function toMutableToolStep(step: ToolStep) {
148
+ function toMutableToolStep(step: ToolStep): MutableToolStep {
149
+ const key = generateIdentifier('ToolStepKey');
147
150
  return {
148
151
  ...step,
149
- toolStepKey: generateIdentifier('ToolStepKey'),
152
+ stepKey: key,
153
+ toolStepKey: key,
150
154
  };
151
155
  }
152
156
 
@@ -163,9 +167,11 @@ function toMutableCase(stepCase: Case): MutableCase {
163
167
  function toMutableBranchStep(step: BranchStep): MutableBranchStep {
164
168
  const { cases, ...rest } = step;
165
169
 
170
+ const key = generateIdentifier('BranchStepKey');
166
171
  return {
167
172
  ...rest,
168
- macroStepKey: generateIdentifier('BranchStepKey'),
173
+ stepKey: key,
174
+ macroStepKey: key,
169
175
  cases: cases.map(toMutableCase) as [MutableCase, ...MutableCase[]],
170
176
  };
171
177
  }
@@ -173,9 +179,11 @@ function toMutableBranchStep(step: BranchStep): MutableBranchStep {
173
179
  function toMutableWhileStep(step: WhileStep): MutableWhileStep {
174
180
  const { case: stepCase, ...rest } = step;
175
181
 
182
+ const key = generateIdentifier('WhileStepKey');
176
183
  return {
177
184
  ...rest,
178
- macroStepKey: generateIdentifier('WhileStepKey'),
185
+ stepKey: key,
186
+ macroStepKey: key,
179
187
  case: toMutableCase(stepCase),
180
188
  };
181
189
  }
@@ -183,9 +191,11 @@ function toMutableWhileStep(step: WhileStep): MutableWhileStep {
183
191
  function toMutableForStep(step: ForStep): MutableForStep {
184
192
  const { case: stepCase, ...rest } = step;
185
193
 
194
+ const key = generateIdentifier('ForStepKey');
186
195
  return {
187
196
  ...rest,
188
- macroStepKey: generateIdentifier('ForStepKey'),
197
+ stepKey: key,
198
+ macroStepKey: key,
189
199
  case: toMutableCase(stepCase),
190
200
  };
191
201
  }
@@ -266,7 +276,7 @@ export function mutableStrategyToStrategy(mutableStrategy: MutableStrategy): Str
266
276
  continue;
267
277
  }
268
278
 
269
- const toolStepPath = executionPathByKey.get(toolStepKey);
279
+ const toolStepPath = executionPathByKey.get(toolStepKey as MutableToolStepKey);
270
280
  if (!toolStepPath) {
271
281
  throw new Error(`Cannot project strategy state: no execution path found for toolStepKey '${toolStepKey}'.`);
272
282
  }
@@ -1,32 +1,29 @@
1
1
  import type {
2
2
  MutableMaterializedResource,
3
3
  MutableExternalInputPotential,
4
- MutableInternalInputPotential,
5
4
  MutableStrategyState,
6
- MutableStrategyStateEntry,
7
- MutableToolStepKey,
8
5
  MutableToolStepRoleAddress,
9
6
  } from './mutableStrategyOverlay.js';
10
7
  import { getStrategyStateEntry } from './mutableStrategyOverlay.js';
11
8
 
12
- export type ResolveResult<TKey extends string = MutableToolStepKey> =
13
- | { status: 'materializedResource'; entry: MutableMaterializedResource; path: MutableToolStepRoleAddress<TKey>[] }
14
- | { status: 'externalInputPotential'; entry: MutableExternalInputPotential; path: MutableToolStepRoleAddress<TKey>[] }
9
+ export type ResolveResult =
10
+ | { status: 'materializedResource'; entry: MutableMaterializedResource; path: MutableToolStepRoleAddress[] }
11
+ | { status: 'externalInputPotential'; entry: MutableExternalInputPotential; path: MutableToolStepRoleAddress[] }
15
12
  | {
16
13
  status: 'unresolved';
17
14
  reason: 'not-found' | 'cycle' | 'depth-exceeded';
18
- path: MutableToolStepRoleAddress<TKey>[];
15
+ path: MutableToolStepRoleAddress[];
19
16
  };
20
17
 
21
- export function resolveStrategyStateChain<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
22
- strategyState: TStrategyState,
23
- start: MutableToolStepRoleAddress<TKey>,
18
+ export function resolveStrategyStateChain(
19
+ strategyState: MutableStrategyState,
20
+ start: MutableToolStepRoleAddress,
24
21
  opts?: { maxDepth?: number },
25
- ): ResolveResult<TKey> {
22
+ ): ResolveResult {
26
23
  const maxDepth = opts?.maxDepth ?? 50;
27
24
  const visited = new Set<string>();
28
- const path: MutableToolStepRoleAddress<TKey>[] = [];
29
- let current: MutableToolStepRoleAddress<TKey> = start;
25
+ const path: MutableToolStepRoleAddress[] = [];
26
+ let current: MutableToolStepRoleAddress = start;
30
27
 
31
28
  for (let depth = 0; depth <= maxDepth; depth++) {
32
29
  path.push(current);
@@ -37,7 +34,7 @@ export function resolveStrategyStateChain<TKey extends string, TStrategyState ex
37
34
  }
38
35
  visited.add(visitKey);
39
36
 
40
- const entry = getStrategyStateEntry(strategyState, current) as MutableStrategyStateEntry<TKey> | undefined;
37
+ const entry = getStrategyStateEntry(strategyState, current);
41
38
  if (!entry) {
42
39
  return { status: 'unresolved', reason: 'not-found', path };
43
40
  }
@@ -50,7 +47,7 @@ export function resolveStrategyStateChain<TKey extends string, TStrategyState ex
50
47
  return { status: 'externalInputPotential', entry, path };
51
48
  }
52
49
 
53
- current = (entry as MutableInternalInputPotential<TKey>).toolStepRoleAddress;
50
+ current = entry.toolStepRoleAddress;
54
51
  }
55
52
 
56
53
  return { status: 'unresolved', reason: 'depth-exceeded', path };