@toolproof-core/lib 1.0.36 → 1.0.37

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 (78) hide show
  1. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +1 -2
  2. package/dist/integrations/firebase/firebaseAdminHelpers.js +5 -14
  3. package/dist/lookups/lookups.d.ts +17 -24
  4. package/dist/lookups/lookups.js +10 -0
  5. package/dist/types/types.d.ts +10 -6
  6. package/dist/utils/cosmosDataExtraction.d.ts +5 -0
  7. package/dist/utils/cosmosDataExtraction.js +15 -0
  8. package/dist/utils/identifierGeneration.d.ts +2 -0
  9. package/dist/utils/identifierGeneration.js +5 -0
  10. package/dist/utils/mutableStrategyOverlay.d.ts +55 -0
  11. package/dist/utils/mutableStrategyOverlay.js +115 -0
  12. package/dist/utils/resourceCreation.d.ts +11 -0
  13. package/dist/utils/resourceCreation.js +32 -0
  14. package/dist/utils/stepCreation.d.ts +9 -0
  15. package/dist/utils/stepCreation.js +79 -0
  16. package/dist/utils/stepParallelization.d.ts +2 -0
  17. package/dist/utils/stepParallelization.js +129 -0
  18. package/dist/utils/strategyAssembly.d.ts +3 -0
  19. package/dist/utils/strategyAssembly.js +6 -0
  20. package/dist/utils/strategyCanonicalization.d.ts +4 -0
  21. package/dist/utils/strategyCanonicalization.js +215 -0
  22. package/dist/utils/strategyExtraction.d.ts +17 -0
  23. package/dist/utils/strategyExtraction.js +88 -0
  24. package/dist/utils/strategyStateResolution.d.ts +17 -0
  25. package/dist/utils/{resolveStrategyStateChain.js → strategyStateResolution.js} +5 -4
  26. package/dist/utils/strategyThreading.d.ts +2 -0
  27. package/dist/utils/strategyThreading.js +10 -0
  28. package/dist/utils/toolStepPaths.d.ts +2 -0
  29. package/dist/utils/toolStepPaths.js +21 -0
  30. package/package.json +41 -21
  31. package/src/integrations/firebase/firebaseAdminHelpers.ts +5 -17
  32. package/src/lookups/lookups.ts +16 -6
  33. package/src/types/types.ts +10 -4
  34. package/src/utils/cosmosDataExtraction.ts +25 -0
  35. package/src/utils/identifierGeneration.ts +12 -0
  36. package/src/utils/mutableStrategyOverlay.ts +286 -0
  37. package/src/utils/resourceCreation.ts +87 -0
  38. package/src/utils/stepCreation.ts +114 -0
  39. package/src/utils/stepParallelization.ts +181 -0
  40. package/src/utils/strategyAssembly.ts +14 -0
  41. package/src/utils/strategyCanonicalization.ts +294 -0
  42. package/src/utils/strategyExtraction.ts +150 -0
  43. package/src/utils/strategyStateResolution.ts +57 -0
  44. package/src/utils/strategyThreading.ts +27 -0
  45. package/src/utils/toolStepPaths.ts +34 -0
  46. package/tsconfig.tsbuildinfo +1 -1
  47. package/dist/integrations/firebase/createStep.d.ts +0 -10
  48. package/dist/integrations/firebase/createStep.js +0 -17
  49. package/dist/integrations/firebase/createThreadedStrategy.d.ts +0 -2
  50. package/dist/integrations/firebase/createThreadedStrategy.js +0 -8
  51. package/dist/utils/creation/resourceCreation.d.ts +0 -21
  52. package/dist/utils/creation/resourceCreation.js +0 -67
  53. package/dist/utils/creation/stepCreation.d.ts +0 -10
  54. package/dist/utils/creation/stepCreation.js +0 -64
  55. package/dist/utils/creation/threadedStrategyCreation.d.ts +0 -18
  56. package/dist/utils/creation/threadedStrategyCreation.js +0 -16
  57. package/dist/utils/extractData.d.ts +0 -12
  58. package/dist/utils/extractData.js +0 -78
  59. package/dist/utils/parallelizeSteps.d.ts +0 -3
  60. package/dist/utils/parallelizeSteps.js +0 -159
  61. package/dist/utils/resolveStrategyStateChain.d.ts +0 -20
  62. package/dist/utils/roleSpec.d.ts +0 -16
  63. package/dist/utils/roleSpec.js +0 -57
  64. package/dist/utils/strategyState.d.ts +0 -12
  65. package/dist/utils/strategyState.js +0 -58
  66. package/dist/utils_2/threadedStrategyCreation.d.ts +0 -5
  67. package/dist/utils_2/threadedStrategyCreation.js +0 -12
  68. package/src/integrations/firebase/createStep.ts +0 -41
  69. package/src/integrations/firebase/createThreadedStrategy.ts +0 -19
  70. package/src/utils/creation/resourceCreation.ts +0 -140
  71. package/src/utils/creation/stepCreation.ts +0 -95
  72. package/src/utils/creation/threadedStrategyCreation.ts +0 -42
  73. package/src/utils/extractData.ts +0 -137
  74. package/src/utils/parallelizeSteps.ts +0 -239
  75. package/src/utils/resolveStrategyStateChain.ts +0 -58
  76. package/src/utils/roleSpec.ts +0 -84
  77. package/src/utils/strategyState.ts +0 -128
  78. package/src/utils_2/threadedStrategyCreation.ts +0 -19
@@ -0,0 +1,129 @@
1
+ import { CONSTANTS } from '../lookups/lookups.js';
2
+ export function getIndependentThreads(steps, strategyState) {
3
+ const getOwnerLabel = (ownerIndex) => {
4
+ const step = steps[ownerIndex];
5
+ return `steps[${ownerIndex}] stepKind=${step?.stepKind ?? 'unknown'}`;
6
+ };
7
+ const toolStepKeyToOwner = new Map();
8
+ const toolStepByKey = new Map();
9
+ const addToolStep = (toolStep, ownerIndex) => {
10
+ if (!toolStep)
11
+ return;
12
+ const existingOwner = toolStepKeyToOwner.get(toolStep.toolStepKey);
13
+ if (existingOwner !== undefined) {
14
+ throw new Error(`Duplicate toolStepKey '${toolStep.toolStepKey}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`);
15
+ }
16
+ toolStepKeyToOwner.set(toolStep.toolStepKey, ownerIndex);
17
+ toolStepByKey.set(toolStep.toolStepKey, toolStep);
18
+ };
19
+ steps.forEach((step, ownerIndex) => {
20
+ if (step.stepKind === CONSTANTS.Enums.StepKind.tool) {
21
+ addToolStep(step, ownerIndex);
22
+ return;
23
+ }
24
+ if (step.stepKind === CONSTANTS.Enums.StepKind.for) {
25
+ const loop = step;
26
+ addToolStep(loop.case?.what, ownerIndex);
27
+ addToolStep(loop.case?.when, ownerIndex);
28
+ return;
29
+ }
30
+ if (step.stepKind === CONSTANTS.Enums.StepKind.while) {
31
+ const loop = step;
32
+ addToolStep(loop.case?.what, ownerIndex);
33
+ addToolStep(loop.case?.when, ownerIndex);
34
+ return;
35
+ }
36
+ if (step.stepKind === CONSTANTS.Enums.StepKind.branch) {
37
+ const branch = step;
38
+ for (const stepCase of branch.cases) {
39
+ addToolStep(stepCase?.what, ownerIndex);
40
+ addToolStep(stepCase?.when, ownerIndex);
41
+ }
42
+ }
43
+ });
44
+ const ownerAdj = new Map();
45
+ const ownerDeps = new Map();
46
+ const ensureOwner = (ownerIndex) => {
47
+ if (!ownerAdj.has(ownerIndex))
48
+ ownerAdj.set(ownerIndex, new Set());
49
+ if (!ownerDeps.has(ownerIndex))
50
+ ownerDeps.set(ownerIndex, new Set());
51
+ };
52
+ for (let index = 0; index < steps.length; index++) {
53
+ ensureOwner(index);
54
+ }
55
+ for (const [toolStepKey, ownerIndex] of toolStepKeyToOwner) {
56
+ ensureOwner(ownerIndex);
57
+ const toolStep = toolStepByKey.get(toolStepKey);
58
+ const inputBindings = toolStep?.roleBindingSpec?.inputBindings ?? [];
59
+ const bucket = strategyState[toolStepKey];
60
+ for (const inputRoleId of inputBindings) {
61
+ const entry = bucket?.[inputRoleId];
62
+ if (!entry || entry.strategyStateInputKind !== 'internalInputPotential')
63
+ continue;
64
+ const producerToolStepKey = entry.toolStepRoleAddress?.toolStepKey;
65
+ if (typeof producerToolStepKey !== 'string') {
66
+ throw new Error(`Unresolvable internalInputPotential in toolStep '${toolStepKey}' (${getOwnerLabel(ownerIndex)}): missing toolStepRoleAddress.toolStepKey for role '${inputRoleId}'`);
67
+ }
68
+ const producerOwner = toolStepKeyToOwner.get(producerToolStepKey);
69
+ if (producerOwner === undefined) {
70
+ throw new Error(`Unresolvable internalInputPotential in toolStep '${toolStepKey}' (${getOwnerLabel(ownerIndex)}): source toolStepKey '${producerToolStepKey}' not found in strategy steps`);
71
+ }
72
+ ensureOwner(producerOwner);
73
+ ownerAdj.get(ownerIndex)?.add(producerOwner);
74
+ ownerAdj.get(producerOwner)?.add(ownerIndex);
75
+ ownerDeps.get(ownerIndex)?.add(producerOwner);
76
+ }
77
+ }
78
+ const visited = new Set();
79
+ const components = [];
80
+ for (let ownerIndex = 0; ownerIndex < steps.length; ownerIndex++) {
81
+ if (visited.has(ownerIndex))
82
+ continue;
83
+ const component = [];
84
+ const queue = [ownerIndex];
85
+ visited.add(ownerIndex);
86
+ while (queue.length > 0) {
87
+ const node = queue.shift();
88
+ if (node === undefined)
89
+ break;
90
+ component.push(node);
91
+ for (const neighbor of ownerAdj.get(node) ?? []) {
92
+ if (visited.has(neighbor))
93
+ continue;
94
+ visited.add(neighbor);
95
+ queue.push(neighbor);
96
+ }
97
+ }
98
+ components.push(component);
99
+ }
100
+ for (const component of components) {
101
+ const componentOwners = new Set(component);
102
+ const ownersInOrder = steps.map((_, index) => index).filter((index) => componentOwners.has(index));
103
+ const position = new Map();
104
+ ownersInOrder.forEach((owner, index) => position.set(owner, index));
105
+ for (const consumerOwner of ownersInOrder) {
106
+ const deps = ownerDeps.get(consumerOwner);
107
+ if (!deps || deps.size === 0)
108
+ continue;
109
+ const consumerPos = position.get(consumerOwner);
110
+ if (consumerPos === undefined)
111
+ continue;
112
+ for (const producerOwner of deps) {
113
+ const producerPos = position.get(producerOwner);
114
+ if (producerPos === undefined) {
115
+ throw new Error(`Internal error: dependency producer ${getOwnerLabel(producerOwner)} missing from computed thread for ${getOwnerLabel(consumerOwner)}`);
116
+ }
117
+ if (producerPos >= consumerPos) {
118
+ throw new Error(`Invalid step order in thread: ${getOwnerLabel(consumerOwner)} depends on ${getOwnerLabel(producerOwner)}, but producer is not scheduled before consumer`);
119
+ }
120
+ }
121
+ }
122
+ }
123
+ return components
124
+ .map((component) => {
125
+ const ownerSet = new Set(component);
126
+ return steps.filter((_step, index) => ownerSet.has(index));
127
+ })
128
+ .filter((group) => group.length > 0);
129
+ }
@@ -0,0 +1,3 @@
1
+ import type { Strategy } from '@toolproof-core/genesis';
2
+ import type { MutableStrategy } from './mutableStrategyOverlay.js';
3
+ export declare function composeStrategyForExecution<TKey extends string>(mutableStrategy: MutableStrategy<TKey>): Strategy;
@@ -0,0 +1,6 @@
1
+ import { toExecutionMutableStrategy } from './strategyThreading.js';
2
+ import { mutableStrategyToStrategy } from './strategyCanonicalization.js';
3
+ export function composeStrategyForExecution(mutableStrategy) {
4
+ const executionStrategy = toExecutionMutableStrategy(mutableStrategy);
5
+ return mutableStrategyToStrategy(executionStrategy);
6
+ }
@@ -0,0 +1,4 @@
1
+ import type { Strategy } from '@toolproof-core/genesis';
2
+ import type { MutableStrategy } from './mutableStrategyOverlay.js';
3
+ export declare function strategyToMutableStrategy(strategy: Strategy): MutableStrategy;
4
+ export declare function mutableStrategyToStrategy(mutableStrategy: MutableStrategy): Strategy;
@@ -0,0 +1,215 @@
1
+ import { CONSTANTS } from '../lookups/lookups.js';
2
+ import { generateIdentifier } from './identifierGeneration.js';
3
+ function stripMutableCase(stepCase) {
4
+ const { when, what, ...rest } = stepCase;
5
+ const { toolStepKey: _whenToolStepKey, ...canonicalWhen } = when;
6
+ const { toolStepKey: _whatToolStepKey, ...canonicalWhat } = what;
7
+ return {
8
+ ...rest,
9
+ when: canonicalWhen,
10
+ what: canonicalWhat,
11
+ };
12
+ }
13
+ function buildExecutionPathMap(strategy) {
14
+ const executionPathByKey = new Map();
15
+ const registerStep = (step, pathPrefix) => {
16
+ switch (step.stepKind) {
17
+ case CONSTANTS.Enums.StepKind.tool:
18
+ executionPathByKey.set(step.toolStepKey, `${pathPrefix}/self`);
19
+ return;
20
+ case CONSTANTS.Enums.StepKind.branch:
21
+ step.cases.forEach((stepCase, caseIndex) => {
22
+ executionPathByKey.set(stepCase.when.toolStepKey, `${pathPrefix}/cases/${caseIndex}/when`);
23
+ executionPathByKey.set(stepCase.what.toolStepKey, `${pathPrefix}/cases/${caseIndex}/what`);
24
+ });
25
+ return;
26
+ case CONSTANTS.Enums.StepKind.while:
27
+ case CONSTANTS.Enums.StepKind.for:
28
+ executionPathByKey.set(step.case.when.toolStepKey, `${pathPrefix}/case/when`);
29
+ executionPathByKey.set(step.case.what.toolStepKey, `${pathPrefix}/case/what`);
30
+ return;
31
+ default:
32
+ throw new Error('Unsupported step kind while projecting strategy.');
33
+ }
34
+ };
35
+ strategy.stepsByThreadIndex.forEach((steps, threadIndex) => {
36
+ steps.forEach((step, stepIndex) => {
37
+ registerStep(step, `/threads/${threadIndex}/steps/${stepIndex}`);
38
+ });
39
+ });
40
+ return executionPathByKey;
41
+ }
42
+ function toCanonicalStrategyStateEntry(entry, executionPathByKey) {
43
+ if (entry.strategyStateInputKind !== 'internalInputPotential') {
44
+ return entry;
45
+ }
46
+ const sourceToolStepPath = executionPathByKey.get(entry.toolStepRoleAddress.toolStepKey);
47
+ if (!sourceToolStepPath) {
48
+ throw new Error(`Cannot project strategy state: no execution path found for toolStepKey '${entry.toolStepRoleAddress.toolStepKey}'.`);
49
+ }
50
+ return {
51
+ strategyStateInputKind: 'internalInputPotential',
52
+ toolStepRoleAddress: {
53
+ toolStepPath: sourceToolStepPath,
54
+ roleName: entry.toolStepRoleAddress.roleName,
55
+ },
56
+ };
57
+ }
58
+ function toCanonicalStep(step) {
59
+ switch (step.stepKind) {
60
+ case CONSTANTS.Enums.StepKind.tool: {
61
+ const { toolStepKey: _toolStepKey, ...canonicalToolStep } = step;
62
+ return canonicalToolStep;
63
+ }
64
+ case CONSTANTS.Enums.StepKind.branch: {
65
+ const { macroStepKey: _macroStepKey, cases, ...rest } = step;
66
+ return {
67
+ ...rest,
68
+ cases: cases.map(stripMutableCase),
69
+ };
70
+ }
71
+ case CONSTANTS.Enums.StepKind.while:
72
+ case CONSTANTS.Enums.StepKind.for: {
73
+ const { macroStepKey: _macroStepKey, case: stepCase, ...rest } = step;
74
+ return {
75
+ ...rest,
76
+ case: stripMutableCase(stepCase),
77
+ };
78
+ }
79
+ default:
80
+ throw new Error('Unsupported step kind while projecting steps.');
81
+ }
82
+ }
83
+ function toMutableStrategyStateEntry(entry, executionKeyByPath) {
84
+ if (entry.strategyStateInputKind !== 'internalInputPotential') {
85
+ return entry;
86
+ }
87
+ const sourceToolStepKey = executionKeyByPath.get(entry.toolStepRoleAddress.toolStepPath);
88
+ if (!sourceToolStepKey) {
89
+ throw new Error(`Cannot deproject strategy state: no mutable key found for toolStepPath '${entry.toolStepRoleAddress.toolStepPath}'.`);
90
+ }
91
+ return {
92
+ strategyStateInputKind: 'internalInputPotential',
93
+ toolStepRoleAddress: {
94
+ toolStepKey: sourceToolStepKey,
95
+ roleName: entry.toolStepRoleAddress.roleName,
96
+ },
97
+ };
98
+ }
99
+ function toMutableToolStep(step) {
100
+ return {
101
+ ...step,
102
+ toolStepKey: generateIdentifier('ToolStepKey'),
103
+ };
104
+ }
105
+ function toMutableCase(stepCase) {
106
+ const { when, what, ...rest } = stepCase;
107
+ return {
108
+ ...rest,
109
+ when: toMutableToolStep(when),
110
+ what: toMutableToolStep(what),
111
+ };
112
+ }
113
+ function toMutableBranchStep(step) {
114
+ const { cases, ...rest } = step;
115
+ return {
116
+ ...rest,
117
+ macroStepKey: generateIdentifier('BranchStepKey'),
118
+ cases: cases.map(toMutableCase),
119
+ };
120
+ }
121
+ function toMutableWhileStep(step) {
122
+ const { case: stepCase, ...rest } = step;
123
+ return {
124
+ ...rest,
125
+ macroStepKey: generateIdentifier('WhileStepKey'),
126
+ case: toMutableCase(stepCase),
127
+ };
128
+ }
129
+ function toMutableForStep(step) {
130
+ const { case: stepCase, ...rest } = step;
131
+ return {
132
+ ...rest,
133
+ macroStepKey: generateIdentifier('ForStepKey'),
134
+ case: toMutableCase(stepCase),
135
+ };
136
+ }
137
+ function toMutableStep(step) {
138
+ switch (step.stepKind) {
139
+ case CONSTANTS.Enums.StepKind.tool:
140
+ return toMutableToolStep(step);
141
+ case CONSTANTS.Enums.StepKind.branch:
142
+ return toMutableBranchStep(step);
143
+ case CONSTANTS.Enums.StepKind.while:
144
+ return toMutableWhileStep(step);
145
+ case CONSTANTS.Enums.StepKind.for:
146
+ return toMutableForStep(step);
147
+ default:
148
+ throw new Error('Unsupported step kind while deprojecting steps.');
149
+ }
150
+ }
151
+ function buildExecutionKeyMap(strategy) {
152
+ const executionPathByKey = buildExecutionPathMap(strategy);
153
+ const executionKeyByPath = new Map();
154
+ for (const [toolStepKey, toolStepPath] of executionPathByKey.entries()) {
155
+ executionKeyByPath.set(toolStepPath, toolStepKey);
156
+ }
157
+ return executionKeyByPath;
158
+ }
159
+ export function strategyToMutableStrategy(strategy) {
160
+ const stepsByThreadIndex = strategy.stepsByThreadIndex.map((steps) => steps.map(toMutableStep));
161
+ const seededMutableStrategy = {
162
+ ...strategy,
163
+ stepsByThreadIndex,
164
+ strategyState: {},
165
+ };
166
+ const executionKeyByPath = buildExecutionKeyMap(seededMutableStrategy);
167
+ const strategyState = {};
168
+ for (const [toolStepPath, inputEntryByRoleName] of Object.entries(strategy.strategyState)) {
169
+ const toolStepKey = executionKeyByPath.get(toolStepPath);
170
+ if (!toolStepKey) {
171
+ throw new Error(`Cannot deproject strategy state: no mutable key found for toolStepPath '${toolStepPath}'.`);
172
+ }
173
+ strategyState[toolStepKey] = Object.fromEntries(Object.entries(inputEntryByRoleName).map(([roleName, entry]) => {
174
+ if (entry === undefined) {
175
+ throw new Error(`Cannot deproject strategy state: role '${roleName}' on toolStepPath '${toolStepPath}' is explicitly undefined.`);
176
+ }
177
+ return [
178
+ roleName,
179
+ toMutableStrategyStateEntry(entry, executionKeyByPath),
180
+ ];
181
+ }));
182
+ }
183
+ return {
184
+ ...strategy,
185
+ stepsByThreadIndex,
186
+ strategyState,
187
+ };
188
+ }
189
+ export function mutableStrategyToStrategy(mutableStrategy) {
190
+ const executionPathByKey = buildExecutionPathMap(mutableStrategy);
191
+ const strategyState = {};
192
+ for (const [toolStepKey, inputEntryByRoleName] of Object.entries(mutableStrategy.strategyState)) {
193
+ if (!inputEntryByRoleName) {
194
+ continue;
195
+ }
196
+ const toolStepPath = executionPathByKey.get(toolStepKey);
197
+ if (!toolStepPath) {
198
+ throw new Error(`Cannot project strategy state: no execution path found for toolStepKey '${toolStepKey}'.`);
199
+ }
200
+ strategyState[toolStepPath] = Object.fromEntries(Object.entries(inputEntryByRoleName).map(([roleName, entry]) => {
201
+ if (entry === undefined) {
202
+ throw new Error(`Cannot project strategy state: role '${roleName}' on toolStepKey '${toolStepKey}' is explicitly undefined.`);
203
+ }
204
+ return [
205
+ roleName,
206
+ toCanonicalStrategyStateEntry(entry, executionPathByKey),
207
+ ];
208
+ }));
209
+ }
210
+ return {
211
+ ...mutableStrategy,
212
+ stepsByThreadIndex: mutableStrategy.stepsByThreadIndex.map((steps) => steps.map(toCanonicalStep)),
213
+ strategyState,
214
+ };
215
+ }
@@ -0,0 +1,17 @@
1
+ import type { RoleName, RoleSpec, ToolHandle, Tool, ResourceTypeHandle, RoleValue } from '@toolproof-core/genesis';
2
+ import type { MutableStrategy, MutableToolStep } from './mutableStrategyOverlay.js';
3
+ export type RoleSpecEntry = [RoleName, RoleValue];
4
+ type RoleSpecDirectionKeys = keyof Pick<RoleSpec, 'inputRoleValueByName' | 'outputRoleValueByName'>;
5
+ export type RoleSpecByDirection = {
6
+ [K in RoleSpecDirectionKeys]: Map<RoleName, RoleValue>;
7
+ };
8
+ export declare function getInputRoleSpecEntriesFromTool(tool: Tool): RoleSpecEntry[];
9
+ export declare function getOutputRoleSpecEntriesFromTool(tool: Tool): RoleSpecEntry[];
10
+ export declare function getInputRoleNamesFromTool(tool: Tool): RoleName[];
11
+ export declare function getOutputRoleNamesFromTool(tool: Tool): RoleName[];
12
+ export declare function extractResourceTypeHandleFromRoleValue(roleValue: RoleValue): ResourceTypeHandle | null;
13
+ export declare function extractToolStepsFromStrategy(strategy: MutableStrategy): MutableToolStep[];
14
+ export declare function extractToolsFromStrategy(strategy: MutableStrategy, toolMap: Map<ToolHandle, Tool>): Tool[];
15
+ export declare function extractRoleSpecByDirectionByToolHandleFromStrategy(strategy: MutableStrategy, toolMap: Map<ToolHandle, Tool>): Map<ToolHandle, RoleSpecByDirection>;
16
+ export declare function extractSingleBranchableOutputResourceTypeHandle(tool: Tool): ResourceTypeHandle | null;
17
+ export {};
@@ -0,0 +1,88 @@
1
+ import { CONSTANTS } from '../lookups/lookups.js';
2
+ export function getInputRoleSpecEntriesFromTool(tool) {
3
+ return Object.entries(tool.roleSpec.inputRoleValueByName);
4
+ }
5
+ export function getOutputRoleSpecEntriesFromTool(tool) {
6
+ return Object.entries(tool.roleSpec.outputRoleValueByName);
7
+ }
8
+ export function getInputRoleNamesFromTool(tool) {
9
+ return getInputRoleSpecEntriesFromTool(tool).map(([roleName]) => roleName);
10
+ }
11
+ export function getOutputRoleNamesFromTool(tool) {
12
+ return getOutputRoleSpecEntriesFromTool(tool).map(([roleName]) => roleName);
13
+ }
14
+ export function extractResourceTypeHandleFromRoleValue(roleValue) {
15
+ const typeRef = roleValue.typeRef;
16
+ if ('resourceTypeHandle' in typeRef) {
17
+ return typeRef.resourceTypeHandle;
18
+ }
19
+ return null;
20
+ }
21
+ export function extractToolStepsFromStrategy(strategy) {
22
+ const toolSteps = [];
23
+ const visitStep = (step) => {
24
+ switch (step.stepKind) {
25
+ case 'tool': {
26
+ toolSteps.push(step);
27
+ return;
28
+ }
29
+ case 'branch': {
30
+ for (const conditional of step.cases) {
31
+ toolSteps.push(conditional.when, conditional.what);
32
+ }
33
+ return;
34
+ }
35
+ case 'while':
36
+ case 'for': {
37
+ toolSteps.push(step.case.when, step.case.what);
38
+ return;
39
+ }
40
+ default: {
41
+ const _exhaustive = step;
42
+ return _exhaustive;
43
+ }
44
+ }
45
+ };
46
+ for (const threadSteps of strategy.stepsByThreadIndex) {
47
+ for (const step of threadSteps) {
48
+ visitStep(step);
49
+ }
50
+ }
51
+ return toolSteps;
52
+ }
53
+ export function extractToolsFromStrategy(strategy, toolMap) {
54
+ const toolSteps = extractToolStepsFromStrategy(strategy);
55
+ const toolHandles = new Set();
56
+ const tools = [];
57
+ for (const toolStep of toolSteps) {
58
+ toolHandles.add(toolStep.toolHandle);
59
+ }
60
+ for (const toolHandle of toolHandles) {
61
+ const tool = toolMap.get(toolHandle);
62
+ if (!tool) {
63
+ throw new Error(`Tool with handle '${toolHandle}' not found in toolMap.`);
64
+ }
65
+ tools.push(tool);
66
+ }
67
+ return tools;
68
+ }
69
+ function extractRoleSpecByDirectionFromTool(tool) {
70
+ return {
71
+ inputRoleValueByName: new Map(getInputRoleSpecEntriesFromTool(tool)),
72
+ outputRoleValueByName: new Map(getOutputRoleSpecEntriesFromTool(tool)),
73
+ };
74
+ }
75
+ export function extractRoleSpecByDirectionByToolHandleFromStrategy(strategy, toolMap) {
76
+ const roleSpecByDirectionByToolHandle = new Map();
77
+ for (const tool of extractToolsFromStrategy(strategy, toolMap)) {
78
+ roleSpecByDirectionByToolHandle.set(tool.handle, extractRoleSpecByDirectionFromTool(tool));
79
+ }
80
+ return roleSpecByDirectionByToolHandle;
81
+ }
82
+ export function extractSingleBranchableOutputResourceTypeHandle(tool) {
83
+ const outputRoleSpecEntries = getOutputRoleSpecEntriesFromTool(tool).filter(([roleName]) => roleName !== CONSTANTS.Cosmos.ErrorOutputRoleName);
84
+ if (!outputRoleSpecEntries[0] || outputRoleSpecEntries.length !== 1) {
85
+ throw new Error(`Tool ${tool.handle} must have exactly one branchable output resource type to be branchable/loopable`);
86
+ }
87
+ return extractResourceTypeHandleFromRoleValue(outputRoleSpecEntries[0][1]);
88
+ }
@@ -0,0 +1,17 @@
1
+ import type { MutableInputResource, MutableExternalInputPotential, MutableStrategyState, MutableToolStepKey, MutableToolStepRoleAddress } from './mutableStrategyOverlay.js';
2
+ export type ResolveResult<TKey extends string = MutableToolStepKey> = {
3
+ status: 'inputResource';
4
+ entry: MutableInputResource;
5
+ path: MutableToolStepRoleAddress<TKey>[];
6
+ } | {
7
+ status: 'externalInputPotential';
8
+ entry: MutableExternalInputPotential;
9
+ path: MutableToolStepRoleAddress<TKey>[];
10
+ } | {
11
+ status: 'unresolved';
12
+ reason: 'not-found' | 'cycle' | 'depth-exceeded';
13
+ path: MutableToolStepRoleAddress<TKey>[];
14
+ };
15
+ export declare function resolveStrategyStateChain<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(strategyState: TStrategyState, start: MutableToolStepRoleAddress<TKey>, opts?: {
16
+ maxDepth?: number;
17
+ }): ResolveResult<TKey>;
@@ -1,4 +1,4 @@
1
- import { getStrategyStateEntry } from './strategyState.js';
1
+ import { getStrategyStateInputEntry } from './mutableStrategyOverlay.js';
2
2
  export function resolveStrategyStateChain(strategyState, start, opts) {
3
3
  const maxDepth = opts?.maxDepth ?? 50;
4
4
  const visited = new Set();
@@ -6,14 +6,15 @@ export function resolveStrategyStateChain(strategyState, start, opts) {
6
6
  let current = start;
7
7
  for (let depth = 0; depth <= maxDepth; depth++) {
8
8
  path.push(current);
9
- const visitKey = `${current.toolStepPath}::${current.roleName}`;
9
+ const visitKey = `${current.toolStepKey}::${current.roleName}`;
10
10
  if (visited.has(visitKey)) {
11
11
  return { status: 'unresolved', reason: 'cycle', path };
12
12
  }
13
13
  visited.add(visitKey);
14
- const entry = getStrategyStateEntry(strategyState, current);
15
- if (!entry)
14
+ const entry = getStrategyStateInputEntry(strategyState, current);
15
+ if (!entry) {
16
16
  return { status: 'unresolved', reason: 'not-found', path };
17
+ }
17
18
  if (entry.strategyStateInputKind === 'inputResource') {
18
19
  return { status: 'inputResource', entry, path };
19
20
  }
@@ -0,0 +1,2 @@
1
+ import type { MutableStrategy, MutableStep, MutableStrategyState, MutableToolStepKey } from './mutableStrategyOverlay.js';
2
+ export declare function toExecutionMutableStrategy<TKey extends string = MutableToolStepKey, TStep extends MutableStep<TKey> = MutableStep<TKey>, TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>>(mutableStrategy: MutableStrategy<TKey, TStep, TState>): MutableStrategy<TKey, TStep, TState>;
@@ -0,0 +1,10 @@
1
+ import { getIndependentThreads } from './stepParallelization.js';
2
+ import { getAuthoringSteps as getAuthoringThreadSteps } from './mutableStrategyOverlay.js';
3
+ export function toExecutionMutableStrategy(mutableStrategy) {
4
+ const authoringSteps = getAuthoringThreadSteps(mutableStrategy);
5
+ const stepsByThreadIndex = getIndependentThreads(authoringSteps, mutableStrategy.strategyState);
6
+ return {
7
+ ...mutableStrategy,
8
+ stepsByThreadIndex: stepsByThreadIndex,
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ import type { ToolStepPath, ToolStepPathSpec } from '@toolproof-core/genesis';
2
+ export declare function toToolStepPath(spec: ToolStepPathSpec): ToolStepPath;
@@ -0,0 +1,21 @@
1
+ function getToolStepPathSuffix(spec) {
2
+ if (spec.toolStepPathSlot === 'self') {
3
+ return '/self';
4
+ }
5
+ if (spec.toolStepPathSlot === 'cases/when' || spec.toolStepPathSlot === 'cases/what') {
6
+ if (typeof spec.caseIndex !== 'number') {
7
+ throw new Error('Expected caseIndex for branch tool-step path construction');
8
+ }
9
+ return `/cases/${spec.caseIndex}/${spec.toolStepPathSlot === 'cases/when' ? 'when' : 'what'}`;
10
+ }
11
+ if (spec.toolStepPathSlot === 'case/when') {
12
+ return '/case/when';
13
+ }
14
+ if (spec.toolStepPathSlot === 'case/what') {
15
+ return '/case/what';
16
+ }
17
+ throw new Error(`Unsupported tool-step path slot '${String(spec.toolStepPathSlot)}'`);
18
+ }
19
+ export function toToolStepPath(spec) {
20
+ return `/threads/${spec.threadIndex}/steps/${spec.stepIndex}${getToolStepPathSuffix(spec)}`;
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-core/lib",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -15,29 +15,49 @@
15
15
  "types": "./dist/types/types.d.ts",
16
16
  "import": "./dist/types/types.js"
17
17
  },
18
- "./extract-data": {
19
- "types": "./dist/utils/extractData.d.ts",
20
- "import": "./dist/utils/extractData.js"
18
+ "./identifier-generation": {
19
+ "types": "./dist/utils/identifierGeneration.d.ts",
20
+ "import": "./dist/utils/identifierGeneration.js"
21
21
  },
22
- "./create-resource": {
23
- "types": "./dist/utils/creation/resourceCreation.d.ts",
24
- "import": "./dist/utils/creation/resourceCreation.js"
22
+ "./strategy-assembly": {
23
+ "types": "./dist/utils/strategyAssembly.d.ts",
24
+ "import": "./dist/utils/strategyAssembly.js"
25
25
  },
26
- "./create-step": {
27
- "types": "./dist/integrations/firebase/createStep.d.ts",
28
- "import": "./dist/integrations/firebase/createStep.js"
26
+ "./cosmos-data-extraction": {
27
+ "types": "./dist/utils/cosmosDataExtraction.d.ts",
28
+ "import": "./dist/utils/cosmosDataExtraction.js"
29
29
  },
30
- "./create-threaded-strategy": {
31
- "types": "./dist/integrations/firebase/createThreadedStrategy.d.ts",
32
- "import": "./dist/integrations/firebase/createThreadedStrategy.js"
30
+ "./strategy-extraction": {
31
+ "types": "./dist/utils/strategyExtraction.d.ts",
32
+ "import": "./dist/utils/strategyExtraction.js"
33
33
  },
34
- "./resolve-strategy-state-chain": {
35
- "types": "./dist/utils/resolveStrategyStateChain.d.ts",
36
- "import": "./dist/utils/resolveStrategyStateChain.js"
34
+ "./resource-creation": {
35
+ "types": "./dist/utils/resourceCreation.d.ts",
36
+ "import": "./dist/utils/resourceCreation.js"
37
37
  },
38
- "./strategy-state": {
39
- "types": "./dist/utils/strategyState.d.ts",
40
- "import": "./dist/utils/strategyState.js"
38
+ "./step-creation": {
39
+ "types": "./dist/utils/stepCreation.d.ts",
40
+ "import": "./dist/utils/stepCreation.js"
41
+ },
42
+ "./tool-step-paths": {
43
+ "types": "./dist/utils/toolStepPaths.d.ts",
44
+ "import": "./dist/utils/toolStepPaths.js"
45
+ },
46
+ "./strategy-state-resolution": {
47
+ "types": "./dist/utils/strategyStateResolution.d.ts",
48
+ "import": "./dist/utils/strategyStateResolution.js"
49
+ },
50
+ "./mutable-strategy-overlay": {
51
+ "types": "./dist/utils/mutableStrategyOverlay.d.ts",
52
+ "import": "./dist/utils/mutableStrategyOverlay.js"
53
+ },
54
+ "./strategy-threading": {
55
+ "types": "./dist/utils/strategyThreading.d.ts",
56
+ "import": "./dist/utils/strategyThreading.js"
57
+ },
58
+ "./strategy-canonicalization": {
59
+ "types": "./dist/utils/strategyCanonicalization.d.ts",
60
+ "import": "./dist/utils/strategyCanonicalization.js"
41
61
  },
42
62
  "./firebase-admin-init": {
43
63
  "types": "./dist/integrations/firebase/firebaseAdminInit.d.ts",
@@ -52,8 +72,8 @@
52
72
  "@types/node": "^22.0.0"
53
73
  },
54
74
  "dependencies": {
55
- "firebase-admin": "^13.7.0",
56
- "@toolproof-core/genesis": "1.0.56"
75
+ "@toolproof-core/genesis": "^1.0.58",
76
+ "firebase-admin": "^13.7.0"
57
77
  },
58
78
  "scripts": {
59
79
  "test": "echo \"Error: no test specified\" && exit 1",