@toolproof-core/lib 1.0.32 → 1.0.33

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.
Files changed (61) hide show
  1. package/dist/artifacts/artifacts.d.ts +51 -51
  2. package/dist/artifacts/artifacts.d.ts.map +1 -1
  3. package/dist/integrations/firebase/createRunnableStrategy.d.ts +2 -2
  4. package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +1 -1
  5. package/dist/integrations/firebase/createRunnableStrategy.js +2 -3
  6. package/dist/integrations/firebase/createRunnableStrategy.js.map +1 -1
  7. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +5 -1
  8. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +1 -1
  9. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +1 -1
  10. package/dist/types/types.d.ts +3 -1
  11. package/dist/types/types.d.ts.map +1 -1
  12. package/dist/utils/bindInputRoleToResource.d.ts +2 -1
  13. package/dist/utils/bindInputRoleToResource.d.ts.map +1 -1
  14. package/dist/utils/bindInputRoleToResource.js +15 -65
  15. package/dist/utils/bindInputRoleToResource.js.map +1 -1
  16. package/dist/utils/creation/resourceCreation.d.ts +19 -7
  17. package/dist/utils/creation/resourceCreation.d.ts.map +1 -1
  18. package/dist/utils/creation/resourceCreation.js +16 -12
  19. package/dist/utils/creation/resourceCreation.js.map +1 -1
  20. package/dist/utils/creation/runnableStrategyCreation.d.ts +3 -3
  21. package/dist/utils/creation/runnableStrategyCreation.d.ts.map +1 -1
  22. package/dist/utils/creation/runnableStrategyCreation.js +8 -14
  23. package/dist/utils/creation/runnableStrategyCreation.js.map +1 -1
  24. package/dist/utils/creation/stepCreation.d.ts +22 -5
  25. package/dist/utils/creation/stepCreation.d.ts.map +1 -1
  26. package/dist/utils/creation/stepCreation.js +3 -2
  27. package/dist/utils/creation/stepCreation.js.map +1 -1
  28. package/dist/utils/extractData.d.ts +5 -4
  29. package/dist/utils/extractData.d.ts.map +1 -1
  30. package/dist/utils/extractData.js +7 -49
  31. package/dist/utils/extractData.js.map +1 -1
  32. package/dist/utils/parallelizeSteps.d.ts +2 -1
  33. package/dist/utils/parallelizeSteps.d.ts.map +1 -1
  34. package/dist/utils/parallelizeSteps.js +17 -10
  35. package/dist/utils/parallelizeSteps.js.map +1 -1
  36. package/dist/utils/resolveResourceChain.d.ts +6 -10
  37. package/dist/utils/resolveResourceChain.d.ts.map +1 -1
  38. package/dist/utils/resolveResourceChain.js +12 -20
  39. package/dist/utils/resolveResourceChain.js.map +1 -1
  40. package/dist/utils/roleSpec.d.ts +12 -0
  41. package/dist/utils/roleSpec.d.ts.map +1 -0
  42. package/dist/utils/roleSpec.js +58 -0
  43. package/dist/utils/roleSpec.js.map +1 -0
  44. package/dist/utils/strategyState.d.ts +7 -0
  45. package/dist/utils/strategyState.d.ts.map +1 -0
  46. package/dist/utils/strategyState.js +29 -0
  47. package/dist/utils/strategyState.js.map +1 -0
  48. package/package.json +3 -3
  49. package/src/integrations/firebase/createRunnableStrategy.ts +5 -7
  50. package/src/integrations/firebase/firebaseAdminHelpers.ts +2 -1
  51. package/src/types/types.ts +1 -1
  52. package/src/utils/bindInputRoleToResource.ts +23 -80
  53. package/src/utils/creation/resourceCreation.ts +40 -22
  54. package/src/utils/creation/runnableStrategyCreation.ts +14 -28
  55. package/src/utils/creation/stepCreation.ts +23 -17
  56. package/src/utils/extractData.ts +21 -63
  57. package/src/utils/parallelizeSteps.ts +24 -15
  58. package/src/utils/resolveResourceChain.ts +18 -32
  59. package/src/utils/roleSpec.ts +80 -0
  60. package/src/utils/strategyState.ts +56 -0
  61. package/tsconfig.tsbuildinfo +1 -1
@@ -18,7 +18,7 @@ export type IdName = keyof typeof MAPPINGS.IdNameToIdPrefix;
18
18
 
19
19
  export type IdStringByIdName<K extends IdName> = `${(typeof MAPPINGS.IdNameToIdPrefix)[K]}${string}`;
20
20
 
21
- export type StepIdStringByStepKind<K extends StepKind> = `${(typeof MAPPINGS.StepKindToStepIdPrefix)[K]}${string}`;
21
+ export type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
22
22
 
23
23
  export interface NucleusBaseSmall<T extends string = string> {
24
24
  id: T;
@@ -1,14 +1,15 @@
1
1
  import type {
2
2
  CreationContext,
3
- ExternalInputPotentialShell,
4
- InternalInputPotentialShell,
5
- OutputPotentialShell,
6
3
  Resource,
7
- ResourcePotential,
8
- StrategyState,
4
+ ReferenceInput,
5
+ ValueInput,
9
6
  } from '@toolproof-core/schema';
10
- import { CONSTANTS } from '../artifacts/artifacts.js';
11
- import { resolveResourceChain } from './resolveResourceChain.js';
7
+ import {
8
+ clearStrategyStateEntry,
9
+ getStrategyStateEntry,
10
+ setStrategyStateEntry,
11
+ type StrategyState,
12
+ } from './strategyState.js';
12
13
 
13
14
 
14
15
  export function bindInputResInStrategyState(
@@ -16,14 +17,13 @@ export function bindInputResInStrategyState(
16
17
  target: CreationContext,
17
18
  resource: Resource
18
19
  ): StrategyState {
19
- const bucket = strategyState[target.toolStepId] ?? {};
20
- return {
21
- ...strategyState,
22
- [target.toolStepId]: {
23
- ...bucket,
24
- [target.resourceRoleId]: resource,
25
- },
20
+ const entry: ValueInput = {
21
+ strategyStateInputKind: 'valueInput',
22
+ resourceTypeId: resource.resourceTypeId,
23
+ nucleus: resource.nucleus,
26
24
  };
25
+
26
+ return setStrategyStateEntry(strategyState, target, entry);
27
27
  }
28
28
 
29
29
  export function bindInputRefInStrategyState(
@@ -31,80 +31,23 @@ export function bindInputRefInStrategyState(
31
31
  target: CreationContext,
32
32
  source: CreationContext
33
33
  ): StrategyState {
34
- const sourceEntry = strategyState?.[source.toolStepId]?.[source.resourceRoleId] as (
35
- | Resource
36
- | ResourcePotential
37
- | undefined
38
- );
34
+ const sourceEntry = getStrategyStateEntry(strategyState, source);
39
35
  if (!sourceEntry) {
40
- throw new Error(`resourceEntry not found for source (${source.toolStepId}, ${source.resourceRoleId})`);
41
- }
42
-
43
- const result = resolveResourceChain(strategyState, source);
44
- const bucket = strategyState[target.toolStepId] ?? {};
45
-
46
- if (result.status === CONSTANTS.Enums.ResourceShellKind.materialized) {
47
- return {
48
- ...strategyState,
49
- [target.toolStepId]: {
50
- ...bucket,
51
- [target.resourceRoleId]: result.entry,
52
- },
53
- };
54
- }
55
-
56
- if (result.status === CONSTANTS.Enums.ResourceShellKind.externalInputPotential) {
57
- const externalInput = result.entry;
58
- const reusedExternalInput: ExternalInputPotentialShell = {
59
- id: externalInput.id,
60
- resourceTypeId: externalInput.resourceTypeId,
61
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.externalInputPotential,
62
- };
63
-
64
- return {
65
- ...strategyState,
66
- [target.toolStepId]: {
67
- ...bucket,
68
- [target.resourceRoleId]: reusedExternalInput,
69
- },
70
- };
36
+ throw new Error(`resourceEntry not found for source (${source.toolStepPath}, ${source.roleName})`);
71
37
  }
72
38
 
73
- if (result.status === CONSTANTS.Enums.ResourceShellKind.outputPotential) {
74
- const potentialInput: InternalInputPotentialShell = {
75
- id: sourceEntry.id,
76
- resourceTypeId: sourceEntry.resourceTypeId,
77
- creationContext: {
78
- toolStepId: source.toolStepId,
79
- resourceRoleId: source.resourceRoleId,
80
- },
81
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.internalInputPotential,
82
- };
83
-
84
- return {
85
- ...strategyState,
86
- [target.toolStepId]: {
87
- ...bucket,
88
- [target.resourceRoleId]: potentialInput,
89
- },
90
- };
91
- }
39
+ const entry: ReferenceInput = {
40
+ strategyStateInputKind: 'referenceInput',
41
+ toolStepPath: source.toolStepPath,
42
+ roleName: source.roleName,
43
+ };
92
44
 
93
- throw new Error(`Failed to resolve resource chain: ${result.reason}`);
45
+ return setStrategyStateEntry(strategyState, target, entry);
94
46
  }
95
47
 
96
48
  export function clearInputBindingInStrategyState(
97
49
  strategyState: StrategyState,
98
50
  target: CreationContext
99
51
  ): StrategyState {
100
- const bucket = strategyState?.[target.toolStepId];
101
- if (!bucket?.[target.resourceRoleId]) return strategyState;
102
-
103
- const nextBucket = { ...bucket } as Record<string, unknown>;
104
- delete nextBucket[target.resourceRoleId];
105
-
106
- return {
107
- ...strategyState,
108
- [target.toolStepId]: nextBucket as any,
109
- };
52
+ return clearStrategyStateEntry(strategyState, target);
110
53
  }
@@ -1,20 +1,33 @@
1
1
  import type {
2
2
  CreationContext,
3
- ExternalInputPotentialShell,
4
- InternalInputPotentialShell,
5
- OutputPotentialShell,
3
+ ExternalInput,
4
+ ReferenceInput,
6
5
  ResourceId,
7
6
  Resource,
8
7
  ResourceTypeId,
8
+ StrategyRunId,
9
9
  } from '@toolproof-core/schema';
10
- import { CONSTANTS } from '../../artifacts/artifacts.js';
10
+
11
+ export interface OutputPotential {
12
+ id: ResourceId;
13
+ resourceTypeId: ResourceTypeId;
14
+ creationContext: CreationContext;
15
+ strategyRunId?: StrategyRunId;
16
+ }
17
+
18
+ export type InputPotential = ReferenceInput & {
19
+ id: ResourceId;
20
+ resourceTypeId: ResourceTypeId;
21
+ creationContext: CreationContext;
22
+ strategyRunId?: StrategyRunId;
23
+ };
11
24
 
12
25
  export function generatePath(resourceTypeId: ResourceTypeId, id: ResourceId): string {
13
26
  return `${resourceTypeId}/${id}.json`;
14
27
  }
15
28
 
16
29
  export function createMaterializedFromOutputPotential(
17
- outputPotential: OutputPotentialShell,
30
+ outputPotential: OutputPotential,
18
31
  nucleus: unknown,
19
32
  timestamp?: string,
20
33
  ): Resource {
@@ -24,8 +37,11 @@ export function createMaterializedFromOutputPotential(
24
37
  return {
25
38
  id,
26
39
  resourceTypeId,
27
- creationContext,
28
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.materialized,
40
+ provenance: {
41
+ resourceProvenanceKind: 'strategy',
42
+ strategyRunId: outputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
43
+ creationContext,
44
+ },
29
45
  version: 1,
30
46
  timestamp: timestamp ?? new Date().toISOString(),
31
47
  path,
@@ -34,7 +50,7 @@ export function createMaterializedFromOutputPotential(
34
50
  }
35
51
 
36
52
  export function createMaterializedFromInputPotential(
37
- inputPotential: InternalInputPotentialShell,
53
+ inputPotential: InputPotential,
38
54
  nucleus: unknown,
39
55
  timestamp?: string,
40
56
  ): Resource {
@@ -44,8 +60,11 @@ export function createMaterializedFromInputPotential(
44
60
  return {
45
61
  id,
46
62
  resourceTypeId,
47
- creationContext,
48
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.materialized,
63
+ provenance: {
64
+ resourceProvenanceKind: 'strategy',
65
+ strategyRunId: inputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
66
+ creationContext,
67
+ },
49
68
  version: 1,
50
69
  timestamp: timestamp ?? new Date().toISOString(),
51
70
  path,
@@ -54,11 +73,11 @@ export function createMaterializedFromInputPotential(
54
73
  }
55
74
 
56
75
  export function createMaterializedFromPotential(
57
- potential: InternalInputPotentialShell | OutputPotentialShell,
76
+ potential: InputPotential | OutputPotential,
58
77
  nucleus: unknown,
59
78
  timestamp?: string,
60
79
  ): Resource {
61
- if (potential.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.internalInputPotential) {
80
+ if ('strategyStateInputKind' in potential && potential.strategyStateInputKind === 'referenceInput') {
62
81
  return createMaterializedFromInputPotential(potential, nucleus, timestamp);
63
82
  }
64
83
 
@@ -66,13 +85,11 @@ export function createMaterializedFromPotential(
66
85
  }
67
86
 
68
87
  export function createExternalInputPotential(
69
- id: ResourceId,
70
- resourceTypeId: ResourceTypeId,
71
- ): ExternalInputPotentialShell {
88
+ _id: ResourceId,
89
+ _resourceTypeId: ResourceTypeId,
90
+ ): ExternalInput {
72
91
  return {
73
- id,
74
- resourceTypeId,
75
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.externalInputPotential,
92
+ strategyStateInputKind: 'externalInput',
76
93
  };
77
94
  }
78
95
 
@@ -80,12 +97,14 @@ export function createInputPotential(
80
97
  id: ResourceId,
81
98
  resourceTypeId: ResourceTypeId,
82
99
  creationContext: CreationContext,
83
- ): InternalInputPotentialShell {
100
+ ): InputPotential {
84
101
  return {
85
102
  id,
86
103
  resourceTypeId,
104
+ strategyStateInputKind: 'referenceInput',
105
+ toolStepPath: creationContext.toolStepPath,
106
+ roleName: creationContext.roleName,
87
107
  creationContext,
88
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.internalInputPotential,
89
108
  };
90
109
  }
91
110
 
@@ -93,11 +112,10 @@ export function createOutputPotential(
93
112
  id: ResourceId,
94
113
  resourceTypeId: ResourceTypeId,
95
114
  creationContext: CreationContext,
96
- ): OutputPotentialShell {
115
+ ): OutputPotential {
97
116
  return {
98
117
  id,
99
118
  resourceTypeId,
100
119
  creationContext,
101
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.outputPotential,
102
120
  };
103
121
  }
@@ -1,42 +1,28 @@
1
1
  import type {
2
- RawStrategy,
3
- RunnableStrategyId,
4
- RunnableStrategy,
5
2
  Step,
6
- StepArrayByStrategyThread,
7
- StrategyThreadId,
3
+ Strategy,
4
+ StrategyId,
8
5
  } from '@toolproof-core/schema';
9
6
  import { getIndependentThreads } from '../parallelizeSteps.js';
7
+ import { CONSTANTS } from '../../artifacts/artifacts.js';
10
8
 
11
- export function getRunnableStrategyThreadGroups(rawStrategy: RawStrategy): Step[][] {
12
- return getIndependentThreads(rawStrategy.steps, rawStrategy.strategyState);
9
+ export function getRunnableStrategyThreadGroups(rawStrategy: Strategy): Step[][] {
10
+ if ('steps' in rawStrategy) {
11
+ return getIndependentThreads(rawStrategy.steps, rawStrategy.strategyState as any);
12
+ }
13
+
14
+ return rawStrategy.stepsByThreadIndex;
13
15
  }
14
16
 
15
17
  export function buildRunnableStrategy(
16
- rawStrategy: RawStrategy,
17
- runnableStrategyId: RunnableStrategyId,
18
18
  threadStepGroups: Step[][],
19
- threadIdentities: StrategyThreadId[],
20
- ): RunnableStrategy {
21
- if (threadStepGroups.length !== threadIdentities.length) {
22
- throw new Error('buildRunnableStrategy requires one thread id per thread group');
23
- }
24
-
25
- const stepsByStrategyThread: StepArrayByStrategyThread = {};
26
-
27
- for (const [index, group] of threadStepGroups.entries()) {
28
- const threadId = threadIdentities[index];
29
-
30
- if (!threadId) {
31
- throw new Error(`Missing thread id for thread group index ${index}`);
32
- }
33
-
34
- stepsByStrategyThread[threadId] = group;
35
- }
36
-
19
+ runnableStrategyId: StrategyId,
20
+ rawStrategy: Strategy,
21
+ ): Strategy {
37
22
  return {
38
23
  id: runnableStrategyId,
39
- stepsByStrategyThread,
24
+ strategyKind: CONSTANTS.Enums.StrategyKind.threaded,
25
+ stepsByThreadIndex: threadStepGroups,
40
26
  strategyState: rawStrategy.strategyState,
41
27
  };
42
28
  }
@@ -1,17 +1,23 @@
1
1
  import type {
2
- BranchStepId,
3
2
  BranchStep,
4
3
  Case,
5
- ForStepId,
6
4
  ForStep,
7
5
  Tool,
8
- ToolStepId,
9
6
  ToolStep,
10
- ResourceRoleId,
11
- WhileStepId,
12
7
  WhileStep,
13
8
  } from '@toolproof-core/schema';
14
9
  import { CONSTANTS } from '../../artifacts/artifacts.js';
10
+ import { getInputRoleNames, getOutputRoleNames } from '../roleSpec.js';
11
+
12
+ type ToolStepId = string;
13
+ type BranchStepId = string;
14
+ type ForStepId = string;
15
+ type WhileStepId = string;
16
+
17
+ type ToolStepWithId = ToolStep & { id: ToolStepId };
18
+ type BranchStepWithId = BranchStep & { id: BranchStepId };
19
+ type ForStepWithId = ForStep & { id: ForStepId };
20
+ type WhileStepWithId = WhileStep & { id: WhileStepId };
15
21
 
16
22
  export type LoopStepBuildIdentities =
17
23
  | {
@@ -40,21 +46,21 @@ function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
40
46
 
41
47
  function getRoleBindingSpec(tool: Tool): ToolStep['roleBindingSpec'] {
42
48
  return {
43
- inputBindings: Object.keys(tool.roleSpec.inputRoleValueById) as ResourceRoleId[],
44
- outputBindings: Object.keys(tool.roleSpec.outputRoleValueById) as ResourceRoleId[],
49
+ inputBindings: getInputRoleNames(tool),
50
+ outputBindings: getOutputRoleNames(tool),
45
51
  };
46
52
  }
47
53
 
48
54
  export function buildToolStepFromTool(
49
55
  tool: Tool,
50
56
  id: ToolStepId,
51
- ): ToolStep {
57
+ ): ToolStepWithId {
52
58
  return {
53
59
  id,
54
60
  stepKind: CONSTANTS.Enums.StepKind.tool,
55
61
  toolId: tool.id,
56
62
  roleBindingSpec: getRoleBindingSpec(tool),
57
- };
63
+ } as ToolStepWithId;
58
64
  }
59
65
 
60
66
  export function buildLoopStepFromToolPair(
@@ -70,21 +76,21 @@ export function buildLoopStepFromToolPair(
70
76
  id: identities.stepId,
71
77
  stepKind: CONSTANTS.Enums.StepKind.for,
72
78
  case: { what, when },
73
- };
79
+ } as ForStepWithId;
74
80
  }
75
81
 
76
82
  return {
77
83
  id: identities.stepId,
78
84
  stepKind: CONSTANTS.Enums.StepKind.while,
79
85
  case: { what, when },
80
- };
86
+ } as WhileStepWithId;
81
87
  }
82
88
 
83
89
  export function buildBranchStepFromToolPairs(
84
90
  cases: Array<{ whatTool: Tool; whenTool: Tool }>,
85
91
  branchId: BranchStepId,
86
92
  caseIdentities: BranchCaseBuildIdentities[],
87
- ): BranchStep {
93
+ ): BranchStepWithId {
88
94
  if (cases.length !== caseIdentities.length) {
89
95
  throw new Error('buildBranchStepFromToolPairs requires one id pair per case');
90
96
  }
@@ -107,7 +113,7 @@ export function buildBranchStepFromToolPairs(
107
113
  id: branchId,
108
114
  stepKind: CONSTANTS.Enums.StepKind.branch,
109
115
  cases: resolved,
110
- };
116
+ } as BranchStepWithId;
111
117
  }
112
118
 
113
119
  export function cloneForStepWithIdentities(
@@ -117,7 +123,7 @@ export function cloneForStepWithIdentities(
117
123
  whatId: ToolStepId;
118
124
  whenId: ToolStepId;
119
125
  },
120
- ): ForStep {
126
+ ): ForStepWithId {
121
127
  return {
122
128
  id: identities.stepId,
123
129
  stepKind: CONSTANTS.Enums.StepKind.for,
@@ -131,7 +137,7 @@ export function cloneForStepWithIdentities(
131
137
  id: identities.whenId,
132
138
  },
133
139
  },
134
- };
140
+ } as ForStepWithId;
135
141
  }
136
142
 
137
143
  export function cloneWhileStepWithIdentities(
@@ -141,7 +147,7 @@ export function cloneWhileStepWithIdentities(
141
147
  whatId: ToolStepId;
142
148
  whenId: ToolStepId;
143
149
  },
144
- ): WhileStep {
150
+ ): WhileStepWithId {
145
151
  return {
146
152
  id: identities.stepId,
147
153
  stepKind: CONSTANTS.Enums.StepKind.while,
@@ -155,5 +161,5 @@ export function cloneWhileStepWithIdentities(
155
161
  id: identities.whenId,
156
162
  },
157
163
  },
158
- };
164
+ } as WhileStepWithId;
159
165
  }
@@ -1,20 +1,25 @@
1
1
  import type {
2
- RawStrategy,
2
+ RoleName,
3
+ RoleValue,
4
+ Strategy,
3
5
  ToolId,
4
6
  Tool,
5
- ToolStepId,
6
7
  ToolStep,
7
- Step,
8
- ResourceRoleId,
9
- ResourceRole,
10
8
  ResourceId,
11
9
  ResourceTypeId,
12
10
  Resource,
13
11
  } from '@toolproof-core/schema';
14
12
  import { CONSTANTS } from '../artifacts/artifacts.js';
13
+ import {
14
+ extractToolStepsFromStrategy,
15
+ getInputRoleEntries,
16
+ getOutputRoleEntries,
17
+ toToolRoleDescriptor,
18
+ type ToolRoleDescriptor,
19
+ } from './roleSpec.js';
15
20
 
16
21
 
17
- export function extractToolIdsFromRawStrategy(rawStrategy: RawStrategy): ToolId[] {
22
+ export function extractToolIdsFromRawStrategy(rawStrategy: Strategy): ToolId[] {
18
23
  const toolSteps = extractToolStepsFromRawStrategy(rawStrategy);
19
24
  const ids = new Set<ToolId>();
20
25
  for (const jStep of toolSteps) {
@@ -23,71 +28,24 @@ export function extractToolIdsFromRawStrategy(rawStrategy: RawStrategy): ToolId[
23
28
  return Array.from(ids);
24
29
  }
25
30
 
26
- export function extractToolStepsFromRawStrategy(rawStrategy: RawStrategy): ToolStep[] {
27
- const toolSteps: ToolStep[] = [];
28
- const seen = new Set<ToolStepId>();
29
-
30
- const addToolStep = (toolStep: ToolStep) => {
31
- if (seen.has(toolStep.id)) {
32
- throw new Error(`Duplicate tool step id encountered: ${toolStep.id}`);
33
- }
34
- seen.add(toolStep.id);
35
- toolSteps.push(toolStep);
36
- };
37
-
38
- const visitStep = (step: Step) => {
39
- switch (step.stepKind) {
40
- case CONSTANTS.Enums.StepKind.tool: {
41
- addToolStep(step);
42
- return;
43
- }
44
- case CONSTANTS.Enums.StepKind.branch: {
45
- for (const conditional of step.cases) {
46
- addToolStep(conditional.when);
47
- addToolStep(conditional.what);
48
- }
49
- return;
50
- }
51
- case CONSTANTS.Enums.StepKind.while:
52
- case CONSTANTS.Enums.StepKind.for: {
53
- addToolStep(step.case.when);
54
- addToolStep(step.case.what);
55
- return;
56
- }
57
- default: {
58
- const _exhaustive: never = step;
59
- return _exhaustive;
60
- }
61
- }
62
- };
63
-
64
- for (const step of rawStrategy.steps) {
65
- visitStep(step);
66
- }
67
-
68
- return toolSteps;
31
+ export function extractToolStepsFromRawStrategy(rawStrategy: Strategy): ToolStep[] {
32
+ return extractToolStepsFromStrategy(rawStrategy);
69
33
  }
70
34
 
71
35
 
72
- export function extractRoleMapFromRawStrategy(rawStrategy: RawStrategy, toolMap: Map<ToolId, Tool>): Map<ResourceRoleId, ResourceRole> {
73
- const roleMap = new Map<ResourceRoleId, ResourceRole>();
36
+ export function extractRoleMapFromRawStrategy(rawStrategy: Strategy, toolMap: Map<ToolId, Tool>): Map<RoleName, ToolRoleDescriptor> {
37
+ const roleMap = new Map<RoleName, ToolRoleDescriptor>();
74
38
  const toolIds = extractToolIdsFromRawStrategy(rawStrategy);
75
39
 
76
40
  for (const toolId of toolIds) {
77
41
  const tool = toolMap.get(toolId);
78
42
  if (!tool) continue;
79
43
 
80
- for (const [rid, role] of Object.entries(tool.roleSpec.inputRoleValueById)) {
81
- roleMap.set(rid as ResourceRoleId, {
82
- id: rid as ResourceRoleId,
83
- ...role,
84
- });
44
+ for (const entry of getInputRoleEntries(tool)) {
45
+ roleMap.set(entry[0], toToolRoleDescriptor(entry));
85
46
  }
86
- for (const [rid, role] of Object.entries(tool.roleSpec.outputRoleValueById)) {
87
- roleMap.set(rid as ResourceRoleId, {
88
- id: rid as ResourceRoleId,
89
- ...role,
90
- });
47
+ for (const entry of getOutputRoleEntries(tool)) {
48
+ roleMap.set(entry[0], toToolRoleDescriptor(entry));
91
49
  }
92
50
  }
93
51
 
@@ -96,11 +54,11 @@ export function extractRoleMapFromRawStrategy(rawStrategy: RawStrategy, toolMap:
96
54
 
97
55
  export function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeId | null {
98
56
  const errorRoleId = CONSTANTS.Cosmos.ROLE_ErrorOutput;
99
- const outputEntries = Object.entries(tool.roleSpec.outputRoleValueById).filter(([roleId]) => roleId !== errorRoleId);
57
+ const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== errorRoleId);
100
58
  if (!outputEntries || !outputEntries[0] || outputEntries.length !== 1) {
101
59
  throw new Error(`Tool ${tool.id} must have exactly one non-error output role to be branchable/loopable`);
102
60
  } // ATTENTION_PUREIFY
103
- const outputRole = outputEntries[0][1];
61
+ const outputRole = outputEntries[0][1] as RoleValue;
104
62
  return outputRole.resourceTypeId;
105
63
  }
106
64
 
@@ -1,14 +1,20 @@
1
1
  import type {
2
2
  BranchStep,
3
3
  ForStep,
4
- ToolStepId,
5
4
  ToolStep,
6
- ResourceRoleId,
7
5
  Step,
8
- StrategyState,
9
6
  WhileStep,
10
7
  } from '@toolproof-core/schema';
11
8
  import { CONSTANTS } from '../artifacts/artifacts.js';
9
+ import type { StrategyState } from './strategyState.js';
10
+
11
+ type ToolStepId = string;
12
+ type ToolStepWithId = ToolStep & { id: ToolStepId };
13
+
14
+ function getToolStepId(toolStep: ToolStep | undefined): ToolStepId | undefined {
15
+ const maybeId = (toolStep as Partial<ToolStepWithId> | undefined)?.id;
16
+ return typeof maybeId === 'string' ? maybeId : undefined;
17
+ }
12
18
 
13
19
 
14
20
  export function getIndependentThreads(steps: Step[], strategyState: StrategyState): Step[][] {
@@ -22,22 +28,23 @@ export function getIndependentThreads(steps: Step[], strategyState: StrategyStat
22
28
  // Map each toolStep.id (including macro-nested tool steps) to owning top-level step index.
23
29
  const toolStepIdToOwner = new Map<ToolStepId, OwnerIndex>();
24
30
  const ownerToToolStepIds = new Map<OwnerIndex, ToolStepId[]>();
25
- const toolStepById = new Map<ToolStepId, ToolStep>();
31
+ const toolStepById = new Map<ToolStepId, ToolStepWithId>();
26
32
 
27
33
  const addToolStep = (toolStep: ToolStep | undefined, ownerIndex: OwnerIndex) => {
28
- if (!toolStep?.id) return;
34
+ const toolStepId = getToolStepId(toolStep);
35
+ if (!toolStepId || !toolStep) return;
29
36
 
30
- const existingOwner = toolStepIdToOwner.get(toolStep.id);
37
+ const existingOwner = toolStepIdToOwner.get(toolStepId);
31
38
  if (existingOwner !== undefined) {
32
39
  throw new Error(
33
- `Duplicate toolStep.id '${toolStep.id}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`
40
+ `Duplicate toolStep.id '${toolStepId}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`
34
41
  );
35
42
  }
36
43
 
37
- toolStepIdToOwner.set(toolStep.id, ownerIndex);
38
- toolStepById.set(toolStep.id, toolStep);
44
+ toolStepIdToOwner.set(toolStepId, ownerIndex);
45
+ toolStepById.set(toolStepId, toolStep as ToolStepWithId);
39
46
  const bucket = ownerToToolStepIds.get(ownerIndex) ?? [];
40
- bucket.push(toolStep.id);
47
+ bucket.push(toolStepId);
41
48
  ownerToToolStepIds.set(ownerIndex, bucket);
42
49
  };
43
50
 
@@ -94,20 +101,22 @@ export function getIndependentThreads(steps: Step[], strategyState: StrategyStat
94
101
  const bucket = strategyState?.[toolStepId] ?? ({} as any);
95
102
 
96
103
  for (const inputRoleId of inputBindings) {
97
- const entry = bucket?.[inputRoleId as ResourceRoleId] as any;
98
- if (!entry || entry.resourceShellKind !== 'internalInputPotential') continue;
104
+ const entry = bucket?.[inputRoleId] as any;
105
+ if (!entry || entry.strategyStateInputKind !== 'referenceInput') continue;
99
106
 
100
- const creatorToolStepId = entry?.creationContext?.toolStepId as ToolStepId | undefined;
107
+ const creatorToolStepId = typeof entry?.toolStepPath === 'string'
108
+ ? entry.toolStepPath as ToolStepId
109
+ : undefined;
101
110
  if (!creatorToolStepId) {
102
111
  throw new Error(
103
- `Unresolvable internalInputPotential in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): missing creationContext.toolStepId for role '${inputRoleId}'`
112
+ `Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): missing toolStepPath for role '${inputRoleId}'`
104
113
  );
105
114
  }
106
115
 
107
116
  const producerOwner = toolStepIdToOwner.get(creatorToolStepId);
108
117
  if (producerOwner === undefined) {
109
118
  throw new Error(
110
- `Unresolvable internalInputPotential in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): creator toolStepId '${creatorToolStepId}' not found in strategy steps`
119
+ `Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): creator toolStepPath '${creatorToolStepId}' not found in strategy steps`
111
120
  );
112
121
  }
113
122