@toolproof-core/lib 1.0.33 → 1.0.36

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 (90) hide show
  1. package/dist/integrations/firebase/createStep.d.ts +2 -3
  2. package/dist/integrations/firebase/createStep.js +7 -33
  3. package/dist/integrations/firebase/createThreadedStrategy.d.ts +2 -0
  4. package/dist/integrations/firebase/createThreadedStrategy.js +8 -0
  5. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +4 -10
  6. package/dist/integrations/firebase/firebaseAdminHelpers.js +43 -136
  7. package/dist/integrations/firebase/firebaseAdminInit.d.ts +0 -1
  8. package/dist/integrations/firebase/firebaseAdminInit.js +0 -1
  9. package/dist/lookups/lookups.d.ts +221 -0
  10. package/dist/lookups/lookups.js +23 -0
  11. package/dist/types/types.d.ts +8 -12
  12. package/dist/types/types.js +2 -3
  13. package/dist/utils/creation/resourceCreation.d.ts +18 -18
  14. package/dist/utils/creation/resourceCreation.js +49 -48
  15. package/dist/utils/creation/stepCreation.d.ts +7 -48
  16. package/dist/utils/creation/stepCreation.js +16 -42
  17. package/dist/utils/creation/threadedStrategyCreation.d.ts +18 -0
  18. package/dist/utils/creation/threadedStrategyCreation.js +16 -0
  19. package/dist/utils/extractData.d.ts +9 -10
  20. package/dist/utils/extractData.js +29 -25
  21. package/dist/utils/parallelizeSteps.d.ts +1 -2
  22. package/dist/utils/parallelizeSteps.js +52 -37
  23. package/dist/utils/resolveStrategyStateChain.d.ts +20 -0
  24. package/dist/utils/{resolveResourceChain.js → resolveStrategyStateChain.js} +6 -11
  25. package/dist/utils/roleSpec.d.ts +7 -3
  26. package/dist/utils/roleSpec.js +0 -1
  27. package/dist/utils/strategyState.d.ts +11 -6
  28. package/dist/utils/strategyState.js +41 -12
  29. package/dist/utils_2/threadedStrategyCreation.d.ts +5 -0
  30. package/dist/utils_2/threadedStrategyCreation.js +12 -0
  31. package/package.json +15 -15
  32. package/src/integrations/firebase/createStep.ts +9 -39
  33. package/src/integrations/firebase/createThreadedStrategy.ts +19 -0
  34. package/src/integrations/firebase/firebaseAdminHelpers.ts +61 -163
  35. package/src/lookups/lookups.ts +25 -0
  36. package/src/types/types.ts +9 -12
  37. package/src/utils/creation/resourceCreation.ts +140 -121
  38. package/src/utils/creation/stepCreation.ts +25 -95
  39. package/src/utils/creation/threadedStrategyCreation.ts +42 -0
  40. package/src/utils/extractData.ts +46 -38
  41. package/src/utils/parallelizeSteps.ts +92 -40
  42. package/src/utils/resolveStrategyStateChain.ts +58 -0
  43. package/src/utils/roleSpec.ts +7 -3
  44. package/src/utils/strategyState.ts +99 -27
  45. package/src/utils_2/threadedStrategyCreation.ts +19 -0
  46. package/tsconfig.tsbuildinfo +1 -1
  47. package/dist/artifacts/artifacts.d.ts +0 -175
  48. package/dist/artifacts/artifacts.d.ts.map +0 -1
  49. package/dist/artifacts/artifacts.js +0 -48
  50. package/dist/artifacts/artifacts.js.map +0 -1
  51. package/dist/integrations/firebase/createRunnableStrategy.d.ts +0 -3
  52. package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +0 -1
  53. package/dist/integrations/firebase/createRunnableStrategy.js +0 -9
  54. package/dist/integrations/firebase/createRunnableStrategy.js.map +0 -1
  55. package/dist/integrations/firebase/createStep.d.ts.map +0 -1
  56. package/dist/integrations/firebase/createStep.js.map +0 -1
  57. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +0 -1
  58. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +0 -1
  59. package/dist/integrations/firebase/firebaseAdminInit.d.ts.map +0 -1
  60. package/dist/integrations/firebase/firebaseAdminInit.js.map +0 -1
  61. package/dist/types/types.d.ts.map +0 -1
  62. package/dist/types/types.js.map +0 -1
  63. package/dist/utils/bindInputRoleToResource.d.ts +0 -6
  64. package/dist/utils/bindInputRoleToResource.d.ts.map +0 -1
  65. package/dist/utils/bindInputRoleToResource.js +0 -25
  66. package/dist/utils/bindInputRoleToResource.js.map +0 -1
  67. package/dist/utils/creation/resourceCreation.d.ts.map +0 -1
  68. package/dist/utils/creation/resourceCreation.js.map +0 -1
  69. package/dist/utils/creation/runnableStrategyCreation.d.ts +0 -4
  70. package/dist/utils/creation/runnableStrategyCreation.d.ts.map +0 -1
  71. package/dist/utils/creation/runnableStrategyCreation.js +0 -17
  72. package/dist/utils/creation/runnableStrategyCreation.js.map +0 -1
  73. package/dist/utils/creation/stepCreation.d.ts.map +0 -1
  74. package/dist/utils/creation/stepCreation.js.map +0 -1
  75. package/dist/utils/extractData.d.ts.map +0 -1
  76. package/dist/utils/extractData.js.map +0 -1
  77. package/dist/utils/parallelizeSteps.d.ts.map +0 -1
  78. package/dist/utils/parallelizeSteps.js.map +0 -1
  79. package/dist/utils/resolveResourceChain.d.ts +0 -19
  80. package/dist/utils/resolveResourceChain.d.ts.map +0 -1
  81. package/dist/utils/resolveResourceChain.js.map +0 -1
  82. package/dist/utils/roleSpec.d.ts.map +0 -1
  83. package/dist/utils/roleSpec.js.map +0 -1
  84. package/dist/utils/strategyState.d.ts.map +0 -1
  85. package/dist/utils/strategyState.js.map +0 -1
  86. package/src/artifacts/artifacts.ts +0 -49
  87. package/src/integrations/firebase/createRunnableStrategy.ts +0 -18
  88. package/src/utils/bindInputRoleToResource.ts +0 -53
  89. package/src/utils/creation/runnableStrategyCreation.ts +0 -28
  90. package/src/utils/resolveResourceChain.ts +0 -52
@@ -1,66 +1,67 @@
1
- export function generatePath(resourceTypeId, id) {
2
- return `${resourceTypeId}/${id}.json`;
3
- }
4
- export function createMaterializedFromOutputPotential(outputPotential, nucleus, timestamp) {
5
- const { id, resourceTypeId, creationContext } = outputPotential;
6
- const path = generatePath(resourceTypeId, id);
1
+ export function createExternalInputPotential() {
7
2
  return {
8
- id,
9
- resourceTypeId,
10
- provenance: {
11
- resourceProvenanceKind: 'strategy',
12
- strategyRunId: outputPotential.strategyRunId ?? 'STRATEGY_RUN-UNKNOWN',
13
- creationContext,
14
- },
15
- version: 1,
16
- timestamp: timestamp ?? new Date().toISOString(),
17
- path,
18
- nucleus,
3
+ strategyStateInputKind: 'externalInputPotential',
19
4
  };
20
5
  }
21
- export function createMaterializedFromInputPotential(inputPotential, nucleus, timestamp) {
22
- const { id, resourceTypeId, creationContext } = inputPotential;
23
- const path = generatePath(resourceTypeId, id);
6
+ export function createInternalInputPotential(toolStepRoleAddress) {
24
7
  return {
25
- id,
26
- resourceTypeId,
27
- provenance: {
28
- resourceProvenanceKind: 'strategy',
29
- strategyRunId: inputPotential.strategyRunId ?? 'STRATEGY_RUN-UNKNOWN',
30
- creationContext,
31
- },
32
- version: 1,
33
- timestamp: timestamp ?? new Date().toISOString(),
34
- path,
35
- nucleus,
8
+ strategyStateInputKind: 'internalInputPotential',
9
+ toolStepRoleAddress,
36
10
  };
37
11
  }
38
- export function createMaterializedFromPotential(potential, nucleus, timestamp) {
39
- if ('strategyStateInputKind' in potential && potential.strategyStateInputKind === 'referenceInput') {
40
- return createMaterializedFromInputPotential(potential, nucleus, timestamp);
41
- }
42
- return createMaterializedFromOutputPotential(potential, nucleus, timestamp);
12
+ export function createStrategyProvenance(strategyTraceHandle, toolStepRoleAddress) {
13
+ return {
14
+ provenanceKind: 'strategy',
15
+ strategyTraceHandle,
16
+ toolStepRoleAddress,
17
+ };
43
18
  }
44
- export function createExternalInputPotential(_id, _resourceTypeId) {
19
+ export function createRuntimeProvenance() {
45
20
  return {
46
- strategyStateInputKind: 'externalInput',
21
+ provenanceKind: 'runtime',
47
22
  };
48
23
  }
49
- export function createInputPotential(id, resourceTypeId, creationContext) {
24
+ export function createResourcePointer(id, resourceTypeHandle, provenance) {
50
25
  return {
51
26
  id,
52
- resourceTypeId,
53
- strategyStateInputKind: 'referenceInput',
54
- toolStepPath: creationContext.toolStepPath,
55
- roleName: creationContext.roleName,
56
- creationContext,
27
+ resourceTypeHandle,
28
+ provenance,
57
29
  };
58
30
  }
59
- export function createOutputPotential(id, resourceTypeId, creationContext) {
31
+ export function createResource(resourcePointer, projection) {
32
+ return {
33
+ ...resourcePointer,
34
+ projection,
35
+ };
36
+ }
37
+ export function createMaterializedFromOutputPotential(outputPotential, projection) {
38
+ const provenance = outputPotential.provenanceKind === 'runtime'
39
+ ? createRuntimeProvenance()
40
+ : createStrategyProvenance(requireStrategyTraceHandle(outputPotential.strategyTraceHandle), requireToolStepRoleAddress(outputPotential.toolStepRoleAddress));
41
+ const resourcePointer = createResourcePointer(outputPotential.id, outputPotential.resourceTypeHandle, provenance);
42
+ return createResource(resourcePointer, projection);
43
+ }
44
+ export function createMaterializedFromPotential(outputPotential, projection) {
45
+ return createMaterializedFromOutputPotential(outputPotential, projection);
46
+ }
47
+ export function createOutputPotential(id, resourceTypeHandle, toolStepRoleAddress, strategyTraceHandle, provenanceKind) {
60
48
  return {
61
49
  id,
62
- resourceTypeId,
63
- creationContext,
50
+ resourceTypeHandle,
51
+ ...(toolStepRoleAddress ? { toolStepRoleAddress } : {}),
52
+ ...(strategyTraceHandle ? { strategyTraceHandle } : {}),
53
+ ...(provenanceKind ? { provenanceKind } : {}),
64
54
  };
65
55
  }
66
- //# sourceMappingURL=resourceCreation.js.map
56
+ function requireStrategyTraceHandle(strategyTraceHandle) {
57
+ if (!strategyTraceHandle) {
58
+ throw new Error('strategyTraceHandle is required to materialize a strategy output potential');
59
+ }
60
+ return strategyTraceHandle;
61
+ }
62
+ function requireToolStepRoleAddress(toolStepRoleAddress) {
63
+ if (!toolStepRoleAddress) {
64
+ throw new Error('toolStepRoleAddress is required to materialize a strategy output potential');
65
+ }
66
+ return toolStepRoleAddress;
67
+ }
@@ -1,51 +1,10 @@
1
- import type { BranchStep, ForStep, Tool, ToolStep, WhileStep } from '@toolproof-core/schema';
2
- import { CONSTANTS } from '../../artifacts/artifacts.js';
3
- type ToolStepId = string;
4
- type BranchStepId = string;
5
- type ForStepId = string;
6
- type WhileStepId = string;
7
- type ToolStepWithId = ToolStep & {
8
- id: ToolStepId;
9
- };
10
- type BranchStepWithId = BranchStep & {
11
- id: BranchStepId;
12
- };
13
- type ForStepWithId = ForStep & {
14
- id: ForStepId;
15
- };
16
- type WhileStepWithId = WhileStep & {
17
- id: WhileStepId;
18
- };
19
- export type LoopStepBuildIdentities = {
20
- stepKind: typeof CONSTANTS.Enums.StepKind.for;
21
- stepId: ForStepId;
22
- whatId: ToolStepId;
23
- whenId: ToolStepId;
24
- } | {
25
- stepKind: typeof CONSTANTS.Enums.StepKind.while;
26
- stepId: WhileStepId;
27
- whatId: ToolStepId;
28
- whenId: ToolStepId;
29
- };
30
- export type BranchCaseBuildIdentities = {
31
- whatId: ToolStepId;
32
- whenId: ToolStepId;
33
- };
34
- export declare function buildToolStepFromTool(tool: Tool, id: ToolStepId): ToolStepWithId;
35
- export declare function buildLoopStepFromToolPair(whatTool: Tool, whenTool: Tool, identities: LoopStepBuildIdentities): ForStep | WhileStep;
1
+ import type { BranchStep, ForStep, Tool, ToolStep, WhileStep } from '@toolproof-core/genesis';
2
+ import { CONSTANTS } from '../../lookups/lookups.js';
3
+ export declare function buildToolStepFromTool(tool: Tool): ToolStep;
4
+ export declare function buildLoopStepFromToolPair(whatTool: Tool, whenTool: Tool, stepKind: typeof CONSTANTS.Enums.StepKind.for | typeof CONSTANTS.Enums.StepKind.while): ForStep | WhileStep;
36
5
  export declare function buildBranchStepFromToolPairs(cases: Array<{
37
6
  whatTool: Tool;
38
7
  whenTool: Tool;
39
- }>, branchId: BranchStepId, caseIdentities: BranchCaseBuildIdentities[]): BranchStepWithId;
40
- export declare function cloneForStepWithIdentities(forStep: ForStep, identities: {
41
- stepId: ForStepId;
42
- whatId: ToolStepId;
43
- whenId: ToolStepId;
44
- }): ForStepWithId;
45
- export declare function cloneWhileStepWithIdentities(whileStep: WhileStep, identities: {
46
- stepId: WhileStepId;
47
- whatId: ToolStepId;
48
- whenId: ToolStepId;
49
- }): WhileStepWithId;
50
- export {};
51
- //# sourceMappingURL=stepCreation.d.ts.map
8
+ }>): BranchStep;
9
+ export declare function cloneForStep(forStep: ForStep): ForStep;
10
+ export declare function cloneWhileStep(whileStep: WhileStep): WhileStep;
@@ -1,4 +1,4 @@
1
- import { CONSTANTS } from '../../artifacts/artifacts.js';
1
+ import { CONSTANTS } from '../../lookups/lookups.js';
2
2
  import { getInputRoleNames, getOutputRoleNames } from '../roleSpec.js';
3
3
  function assertNonEmpty(arr, msg) {
4
4
  if (arr.length === 0) {
@@ -11,80 +11,54 @@ function getRoleBindingSpec(tool) {
11
11
  outputBindings: getOutputRoleNames(tool),
12
12
  };
13
13
  }
14
- export function buildToolStepFromTool(tool, id) {
14
+ export function buildToolStepFromTool(tool) {
15
15
  return {
16
- id,
17
16
  stepKind: CONSTANTS.Enums.StepKind.tool,
18
- toolId: tool.id,
17
+ toolHandle: tool.handle,
19
18
  roleBindingSpec: getRoleBindingSpec(tool),
20
19
  };
21
20
  }
22
- export function buildLoopStepFromToolPair(whatTool, whenTool, identities) {
23
- const what = buildToolStepFromTool(whatTool, identities.whatId);
24
- const when = buildToolStepFromTool(whenTool, identities.whenId);
25
- if (identities.stepKind === CONSTANTS.Enums.StepKind.for) {
21
+ export function buildLoopStepFromToolPair(whatTool, whenTool, stepKind) {
22
+ const what = buildToolStepFromTool(whatTool);
23
+ const when = buildToolStepFromTool(whenTool);
24
+ if (stepKind === CONSTANTS.Enums.StepKind.for) {
26
25
  return {
27
- id: identities.stepId,
28
26
  stepKind: CONSTANTS.Enums.StepKind.for,
29
27
  case: { what, when },
30
28
  };
31
29
  }
32
30
  return {
33
- id: identities.stepId,
34
31
  stepKind: CONSTANTS.Enums.StepKind.while,
35
32
  case: { what, when },
36
33
  };
37
34
  }
38
- export function buildBranchStepFromToolPairs(cases, branchId, caseIdentities) {
39
- if (cases.length !== caseIdentities.length) {
40
- throw new Error('buildBranchStepFromToolPairs requires one id pair per case');
41
- }
35
+ export function buildBranchStepFromToolPairs(cases) {
42
36
  const resolved = cases.map(({ whatTool, whenTool }, index) => {
43
- const identities = caseIdentities[index];
44
- if (!identities) {
45
- throw new Error(`Missing case identities for case index ${index}`);
46
- }
47
- const what = buildToolStepFromTool(whatTool, identities.whatId);
48
- const when = buildToolStepFromTool(whenTool, identities.whenId);
37
+ const what = buildToolStepFromTool(whatTool);
38
+ const when = buildToolStepFromTool(whenTool);
49
39
  return { what, when };
50
40
  });
51
41
  assertNonEmpty(resolved, 'buildBranchStepFromToolPairs requires at least one case');
52
42
  return {
53
- id: branchId,
54
43
  stepKind: CONSTANTS.Enums.StepKind.branch,
55
44
  cases: resolved,
56
45
  };
57
46
  }
58
- export function cloneForStepWithIdentities(forStep, identities) {
47
+ export function cloneForStep(forStep) {
59
48
  return {
60
- id: identities.stepId,
61
49
  stepKind: CONSTANTS.Enums.StepKind.for,
62
50
  case: {
63
- what: {
64
- ...forStep.case.what,
65
- id: identities.whatId,
66
- },
67
- when: {
68
- ...forStep.case.when,
69
- id: identities.whenId,
70
- },
51
+ what: { ...forStep.case.what },
52
+ when: { ...forStep.case.when },
71
53
  },
72
54
  };
73
55
  }
74
- export function cloneWhileStepWithIdentities(whileStep, identities) {
56
+ export function cloneWhileStep(whileStep) {
75
57
  return {
76
- id: identities.stepId,
77
58
  stepKind: CONSTANTS.Enums.StepKind.while,
78
59
  case: {
79
- what: {
80
- ...whileStep.case.what,
81
- id: identities.whatId,
82
- },
83
- when: {
84
- ...whileStep.case.when,
85
- id: identities.whenId,
86
- },
60
+ what: { ...whileStep.case.what },
61
+ when: { ...whileStep.case.when },
87
62
  },
88
63
  };
89
64
  }
90
- //# sourceMappingURL=stepCreation.js.map
@@ -0,0 +1,18 @@
1
+ import type { Step, StrategyHandle } from '@toolproof-core/genesis';
2
+ import { CONSTANTS } from '../../lookups/lookups.js';
3
+ export type ThreadableStrategy<TStrategyState = unknown> = {
4
+ handle: StrategyHandle;
5
+ strategyState: TStrategyState;
6
+ } & ({
7
+ steps: Step[];
8
+ } | {
9
+ stepsByThreadIndex: Step[][];
10
+ });
11
+ export type ThreadedStrategyLike<TStrategyState = unknown> = {
12
+ handle: StrategyHandle;
13
+ strategyKind: typeof CONSTANTS.Enums.StrategyKind.threaded;
14
+ stepsByThreadIndex: Step[][];
15
+ strategyState: TStrategyState;
16
+ };
17
+ export declare function getThreadedStrategyStepGroups<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): Step[][];
18
+ export declare function buildThreadedStrategy<TStrategyState>(threadStepGroups: Step[][], threadedStrategyHandle: StrategyHandle, strategy: ThreadableStrategy<TStrategyState>): ThreadedStrategyLike<TStrategyState>;
@@ -0,0 +1,16 @@
1
+ import { getIndependentThreads } from '../parallelizeSteps.js';
2
+ import { CONSTANTS } from '../../lookups/lookups.js';
3
+ export function getThreadedStrategyStepGroups(strategy) {
4
+ if ('steps' in strategy) {
5
+ return getIndependentThreads(strategy.steps, strategy.strategyState);
6
+ }
7
+ return strategy.stepsByThreadIndex;
8
+ }
9
+ export function buildThreadedStrategy(threadStepGroups, threadedStrategyHandle, strategy) {
10
+ return {
11
+ handle: threadedStrategyHandle,
12
+ strategyKind: CONSTANTS.Enums.StrategyKind.threaded,
13
+ stepsByThreadIndex: threadStepGroups,
14
+ strategyState: strategy.strategyState,
15
+ };
16
+ }
@@ -1,13 +1,12 @@
1
- import type { RoleName, Strategy, ToolId, Tool, ToolStep, ResourceId, ResourceTypeId, Resource } from '@toolproof-core/schema';
2
- import { type ToolRoleDescriptor } from './roleSpec.js';
3
- export declare function extractToolIdsFromRawStrategy(rawStrategy: Strategy): ToolId[];
4
- export declare function extractToolStepsFromRawStrategy(rawStrategy: Strategy): ToolStep[];
5
- export declare function extractRoleMapFromRawStrategy(rawStrategy: Strategy, toolMap: Map<ToolId, Tool>): Map<RoleName, ToolRoleDescriptor>;
6
- export declare function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeId | null;
7
- export declare function extractResourceMapForType<TResource extends Resource = Resource>(resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>, resourceTypeId: ResourceTypeId): Map<ResourceId, TResource>;
8
- export declare function extractIdKeyedNucleusMapForType<TKey extends string = string, TNucleus extends {
1
+ import type { RoleName, ToolHandle, Tool, ToolStep, ResourceId, ResourceTypeHandle, Resource } from '@toolproof-core/genesis';
2
+ import { type StrategyStepGraph, type ToolRoleDescriptor } from './roleSpec.js';
3
+ export declare function extractToolHandlesFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph): ToolHandle[];
4
+ export declare function extractToolStepsFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph): ToolStep[];
5
+ export declare function extractRoleMapFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph, toolMap: Map<ToolHandle, Tool>): Map<RoleName, ToolRoleDescriptor>;
6
+ export declare function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeHandle | null;
7
+ export declare function extractResourceMapForType<TResource extends Resource = Resource>(resourcesByType: Partial<Record<ResourceTypeHandle, TResource[]>>, resourceTypeHandle: ResourceTypeHandle): Map<ResourceId, TResource>;
8
+ export declare function extractIdKeyedValueMapForType<TKey extends string = string, TValue extends {
9
9
  id: string | number | boolean;
10
10
  } = {
11
11
  id: string | number | boolean;
12
- }, TResource extends Resource = Resource>(resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>, resourceTypeId: ResourceTypeId): Map<TKey, TNucleus>;
13
- //# sourceMappingURL=extractData.d.ts.map
12
+ }, TResource extends Resource = Resource>(resourcesByType: Partial<Record<ResourceTypeHandle, TResource[]>>, resourceTypeHandle: ResourceTypeHandle): Map<TKey, TValue>;
@@ -1,21 +1,28 @@
1
- import { CONSTANTS } from '../artifacts/artifacts.js';
2
1
  import { extractToolStepsFromStrategy, getInputRoleEntries, getOutputRoleEntries, toToolRoleDescriptor, } from './roleSpec.js';
3
- export function extractToolIdsFromRawStrategy(rawStrategy) {
4
- const toolSteps = extractToolStepsFromRawStrategy(rawStrategy);
2
+ const ERROR_OUTPUT_ROLE_NAME = 'ErrorOutput';
3
+ function extractResourceTypeHandleFromRoleValue(toolRoleValue) {
4
+ const typeRef = toolRoleValue.typeRef;
5
+ if ('resourceTypeHandle' in typeRef) {
6
+ return typeRef.resourceTypeHandle;
7
+ }
8
+ return null;
9
+ }
10
+ export function extractToolHandlesFromUnthreadedStrategy(unthreadedStrategy) {
11
+ const toolSteps = extractToolStepsFromUnthreadedStrategy(unthreadedStrategy);
5
12
  const ids = new Set();
6
13
  for (const jStep of toolSteps) {
7
- ids.add(jStep.toolId);
14
+ ids.add(jStep.toolHandle);
8
15
  }
9
16
  return Array.from(ids);
10
17
  }
11
- export function extractToolStepsFromRawStrategy(rawStrategy) {
12
- return extractToolStepsFromStrategy(rawStrategy);
18
+ export function extractToolStepsFromUnthreadedStrategy(unthreadedStrategy) {
19
+ return extractToolStepsFromStrategy(unthreadedStrategy);
13
20
  }
14
- export function extractRoleMapFromRawStrategy(rawStrategy, toolMap) {
21
+ export function extractRoleMapFromUnthreadedStrategy(unthreadedStrategy, toolMap) {
15
22
  const roleMap = new Map();
16
- const toolIds = extractToolIdsFromRawStrategy(rawStrategy);
17
- for (const toolId of toolIds) {
18
- const tool = toolMap.get(toolId);
23
+ const toolHandles = extractToolHandlesFromUnthreadedStrategy(unthreadedStrategy);
24
+ for (const toolHandle of toolHandles) {
25
+ const tool = toolMap.get(toolHandle);
19
26
  if (!tool)
20
27
  continue;
21
28
  for (const entry of getInputRoleEntries(tool)) {
@@ -28,16 +35,14 @@ export function extractRoleMapFromRawStrategy(rawStrategy, toolMap) {
28
35
  return roleMap;
29
36
  }
30
37
  export function extractSingleNonErrorOutputTypeId(tool) {
31
- const errorRoleId = CONSTANTS.Cosmos.ROLE_ErrorOutput;
32
- const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== errorRoleId);
38
+ const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== ERROR_OUTPUT_ROLE_NAME);
33
39
  if (!outputEntries || !outputEntries[0] || outputEntries.length !== 1) {
34
- throw new Error(`Tool ${tool.id} must have exactly one non-error output role to be branchable/loopable`);
40
+ throw new Error(`Tool ${tool.handle} must have exactly one non-error output role to be branchable/loopable`);
35
41
  } // ATTENTION_PUREIFY
36
- const outputRole = outputEntries[0][1];
37
- return outputRole.resourceTypeId;
42
+ return extractResourceTypeHandleFromRoleValue(toToolRoleDescriptor(outputEntries[0]));
38
43
  }
39
- export function extractResourceMapForType(resourcesByType, resourceTypeId) {
40
- const resources = resourcesByType[resourceTypeId] ?? [];
44
+ export function extractResourceMapForType(resourcesByType, resourceTypeHandle) {
45
+ const resources = resourcesByType[resourceTypeHandle] ?? [];
41
46
  const map = new Map();
42
47
  for (const resource of resources) {
43
48
  map.set(resource.id, resource);
@@ -45,25 +50,25 @@ export function extractResourceMapForType(resourcesByType, resourceTypeId) {
45
50
  return map;
46
51
  }
47
52
  // Only use this for resource types whose nuclei are entity-like objects with their own intrinsic id.
48
- export function extractIdKeyedNucleusMapForType(resourcesByType, resourceTypeId) {
49
- const resourceMap = extractResourceMapForType(resourcesByType, resourceTypeId);
53
+ export function extractIdKeyedValueMapForType(resourcesByType, resourceTypeHandle) {
54
+ const resourceMap = extractResourceMapForType(resourcesByType, resourceTypeHandle);
50
55
  const out = new Map();
51
56
  for (const resource of resourceMap.values()) {
52
- const data = resource.nucleus;
57
+ const data = resource.projection;
53
58
  if (data === null || data === undefined) {
54
- throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeId}' to have a nucleus`);
59
+ throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have a projection`);
55
60
  }
56
61
  if (typeof data !== 'object') {
57
- throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeId}' to have an object nucleus with id`);
62
+ throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have an object projection with id`);
58
63
  }
59
64
  const maybeId = data.id;
60
65
  if (maybeId === null || maybeId === undefined) {
61
- throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeId}' to have nucleus.id`);
66
+ throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have projection.id`);
62
67
  }
63
68
  if (typeof maybeId !== 'string' &&
64
69
  typeof maybeId !== 'number' &&
65
70
  typeof maybeId !== 'boolean') {
66
- throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeId}' to have nucleus.id as string, number, or boolean`);
71
+ throw new Error(`Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have projection.id as string, number, or boolean`);
67
72
  }
68
73
  const value = data;
69
74
  const key = String(maybeId);
@@ -71,4 +76,3 @@ export function extractIdKeyedNucleusMapForType(resourcesByType, resourceTypeId)
71
76
  }
72
77
  return out;
73
78
  }
74
- //# sourceMappingURL=extractData.js.map
@@ -1,4 +1,3 @@
1
- import type { Step } from '@toolproof-core/schema';
1
+ import type { Step } from '@toolproof-core/genesis';
2
2
  import type { StrategyState } from './strategyState.js';
3
3
  export declare function getIndependentThreads(steps: Step[], strategyState: StrategyState): Step[][];
4
- //# sourceMappingURL=parallelizeSteps.d.ts.map
@@ -1,53 +1,69 @@
1
- import { CONSTANTS } from '../artifacts/artifacts.js';
2
- function getToolStepId(toolStep) {
3
- const maybeId = toolStep?.id;
4
- return typeof maybeId === 'string' ? maybeId : undefined;
1
+ import { CONSTANTS } from '../lookups/lookups.js';
2
+ function toUnthreadedToolStepPath(opts) {
3
+ const base = `/steps/${opts.stepIndex}`;
4
+ switch (opts.toolStepSlot) {
5
+ case 'self':
6
+ return `${base}/self`;
7
+ case 'case.when':
8
+ return `${base}/case/when`;
9
+ case 'case.what':
10
+ return `${base}/case/what`;
11
+ case 'cases.when':
12
+ if (typeof opts.caseIndex !== 'number') {
13
+ throw new Error('Expected caseIndex for cases.when path');
14
+ }
15
+ return `${base}/cases/${opts.caseIndex}/when`;
16
+ case 'cases.what':
17
+ if (typeof opts.caseIndex !== 'number') {
18
+ throw new Error('Expected caseIndex for cases.what path');
19
+ }
20
+ return `${base}/cases/${opts.caseIndex}/what`;
21
+ default: {
22
+ const _exhaustive = opts.toolStepSlot;
23
+ return _exhaustive;
24
+ }
25
+ }
5
26
  }
6
27
  export function getIndependentThreads(steps, strategyState) {
7
28
  const getOwnerLabel = (ownerIndex) => {
8
29
  const step = steps[ownerIndex];
9
30
  return `steps[${ownerIndex}] stepKind=${step?.stepKind ?? 'unknown'}`;
10
31
  };
11
- // Map each toolStep.id (including macro-nested tool steps) to owning top-level step index.
12
- const toolStepIdToOwner = new Map();
13
- const ownerToToolStepIds = new Map();
14
- const toolStepById = new Map();
15
- const addToolStep = (toolStep, ownerIndex) => {
16
- const toolStepId = getToolStepId(toolStep);
17
- if (!toolStepId || !toolStep)
32
+ // Map each canonical toolStepPath (including macro-nested tool steps) to its owning top-level step index.
33
+ const toolStepPathToOwner = new Map();
34
+ const toolStepByPath = new Map();
35
+ const addToolStep = (toolStep, ownerIndex, toolStepPath) => {
36
+ if (!toolStep)
18
37
  return;
19
- const existingOwner = toolStepIdToOwner.get(toolStepId);
38
+ const existingOwner = toolStepPathToOwner.get(toolStepPath);
20
39
  if (existingOwner !== undefined) {
21
- throw new Error(`Duplicate toolStep.id '${toolStepId}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`);
40
+ throw new Error(`Duplicate toolStepPath '${toolStepPath}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`);
22
41
  }
23
- toolStepIdToOwner.set(toolStepId, ownerIndex);
24
- toolStepById.set(toolStepId, toolStep);
25
- const bucket = ownerToToolStepIds.get(ownerIndex) ?? [];
26
- bucket.push(toolStepId);
27
- ownerToToolStepIds.set(ownerIndex, bucket);
42
+ toolStepPathToOwner.set(toolStepPath, ownerIndex);
43
+ toolStepByPath.set(toolStepPath, toolStep);
28
44
  };
29
45
  steps.forEach((step, ownerIndex) => {
30
46
  if (step.stepKind === CONSTANTS.Enums.StepKind.tool) {
31
- addToolStep(step, ownerIndex);
47
+ addToolStep(step, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'self' }));
32
48
  return;
33
49
  }
34
50
  if (step.stepKind === CONSTANTS.Enums.StepKind.for) {
35
51
  const loop = step;
36
- addToolStep(loop.case?.what, ownerIndex);
37
- addToolStep(loop.case?.when, ownerIndex);
52
+ addToolStep(loop.case?.what, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'case.what' }));
53
+ addToolStep(loop.case?.when, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'case.when' }));
38
54
  return;
39
55
  }
40
56
  if (step.stepKind === CONSTANTS.Enums.StepKind.while) {
41
57
  const loop = step;
42
- addToolStep(loop.case?.what, ownerIndex);
43
- addToolStep(loop.case?.when, ownerIndex);
58
+ addToolStep(loop.case?.what, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'case.what' }));
59
+ addToolStep(loop.case?.when, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'case.when' }));
44
60
  return;
45
61
  }
46
62
  if (step.stepKind === CONSTANTS.Enums.StepKind.branch) {
47
63
  const branch = step;
48
- for (const caseItem of branch.cases ?? []) {
49
- addToolStep(caseItem?.what, ownerIndex);
50
- addToolStep(caseItem?.when, ownerIndex);
64
+ for (const [caseIndex, caseItem] of (branch.cases ?? []).entries()) {
65
+ addToolStep(caseItem?.what, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'cases.what', caseIndex }));
66
+ addToolStep(caseItem?.when, ownerIndex, toUnthreadedToolStepPath({ stepIndex: ownerIndex, toolStepSlot: 'cases.when', caseIndex }));
51
67
  }
52
68
  return;
53
69
  }
@@ -66,24 +82,24 @@ export function getIndependentThreads(steps, strategyState) {
66
82
  ensureOwner(i);
67
83
  }
68
84
  // Discover dependencies from strategyState.
69
- for (const [toolStepId, ownerIndex] of toolStepIdToOwner) {
85
+ for (const [toolStepPath, ownerIndex] of toolStepPathToOwner) {
70
86
  ensureOwner(ownerIndex);
71
- const toolStep = toolStepById.get(toolStepId);
87
+ const toolStep = toolStepByPath.get(toolStepPath);
72
88
  const inputBindings = toolStep?.roleBindingSpec?.inputBindings ?? [];
73
- const bucket = strategyState?.[toolStepId] ?? {};
89
+ const bucket = strategyState?.[toolStepPath] ?? {};
74
90
  for (const inputRoleId of inputBindings) {
75
91
  const entry = bucket?.[inputRoleId];
76
- if (!entry || entry.strategyStateInputKind !== 'referenceInput')
92
+ if (!entry || entry.strategyStateInputKind !== 'internalInputPotential')
77
93
  continue;
78
- const creatorToolStepId = typeof entry?.toolStepPath === 'string'
79
- ? entry.toolStepPath
94
+ const creatorToolStepPath = typeof entry?.toolStepRoleAddress?.toolStepPath === 'string'
95
+ ? entry.toolStepRoleAddress.toolStepPath
80
96
  : undefined;
81
- if (!creatorToolStepId) {
82
- throw new Error(`Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): missing toolStepPath for role '${inputRoleId}'`);
97
+ if (!creatorToolStepPath) {
98
+ throw new Error(`Unresolvable internalInputPotential in toolStep '${toolStepPath}' (${getOwnerLabel(ownerIndex)}): missing toolStepRoleAddress.toolStepPath for role '${inputRoleId}'`);
83
99
  }
84
- const producerOwner = toolStepIdToOwner.get(creatorToolStepId);
100
+ const producerOwner = toolStepPathToOwner.get(creatorToolStepPath);
85
101
  if (producerOwner === undefined) {
86
- throw new Error(`Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): creator toolStepPath '${creatorToolStepId}' not found in strategy steps`);
102
+ throw new Error(`Unresolvable internalInputPotential in toolStep '${toolStepPath}' (${getOwnerLabel(ownerIndex)}): source toolStepPath '${creatorToolStepPath}' not found in strategy steps`);
87
103
  }
88
104
  ensureOwner(producerOwner);
89
105
  ownerAdj.get(ownerIndex).add(producerOwner);
@@ -141,4 +157,3 @@ export function getIndependentThreads(steps, strategyState) {
141
157
  })
142
158
  .filter((group) => group.length > 0);
143
159
  }
144
- //# sourceMappingURL=parallelizeSteps.js.map
@@ -0,0 +1,20 @@
1
+ import type { ToolStepRoleAddress, ExternalInputPotential, InputResource, ThreadedStrategyState, ThreadedToolStepRoleAddress, UnthreadedStrategyState, UnthreadedToolStepRoleAddress } from '@toolproof-core/genesis';
2
+ import { type StrategyState } from './strategyState.js';
3
+ type ToolStepRoleAddressForStrategyState<TStrategyState extends StrategyState> = TStrategyState extends UnthreadedStrategyState ? UnthreadedToolStepRoleAddress : TStrategyState extends ThreadedStrategyState ? ThreadedToolStepRoleAddress : ToolStepRoleAddress;
4
+ export type ResolveResult = {
5
+ status: 'inputResource';
6
+ entry: InputResource;
7
+ path: ToolStepRoleAddress[];
8
+ } | {
9
+ status: 'externalInputPotential';
10
+ entry: ExternalInputPotential;
11
+ path: ToolStepRoleAddress[];
12
+ } | {
13
+ status: 'unresolved';
14
+ reason: 'not-found' | 'cycle' | 'depth-exceeded';
15
+ path: ToolStepRoleAddress[];
16
+ };
17
+ export declare function resolveStrategyStateChain<TStrategyState extends StrategyState>(strategyState: TStrategyState, start: ToolStepRoleAddressForStrategyState<TStrategyState>, opts?: {
18
+ maxDepth?: number;
19
+ }): ResolveResult;
20
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { getStrategyStateEntry } from './strategyState.js';
2
- export function resolveResourceChain(strategyState, start, opts) {
2
+ export function resolveStrategyStateChain(strategyState, start, opts) {
3
3
  const maxDepth = opts?.maxDepth ?? 50;
4
4
  const visited = new Set();
5
5
  const path = [];
@@ -14,18 +14,13 @@ export function resolveResourceChain(strategyState, start, opts) {
14
14
  const entry = getStrategyStateEntry(strategyState, current);
15
15
  if (!entry)
16
16
  return { status: 'unresolved', reason: 'not-found', path };
17
- if (entry.strategyStateInputKind === 'valueInput') {
18
- return { status: 'valueInput', entry: entry, path };
17
+ if (entry.strategyStateInputKind === 'inputResource') {
18
+ return { status: 'inputResource', entry, path };
19
19
  }
20
- if (entry.strategyStateInputKind === 'externalInput') {
21
- return { status: 'externalInput', entry: entry, path };
20
+ if (entry.strategyStateInputKind === 'externalInputPotential') {
21
+ return { status: 'externalInputPotential', entry, path };
22
22
  }
23
- current = {
24
- toolStepPath: entry.toolStepPath,
25
- roleName: entry.roleName,
26
- };
27
- continue;
23
+ current = entry.toolStepRoleAddress;
28
24
  }
29
25
  return { status: 'unresolved', reason: 'depth-exceeded', path };
30
26
  }
31
- //# sourceMappingURL=resolveResourceChain.js.map