@toolproof-core/lib 1.0.32 → 1.0.34

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 (75) hide show
  1. package/dist/artifacts/artifacts.d.ts +53 -53
  2. package/dist/artifacts/artifacts.d.ts.map +1 -1
  3. package/dist/artifacts/artifacts.js +2 -2
  4. package/dist/artifacts/artifacts.js.map +1 -1
  5. package/dist/integrations/firebase/createThreadedStrategy.d.ts +3 -0
  6. package/dist/integrations/firebase/createThreadedStrategy.d.ts.map +1 -0
  7. package/dist/integrations/firebase/createThreadedStrategy.js +9 -0
  8. package/dist/integrations/firebase/createThreadedStrategy.js.map +1 -0
  9. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +5 -1
  10. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +1 -1
  11. package/dist/integrations/firebase/firebaseAdminHelpers.js +15 -12
  12. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +1 -1
  13. package/dist/types/types.d.ts +3 -1
  14. package/dist/types/types.d.ts.map +1 -1
  15. package/dist/utils/bindInputRoleToResource.d.ts +5 -4
  16. package/dist/utils/bindInputRoleToResource.d.ts.map +1 -1
  17. package/dist/utils/bindInputRoleToResource.js +15 -65
  18. package/dist/utils/bindInputRoleToResource.js.map +1 -1
  19. package/dist/utils/creation/resourceCreation.d.ts +19 -7
  20. package/dist/utils/creation/resourceCreation.d.ts.map +1 -1
  21. package/dist/utils/creation/resourceCreation.js +16 -12
  22. package/dist/utils/creation/resourceCreation.js.map +1 -1
  23. package/dist/utils/creation/stepCreation.d.ts +22 -5
  24. package/dist/utils/creation/stepCreation.d.ts.map +1 -1
  25. package/dist/utils/creation/stepCreation.js +3 -2
  26. package/dist/utils/creation/stepCreation.js.map +1 -1
  27. package/dist/utils/creation/threadedStrategyCreation.d.ts +19 -0
  28. package/dist/utils/creation/threadedStrategyCreation.d.ts.map +1 -0
  29. package/dist/utils/creation/threadedStrategyCreation.js +17 -0
  30. package/dist/utils/creation/threadedStrategyCreation.js.map +1 -0
  31. package/dist/utils/extractData.d.ts +5 -4
  32. package/dist/utils/extractData.d.ts.map +1 -1
  33. package/dist/utils/extractData.js +12 -54
  34. package/dist/utils/extractData.js.map +1 -1
  35. package/dist/utils/parallelizeSteps.d.ts +2 -1
  36. package/dist/utils/parallelizeSteps.d.ts.map +1 -1
  37. package/dist/utils/parallelizeSteps.js +17 -10
  38. package/dist/utils/parallelizeSteps.js.map +1 -1
  39. package/dist/utils/resolveResourceChain.d.ts +6 -10
  40. package/dist/utils/resolveResourceChain.d.ts.map +1 -1
  41. package/dist/utils/resolveResourceChain.js +12 -20
  42. package/dist/utils/resolveResourceChain.js.map +1 -1
  43. package/dist/utils/roleSpec.d.ts +17 -0
  44. package/dist/utils/roleSpec.d.ts.map +1 -0
  45. package/dist/utils/roleSpec.js +58 -0
  46. package/dist/utils/roleSpec.js.map +1 -0
  47. package/dist/utils/strategyState.d.ts +8 -0
  48. package/dist/utils/strategyState.d.ts.map +1 -0
  49. package/dist/utils/strategyState.js +29 -0
  50. package/dist/utils/strategyState.js.map +1 -0
  51. package/package.json +6 -6
  52. package/src/artifacts/artifacts.ts +2 -2
  53. package/src/integrations/firebase/createThreadedStrategy.ts +19 -0
  54. package/src/integrations/firebase/firebaseAdminHelpers.ts +23 -13
  55. package/src/types/types.ts +1 -1
  56. package/src/utils/bindInputRoleToResource.ts +33 -89
  57. package/src/utils/creation/resourceCreation.ts +40 -22
  58. package/src/utils/creation/stepCreation.ts +23 -17
  59. package/src/utils/creation/threadedStrategyCreation.ts +42 -0
  60. package/src/utils/extractData.ts +25 -67
  61. package/src/utils/parallelizeSteps.ts +24 -15
  62. package/src/utils/resolveResourceChain.ts +18 -32
  63. package/src/utils/roleSpec.ts +84 -0
  64. package/src/utils/strategyState.ts +57 -0
  65. package/tsconfig.tsbuildinfo +1 -1
  66. package/dist/integrations/firebase/createRunnableStrategy.d.ts +0 -3
  67. package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +0 -1
  68. package/dist/integrations/firebase/createRunnableStrategy.js +0 -10
  69. package/dist/integrations/firebase/createRunnableStrategy.js.map +0 -1
  70. package/dist/utils/creation/runnableStrategyCreation.d.ts +0 -4
  71. package/dist/utils/creation/runnableStrategyCreation.d.ts.map +0 -1
  72. package/dist/utils/creation/runnableStrategyCreation.js +0 -23
  73. package/dist/utils/creation/runnableStrategyCreation.js.map +0 -1
  74. package/src/integrations/firebase/createRunnableStrategy.ts +0 -20
  75. package/src/utils/creation/runnableStrategyCreation.ts +0 -42
@@ -0,0 +1,29 @@
1
+ export function getStrategyStateBucket(strategyState, toolStepPath) {
2
+ return strategyState[toolStepPath];
3
+ }
4
+ export function getStrategyStateEntry(strategyState, context) {
5
+ return getStrategyStateBucket(strategyState, context.toolStepPath)?.[context.roleName];
6
+ }
7
+ export function setStrategyStateEntry(strategyState, context, entry) {
8
+ const bucket = getStrategyStateBucket(strategyState, context.toolStepPath) ?? {};
9
+ return {
10
+ ...strategyState,
11
+ [context.toolStepPath]: {
12
+ ...bucket,
13
+ [context.roleName]: entry,
14
+ },
15
+ };
16
+ }
17
+ export function clearStrategyStateEntry(strategyState, context) {
18
+ const bucket = getStrategyStateBucket(strategyState, context.toolStepPath);
19
+ if (!bucket || !(context.roleName in bucket)) {
20
+ return strategyState;
21
+ }
22
+ const nextBucket = { ...bucket };
23
+ delete nextBucket[context.roleName];
24
+ return {
25
+ ...strategyState,
26
+ [context.toolStepPath]: nextBucket,
27
+ };
28
+ }
29
+ //# sourceMappingURL=strategyState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategyState.js","sourceRoot":"","sources":["../../src/utils/strategyState.ts"],"names":[],"mappings":"AAUA,MAAM,UAAU,sBAAsB,CAClC,aAAgC,EAChC,YAA6C;IAE7C,OAAO,aAAa,CAAC,YAAY,CAAkD,CAAC;AACxF,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,aAAgC,EAChC,OAAwB;IAExB,OAAO,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC3F,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,aAA6B,EAC7B,OAAwB,EACxB,KAA8B;IAE9B,MAAM,MAAM,GAAG,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IAEjF,OAAO;QACH,GAAG,aAAa;QAChB,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE;YACpB,GAAG,MAAM;YACT,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,KAAK;SAC5B;KACc,CAAC;AACxB,CAAC;AAED,MAAM,UAAU,uBAAuB,CACnC,aAA6B,EAC7B,OAAwB;IAExB,MAAM,MAAM,GAAG,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAC3E,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,IAAI,MAAM,CAAC,EAAE,CAAC;QAC3C,OAAO,aAAa,CAAC;IACzB,CAAC;IAED,MAAM,UAAU,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IACjC,OAAO,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpC,OAAO;QACH,GAAG,aAAa;QAChB,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,UAAU;KACnB,CAAC;AACxB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-core/lib",
3
- "version": "1.0.32",
3
+ "version": "1.0.34",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -27,9 +27,9 @@
27
27
  "types": "./dist/integrations/firebase/createStep.d.ts",
28
28
  "import": "./dist/integrations/firebase/createStep.js"
29
29
  },
30
- "./create-runnable-strategy": {
31
- "types": "./dist/integrations/firebase/createRunnableStrategy.d.ts",
32
- "import": "./dist/integrations/firebase/createRunnableStrategy.js"
30
+ "./create-threaded-strategy": {
31
+ "types": "./dist/integrations/firebase/createThreadedStrategy.d.ts",
32
+ "import": "./dist/integrations/firebase/createThreadedStrategy.js"
33
33
  },
34
34
  "./resolve-resource-chain": {
35
35
  "types": "./dist/utils/resolveResourceChain.d.ts",
@@ -52,11 +52,11 @@
52
52
  "@types/node": "^22.0.0"
53
53
  },
54
54
  "dependencies": {
55
- "@toolproof-core/schema": "^1.0.44",
55
+ "@toolproof-core/schema": "^1.0.46",
56
56
  "firebase-admin": "^13.7.0"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "echo \"Error: no test specified\" && exit 1",
60
- "build": "tsc -b"
60
+ "build": "if exist dist rmdir /s /q dist && if exist tsconfig.tsbuildinfo del /q tsconfig.tsbuildinfo && tsc -b"
61
61
  }
62
62
  }
@@ -24,8 +24,8 @@ export const CONSTANTS = {
24
24
  TYPE_ResourceType: 'TYPE-ResourceType',
25
25
  TYPE_Tool: 'TYPE-Tool',
26
26
  TYPE_Error: 'TYPE-Error',
27
- TYPE_RawStrategy: 'TYPE-RawStrategy',
28
- TYPE_RunnableStrategy: 'TYPE-RunnableStrategy',
27
+ TYPE_UnthreadedStrategy: 'TYPE-UnthreadedStrategy',
28
+ TYPE_ThreadedStrategy: 'TYPE-ThreadedStrategy',
29
29
  TYPE_StrategyRun: 'TYPE-StrategyRun',
30
30
  ROLE_Manual: 'ROLE-Manual',
31
31
  ROLE_ErrorOutput: 'ROLE-ErrorOutput',
@@ -0,0 +1,19 @@
1
+ import { CONSTANTS } from '../../artifacts/artifacts.js';
2
+ import {
3
+ buildThreadedStrategy,
4
+ getThreadedStrategyStepGroups,
5
+ type ThreadableStrategy,
6
+ type ThreadedStrategyLike,
7
+ } from '../../utils/creation/threadedStrategyCreation.js';
8
+ import { getNewId } from './firebaseAdminHelpers.js';
9
+
10
+ export function createThreadedStrategy<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): ThreadedStrategyLike<TStrategyState> {
11
+ const threadStepGroups = getThreadedStrategyStepGroups(strategy);
12
+ const strategyId = getNewId(CONSTANTS.Names.StrategyId);
13
+
14
+ return buildThreadedStrategy(
15
+ threadStepGroups,
16
+ strategyId,
17
+ strategy,
18
+ );
19
+ }
@@ -1,5 +1,7 @@
1
1
  import type {
2
+ CreationContext,
2
3
  Resource,
4
+ ResourceProvenance,
3
5
  ResourceTypeId,
4
6
  StepKind,
5
7
  } from '@toolproof-core/schema';
@@ -7,11 +9,12 @@ import type {
7
9
  IdName,
8
10
  IdStringByIdName,
9
11
  ResourcesByType,
10
- StepIdStringByStepKind,
11
12
  } from '../../types/types.js';
12
13
  import { CONSTANTS, MAPPINGS } from '../../artifacts/artifacts.js';
13
14
  import { dbAdmin, storageAdmin } from './firebaseAdminInit.js';
14
15
 
16
+ type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
17
+
15
18
  function capitalizeFirst(value: string): string {
16
19
  if (!value) {
17
20
  return value;
@@ -50,7 +53,7 @@ export async function listResources(
50
53
  async function fetchFilesUnder(
51
54
  resourceTypeId: ResourceTypeId,
52
55
  ): Promise<Array<{ data: unknown; meta: any; name: string }>> {
53
- const bucket = resourceTypeId === CONSTANTS.Cosmos.TYPE_RunnableStrategy
56
+ const bucket = resourceTypeId === CONSTANTS.Cosmos.TYPE_ThreadedStrategy
54
57
  ? bucketStrategies
55
58
  : bucketResources;
56
59
  const prefix = `${resourceTypeId}/`;
@@ -147,9 +150,7 @@ export async function listResources(
147
150
  }
148
151
 
149
152
  const id = root.id;
150
- const resourceRoleId = root.creationContext.resourceRoleId;
151
- const toolStepId = root.creationContext.toolStepId;
152
- const resourceShellKind = root.resourceShellKind;
153
+ const provenance = root.provenance as ResourceProvenance | undefined;
153
154
  const versionRaw = root.version;
154
155
  const version = typeof versionRaw === 'number'
155
156
  ? versionRaw
@@ -159,9 +160,6 @@ export async function listResources(
159
160
 
160
161
  const missing = [
161
162
  ['id', id],
162
- ['resourceRoleId', resourceRoleId],
163
- ['toolStepId', toolStepId],
164
- ['resourceShellKind', resourceShellKind],
165
163
  ['version', Number.isFinite(version) ? String(version) : ''],
166
164
  ['timestamp', timestamp],
167
165
  ['path', resourcePath],
@@ -172,14 +170,26 @@ export async function listResources(
172
170
  throw new Error(`Missing required metadata keys [${keys}] for resource file: ${name}`);
173
171
  }
174
172
 
173
+ if (!provenance || typeof provenance !== 'object' || typeof provenance.resourceProvenanceKind !== 'string') {
174
+ throw new Error(`Missing required metadata key [provenance] for resource file: ${name}`);
175
+ }
176
+
177
+ if (provenance.resourceProvenanceKind === 'strategy') {
178
+ const creationContext = (provenance as { creationContext?: CreationContext }).creationContext;
179
+ if (
180
+ !creationContext ||
181
+ typeof creationContext !== 'object' ||
182
+ typeof creationContext.roleName !== 'string' ||
183
+ typeof creationContext.toolStepPath !== 'string'
184
+ ) {
185
+ throw new Error(`Missing required strategy provenance creationContext for resource file: ${name}`);
186
+ }
187
+ }
188
+
175
189
  return {
176
190
  id,
177
191
  resourceTypeId,
178
- creationContext: {
179
- resourceRoleId,
180
- toolStepId,
181
- },
182
- resourceShellKind,
192
+ provenance,
183
193
  version,
184
194
  timestamp: timestamp as string,
185
195
  path: resourcePath as string,
@@ -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,110 +1,54 @@
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
+ type StrategyStateLike,
13
+ } from './strategyState.js';
12
14
 
13
15
 
14
- export function bindInputResInStrategyState(
15
- strategyState: StrategyState,
16
+ export function bindInputResInStrategyState<TStrategyState extends StrategyStateLike>(
17
+ strategyState: TStrategyState,
16
18
  target: CreationContext,
17
19
  resource: Resource
18
- ): StrategyState {
19
- const bucket = strategyState[target.toolStepId] ?? {};
20
- return {
21
- ...strategyState,
22
- [target.toolStepId]: {
23
- ...bucket,
24
- [target.resourceRoleId]: resource,
25
- },
20
+ ): TStrategyState {
21
+ const entry: ValueInput = {
22
+ strategyStateInputKind: 'valueInput',
23
+ resourceTypeId: resource.resourceTypeId,
24
+ nucleus: resource.nucleus,
26
25
  };
26
+
27
+ return setStrategyStateEntry(strategyState, target, entry);
27
28
  }
28
29
 
29
- export function bindInputRefInStrategyState(
30
- strategyState: StrategyState,
30
+ export function bindInputRefInStrategyState<TStrategyState extends StrategyStateLike>(
31
+ strategyState: TStrategyState,
31
32
  target: CreationContext,
32
33
  source: CreationContext
33
- ): StrategyState {
34
- const sourceEntry = strategyState?.[source.toolStepId]?.[source.resourceRoleId] as (
35
- | Resource
36
- | ResourcePotential
37
- | undefined
38
- );
34
+ ): TStrategyState {
35
+ const sourceEntry = getStrategyStateEntry(strategyState, source);
39
36
  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
- };
37
+ throw new Error(`resourceEntry not found for source (${source.toolStepPath}, ${source.roleName})`);
71
38
  }
72
39
 
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
- }
40
+ const entry: ReferenceInput = {
41
+ strategyStateInputKind: 'referenceInput',
42
+ toolStepPath: source.toolStepPath,
43
+ roleName: source.roleName,
44
+ };
92
45
 
93
- throw new Error(`Failed to resolve resource chain: ${result.reason}`);
46
+ return setStrategyStateEntry(strategyState, target, entry);
94
47
  }
95
48
 
96
- export function clearInputBindingInStrategyState(
97
- strategyState: StrategyState,
49
+ export function clearInputBindingInStrategyState<TStrategyState extends StrategyStateLike>(
50
+ strategyState: TStrategyState,
98
51
  target: CreationContext
99
- ): 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
+ ): TStrategyState {
53
+ return clearStrategyStateEntry(strategyState, target);
110
54
  }
@@ -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,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
  }
@@ -0,0 +1,42 @@
1
+ import type {
2
+ Step,
3
+ StrategyId,
4
+ } from '@toolproof-core/schema';
5
+ import { getIndependentThreads } from '../parallelizeSteps.js';
6
+ import { CONSTANTS } from '../../artifacts/artifacts.js';
7
+
8
+ export type ThreadableStrategy<TStrategyState = unknown> = {
9
+ id: StrategyId;
10
+ strategyState: TStrategyState;
11
+ } & (
12
+ | { steps: Step[] }
13
+ | { stepsByThreadIndex: Step[][] }
14
+ );
15
+
16
+ export type ThreadedStrategyLike<TStrategyState = unknown> = {
17
+ id: StrategyId;
18
+ strategyKind: typeof CONSTANTS.Enums.StrategyKind.threaded;
19
+ stepsByThreadIndex: Step[][];
20
+ strategyState: TStrategyState;
21
+ };
22
+
23
+ export function getThreadedStrategyStepGroups<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): Step[][] {
24
+ if ('steps' in strategy) {
25
+ return getIndependentThreads(strategy.steps, strategy.strategyState as any);
26
+ }
27
+
28
+ return strategy.stepsByThreadIndex;
29
+ }
30
+
31
+ export function buildThreadedStrategy<TStrategyState>(
32
+ threadStepGroups: Step[][],
33
+ threadedStrategyId: StrategyId,
34
+ strategy: ThreadableStrategy<TStrategyState>,
35
+ ): ThreadedStrategyLike<TStrategyState> {
36
+ return {
37
+ id: threadedStrategyId,
38
+ strategyKind: CONSTANTS.Enums.StrategyKind.threaded,
39
+ stepsByThreadIndex: threadStepGroups,
40
+ strategyState: strategy.strategyState,
41
+ };
42
+ }