@leitwerk-dev/process-sdk 0.1.8 → 0.2.0

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 (171) hide show
  1. package/dist/{chunk-TRQJ5PRM.js → chunk-TA4KO53Q.js} +1 -5
  2. package/dist/chunk-TA4KO53Q.js.map +1 -0
  3. package/dist/{chunk-5RJZ2E7D.js → chunk-WFY2WWZE.js} +2 -6
  4. package/dist/{chunk-5RJZ2E7D.js.map → chunk-WFY2WWZE.js.map} +1 -1
  5. package/dist/core-capabilities.d.ts +76 -34
  6. package/dist/core-capabilities.d.ts.map +1 -1
  7. package/dist/define-process.d.ts +36 -55
  8. package/dist/define-process.d.ts.map +1 -1
  9. package/dist/{extension-api-CqPGRqc9.d.ts → extension-api-CcqZCYJx.d.ts} +160 -74
  10. package/dist/extension-api.d.ts +120 -5
  11. package/dist/extension-api.d.ts.map +1 -1
  12. package/dist/external-action-source.d.ts +7 -0
  13. package/dist/external-action-source.d.ts.map +1 -0
  14. package/dist/external-source-poll.d.ts +15 -0
  15. package/dist/external-source-poll.d.ts.map +1 -0
  16. package/dist/flow.d.ts +92 -110
  17. package/dist/flow.d.ts.map +1 -1
  18. package/dist/fs-utils.d.ts +2 -0
  19. package/dist/fs-utils.d.ts.map +1 -1
  20. package/dist/git-binary.js +25 -33
  21. package/dist/git-binary.js.map +1 -1
  22. package/dist/index.d.ts +21 -9
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +4694 -29
  25. package/dist/index.js.map +1 -1
  26. package/dist/integration-http.d.ts +21 -0
  27. package/dist/integration-http.d.ts.map +1 -0
  28. package/dist/json-data.d.ts +3 -0
  29. package/dist/json-data.d.ts.map +1 -0
  30. package/dist/leaf-outcome-renderer.js +74 -64
  31. package/dist/leaf-outcome-renderer.js.map +1 -1
  32. package/dist/leaf-outcomes.d.ts.map +1 -1
  33. package/dist/model-provider.d.ts.map +1 -1
  34. package/dist/pi-config.d.ts +1 -5
  35. package/dist/pi-config.d.ts.map +1 -1
  36. package/dist/pi-config.js +20 -180
  37. package/dist/pi-config.js.map +1 -1
  38. package/dist/process-flow-view.d.ts.map +1 -1
  39. package/dist/process-graph.d.ts +1 -2
  40. package/dist/process-graph.d.ts.map +1 -1
  41. package/dist/process-watcher-source.d.ts +3 -1
  42. package/dist/process-watcher-source.d.ts.map +1 -1
  43. package/dist/project-binding.d.ts +9 -0
  44. package/dist/project-binding.d.ts.map +1 -0
  45. package/dist/registry-utils.d.ts.map +1 -1
  46. package/dist/repository-feedback.d.ts +29 -0
  47. package/dist/repository-feedback.d.ts.map +1 -0
  48. package/dist/repository-git-env.d.ts +2 -0
  49. package/dist/repository-git-env.d.ts.map +1 -1
  50. package/dist/repository-http.d.ts +19 -0
  51. package/dist/repository-http.d.ts.map +1 -0
  52. package/dist/repository-https.d.ts +3 -0
  53. package/dist/repository-https.d.ts.map +1 -0
  54. package/dist/repository-issue-watcher.d.ts +31 -0
  55. package/dist/repository-issue-watcher.d.ts.map +1 -0
  56. package/dist/repository-source-config.d.ts +32 -0
  57. package/dist/repository-source-config.d.ts.map +1 -0
  58. package/dist/repository-types.d.ts +36 -0
  59. package/dist/repository-types.d.ts.map +1 -0
  60. package/dist/review-flow.js +12 -43
  61. package/dist/review-flow.js.map +1 -1
  62. package/dist/runtime-internals.js +18 -2
  63. package/dist/runtime-internals.js.map +1 -1
  64. package/dist/state-helpers.d.ts +3 -2
  65. package/dist/state-helpers.d.ts.map +1 -1
  66. package/dist/subprocess-env.d.ts +1 -0
  67. package/dist/subprocess-env.d.ts.map +1 -1
  68. package/dist/tool-arguments.d.ts +7 -0
  69. package/dist/tool-arguments.d.ts.map +1 -0
  70. package/dist/turn-semantics.d.ts +5 -6
  71. package/dist/turn-semantics.d.ts.map +1 -1
  72. package/dist/types.d.ts +5 -15
  73. package/dist/types.d.ts.map +1 -1
  74. package/package.json +6 -5
  75. package/src/core-capabilities.ts +90 -37
  76. package/src/define-process.ts +231 -277
  77. package/src/extension-api.ts +148 -4
  78. package/src/external-action-source.ts +12 -0
  79. package/src/external-source-poll.ts +49 -0
  80. package/src/flow.ts +427 -646
  81. package/src/fs-utils.ts +17 -2
  82. package/src/index.ts +56 -13
  83. package/src/integration-http.ts +58 -0
  84. package/src/json-data.ts +25 -0
  85. package/src/leaf-outcomes.ts +1 -9
  86. package/src/model-provider.ts +10 -23
  87. package/src/pi-config.ts +1 -6
  88. package/src/process-flow-view.ts +32 -88
  89. package/src/process-graph.ts +25 -54
  90. package/src/process-watcher-source.ts +13 -8
  91. package/src/project-binding.ts +36 -0
  92. package/src/registry-utils.ts +1 -5
  93. package/src/repository-feedback.ts +59 -0
  94. package/src/repository-git-env.ts +34 -0
  95. package/src/repository-http.ts +71 -0
  96. package/src/repository-https.ts +22 -0
  97. package/src/repository-issue-watcher.ts +92 -0
  98. package/src/repository-source-config.ts +60 -0
  99. package/src/repository-types.ts +25 -0
  100. package/src/state-helpers.ts +8 -9
  101. package/src/subprocess-env.ts +6 -0
  102. package/src/tool-arguments.ts +37 -0
  103. package/src/turn-semantics.ts +31 -75
  104. package/src/types.ts +5 -15
  105. package/dist/browser-ui-extension.js +0 -2
  106. package/dist/browser-ui-extension.js.map +0 -1
  107. package/dist/capabilities.js +0 -7
  108. package/dist/capabilities.js.map +0 -1
  109. package/dist/chunk-TRQJ5PRM.js.map +0 -1
  110. package/dist/codecs.js +0 -9
  111. package/dist/codecs.js.map +0 -1
  112. package/dist/commit-message.js +0 -3
  113. package/dist/commit-message.js.map +0 -1
  114. package/dist/core-capabilities.js +0 -6
  115. package/dist/core-capabilities.js.map +0 -1
  116. package/dist/define-process.js +0 -1040
  117. package/dist/define-process.js.map +0 -1
  118. package/dist/event-bus.js +0 -31
  119. package/dist/event-bus.js.map +0 -1
  120. package/dist/extension-api.js +0 -5
  121. package/dist/extension-api.js.map +0 -1
  122. package/dist/flow.js +0 -1565
  123. package/dist/flow.js.map +0 -1
  124. package/dist/form-contract.js +0 -2
  125. package/dist/form-contract.js.map +0 -1
  126. package/dist/fs-utils.js +0 -23
  127. package/dist/fs-utils.js.map +0 -1
  128. package/dist/graph-fragments.js +0 -57
  129. package/dist/graph-fragments.js.map +0 -1
  130. package/dist/host-capabilities.js +0 -42
  131. package/dist/host-capabilities.js.map +0 -1
  132. package/dist/launcher-contract.js +0 -2
  133. package/dist/launcher-contract.js.map +0 -1
  134. package/dist/leaf-outcomes.js +0 -52
  135. package/dist/leaf-outcomes.js.map +0 -1
  136. package/dist/model-provider.js +0 -171
  137. package/dist/model-provider.js.map +0 -1
  138. package/dist/process-definition-internals.js +0 -25
  139. package/dist/process-definition-internals.js.map +0 -1
  140. package/dist/process-flow-view.js +0 -418
  141. package/dist/process-flow-view.js.map +0 -1
  142. package/dist/process-graph.js +0 -213
  143. package/dist/process-graph.js.map +0 -1
  144. package/dist/process-launcher-builder.js +0 -43
  145. package/dist/process-launcher-builder.js.map +0 -1
  146. package/dist/process-watcher-builder.js +0 -40
  147. package/dist/process-watcher-builder.js.map +0 -1
  148. package/dist/process-watcher-source.js +0 -45
  149. package/dist/process-watcher-source.js.map +0 -1
  150. package/dist/registry-utils.js +0 -18
  151. package/dist/registry-utils.js.map +0 -1
  152. package/dist/repository-git-env.js +0 -26
  153. package/dist/repository-git-env.js.map +0 -1
  154. package/dist/server-events.js +0 -2
  155. package/dist/server-events.js.map +0 -1
  156. package/dist/server-process-builder.js +0 -52
  157. package/dist/server-process-builder.js.map +0 -1
  158. package/dist/state-helpers.js +0 -20
  159. package/dist/state-helpers.js.map +0 -1
  160. package/dist/subprocess-env.js +0 -34
  161. package/dist/subprocess-env.js.map +0 -1
  162. package/dist/tool-renderers.js +0 -63
  163. package/dist/tool-renderers.js.map +0 -1
  164. package/dist/turn-semantics.js +0 -365
  165. package/dist/turn-semantics.js.map +0 -1
  166. package/dist/types.js +0 -23
  167. package/dist/types.js.map +0 -1
  168. package/dist/ui-process-builder.js +0 -30
  169. package/dist/ui-process-builder.js.map +0 -1
  170. package/dist/worker-process-builder.js +0 -22
  171. package/dist/worker-process-builder.js.map +0 -1
@@ -1,34 +1,26 @@
1
1
  import {
2
2
  assertValidProcessProductName,
3
- type InputKind,
4
- type InputSource,
5
- type ProcessInputTarget,
3
+ trimString as normalizeEffectString,
4
+ normalizeStringArray as normalizeEffectStringArray,
6
5
  type ProcessInstance,
7
6
  type ProcessSemanticEntryRefKey,
8
7
  type ProcessTurnStartSelection,
9
8
  type ProcessTurnTerminalLifecycleStatus,
10
9
  type ProcessTurnTransition,
11
- type ReviewSubject,
12
10
  type TurnId,
11
+ type TurnProgressReport,
13
12
  } from "@leitwerk-dev/domain";
14
13
  import type {
15
- Codec,
16
14
  ExtensionProcessDefinition,
17
15
  ExternalActionSource,
18
16
  ExternalSourceEffect,
17
+ ExternalSourceResolveContext,
19
18
  FormDefinition,
20
- ProcessLauncherAPI,
21
19
  ProcessTurnBinding,
22
20
  ProcessTurnOutcomeEvent,
23
- ProcessWatcherAPI,
24
- RepositoryCredentialProject,
25
- RepositoryCredentialRequirement,
26
- ServerProcessAPI,
27
21
  ServerProcessContext,
28
22
  ServerTransitionRequest,
29
- UiProcessAPI,
30
23
  WorkerCompleteInput,
31
- WorkerProcessAPI,
32
24
  WorkerProcessContext,
33
25
  } from "./extension-api.js";
34
26
  import {
@@ -46,7 +38,6 @@ import type {
46
38
  PiBuiltInToolName,
47
39
  ProcessActionPreviewDefinition,
48
40
  ProcessActionSchedulingDefinition,
49
- ProcessPiConfig,
50
41
  TurnAcceptanceState,
51
42
  TurnBranchType,
52
43
  TurnCompletionMode,
@@ -61,12 +52,7 @@ type ProcessEmittedEvent = {
61
52
  data: unknown;
62
53
  };
63
54
 
64
- type ProcessQueuedInput = {
65
- source: InputSource;
66
- kind: InputKind;
67
- bodyMarkdown: string;
68
- target?: ProcessInputTarget;
69
- };
55
+ type ProcessQueuedInput = Parameters<ServerProcessContext["queueInput"]>[0];
70
56
 
71
57
  type MaybePromise<T> = T | Promise<T>;
72
58
 
@@ -88,6 +74,12 @@ type NormalizedActionRoute = NormalizedRouteTarget & {
88
74
  export interface ProcessRuntimeTurnContext<TParams = unknown, TState = unknown>
89
75
  extends WorkerProcessContext<TParams, TState> {}
90
76
 
77
+ export interface LlmTurnPreparationContext<TParams = unknown, TState = unknown>
78
+ extends ProcessRuntimeTurnContext<TParams, TState> {
79
+ callIntegrationTool(name: string, args: Record<string, unknown>): Promise<unknown>;
80
+ reportProgress(report: TurnProgressReport): void;
81
+ }
82
+
91
83
  export interface ProcessServerRuntimeContext<TParams = unknown, TState = unknown> {
92
84
  readonly process: ServerProcessContext<TParams, TState>["process"];
93
85
  readonly projects: ServerProcessContext<TParams, TState>["projects"];
@@ -95,6 +87,7 @@ export interface ProcessServerRuntimeContext<TParams = unknown, TState = unknown
95
87
  readonly state: TState;
96
88
  readSemanticTurnResultMarkdown(ref: ProcessSemanticEntryRefKey): string | null;
97
89
  readProductTurnResultMarkdown(productName: string): string | null;
90
+ reportProgress?(report: TurnProgressReport): void;
98
91
  }
99
92
 
100
93
  export interface ProcessBroadcastEffect {
@@ -105,6 +98,7 @@ export interface ProcessBroadcastEffect {
105
98
  export type ProcessPatchEffect = Partial<
106
99
  Pick<
107
100
  ProcessInstance,
101
+ | "lifecycleStatus"
108
102
  | "planRevision"
109
103
  | "title"
110
104
  | "externalId"
@@ -168,6 +162,14 @@ export type ProcessOutcomeEffect<TParams = unknown, TState = unknown> = (
168
162
  input: ProcessOutcomeExecution<TParams, TState>,
169
163
  ) => MaybePromise<ProcessEffectPlan<TState> | undefined>;
170
164
 
165
+ export type ProcessOutcomeBranchSelector<TParams = unknown, TState = unknown> = (
166
+ input: ProcessOutcomeExecution<TParams, TState>,
167
+ ) => MaybePromise<string>;
168
+
169
+ export interface ProcessOutcomeBranchSpec extends StaticRouteTarget {
170
+ trigger?: string;
171
+ }
172
+
171
173
  export interface ProcessActionExternalTrigger {
172
174
  id: string;
173
175
  label: string;
@@ -187,9 +189,12 @@ export interface ProcessHumanTurnExternalActionSpec<
187
189
  > extends StaticRouteTarget {
188
190
  id: string;
189
191
  source: ExternalActionSource<TParams, TState, TEvent, TInput>;
192
+ /** Arms and exposes this action only when the current process snapshot matches. */
193
+ when?: (ctx: ExternalSourceResolveContext<TParams, TState>) => boolean;
190
194
  label?: string;
191
195
  description?: string;
192
196
  publishInput?: ExternalActionInputPublication;
197
+ effect?: ExternalSourceEffect<TParams, TState, TEvent, TInput>;
193
198
  }
194
199
 
195
200
  export interface ProcessActionBranchSpec extends StaticRouteTarget {
@@ -222,24 +227,36 @@ export type ProcessHumanTurnActionSpec<TParams = unknown, TState = unknown> = Ba
222
227
  externalTriggers?: readonly ProcessActionExternalTrigger[];
223
228
  } & (StaticActionRouting | BranchingActionRouting<TParams, TState>);
224
229
 
225
- export interface ProcessToolOutcomeSpec<TParams = unknown, TState = unknown>
226
- extends StaticRouteTarget {
230
+ interface ProcessToolOutcomeBaseSpec<TParams = unknown, TState = unknown> {
227
231
  description: string;
228
232
  parameters: Record<string, OutcomeToolParameterSpec>;
229
233
  /** Product published from this outcome's turn-result markdown, when this outcome is selected. */
230
234
  publishedProduct?: string;
231
235
  /** Outcome parameter whose markdown value is captured as the turn result. */
232
236
  turnResultMarkdownParameter?: string;
237
+ /** Outcome parameter containing a concise operator-facing summary. */
238
+ resultSummaryParameter?: string;
233
239
  effect?: ProcessOutcomeEffect<TParams, TState>;
234
240
  lifecycleIntent?: ProcessOutcomeLifecycleIntent<TParams, TState>;
235
241
  }
242
+
243
+ export type ProcessToolOutcomeSpec<
244
+ TParams = unknown,
245
+ TState = unknown,
246
+ > = ProcessToolOutcomeBaseSpec<TParams, TState> &
247
+ (
248
+ | StaticRouteTarget
249
+ | {
250
+ branches: Record<string, ProcessOutcomeBranchSpec>;
251
+ choose: ProcessOutcomeBranchSelector<TParams, TState>;
252
+ }
253
+ );
236
254
  export type HumanTurnOperatorAttention = "required" | "passive";
237
255
 
238
256
  export interface HumanTurnDefinition<TParams = unknown, TState = unknown> {
239
257
  kind: "human";
240
258
  description: string;
241
- /** Optional for generic operator-waiting turns; review turns should set it. */
242
- reviewSubject?: ReviewSubject;
259
+ /** Named product rendered as the subject of this human review turn. */
243
260
  reviewProduct?: string;
244
261
  reviewSemanticRef?: ProcessSemanticEntryRefKey;
245
262
  /** Controls whether entering this turn should raise an action-required toast. */
@@ -282,8 +299,12 @@ export interface LlmTurnDefinition<
282
299
  availableTools: readonly PiBuiltInToolName[];
283
300
  /** Server-owned integration tools proxied over authenticated worker IPC. */
284
301
  integrationTools?: readonly string[];
302
+ /** Resolve a constrained tool set from validated durable process data at turn start. */
303
+ resolveIntegrationTools?: (params: TParams, state: TState) => readonly string[];
285
304
  /** Opt in to the durable, operator-facing ask_questions custom tool. */
286
305
  askQuestions?: boolean;
306
+ /** Deterministic worker preparation that must complete before Pi is prompted. */
307
+ prepare?(ctx: LlmTurnPreparationContext<TParams, TState>): MaybePromise<unknown>;
287
308
  completionMode?: TurnCompletionMode;
288
309
  branchType: TurnBranchType;
289
310
  context: TurnContextMode;
@@ -293,7 +314,6 @@ export interface LlmTurnDefinition<
293
314
  outcomes?: Partial<Record<TOutcome, ProcessToolOutcomeSpec<TParams, TState>>>;
294
315
  turnEnd?: ProcessTurnEndSpec<TParams, TState, TOutcome>;
295
316
  turnResultMarkdown?: TurnResultMarkdownBehavior;
296
- reviewSubject?: ReviewSubject;
297
317
  reviewSemanticRef?: ProcessSemanticEntryRefKey;
298
318
  resultSemanticRef?: ProcessSemanticEntryRefKey;
299
319
  publishedProduct?: string;
@@ -310,46 +330,18 @@ export interface AutomaticTurnDefinition<
310
330
  > {
311
331
  kind: "automatic";
312
332
  description: string;
333
+ /** Server-owned integration tools callable by this deterministic worker turn. */
334
+ integrationTools?: readonly string[];
335
+ /** External events armed while this automatic turn is selected and waiting. */
336
+ externalActions?: Record<string, ProcessHumanTurnExternalActionSpec<TParams, TState>>;
313
337
  outcomes?: Partial<Record<TOutcome, ProcessToolOutcomeSpec<TParams, TState>>>;
314
338
  turnEnd?: ProcessTurnEndSpec<TParams, TState, TOutcome>;
315
- reviewSubject?: ReviewSubject;
316
339
  reviewSemanticRef?: ProcessSemanticEntryRefKey;
317
340
  run(
318
341
  ctx: ProcessRuntimeTurnContext<TParams, TState>,
319
342
  ): Promise<WorkerCompleteInput<TOutcome>> | WorkerCompleteInput<TOutcome>;
320
343
  }
321
344
 
322
- export interface ServerAutomaticTurnRunResult<TOutcome extends string = string, TState = unknown>
323
- extends WorkerCompleteInput<TOutcome> {
324
- state?: TState;
325
- }
326
-
327
- export type ServerAutomaticTurnRestartBehavior = "rerun" | "fail_running";
328
-
329
- export interface ServerAutomaticTurnDefinition<
330
- TOutcome extends string = string,
331
- TParams = unknown,
332
- TState = unknown,
333
- > {
334
- kind: "server_automatic";
335
- description: string;
336
- outcomes?: Partial<Record<TOutcome, ProcessToolOutcomeSpec<TParams, TState>>>;
337
- turnEnd?: ProcessTurnEndSpec<TParams, TState, TOutcome>;
338
- reviewSubject?: ReviewSubject;
339
- reviewSemanticRef?: ProcessSemanticEntryRefKey;
340
- /**
341
- * Controls startup/drainer behavior when a durable running turn record already exists.
342
- * The default reruns the deterministic handler. Use fail_running for non-idempotent
343
- * host side effects that must not be replayed after a server crash/restart.
344
- */
345
- restartBehavior?: ServerAutomaticTurnRestartBehavior;
346
- run(
347
- ctx: ProcessServerRuntimeContext<TParams, TState>,
348
- ):
349
- | Promise<ServerAutomaticTurnRunResult<TOutcome, TState>>
350
- | ServerAutomaticTurnRunResult<TOutcome, TState>;
351
- }
352
-
353
345
  export interface ExternalSourceTransition<
354
346
  TParams = unknown,
355
347
  TState = unknown,
@@ -364,14 +356,12 @@ export interface ExternalTurnDefinition<TParams = unknown, TState = unknown> {
364
356
  kind: "external";
365
357
  description: string;
366
358
  transitions: readonly ExternalSourceTransition<TParams, TState>[];
367
- reviewSubject?: ReviewSubject;
368
359
  reviewSemanticRef?: ProcessSemanticEntryRefKey;
369
360
  }
370
361
 
371
362
  export type TurnDefinition<TParams = unknown, TState = unknown> =
372
363
  | LlmTurnDefinition<string, TParams, TState>
373
364
  | AutomaticTurnDefinition<string, TParams, TState>
374
- | ServerAutomaticTurnDefinition<string, TParams, TState>
375
365
  | HumanTurnDefinition<TParams, TState>
376
366
  | ExternalTurnDefinition<TParams, TState>;
377
367
 
@@ -385,28 +375,16 @@ export type ProcessDefinition<TParams = unknown, TState = unknown> = ExtensionPr
385
375
  TState
386
376
  >;
387
377
 
388
- export interface DefinedProcessInput<TParams = unknown, TState = unknown> {
389
- id: string;
390
- displayName: string;
378
+ export interface DefinedProcessInput<TParams = unknown, TState = unknown>
379
+ extends Omit<
380
+ ExtensionProcessDefinition<TParams, TState>,
381
+ "entryTurnId" | "alternateEntryTurnIds" | "turns"
382
+ > {
391
383
  /** Primary entry used when a launch does not select a start turn explicitly. */
392
384
  entry: TurnId;
393
385
  /** Additional entry turns that launchers may select explicitly. */
394
386
  alternateEntries?: readonly TurnId[];
395
- happyPath?: readonly TurnId[];
396
- paramsCodec: Codec<TParams>;
397
- stateCodec: Codec<TState>;
398
- initialState(params: TParams): TState;
399
- repositoryCredentials?(input: {
400
- params: TParams;
401
- projects: readonly RepositoryCredentialProject[];
402
- }): readonly RepositoryCredentialRequirement[];
403
- piConfig?: ProcessPiConfig;
404
387
  turns: TurnDefinitionRecord<TParams, TState>;
405
- worker?: (api: WorkerProcessAPI<TParams, TState>) => void;
406
- server?: (api: ServerProcessAPI<TParams, TState>) => void;
407
- ui?: (api: UiProcessAPI<TParams, TState>) => void;
408
- launchers?: (api: ProcessLauncherAPI<TParams>) => void;
409
- watchers?: (api: ProcessWatcherAPI<TParams>) => void;
410
388
  }
411
389
 
412
390
  type CompiledActionRouting<TParams, TState> =
@@ -428,20 +406,9 @@ type CompiledActionUse<TParams, TState> = {
428
406
  routing: CompiledActionRouting<TParams, TState>;
429
407
  };
430
408
 
431
- type CompiledExecutableTurn<TParams, TState> =
432
- | {
433
- kind: "llm";
434
- id: TurnId;
435
- spec: LlmTurnDefinition<string, TParams, TState>;
436
- }
437
- | {
438
- kind: "automatic";
439
- id: TurnId;
440
- spec: AutomaticTurnDefinition<string, TParams, TState>;
441
- };
442
-
443
- function createTransitionTargetError(context: string, detail: string): Error {
444
- return new Error(`${context} ${detail}`);
409
+ interface CompiledOutcomeHandlers<TParams, TState> {
410
+ effects: ReadonlyMap<string, ProcessOutcomeEffect<TParams, TState> | undefined>;
411
+ routings: ReadonlyMap<string, CompiledOutcomeRouting<TParams, TState>>;
445
412
  }
446
413
 
447
414
  function createProcessTurnBinding<TTurn>(
@@ -465,14 +432,13 @@ function normalizeStaticRouteTarget(
465
432
  (target.complete === true ? 1 : 0) +
466
433
  (target.lifecycleStatus !== undefined ? 1 : 0);
467
434
  if (declaredTargetCount !== 1) {
468
- throw createTransitionTargetError(
469
- context,
470
- "must declare exactly one target via 'to', 'complete', or 'lifecycleStatus'",
435
+ throw new Error(
436
+ `${context} must declare exactly one target via 'to', 'complete', or 'lifecycleStatus'`,
471
437
  );
472
438
  }
473
439
  if (target.to !== undefined) {
474
440
  if (knownTurnIds && !knownTurnIds.has(target.to)) {
475
- throw createTransitionTargetError(context, `references unknown turn '${target.to}'`);
441
+ throw new Error(`${context} references unknown turn '${target.to}'`);
476
442
  }
477
443
  return { nextTurnId: target.to };
478
444
  }
@@ -488,12 +454,81 @@ function buildOutcomeTransition<TParams, TState>(
488
454
  target: ProcessToolOutcomeSpec<TParams, TState> | ProcessTurnEndSpec<TParams, TState>,
489
455
  knownTurnIds?: ReadonlySet<TurnId>,
490
456
  ): NormalizedRouteTarget {
457
+ if ("branches" in target) {
458
+ throw new Error(
459
+ `Turn '${turnId}' outcome '${outcome}' must compile state routing before a static transition`,
460
+ );
461
+ }
491
462
  if (!hasDeclaredStaticRouteTarget(target)) {
492
463
  return { nextTurnId: turnId };
493
464
  }
494
465
  return normalizeStaticRouteTarget(`Turn '${turnId}' outcome '${outcome}'`, target, knownTurnIds);
495
466
  }
496
467
 
468
+ type CompiledOutcomeRouting<TParams, TState> = {
469
+ routes: Readonly<Record<string, NormalizedActionRoute>>;
470
+ choose: ProcessOutcomeBranchSelector<TParams, TState>;
471
+ };
472
+
473
+ type BranchRouteSpec = StaticRouteTarget & { trigger?: string };
474
+
475
+ function compileBranchRoutes(input: {
476
+ branches: Record<string, BranchRouteSpec>;
477
+ knownTurnIds?: ReadonlySet<TurnId>;
478
+ targetContext(branchId: string): string;
479
+ emptyBranchError: string;
480
+ duplicateTriggerError(trigger: string): string;
481
+ }): {
482
+ routes: Record<string, NormalizedActionRoute>;
483
+ transitions: NormalizedActionRoute[];
484
+ } {
485
+ const routes: Record<string, NormalizedActionRoute> = {};
486
+ const transitions: NormalizedActionRoute[] = [];
487
+ const usedTriggers = new Set<string>();
488
+ for (const [branchId, branchSpec] of Object.entries(input.branches)) {
489
+ if (branchId.trim() === "") throw new Error(input.emptyBranchError);
490
+ const target = normalizeStaticRouteTarget(
491
+ input.targetContext(branchId),
492
+ branchSpec,
493
+ input.knownTurnIds,
494
+ );
495
+ const trigger = branchSpec.trigger ?? branchId;
496
+ if (usedTriggers.has(trigger)) throw new Error(input.duplicateTriggerError(trigger));
497
+ usedTriggers.add(trigger);
498
+ const route = { ...target, trigger };
499
+ routes[branchId] = route;
500
+ transitions.push(route);
501
+ }
502
+ return { routes, transitions };
503
+ }
504
+
505
+ function resolveOutcomeRouting<TParams, TState>(input: {
506
+ turnId: TurnId;
507
+ outcome: string;
508
+ spec: ProcessToolOutcomeSpec<TParams, TState>;
509
+ knownTurnIds: ReadonlySet<TurnId>;
510
+ }): CompiledOutcomeRouting<TParams, TState> | null {
511
+ if (!("branches" in input.spec)) {
512
+ return null;
513
+ }
514
+ const entries = Object.entries(input.spec.branches);
515
+ if (entries.length === 0) {
516
+ throw new Error(
517
+ `Turn '${input.turnId}' outcome '${input.outcome}' must declare at least one branch`,
518
+ );
519
+ }
520
+ const { routes } = compileBranchRoutes({
521
+ branches: input.spec.branches,
522
+ knownTurnIds: input.knownTurnIds,
523
+ targetContext: (branchId) =>
524
+ `Turn '${input.turnId}' outcome '${input.outcome}' branch '${branchId}'`,
525
+ emptyBranchError: `Turn '${input.turnId}' outcome '${input.outcome}' contains an empty branch id`,
526
+ duplicateTriggerError: (trigger) =>
527
+ `Turn '${input.turnId}' outcome '${input.outcome}' contains duplicate branch trigger '${trigger}'`,
528
+ });
529
+ return { routes, choose: input.spec.choose };
530
+ }
531
+
497
532
  function resolveActionRouting<TParams, TState>(input: {
498
533
  turnId: TurnId;
499
534
  actionId: string;
@@ -510,42 +545,22 @@ function resolveActionRouting<TParams, TState>(input: {
510
545
  `Action '${input.actionId}' on turn '${input.turnId}' must declare at least one branch`,
511
546
  );
512
547
  }
513
- const routes: Record<string, NormalizedActionRoute> = {};
514
- const resolvedTransitions: NormalizedActionRoute[] = [];
515
- const usedTriggers = new Set<string>();
516
- for (const [branchId, branchSpec] of entries) {
517
- if (branchId.trim() === "") {
518
- throw new Error(
519
- `Action '${input.actionId}' on turn '${input.turnId}' contains an empty branch id`,
520
- );
521
- }
522
- const target = normalizeStaticRouteTarget(
548
+ const { routes, transitions } = compileBranchRoutes({
549
+ branches: input.spec.branches,
550
+ knownTurnIds: input.knownTurnIds,
551
+ targetContext: (branchId) =>
523
552
  `Action '${input.actionId}' branch '${branchId}' on turn '${input.turnId}'`,
524
- branchSpec,
525
- input.knownTurnIds,
526
- );
527
- const trigger = branchSpec.trigger ?? branchId;
528
- if (usedTriggers.has(trigger)) {
529
- throw new Error(
530
- `Action '${input.actionId}' on turn '${input.turnId}' contains duplicate branch trigger '${trigger}'`,
531
- );
532
- }
533
- usedTriggers.add(trigger);
534
- const normalized = {
535
- ...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
536
- ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
537
- trigger,
538
- };
539
- routes[branchId] = normalized;
540
- resolvedTransitions.push(normalized);
541
- }
553
+ emptyBranchError: `Action '${input.actionId}' on turn '${input.turnId}' contains an empty branch id`,
554
+ duplicateTriggerError: (trigger) =>
555
+ `Action '${input.actionId}' on turn '${input.turnId}' contains duplicate branch trigger '${trigger}'`,
556
+ });
542
557
  return {
543
558
  routing: {
544
559
  kind: "branches",
545
560
  routes,
546
561
  choose: input.spec.choose,
547
562
  },
548
- transitions: resolvedTransitions,
563
+ transitions,
549
564
  };
550
565
  }
551
566
  const target = normalizeStaticRouteTarget(
@@ -567,7 +582,7 @@ function resolveActionRouting<TParams, TState>(input: {
567
582
  };
568
583
  }
569
584
 
570
- function externalSourceTransitionId(input: {
585
+ export function getExternalSourceTransitionId(input: {
571
586
  turnId: TurnId;
572
587
  source: ExternalActionSource;
573
588
  index: number;
@@ -575,11 +590,14 @@ function externalSourceTransitionId(input: {
575
590
  return `${input.turnId}:${input.source.kind}:${input.index}`;
576
591
  }
577
592
 
578
- function externalActionArmingId(input: { turnId: TurnId; externalActionId: string }): string {
593
+ export function getExternalActionArmingId(input: {
594
+ turnId: TurnId;
595
+ externalActionId: string;
596
+ }): string {
579
597
  return `${input.turnId}:${input.externalActionId}`;
580
598
  }
581
599
 
582
- function externalActionTransitionTrigger(input: { externalActionId: string }): string {
600
+ export function getExternalActionTransitionTrigger(input: { externalActionId: string }): string {
583
601
  return `external:${input.externalActionId}`;
584
602
  }
585
603
 
@@ -605,7 +623,7 @@ function compileExternalSourceTransitions<TParams, TState>(input: {
605
623
  return {
606
624
  ...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
607
625
  ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
608
- trigger: externalSourceTransitionId({
626
+ trigger: getExternalSourceTransitionId({
609
627
  turnId: input.turnId,
610
628
  source: transition.source,
611
629
  index,
@@ -614,25 +632,6 @@ function compileExternalSourceTransitions<TParams, TState>(input: {
614
632
  });
615
633
  }
616
634
 
617
- export function getExternalSourceTransitionId(input: {
618
- turnId: TurnId;
619
- source: ExternalActionSource;
620
- index: number;
621
- }): string {
622
- return externalSourceTransitionId(input);
623
- }
624
-
625
- export function getExternalActionArmingId(input: {
626
- turnId: TurnId;
627
- externalActionId: string;
628
- }): string {
629
- return externalActionArmingId(input);
630
- }
631
-
632
- export function getExternalActionTransitionTrigger(input: { externalActionId: string }): string {
633
- return externalActionTransitionTrigger(input);
634
- }
635
-
636
635
  function previewMatchesTransition(
637
636
  preview: ProcessActionPreviewDefinition,
638
637
  transition: NormalizedActionRoute,
@@ -756,46 +755,11 @@ function mergeProcessEffectPlans<TState>(
756
755
  };
757
756
  }
758
757
 
759
- function normalizeEffectString(value: unknown): string {
760
- return typeof value === "string" ? value.trim() : "";
761
- }
762
-
763
- function normalizeEffectStringArray(value: unknown): string[] {
764
- return Array.isArray(value)
765
- ? value
766
- .map((entry) => (typeof entry === "string" ? entry.trim() : ""))
767
- .filter((entry) => entry !== "")
768
- : [];
769
- }
770
-
771
- function getTurnReviewSubject(turnDef: TurnDefinition<unknown, unknown> | undefined) {
772
- if (!turnDef || !("reviewSubject" in turnDef)) {
773
- return null;
774
- }
775
- return turnDef.reviewSubject ?? null;
776
- }
777
-
778
- function patchReviewSubjectIntoState<TState>(
779
- state: TState,
780
- reviewSubject: ReviewSubject | null,
781
- ): TState {
782
- if (typeof state !== "object" || state === null || Array.isArray(state)) {
783
- return state;
784
- }
785
- return {
786
- ...(state as Record<string, unknown>),
787
- reviewSubject,
788
- } as TState;
789
- }
790
-
791
758
  async function buildSavePlanResultEffect<TParams, TState>(input: {
792
759
  intent: SavePlanResultLifecycleIntent<TParams, TState>;
793
760
  execution: ProcessOutcomeExecution<TParams, TState>;
794
- targetTurn: TurnDefinition<unknown, unknown> | undefined;
795
761
  }): Promise<ProcessEffectPlan<TState>> {
796
762
  const statePlan = input.intent.state ? await input.intent.state(input.execution) : undefined;
797
- const targetReviewSubject = getTurnReviewSubject(input.targetTurn);
798
- const baseState = statePlan?.state ?? input.execution.ctx.state;
799
763
  const planRevision = input.execution.ctx.process.planRevision + 1;
800
764
  const summary = normalizeEffectString(input.execution.event.params[input.intent.summaryParam]);
801
765
  const acceptanceCriteria = normalizeEffectStringArray(
@@ -807,7 +771,6 @@ async function buildSavePlanResultEffect<TParams, TState>(input: {
807
771
  : normalizeEffectString(input.execution.event.params[input.intent.planMarkdownParam]);
808
772
 
809
773
  return mergeProcessEffectPlans(statePlan, {
810
- state: patchReviewSubjectIntoState(baseState, targetReviewSubject),
811
774
  processPatch: { planRevision },
812
775
  broadcasts: [
813
776
  {
@@ -836,7 +799,6 @@ async function buildSavePlanResultEffect<TParams, TState>(input: {
836
799
 
837
800
  function resolveOutcomeEffect<TParams, TState>(input: {
838
801
  spec: ProcessToolOutcomeSpec<TParams, TState> | ProcessTurnEndSpec<TParams, TState, string>;
839
- targetTurn: TurnDefinition<unknown, unknown> | undefined;
840
802
  }): ProcessOutcomeEffect<TParams, TState> | undefined {
841
803
  const lifecycleIntent = "lifecycleIntent" in input.spec ? input.spec.lifecycleIntent : undefined;
842
804
  if (!lifecycleIntent) {
@@ -850,7 +812,6 @@ function resolveOutcomeEffect<TParams, TState>(input: {
850
812
  lifecyclePlan = await buildSavePlanResultEffect({
851
813
  intent: lifecycleIntent,
852
814
  execution,
853
- targetTurn: input.targetTurn,
854
815
  });
855
816
  break;
856
817
  }
@@ -993,10 +954,8 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
993
954
  outcomes?: Partial<Record<string, ProcessToolOutcomeSpec<TParams, TState>>>;
994
955
  turnEnd?: ProcessTurnEndSpec<TParams, TState, string>;
995
956
  knownTurnIds: ReadonlySet<TurnId>;
996
- turnDefinitionsById: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
997
- }): {
957
+ }): CompiledOutcomeHandlers<TParams, TState> & {
998
958
  transitions: readonly ProcessTurnTransition[];
999
- effects: ReadonlyMap<string, ProcessOutcomeEffect<TParams, TState> | undefined>;
1000
959
  } {
1001
960
  const outcomeEntries = Object.entries(input.outcomes ?? {}) as Array<
1002
961
  [string, ProcessToolOutcomeSpec<TParams, TState>]
@@ -1014,25 +973,38 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
1014
973
 
1015
974
  const transitions: ProcessTurnTransition[] = [];
1016
975
  const effects = new Map<string, ProcessOutcomeEffect<TParams, TState> | undefined>();
976
+ const routings = new Map<string, CompiledOutcomeRouting<TParams, TState>>();
1017
977
  for (const [outcome, spec] of outcomeEntries) {
1018
978
  if (outcome.trim() === "") {
1019
979
  throw new Error(`Turn '${input.turnId}' declares an empty outcome id`);
1020
980
  }
1021
- const target = buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
1022
- transitions.push({
1023
- ...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
1024
- ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
981
+ const routing = resolveOutcomeRouting({
982
+ turnId: input.turnId,
1025
983
  outcome,
984
+ spec,
985
+ knownTurnIds: input.knownTurnIds,
1026
986
  });
1027
- effects.set(
1028
- outcome,
1029
- resolveOutcomeEffect({
1030
- spec,
1031
- targetTurn: target.nextTurnId
1032
- ? input.turnDefinitionsById.get(target.nextTurnId)
1033
- : undefined,
1034
- }),
1035
- );
987
+ const target = routing
988
+ ? null
989
+ : buildOutcomeTransition(input.turnId, outcome, spec, input.knownTurnIds);
990
+ if (routing) {
991
+ routings.set(outcome, routing);
992
+ for (const route of Object.values(routing.routes)) {
993
+ transitions.push({
994
+ ...(route.nextTurnId ? { nextTurnId: route.nextTurnId } : {}),
995
+ ...(route.lifecycleStatus ? { lifecycleStatus: route.lifecycleStatus } : {}),
996
+ trigger: route.trigger,
997
+ outcome,
998
+ });
999
+ }
1000
+ } else if (target) {
1001
+ transitions.push({
1002
+ ...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
1003
+ ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
1004
+ outcome,
1005
+ });
1006
+ }
1007
+ effects.set(outcome, resolveOutcomeEffect({ spec }));
1036
1008
  }
1037
1009
 
1038
1010
  if (input.turnEnd) {
@@ -1050,20 +1022,13 @@ function compileTurnOutcomeDefinitions<TParams, TState>(input: {
1050
1022
  ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
1051
1023
  outcome: input.turnEnd.outcome,
1052
1024
  });
1053
- effects.set(
1054
- input.turnEnd.outcome,
1055
- resolveOutcomeEffect({
1056
- spec: input.turnEnd,
1057
- targetTurn: target.nextTurnId
1058
- ? input.turnDefinitionsById.get(target.nextTurnId)
1059
- : undefined,
1060
- }),
1061
- );
1025
+ effects.set(input.turnEnd.outcome, resolveOutcomeEffect({ spec: input.turnEnd }));
1062
1026
  }
1063
1027
 
1064
1028
  return {
1065
1029
  transitions,
1066
1030
  effects,
1031
+ routings,
1067
1032
  };
1068
1033
  }
1069
1034
 
@@ -1131,7 +1096,7 @@ function resolveExternalActionRoute<TParams, TState>(input: {
1131
1096
  return {
1132
1097
  ...(target.nextTurnId ? { nextTurnId: target.nextTurnId } : {}),
1133
1098
  ...(target.lifecycleStatus ? { lifecycleStatus: target.lifecycleStatus } : {}),
1134
- trigger: externalActionTransitionTrigger({ externalActionId: input.externalActionId }),
1099
+ trigger: getExternalActionTransitionTrigger({ externalActionId: input.externalActionId }),
1135
1100
  };
1136
1101
  }
1137
1102
 
@@ -1199,6 +1164,7 @@ function deriveHumanTurnActions<TParams, TState>(input: {
1199
1164
  spec: HumanTurnDefinition<TParams, TState>;
1200
1165
  knownTurnIds?: ReadonlySet<TurnId>;
1201
1166
  turnDefinitionsById?: ReadonlyMap<TurnId, TurnDefinition<unknown, unknown>>;
1167
+ requireHumanActions?: boolean;
1202
1168
  }): {
1203
1169
  actions: readonly DerivedHumanTurnAction<TParams, TState>[];
1204
1170
  externalActions: readonly DerivedHumanTurnExternalAction<TParams, TState>[];
@@ -1207,7 +1173,7 @@ function deriveHumanTurnActions<TParams, TState>(input: {
1207
1173
  const actionEntries = Object.entries(input.spec.actions) as Array<
1208
1174
  [string, ProcessHumanTurnActionSpec<TParams, TState>]
1209
1175
  >;
1210
- if (actionEntries.length === 0 && input.knownTurnIds) {
1176
+ if (actionEntries.length === 0 && input.knownTurnIds && input.requireHumanActions !== false) {
1211
1177
  throw new Error(`Human turn '${input.turnId}' must declare at least one action`);
1212
1178
  }
1213
1179
 
@@ -1330,7 +1296,7 @@ function deriveHumanTurnActions<TParams, TState>(input: {
1330
1296
  actionSpec,
1331
1297
  transition,
1332
1298
  view: {
1333
- id: externalActionArmingId({ turnId: input.turnId, externalActionId }),
1299
+ id: getExternalActionArmingId({ turnId: input.turnId, externalActionId }),
1334
1300
  externalActionId,
1335
1301
  sourceKind: actionSpec.source.kind,
1336
1302
  label: actionSpec.label ?? actionSpec.source.label ?? null,
@@ -1416,12 +1382,6 @@ function compileExternalTurn<TParams, TState>(input: {
1416
1382
  };
1417
1383
  }
1418
1384
 
1419
- function getTurnEntries<TParams, TState>(
1420
- turns: TurnDefinitionRecord<TParams, TState>,
1421
- ): Array<[TurnId, TurnDefinition<TParams, TState>]> {
1422
- return Object.entries(turns) as Array<[TurnId, TurnDefinition<TParams, TState>]>;
1423
- }
1424
-
1425
1385
  function resolveActionRouteForExecution<TParams, TState>(input: {
1426
1386
  use: CompiledActionUse<TParams, TState>;
1427
1387
  actionId: string;
@@ -1521,7 +1481,7 @@ function validateHappyPathConnectivity<TParams, TState>(input: {
1521
1481
  function buildDefinedProcess<TParams, TState>(
1522
1482
  input: DefinedProcessInput<TParams, TState>,
1523
1483
  ): Pick<ExtensionProcessDefinition<TParams, TState>, "turns" | "worker" | "server"> {
1524
- const turnEntries = getTurnEntries(input.turns);
1484
+ const turnEntries = Object.entries(input.turns);
1525
1485
  if (turnEntries.length === 0) {
1526
1486
  throw new Error(`Process '${input.id}' must declare at least one turn`);
1527
1487
  }
@@ -1566,48 +1526,41 @@ function buildDefinedProcess<TParams, TState>(
1566
1526
 
1567
1527
  const turns = new Map<TurnId, ProcessTurnBinding<TurnDefinition<TParams, TState>>>();
1568
1528
  const actionUses = new Map<string, CompiledActionUse<TParams, TState>[]>();
1569
- const outcomeEffects = new Map<
1529
+ const executableTurns = new Map<
1570
1530
  TurnId,
1571
- ReadonlyMap<string, ProcessOutcomeEffect<TParams, TState> | undefined>
1531
+ CompiledOutcomeHandlers<TParams, TState> & {
1532
+ spec: RoutableTurnDefinition<string, TParams, TState>;
1533
+ }
1572
1534
  >();
1573
- const executableTurns = new Map<TurnId, CompiledExecutableTurn<TParams, TState>>();
1574
1535
 
1575
1536
  for (const [turnId, turnSpec] of turnEntries) {
1576
- if (turnSpec.kind === "llm") {
1577
- const compiledOutcomes = compileTurnOutcomeDefinitions({
1537
+ if (turnSpec.kind === "llm" || turnSpec.kind === "automatic") {
1538
+ const { transitions, effects, routings } = compileTurnOutcomeDefinitions({
1578
1539
  turnId,
1579
1540
  outcomes: turnSpec.outcomes,
1580
1541
  turnEnd: turnSpec.turnEnd,
1581
1542
  knownTurnIds,
1582
- turnDefinitionsById,
1583
- });
1584
- turns.set(turnId, createProcessTurnBinding(turnSpec, compiledOutcomes.transitions));
1585
- outcomeEffects.set(turnId, compiledOutcomes.effects);
1586
- executableTurns.set(turnId, {
1587
- kind: "llm",
1588
- id: turnId,
1589
- spec: turnSpec,
1590
1543
  });
1591
- continue;
1592
- }
1593
-
1594
- if (turnSpec.kind === "automatic" || turnSpec.kind === "server_automatic") {
1595
- const compiledOutcomes = compileTurnOutcomeDefinitions({
1544
+ const externalTransitions =
1545
+ turnSpec.kind === "automatic" && turnSpec.externalActions
1546
+ ? deriveHumanTurnActions({
1547
+ turnId,
1548
+ spec: {
1549
+ kind: "human",
1550
+ description: turnSpec.description,
1551
+ actions: {},
1552
+ externalActions: turnSpec.externalActions,
1553
+ },
1554
+ knownTurnIds,
1555
+ turnDefinitionsById,
1556
+ requireHumanActions: false,
1557
+ }).transitions
1558
+ : [];
1559
+ turns.set(
1596
1560
  turnId,
1597
- outcomes: turnSpec.outcomes,
1598
- turnEnd: turnSpec.turnEnd,
1599
- knownTurnIds,
1600
- turnDefinitionsById,
1601
- });
1602
- turns.set(turnId, createProcessTurnBinding(turnSpec, compiledOutcomes.transitions));
1603
- outcomeEffects.set(turnId, compiledOutcomes.effects);
1604
- if (turnSpec.kind === "automatic") {
1605
- executableTurns.set(turnId, {
1606
- kind: "automatic",
1607
- id: turnId,
1608
- spec: turnSpec,
1609
- });
1610
- }
1561
+ createProcessTurnBinding(turnSpec, [...transitions, ...externalTransitions]),
1562
+ );
1563
+ executableTurns.set(turnId, { spec: turnSpec, effects, routings });
1611
1564
  continue;
1612
1565
  }
1613
1566
 
@@ -1650,18 +1603,18 @@ function buildDefinedProcess<TParams, TState>(
1650
1603
  api.turn(turnId, handler);
1651
1604
  },
1652
1605
  });
1653
- for (const executable of executableTurns.values()) {
1654
- if (manuallyRegisteredTurns.has(executable.id)) {
1606
+ for (const [turnId, { spec }] of executableTurns) {
1607
+ if (manuallyRegisteredTurns.has(turnId)) {
1655
1608
  continue;
1656
1609
  }
1657
- if (executable.kind === "llm") {
1658
- api.turn(executable.id, async (run) => {
1659
- await run.turn(executable.spec);
1610
+ if (spec.kind === "llm") {
1611
+ api.turn(turnId, async (run) => {
1612
+ await run.turn(spec);
1660
1613
  });
1661
1614
  continue;
1662
1615
  }
1663
- api.turn(executable.id, async (run) => {
1664
- await run.complete(await executable.spec.run(run.ctx));
1616
+ api.turn(turnId, async (run) => {
1617
+ await run.complete(await spec.run(run.ctx));
1665
1618
  });
1666
1619
  }
1667
1620
  };
@@ -1723,19 +1676,32 @@ function buildDefinedProcess<TParams, TState>(
1723
1676
  });
1724
1677
  }
1725
1678
 
1726
- for (const [turnId, effects] of outcomeEffects) {
1679
+ for (const [turnId, { effects, routings }] of executableTurns) {
1727
1680
  api.onTurnOutcome(turnId, async (event, ctx) => {
1728
1681
  const effect = effects.get(event.outcome);
1729
- if (!effect) {
1682
+ const routing = routings.get(event.outcome);
1683
+ if (!effect && !routing) {
1730
1684
  return;
1731
1685
  }
1732
- const result = await effect({
1686
+ const execution = {
1733
1687
  ctx: makeProcessServerRuntimeContext(ctx),
1734
1688
  event,
1735
1689
  turnId,
1736
1690
  outcome: event.outcome,
1737
- });
1738
- await applyProcessEffectPlan({ ctx, result });
1691
+ };
1692
+ const result = effect ? await effect(execution) : undefined;
1693
+ let transition: Omit<ServerTransitionRequest<TState>, "state"> | undefined;
1694
+ if (routing) {
1695
+ const branchId = await routing.choose(execution);
1696
+ const route = routing.routes[branchId];
1697
+ if (!route) {
1698
+ throw new Error(
1699
+ `Turn '${turnId}' outcome '${event.outcome}' selected unknown branch '${branchId}'`,
1700
+ );
1701
+ }
1702
+ transition = buildActionTransitionRequest(route);
1703
+ }
1704
+ await applyProcessEffectPlan({ ctx, result, transition });
1739
1705
  });
1740
1706
  }
1741
1707
 
@@ -1763,6 +1729,8 @@ export function defineProcess<TParams = unknown, TState = unknown>(
1763
1729
  paramsCodec: input.paramsCodec,
1764
1730
  stateCodec: input.stateCodec,
1765
1731
  initialState: input.initialState,
1732
+ ...(input.runtime ? { runtime: { ...input.runtime } } : {}),
1733
+ ...(input.resolveStorageSize ? { resolveStorageSize: input.resolveStorageSize } : {}),
1766
1734
  ...(input.repositoryCredentials ? { repositoryCredentials: input.repositoryCredentials } : {}),
1767
1735
  ...(input.piConfig ? { piConfig: input.piConfig } : {}),
1768
1736
  server: compiled.server,
@@ -1777,8 +1745,7 @@ export function defineProcess<TParams = unknown, TState = unknown>(
1777
1745
 
1778
1746
  type RoutableTurnDefinition<TOutcome extends string, TParams, TState> =
1779
1747
  | LlmTurnDefinition<TOutcome, TParams, TState>
1780
- | AutomaticTurnDefinition<TOutcome, TParams, TState>
1781
- | ServerAutomaticTurnDefinition<TOutcome, TParams, TState>;
1748
+ | AutomaticTurnDefinition<TOutcome, TParams, TState>;
1782
1749
 
1783
1750
  // biome-ignore-start lint/suspicious/noExplicitAny: conditional helper types must match and preserve any routable turn instantiation
1784
1751
  type RoutableOutcome<TTurn> =
@@ -1849,19 +1816,6 @@ export function automaticTurn<
1849
1816
  };
1850
1817
  }
1851
1818
 
1852
- export function serverAutomaticTurn<
1853
- TParams = unknown,
1854
- TState = unknown,
1855
- TOutcome extends string = string,
1856
- >(
1857
- input: Omit<ServerAutomaticTurnDefinition<TOutcome, TParams, TState>, "kind">,
1858
- ): ServerAutomaticTurnDefinition<TOutcome, TParams, TState> {
1859
- return {
1860
- ...input,
1861
- kind: "server_automatic",
1862
- };
1863
- }
1864
-
1865
1819
  export function humanTurn<TParams = unknown, TState = unknown>(
1866
1820
  input: Omit<HumanTurnDefinition<TParams, TState>, "kind">,
1867
1821
  ): HumanTurnDefinition<TParams, TState> {