@toolproof-npm/shared 0.1.107 → 0.1.108

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.
@@ -1,4 +1,169 @@
1
1
  import { CONSTANTS } from '../../constants.js';
2
+ function normalizeExecutionInputBindingMap(execution, strategyState) {
3
+ const inputBindingMap = execution.roleBindings?.inputBindingMap ?? {};
4
+ const outputBindingMap = execution.roleBindings?.outputBindingMap ?? {};
5
+ const bucket = strategyState?.[execution.identity];
6
+ if (!bucket) {
7
+ return execution;
8
+ }
9
+ let changed = false;
10
+ const nextInputBindingMap = { ...inputBindingMap };
11
+ for (const [roleRef, currentResourceId] of Object.entries(inputBindingMap)) {
12
+ const entryIdentity = bucket?.[roleRef]?.identity;
13
+ if (typeof entryIdentity === 'string' && entryIdentity.length > 0 && entryIdentity !== currentResourceId) {
14
+ nextInputBindingMap[roleRef] = entryIdentity;
15
+ changed = true;
16
+ }
17
+ }
18
+ if (!changed) {
19
+ return execution;
20
+ }
21
+ return {
22
+ ...execution,
23
+ roleBindings: {
24
+ inputBindingMap: nextInputBindingMap,
25
+ outputBindingMap,
26
+ },
27
+ };
28
+ }
29
+ function normalizeStepRoleBindings(step, strategyState) {
30
+ if (step.kind === CONSTANTS.STEPS.work) {
31
+ const work = step;
32
+ if (!work.execution)
33
+ return step;
34
+ const nextExecution = normalizeExecutionInputBindingMap(work.execution, strategyState);
35
+ if (nextExecution === work.execution)
36
+ return step;
37
+ return {
38
+ ...work,
39
+ execution: nextExecution,
40
+ };
41
+ }
42
+ if (step.kind === CONSTANTS.STEPS.for) {
43
+ const loop = step;
44
+ const whatExecution = loop.case?.what?.execution;
45
+ const whenExecution = loop.case?.when?.execution;
46
+ const nextWhatExecution = whatExecution
47
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
48
+ : whatExecution;
49
+ const nextWhenExecution = whenExecution
50
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
51
+ : whenExecution;
52
+ if (nextWhatExecution === whatExecution && nextWhenExecution === whenExecution)
53
+ return step;
54
+ return {
55
+ ...loop,
56
+ case: {
57
+ ...loop.case,
58
+ what: loop.case?.what
59
+ ? {
60
+ ...loop.case.what,
61
+ execution: nextWhatExecution,
62
+ }
63
+ : loop.case?.what,
64
+ when: loop.case?.when
65
+ ? {
66
+ ...loop.case.when,
67
+ execution: nextWhenExecution,
68
+ }
69
+ : loop.case?.when,
70
+ },
71
+ };
72
+ }
73
+ if (step.kind === CONSTANTS.STEPS.while) {
74
+ const loop = step;
75
+ const whatExecution = loop.case?.what?.execution;
76
+ const whenExecution = loop.case?.when?.execution;
77
+ const nextWhatExecution = whatExecution
78
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
79
+ : whatExecution;
80
+ const nextWhenExecution = whenExecution
81
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
82
+ : whenExecution;
83
+ if (nextWhatExecution === whatExecution && nextWhenExecution === whenExecution)
84
+ return step;
85
+ return {
86
+ ...loop,
87
+ case: {
88
+ ...loop.case,
89
+ what: loop.case?.what
90
+ ? {
91
+ ...loop.case.what,
92
+ execution: nextWhatExecution,
93
+ }
94
+ : loop.case?.what,
95
+ when: loop.case?.when
96
+ ? {
97
+ ...loop.case.when,
98
+ execution: nextWhenExecution,
99
+ }
100
+ : loop.case?.when,
101
+ },
102
+ };
103
+ }
104
+ if (step.kind === CONSTANTS.STEPS.branch) {
105
+ const branch = step;
106
+ const cases = branch.cases ?? [];
107
+ let changed = false;
108
+ const nextCases = cases.map((caseItem) => {
109
+ const whatExecution = caseItem?.what?.execution;
110
+ const whenExecution = caseItem?.when?.execution;
111
+ const nextWhatExecution = whatExecution
112
+ ? normalizeExecutionInputBindingMap(whatExecution, strategyState)
113
+ : whatExecution;
114
+ const nextWhenExecution = whenExecution
115
+ ? normalizeExecutionInputBindingMap(whenExecution, strategyState)
116
+ : whenExecution;
117
+ if (nextWhatExecution !== whatExecution || nextWhenExecution !== whenExecution) {
118
+ changed = true;
119
+ }
120
+ return {
121
+ ...caseItem,
122
+ what: caseItem?.what
123
+ ? {
124
+ ...caseItem.what,
125
+ execution: nextWhatExecution,
126
+ }
127
+ : caseItem?.what,
128
+ when: caseItem?.when
129
+ ? {
130
+ ...caseItem.when,
131
+ execution: nextWhenExecution,
132
+ }
133
+ : caseItem?.when,
134
+ };
135
+ });
136
+ if (!changed)
137
+ return step;
138
+ return {
139
+ ...branch,
140
+ cases: nextCases,
141
+ };
142
+ }
143
+ return step;
144
+ }
145
+ function normalizeStatefulStrategyInputBindingMaps(statefulStrategy) {
146
+ const steps = statefulStrategy.statelessStrategy?.steps;
147
+ if (!steps || steps.length === 0)
148
+ return statefulStrategy;
149
+ const strategyState = (statefulStrategy.strategyState ?? {});
150
+ let changed = false;
151
+ const nextSteps = steps.map((step) => {
152
+ const nextStep = normalizeStepRoleBindings(step, strategyState);
153
+ if (nextStep !== step)
154
+ changed = true;
155
+ return nextStep;
156
+ });
157
+ if (!changed)
158
+ return statefulStrategy;
159
+ return {
160
+ ...statefulStrategy,
161
+ statelessStrategy: {
162
+ ...statefulStrategy.statelessStrategy,
163
+ steps: nextSteps,
164
+ },
165
+ };
166
+ }
2
167
  function getIndependentThreads(steps, strategyState) {
3
168
  const getStepId = (s, i) => s?.execution?.identity || `step-${i}`;
4
169
  const stepById = new Map(steps.map((s, i) => [getStepId(s, i), s]));
@@ -57,9 +222,12 @@ export async function statefulStrategyToStrategyRun(statefulStrategy, opts) {
57
222
  if (!opts?.getNewIdentity) {
58
223
  throw new Error('statefulStrategyToStrategyRun: opts.getNewIdentity is required');
59
224
  }
225
+ // Normalize roleBindings.inputBindingMap to reflect the identities present in strategyState.
226
+ // This is intentionally a pure transformation (no mutation of the provided statefulStrategy object graph).
227
+ const normalizedStatefulStrategy = normalizeStatefulStrategyInputBindingMaps(statefulStrategy);
60
228
  const strategyRunIdentity = (await opts.getNewIdentity(CONSTANTS.TERMINALS.strategy_run));
61
- const steps = statefulStrategy.statelessStrategy?.steps ?? [];
62
- const strategyState = (statefulStrategy.strategyState ?? {});
229
+ const steps = normalizedStatefulStrategy.statelessStrategy?.steps ?? [];
230
+ const strategyState = (normalizedStatefulStrategy.strategyState ?? {});
63
231
  // Algorithm to find what steps can run in parallel
64
232
  const threadStepsGroups = getIndependentThreads(steps, strategyState);
65
233
  const strategyThreadMap = {};
@@ -69,7 +237,7 @@ export async function statefulStrategyToStrategyRun(statefulStrategy, opts) {
69
237
  }
70
238
  return {
71
239
  identity: strategyRunIdentity,
72
- statefulStrategyRef: statefulStrategy.identity,
240
+ statefulStrategyRef: normalizedStatefulStrategy.identity,
73
241
  strategyState: strategyState,
74
242
  strategyRunContext: {
75
243
  status: opts.status ?? 'running',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-npm/shared",
3
- "version": "0.1.107",
3
+ "version": "0.1.108",
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.72",
59
+ "@toolproof-npm/schema": "^0.1.73",
60
60
  "firebase-admin": "^13.6.0"
61
61
  },
62
62
  "scripts": {