@toolproof-core/lib 1.0.47 → 1.0.49

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.
@@ -23,6 +23,7 @@ import type {
23
23
  MutableStep,
24
24
  MutableStrategyStateEntryByRoleName,
25
25
  MutableStrategyStateEntry,
26
+ MutableToolStep,
26
27
  MutableToolStepKey,
27
28
  MutableWhileStep,
28
29
  } from './mutableStrategyOverlay.js';
@@ -30,8 +31,8 @@ import { generateIdentifier } from './identifierGeneration.js';
30
31
 
31
32
  function stripMutableCase(stepCase: MutableCase): Case {
32
33
  const { when, what, ...rest } = stepCase;
33
- const { toolStepKey: _whenToolStepKey, ...canonicalWhen } = when;
34
- const { toolStepKey: _whatToolStepKey, ...canonicalWhat } = what;
34
+ const { stepKey: _whenStepKey, ...canonicalWhen } = when;
35
+ const { stepKey: _whatStepKey, ...canonicalWhat } = what;
35
36
 
36
37
  return {
37
38
  ...rest,
@@ -46,18 +47,18 @@ function buildExecutionPathMap(strategy: MutableStrategy): Map<MutableToolStepKe
46
47
  const registerStep = (step: MutableStep, pathPrefix: string) => {
47
48
  switch (step.stepKind) {
48
49
  case CONSTANTS.Enums.StepKind.tool:
49
- executionPathByKey.set(step.toolStepKey, `${pathPrefix}/self` as ToolStepPath);
50
+ executionPathByKey.set(step.stepKey, `${pathPrefix}/self` as ToolStepPath);
50
51
  return;
51
52
  case CONSTANTS.Enums.StepKind.branch:
52
53
  step.cases.forEach((stepCase, caseIndex) => {
53
- executionPathByKey.set(stepCase.when.toolStepKey, `${pathPrefix}/cases/${caseIndex}/when` as ToolStepPath);
54
- executionPathByKey.set(stepCase.what.toolStepKey, `${pathPrefix}/cases/${caseIndex}/what` as ToolStepPath);
54
+ executionPathByKey.set(stepCase.when.stepKey, `${pathPrefix}/cases/${caseIndex}/when` as ToolStepPath);
55
+ executionPathByKey.set(stepCase.what.stepKey, `${pathPrefix}/cases/${caseIndex}/what` as ToolStepPath);
55
56
  });
56
57
  return;
57
58
  case CONSTANTS.Enums.StepKind.while:
58
59
  case CONSTANTS.Enums.StepKind.for:
59
- executionPathByKey.set(step.case.when.toolStepKey, `${pathPrefix}/case/when` as ToolStepPath);
60
- executionPathByKey.set(step.case.what.toolStepKey, `${pathPrefix}/case/what` as ToolStepPath);
60
+ executionPathByKey.set(step.case.when.stepKey, `${pathPrefix}/case/when` as ToolStepPath);
61
+ executionPathByKey.set(step.case.what.stepKey, `${pathPrefix}/case/what` as ToolStepPath);
61
62
  return;
62
63
  default:
63
64
  throw new Error('Unsupported step kind while projecting strategy.');
@@ -81,9 +82,9 @@ function toCanonicalStrategyStateEntry(
81
82
  return entry as MaterializedResource | ExternalInputPotential;
82
83
  }
83
84
 
84
- const sourceToolStepPath = executionPathByKey.get(entry.toolStepRoleAddress.toolStepKey);
85
+ const sourceToolStepPath = executionPathByKey.get(entry.toolStepRoleAddress.stepKey);
85
86
  if (!sourceToolStepPath) {
86
- throw new Error(`Cannot project strategy state: no execution path found for toolStepKey '${entry.toolStepRoleAddress.toolStepKey}'.`);
87
+ throw new Error(`Cannot project strategy state: no execution path found for stepKey '${entry.toolStepRoleAddress.stepKey}'.`);
87
88
  }
88
89
 
89
90
  return {
@@ -98,11 +99,11 @@ function toCanonicalStrategyStateEntry(
98
99
  function toCanonicalStep(step: MutableStep): Step {
99
100
  switch (step.stepKind) {
100
101
  case CONSTANTS.Enums.StepKind.tool: {
101
- const { toolStepKey: _toolStepKey, ...canonicalToolStep } = step;
102
+ const { stepKey: _stepKey, ...canonicalToolStep } = step;
102
103
  return canonicalToolStep;
103
104
  }
104
105
  case CONSTANTS.Enums.StepKind.branch: {
105
- const { macroStepKey: _macroStepKey, cases, ...rest } = step;
106
+ const { stepKey: _stepKey, macroStepKey: _macroStepKey, cases, ...rest } = step;
106
107
  return {
107
108
  ...rest,
108
109
  cases: cases.map(stripMutableCase) as [Case, ...Case[]],
@@ -110,7 +111,7 @@ function toCanonicalStep(step: MutableStep): Step {
110
111
  }
111
112
  case CONSTANTS.Enums.StepKind.while:
112
113
  case CONSTANTS.Enums.StepKind.for: {
113
- const { macroStepKey: _macroStepKey, case: stepCase, ...rest } = step;
114
+ const { stepKey: _stepKey, macroStepKey: _macroStepKey, case: stepCase, ...rest } = step;
114
115
  return {
115
116
  ...rest,
116
117
  case: stripMutableCase(stepCase),
@@ -137,16 +138,17 @@ function toMutableStrategyStateEntry(
137
138
  return {
138
139
  strategyStateEntryKind: 'internalInputPotential',
139
140
  toolStepRoleAddress: {
140
- toolStepKey: sourceToolStepKey,
141
+ stepKey: sourceToolStepKey,
141
142
  roleName: entry.toolStepRoleAddress.roleName,
142
143
  },
143
144
  } satisfies MutableInternalInputPotential;
144
145
  }
145
146
 
146
- function toMutableToolStep(step: ToolStep) {
147
+ function toMutableToolStep(step: ToolStep): MutableToolStep {
148
+ const key = generateIdentifier('ToolStepKey');
147
149
  return {
148
150
  ...step,
149
- toolStepKey: generateIdentifier('ToolStepKey'),
151
+ stepKey: key,
150
152
  };
151
153
  }
152
154
 
@@ -163,9 +165,11 @@ function toMutableCase(stepCase: Case): MutableCase {
163
165
  function toMutableBranchStep(step: BranchStep): MutableBranchStep {
164
166
  const { cases, ...rest } = step;
165
167
 
168
+ const key = generateIdentifier('BranchStepKey');
166
169
  return {
167
170
  ...rest,
168
- macroStepKey: generateIdentifier('BranchStepKey'),
171
+ stepKey: key,
172
+ macroStepKey: key,
169
173
  cases: cases.map(toMutableCase) as [MutableCase, ...MutableCase[]],
170
174
  };
171
175
  }
@@ -173,9 +177,11 @@ function toMutableBranchStep(step: BranchStep): MutableBranchStep {
173
177
  function toMutableWhileStep(step: WhileStep): MutableWhileStep {
174
178
  const { case: stepCase, ...rest } = step;
175
179
 
180
+ const key = generateIdentifier('WhileStepKey');
176
181
  return {
177
182
  ...rest,
178
- macroStepKey: generateIdentifier('WhileStepKey'),
183
+ stepKey: key,
184
+ macroStepKey: key,
179
185
  case: toMutableCase(stepCase),
180
186
  };
181
187
  }
@@ -183,9 +189,11 @@ function toMutableWhileStep(step: WhileStep): MutableWhileStep {
183
189
  function toMutableForStep(step: ForStep): MutableForStep {
184
190
  const { case: stepCase, ...rest } = step;
185
191
 
192
+ const key = generateIdentifier('ForStepKey');
186
193
  return {
187
194
  ...rest,
188
- macroStepKey: generateIdentifier('ForStepKey'),
195
+ stepKey: key,
196
+ macroStepKey: key,
189
197
  case: toMutableCase(stepCase),
190
198
  };
191
199
  }
@@ -209,8 +217,8 @@ function buildExecutionKeyMap(strategy: MutableStrategy): Map<ToolStepPath, Muta
209
217
  const executionPathByKey = buildExecutionPathMap(strategy);
210
218
  const executionKeyByPath = new Map<ToolStepPath, MutableToolStepKey>();
211
219
 
212
- for (const [toolStepKey, toolStepPath] of executionPathByKey.entries()) {
213
- executionKeyByPath.set(toolStepPath, toolStepKey);
220
+ for (const [stepKey, toolStepPath] of executionPathByKey.entries()) {
221
+ executionKeyByPath.set(toolStepPath, stepKey);
214
222
  }
215
223
 
216
224
  return executionKeyByPath;
@@ -261,21 +269,21 @@ export function mutableStrategyToStrategy(mutableStrategy: MutableStrategy): Str
261
269
  const executionPathByKey = buildExecutionPathMap(mutableStrategy);
262
270
  const strategyState: Strategy['strategyState'] = {};
263
271
 
264
- for (const [toolStepKey, inputEntryByRoleName] of Object.entries(mutableStrategy.strategyState)) {
272
+ for (const [stepKey, inputEntryByRoleName] of Object.entries(mutableStrategy.strategyState)) {
265
273
  if (!inputEntryByRoleName) {
266
274
  continue;
267
275
  }
268
276
 
269
- const toolStepPath = executionPathByKey.get(toolStepKey);
277
+ const toolStepPath = executionPathByKey.get(stepKey as MutableToolStepKey);
270
278
  if (!toolStepPath) {
271
- throw new Error(`Cannot project strategy state: no execution path found for toolStepKey '${toolStepKey}'.`);
279
+ throw new Error(`Cannot project strategy state: no execution path found for stepKey '${stepKey}'.`);
272
280
  }
273
281
 
274
282
  strategyState[toolStepPath] = Object.fromEntries(
275
283
  Object.entries(inputEntryByRoleName).map(([roleName, entry]) => {
276
284
  if (entry === undefined) {
277
285
  throw new Error(
278
- `Cannot project strategy state: role '${roleName}' on toolStepKey '${toolStepKey}' is explicitly undefined.`
286
+ `Cannot project strategy state: role '${roleName}' on stepKey '${stepKey}' is explicitly undefined.`
279
287
  );
280
288
  }
281
289
 
@@ -294,28 +302,28 @@ export function mutableStrategyToStrategy(mutableStrategy: MutableStrategy): Str
294
302
  };
295
303
  }
296
304
 
297
- export function mutableToolStepKeyToToolStepPath(
305
+ export function mutableStepKeyToToolStepPath(
298
306
  mutableStrategy: MutableStrategy,
299
- toolStepKey: MutableToolStepKey,
307
+ stepKey: MutableToolStepKey,
300
308
  ): ToolStepPath {
301
309
  const executionPathByKey = buildExecutionPathMap(mutableStrategy);
302
- const toolStepPath = executionPathByKey.get(toolStepKey);
310
+ const toolStepPath = executionPathByKey.get(stepKey);
303
311
  if (!toolStepPath) {
304
- throw new Error(`Cannot project toolStepPath: no execution path found for toolStepKey '${toolStepKey}'.`);
312
+ throw new Error(`Cannot project toolStepPath: no execution path found for stepKey '${stepKey}'.`);
305
313
  }
306
314
 
307
315
  return toolStepPath;
308
316
  }
309
317
 
310
- export function projectMutableStrategyStateEntriesByToolStepKeyToCanonicalDelta(
318
+ export function projectMutableStrategyStateEntriesByStepKeyToCanonicalDelta(
311
319
  mutableStrategy: MutableStrategy,
312
- toolStepKey: MutableToolStepKey,
320
+ stepKey: MutableToolStepKey,
313
321
  entries: MutableStrategyStateEntryByRoleName,
314
322
  ): Strategy['strategyState'] {
315
323
  const executionPathByKey = buildExecutionPathMap(mutableStrategy);
316
- const toolStepPath = executionPathByKey.get(toolStepKey);
324
+ const toolStepPath = executionPathByKey.get(stepKey);
317
325
  if (!toolStepPath) {
318
- throw new Error(`Cannot project strategy state delta: no execution path found for toolStepKey '${toolStepKey}'.`);
326
+ throw new Error(`Cannot project strategy state delta: no execution path found for stepKey '${stepKey}'.`);
319
327
  }
320
328
 
321
329
  const canonicalEntries: Strategy['strategyState'][ToolStepPath] = Object.fromEntries(
@@ -324,7 +332,7 @@ export function projectMutableStrategyStateEntriesByToolStepKeyToCanonicalDelta(
324
332
  .map(([roleName, entry]) => {
325
333
  if (entry === undefined) {
326
334
  throw new Error(
327
- `Cannot project strategy state delta: role '${roleName}' on toolStepKey '${toolStepKey}' is explicitly undefined.`
335
+ `Cannot project strategy state delta: role '${roleName}' on stepKey '${stepKey}' is explicitly undefined.`
328
336
  );
329
337
  }
330
338
 
@@ -1,43 +1,40 @@
1
1
  import type {
2
2
  MutableMaterializedResource,
3
3
  MutableExternalInputPotential,
4
- MutableInternalInputPotential,
5
4
  MutableStrategyState,
6
- MutableStrategyStateEntry,
7
- MutableToolStepKey,
8
5
  MutableToolStepRoleAddress,
9
6
  } from './mutableStrategyOverlay.js';
10
7
  import { getStrategyStateEntry } from './mutableStrategyOverlay.js';
11
8
 
12
- export type ResolveResult<TKey extends string = MutableToolStepKey> =
13
- | { status: 'materializedResource'; entry: MutableMaterializedResource; path: MutableToolStepRoleAddress<TKey>[] }
14
- | { status: 'externalInputPotential'; entry: MutableExternalInputPotential; path: MutableToolStepRoleAddress<TKey>[] }
9
+ export type ResolveResult =
10
+ | { status: 'materializedResource'; entry: MutableMaterializedResource; path: MutableToolStepRoleAddress[] }
11
+ | { status: 'externalInputPotential'; entry: MutableExternalInputPotential; path: MutableToolStepRoleAddress[] }
15
12
  | {
16
13
  status: 'unresolved';
17
14
  reason: 'not-found' | 'cycle' | 'depth-exceeded';
18
- path: MutableToolStepRoleAddress<TKey>[];
15
+ path: MutableToolStepRoleAddress[];
19
16
  };
20
17
 
21
- export function resolveStrategyStateChain<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
22
- strategyState: TStrategyState,
23
- start: MutableToolStepRoleAddress<TKey>,
18
+ export function resolveStrategyStateChain(
19
+ strategyState: MutableStrategyState,
20
+ start: MutableToolStepRoleAddress,
24
21
  opts?: { maxDepth?: number },
25
- ): ResolveResult<TKey> {
22
+ ): ResolveResult {
26
23
  const maxDepth = opts?.maxDepth ?? 50;
27
24
  const visited = new Set<string>();
28
- const path: MutableToolStepRoleAddress<TKey>[] = [];
29
- let current: MutableToolStepRoleAddress<TKey> = start;
25
+ const path: MutableToolStepRoleAddress[] = [];
26
+ let current: MutableToolStepRoleAddress = start;
30
27
 
31
28
  for (let depth = 0; depth <= maxDepth; depth++) {
32
29
  path.push(current);
33
30
 
34
- const visitKey = `${current.toolStepKey}::${current.roleName}`;
31
+ const visitKey = `${current.stepKey}::${current.roleName}`;
35
32
  if (visited.has(visitKey)) {
36
33
  return { status: 'unresolved', reason: 'cycle', path };
37
34
  }
38
35
  visited.add(visitKey);
39
36
 
40
- const entry = getStrategyStateEntry(strategyState, current) as MutableStrategyStateEntry<TKey> | undefined;
37
+ const entry = getStrategyStateEntry(strategyState, current);
41
38
  if (!entry) {
42
39
  return { status: 'unresolved', reason: 'not-found', path };
43
40
  }
@@ -50,7 +47,7 @@ export function resolveStrategyStateChain<TKey extends string, TStrategyState ex
50
47
  return { status: 'externalInputPotential', entry, path };
51
48
  }
52
49
 
53
- current = (entry as MutableInternalInputPotential<TKey>).toolStepRoleAddress;
50
+ current = entry.toolStepRoleAddress;
54
51
  }
55
52
 
56
53
  return { status: 'unresolved', reason: 'depth-exceeded', path };
@@ -1,19 +1,12 @@
1
1
  import { getIndependentThreads } from './stepParallelization.js';
2
2
  import type {
3
3
  MutableStrategy,
4
- MutableStep,
5
- MutableStrategyState,
6
- MutableToolStepKey,
7
4
  } from './mutableStrategyOverlay.js';
8
5
  import { getAuthoringSteps as getAuthoringThreadSteps } from './mutableStrategyOverlay.js';
9
6
 
10
- export function toExecutionMutableStrategy<
11
- TKey extends string = MutableToolStepKey,
12
- TStep extends MutableStep<TKey> = MutableStep<TKey>,
13
- TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
14
- >(
15
- mutableStrategy: MutableStrategy<TKey, TStep, TState>,
16
- ): MutableStrategy<TKey, TStep, TState> {
7
+ export function toExecutionMutableStrategy(
8
+ mutableStrategy: MutableStrategy,
9
+ ): MutableStrategy {
17
10
  const authoringSteps = getAuthoringThreadSteps(mutableStrategy);
18
11
  const stepsByThreadIndex = getIndependentThreads(
19
12
  authoringSteps,