@toolproof-npm/shared 0.1.117 → 0.1.119

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.
@@ -0,0 +1,8 @@
1
+ import type { ExecutionJson, StatefulStrategyJson, RunnableStrategyJson, RunnableStrategyStatusJson, StrategyStateJson } from '@toolproof-npm/schema';
2
+ import type { IdentityPrefix } from '../types.js';
3
+ export type GetNewIdentity = (identifiable: IdentityPrefix) => string | Promise<string>;
4
+ export declare function normalizeExecutionInputBindingMap(execution: ExecutionJson, strategyState: StrategyStateJson): ExecutionJson;
5
+ export declare function createRunnableStrategy(statefulStrategy: StatefulStrategyJson, opts?: {
6
+ getNewIdentity: GetNewIdentity;
7
+ status?: RunnableStrategyStatusJson;
8
+ }): Promise<RunnableStrategyJson>;
@@ -0,0 +1,194 @@
1
+ import { CONSTANTS } from '../../constants.js';
2
+ import { getIndependentThreads } from './parallelization.js';
3
+ export function normalizeExecutionInputBindingMap(execution, strategyState) {
4
+ const inputBindingMap = execution.roleBindings?.inputBindingMap ?? {};
5
+ const outputBindingMap = execution.roleBindings?.outputBindingMap ?? {};
6
+ const bucket = strategyState?.[execution.identity];
7
+ if (!bucket) {
8
+ return execution;
9
+ }
10
+ let changed = false;
11
+ const nextInputBindingMap = { ...inputBindingMap };
12
+ for (const [roleRef, currentResourceId] of Object.entries(inputBindingMap)) {
13
+ const entryIdentity = bucket?.[roleRef]?.identity;
14
+ if (typeof entryIdentity === 'string' && entryIdentity.length > 0 && entryIdentity !== currentResourceId) {
15
+ nextInputBindingMap[roleRef] = entryIdentity;
16
+ changed = true;
17
+ }
18
+ }
19
+ if (!changed) {
20
+ return execution;
21
+ }
22
+ return {
23
+ ...execution,
24
+ roleBindings: {
25
+ inputBindingMap: nextInputBindingMap,
26
+ outputBindingMap,
27
+ },
28
+ };
29
+ }
30
+ function normalizeStepRoleBindings(step, strategyState) {
31
+ if (step.kind === CONSTANTS.STEPS.work) {
32
+ const work = step;
33
+ if (!work.execution)
34
+ return step;
35
+ const nextExecution = normalizeExecutionInputBindingMap(work.execution, strategyState);
36
+ if (nextExecution === work.execution)
37
+ return step;
38
+ return {
39
+ ...work,
40
+ execution: nextExecution,
41
+ };
42
+ }
43
+ if (step.kind === CONSTANTS.STEPS.for) {
44
+ const loop = step;
45
+ const whatExecution = loop.case?.what?.execution;
46
+ const whenExecution = loop.case?.when?.execution;
47
+ const nextWhatExecution = whatExecution
48
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
49
+ : whatExecution;
50
+ const nextWhenExecution = whenExecution
51
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
52
+ : whenExecution;
53
+ if (nextWhatExecution === whatExecution && nextWhenExecution === whenExecution)
54
+ return step;
55
+ return {
56
+ ...loop,
57
+ case: {
58
+ ...loop.case,
59
+ what: loop.case?.what
60
+ ? {
61
+ ...loop.case.what,
62
+ execution: nextWhatExecution,
63
+ }
64
+ : loop.case?.what,
65
+ when: loop.case?.when
66
+ ? {
67
+ ...loop.case.when,
68
+ execution: nextWhenExecution,
69
+ }
70
+ : loop.case?.when,
71
+ },
72
+ };
73
+ }
74
+ if (step.kind === CONSTANTS.STEPS.while) {
75
+ const loop = step;
76
+ const whatExecution = loop.case?.what?.execution;
77
+ const whenExecution = loop.case?.when?.execution;
78
+ const nextWhatExecution = whatExecution
79
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
80
+ : whatExecution;
81
+ const nextWhenExecution = whenExecution
82
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
83
+ : whenExecution;
84
+ if (nextWhatExecution === whatExecution && nextWhenExecution === whenExecution)
85
+ return step;
86
+ return {
87
+ ...loop,
88
+ case: {
89
+ ...loop.case,
90
+ what: loop.case?.what
91
+ ? {
92
+ ...loop.case.what,
93
+ execution: nextWhatExecution,
94
+ }
95
+ : loop.case?.what,
96
+ when: loop.case?.when
97
+ ? {
98
+ ...loop.case.when,
99
+ execution: nextWhenExecution,
100
+ }
101
+ : loop.case?.when,
102
+ },
103
+ };
104
+ }
105
+ if (step.kind === CONSTANTS.STEPS.branch) {
106
+ const branch = step;
107
+ const cases = branch.cases ?? [];
108
+ let changed = false;
109
+ const nextCases = cases.map((caseItem) => {
110
+ const whatExecution = caseItem?.what?.execution;
111
+ const whenExecution = caseItem?.when?.execution;
112
+ const nextWhatExecution = whatExecution
113
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
114
+ : whatExecution;
115
+ const nextWhenExecution = whenExecution
116
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
117
+ : whenExecution;
118
+ if (nextWhatExecution !== whatExecution || nextWhenExecution !== whenExecution) {
119
+ changed = true;
120
+ }
121
+ return {
122
+ ...caseItem,
123
+ what: caseItem?.what
124
+ ? {
125
+ ...caseItem.what,
126
+ execution: nextWhatExecution,
127
+ }
128
+ : caseItem?.what,
129
+ when: caseItem?.when
130
+ ? {
131
+ ...caseItem.when,
132
+ execution: nextWhenExecution,
133
+ }
134
+ : caseItem?.when,
135
+ };
136
+ });
137
+ if (!changed)
138
+ return step;
139
+ return {
140
+ ...branch,
141
+ cases: nextCases,
142
+ };
143
+ }
144
+ return step;
145
+ }
146
+ function normalizeStatefulStrategyInputBindingMaps(statefulStrategy) {
147
+ const steps = statefulStrategy.statelessStrategy?.steps;
148
+ if (!steps || steps.length === 0)
149
+ return statefulStrategy;
150
+ const strategyState = (statefulStrategy.strategyState ?? {});
151
+ let changed = false;
152
+ const nextSteps = steps.map((step) => {
153
+ const nextStep = normalizeStepRoleBindings(step, strategyState);
154
+ if (nextStep !== step)
155
+ changed = true;
156
+ return nextStep;
157
+ });
158
+ if (!changed)
159
+ return statefulStrategy;
160
+ return {
161
+ ...statefulStrategy,
162
+ statelessStrategy: {
163
+ ...statefulStrategy.statelessStrategy,
164
+ steps: nextSteps,
165
+ },
166
+ };
167
+ }
168
+ export async function createRunnableStrategy(statefulStrategy, opts) {
169
+ if (!opts?.getNewIdentity) {
170
+ throw new Error('statefulStrategyToRunnableStrategy: opts.getNewIdentity is required');
171
+ }
172
+ // Normalize roleBindings.inputBindingMap to reflect the identities present in strategyState.
173
+ // This is intentionally a pure transformation (no mutation of the provided statefulStrategy object graph).
174
+ const normalizedStatefulStrategy = normalizeStatefulStrategyInputBindingMaps(statefulStrategy);
175
+ const runnableStrategyIdentity = (await opts.getNewIdentity(CONSTANTS.IDENTITY_PREFIXES.runnable_strategy));
176
+ const steps = normalizedStatefulStrategy.statelessStrategy?.steps ?? [];
177
+ const strategyState = (normalizedStatefulStrategy.strategyState ?? {});
178
+ // Algorithm to find what steps can run in parallel
179
+ const threadStepsGroups = getIndependentThreads(steps, strategyState);
180
+ const strategyThreadMap = {};
181
+ for (const group of threadStepsGroups) {
182
+ const threadIdentity = await opts.getNewIdentity(CONSTANTS.IDENTITY_PREFIXES.strategy_thread);
183
+ strategyThreadMap[threadIdentity] = group;
184
+ }
185
+ return {
186
+ identity: runnableStrategyIdentity,
187
+ statefulStrategyRef: normalizedStatefulStrategy.identity,
188
+ strategyState: strategyState,
189
+ runnableStrategyContext: {
190
+ status: opts.status ?? 'running',
191
+ },
192
+ strategyThreadMap,
193
+ };
194
+ }
@@ -1,8 +1,8 @@
1
- import type { ExecutionJson, StatefulStrategyJson, StrategyRunJson, StrategyRunStatusJson, StrategyStateJson } from '@toolproof-npm/schema';
1
+ import type { ExecutionJson, StatefulStrategyJson, RunnableStrategyJson, RunnableStrategyStatusJson, StrategyStateJson } from '@toolproof-npm/schema';
2
2
  import type { IdentityPrefix } from '../types.js';
3
3
  export type GetNewIdentity = (identifiable: IdentityPrefix) => string | Promise<string>;
4
4
  export declare function normalizeExecutionInputBindingMap(execution: ExecutionJson, strategyState: StrategyStateJson): ExecutionJson;
5
- export declare function createStrategyRun(statefulStrategy: StatefulStrategyJson, opts?: {
5
+ export declare function createRunnableStrategy(statefulStrategy: StatefulStrategyJson, opts?: {
6
6
  getNewIdentity: GetNewIdentity;
7
- status?: StrategyRunStatusJson;
8
- }): Promise<StrategyRunJson>;
7
+ status?: RunnableStrategyStatusJson;
8
+ }): Promise<RunnableStrategyJson>;
@@ -165,14 +165,14 @@ function normalizeStatefulStrategyInputBindingMaps(statefulStrategy) {
165
165
  },
166
166
  };
167
167
  }
168
- export async function createStrategyRun(statefulStrategy, opts) {
168
+ export async function createRunnableStrategy(statefulStrategy, opts) {
169
169
  if (!opts?.getNewIdentity) {
170
- throw new Error('statefulStrategyToStrategyRun: opts.getNewIdentity is required');
170
+ throw new Error('statefulStrategyToRunnableStrategy: opts.getNewIdentity is required');
171
171
  }
172
172
  // Normalize roleBindings.inputBindingMap to reflect the identities present in strategyState.
173
173
  // This is intentionally a pure transformation (no mutation of the provided statefulStrategy object graph).
174
174
  const normalizedStatefulStrategy = normalizeStatefulStrategyInputBindingMaps(statefulStrategy);
175
- const strategyRunIdentity = (await opts.getNewIdentity(CONSTANTS.IDENTITY_PREFIXES.strategy_run));
175
+ const runnableStrategyIdentity = (await opts.getNewIdentity(CONSTANTS.IDENTITY_PREFIXES.strategy_run));
176
176
  const steps = normalizedStatefulStrategy.statelessStrategy?.steps ?? [];
177
177
  const strategyState = (normalizedStatefulStrategy.strategyState ?? {});
178
178
  // Algorithm to find what steps can run in parallel
@@ -183,10 +183,10 @@ export async function createStrategyRun(statefulStrategy, opts) {
183
183
  strategyThreadMap[threadIdentity] = group;
184
184
  }
185
185
  return {
186
- identity: strategyRunIdentity,
186
+ identity: runnableStrategyIdentity,
187
187
  statefulStrategyRef: normalizedStatefulStrategy.identity,
188
188
  strategyState: strategyState,
189
- strategyRunContext: {
189
+ runnableStrategyContext: {
190
190
  status: opts.status ?? 'running',
191
191
  },
192
192
  strategyThreadMap,
@@ -25,7 +25,7 @@ export declare const CONSTANTS: {
25
25
  readonly stateless_strategy: "stateless_strategy";
26
26
  readonly stateful_strategy: "stateful_strategy";
27
27
  readonly strategy_thread: "strategy_thread";
28
- readonly strategy_run: "strategy_run";
28
+ readonly runnable_strategy: "runnable_strategy";
29
29
  };
30
30
  readonly STEPS: {
31
31
  readonly work: "work";
@@ -59,18 +59,18 @@ export declare const CONSTANTS: {
59
59
  readonly TYPE_Error: "TYPE-Error";
60
60
  readonly TYPE_StatelessStrategy: "TYPE-StatelessStrategy";
61
61
  readonly TYPE_StatefulStrategy: "TYPE-StatefulStrategy";
62
- readonly TYPE_StrategyRun: "TYPE-StrategyRun";
62
+ readonly TYPE_RunnableStrategy: "TYPE-RunnableStrategy";
63
63
  readonly TYPE_RunRecording: "TYPE-RunRecording";
64
64
  readonly ROLE_Manual: "ROLE-Manual";
65
65
  readonly ROLE_Iteration: "ROLE-Iteration";
66
66
  readonly ROLE_ErrorOutput: "ROLE-ErrorOutput";
67
- readonly ROLE_DynamicSource: "ROLE-u4vW7PDlX6V9IGJoNxhl";
68
- readonly ROLE_StaticTarget: "ROLE-xLaQo8L1fMxKTQUJcaJn";
69
- readonly ROLE_Decison: "ROLE-C4FKVwXipgngzPQc3MDc";
67
+ readonly ROLE_LessThanSource: "ROLE-LessThanSource";
68
+ readonly ROLE_LessThanTarget: "ROLE-LessThanTarget";
69
+ readonly ROLE_Decision: "ROLE-Decision";
70
70
  readonly JOB_Engine: "JOB-Engine";
71
71
  readonly JOB_LessThan: "JOB-LessThan";
72
- readonly BOOLEAN_false: "RESOURCE-qadnfFGjZsjpqLI0Du1d";
73
- readonly BOOLEAN_true: "RESOURCE-iZX1cxZ9ImJRzty9Ob4G";
72
+ readonly BOOLEAN_false: "RESOURCE-BooleanFalse";
73
+ readonly BOOLEAN_true: "RESOURCE-BooleanTrue";
74
74
  };
75
75
  readonly TESTING: {
76
76
  readonly Natural_Zero: "TYPE-Natural/3335e31095a13a9a2b0ea41ca7d92a458780cd5671dc0a440a72cc1b1c4f2c81";
package/dist/constants.js CHANGED
@@ -25,7 +25,7 @@ export const CONSTANTS = {
25
25
  stateless_strategy: 'stateless_strategy',
26
26
  stateful_strategy: 'stateful_strategy',
27
27
  strategy_thread: 'strategy_thread',
28
- strategy_run: 'strategy_run',
28
+ runnable_strategy: 'runnable_strategy',
29
29
  },
30
30
  STEPS: {
31
31
  work: 'work',
@@ -59,18 +59,18 @@ export const CONSTANTS = {
59
59
  TYPE_Error: 'TYPE-Error',
60
60
  TYPE_StatelessStrategy: 'TYPE-StatelessStrategy',
61
61
  TYPE_StatefulStrategy: 'TYPE-StatefulStrategy',
62
- TYPE_StrategyRun: 'TYPE-StrategyRun',
62
+ TYPE_RunnableStrategy: 'TYPE-RunnableStrategy',
63
63
  TYPE_RunRecording: 'TYPE-RunRecording',
64
64
  ROLE_Manual: 'ROLE-Manual',
65
65
  ROLE_Iteration: 'ROLE-Iteration',
66
66
  ROLE_ErrorOutput: 'ROLE-ErrorOutput',
67
- ROLE_DynamicSource: 'ROLE-u4vW7PDlX6V9IGJoNxhl',
68
- ROLE_StaticTarget: 'ROLE-xLaQo8L1fMxKTQUJcaJn',
69
- ROLE_Decison: 'ROLE-C4FKVwXipgngzPQc3MDc',
67
+ ROLE_LessThanSource: 'ROLE-LessThanSource',
68
+ ROLE_LessThanTarget: 'ROLE-LessThanTarget',
69
+ ROLE_Decision: 'ROLE-Decision',
70
70
  JOB_Engine: 'JOB-Engine',
71
71
  JOB_LessThan: 'JOB-LessThan',
72
- BOOLEAN_false: 'RESOURCE-qadnfFGjZsjpqLI0Du1d',
73
- BOOLEAN_true: 'RESOURCE-iZX1cxZ9ImJRzty9Ob4G',
72
+ BOOLEAN_false: 'RESOURCE-BooleanFalse',
73
+ BOOLEAN_true: 'RESOURCE-BooleanTrue',
74
74
  },
75
75
  TESTING: {
76
76
  Natural_Zero: 'TYPE-Natural/3335e31095a13a9a2b0ea41ca7d92a458780cd5671dc0a440a72cc1b1c4f2c81',
@@ -1,4 +1,4 @@
1
- import type { BranchStepIdentityJson, ExecutionIdentityJson, ForStepIdentityJson, JobIdentityJson, ResourceFormatIdentityJson, ResourceIdentityJson, ResourceRoleIdentityJson, ResourceTypeIdentityJson, StatefulStrategyIdentityJson, StatelessStrategyIdentityJson, StrategyRunIdentityJson, StrategyThreadIdentityJson, WhileStepIdentityJson, WorkStepIdentityJson } from '@toolproof-npm/schema';
1
+ import type { BranchStepIdentityJson, ExecutionIdentityJson, ForStepIdentityJson, JobIdentityJson, ResourceFormatIdentityJson, ResourceIdentityJson, ResourceRoleIdentityJson, ResourceTypeIdentityJson, StatefulStrategyIdentityJson, StatelessStrategyIdentityJson, RunnableStrategyIdentityJson, StrategyThreadIdentityJson, WhileStepIdentityJson, WorkStepIdentityJson } from '@toolproof-npm/schema';
2
2
  import type { ResourceMap, IdentityPrefix } from './_lib/types.js';
3
3
  import { CONSTANTS } from './constants.js';
4
4
  type StripTrailingS<S extends string> = S extends `${infer Rest}S` ? Rest : S;
@@ -17,7 +17,7 @@ type IdentityJsonByPrefix = {
17
17
  stateless_strategy: StatelessStrategyIdentityJson;
18
18
  stateful_strategy: StatefulStrategyIdentityJson;
19
19
  strategy_thread: StrategyThreadIdentityJson;
20
- strategy_run: StrategyRunIdentityJson;
20
+ runnable_strategy: RunnableStrategyIdentityJson;
21
21
  };
22
22
  export type NewIdentity<T extends AnyIdentityPrefix = AnyIdentityPrefix> = T extends keyof IdentityJsonByPrefix ? IdentityJsonByPrefix[T] : T extends StepIdentityPrefix ? StepIdentityForPrefix<T> : `${IdentityPrefixFor<T>}-${string}`;
23
23
  export declare function getNewIdentity<T extends AnyIdentityPrefix>(identifiable: T): NewIdentity<T>;
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
4
  export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
5
5
  export * as STEP_CREATION from './_lib/utils/stepCreation.js';
6
- export * as STRATEGY_RUN_CREATION from './_lib/utils/strategyRunCreation.js';
6
+ export * as RUNNABLE_STRATEGY_CREATION from './_lib/utils/runnableStrategyCreation.js';
7
7
  export * as ROLE_RESOURCE_BINDINGS from './_lib/utils/roleResourceBinding.js';
8
8
  export * as RESOURCE_CHAIN from './_lib/utils/resourceChain.js';
9
9
  export * from './firebaseAdminHelpers.js';
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ export * as TYPES from './_lib/types.js';
3
3
  export * as UTILS from './_lib/utils/utils.js';
4
4
  export * as RESOURCE_CREATION from './_lib/utils/resourceCreation.js';
5
5
  export * as STEP_CREATION from './_lib/utils/stepCreation.js';
6
- export * as STRATEGY_RUN_CREATION from './_lib/utils/strategyRunCreation.js';
6
+ export * as RUNNABLE_STRATEGY_CREATION from './_lib/utils/runnableStrategyCreation.js';
7
7
  export * as ROLE_RESOURCE_BINDINGS from './_lib/utils/roleResourceBinding.js';
8
8
  export * as RESOURCE_CHAIN from './_lib/utils/resourceChain.js';
9
9
  export * from './firebaseAdminHelpers.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-npm/shared",
3
- "version": "0.1.117",
3
+ "version": "0.1.119",
4
4
  "description": "Core library utilities for ToolProof",
5
5
  "keywords": [
6
6
  "toolproof",
@@ -56,7 +56,7 @@
56
56
  "typescript": "^5.9.3"
57
57
  },
58
58
  "dependencies": {
59
- "@toolproof-npm/schema": "^0.1.82",
59
+ "@toolproof-npm/schema": "^0.1.83",
60
60
  "firebase-admin": "^13.6.0"
61
61
  },
62
62
  "scripts": {