@leitwerk-dev/server 0.1.6 → 0.1.7

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 (139) hide show
  1. package/dist/app.d.ts.map +1 -1
  2. package/dist/app.js +29 -31
  3. package/dist/app.js.map +1 -1
  4. package/dist/auth/auth-client.d.ts +25 -0
  5. package/dist/auth/auth-client.d.ts.map +1 -0
  6. package/dist/auth/auth-client.js +19 -0
  7. package/dist/auth/auth-client.js.map +1 -0
  8. package/dist/auth/auth-config.d.ts +8 -2
  9. package/dist/auth/auth-config.d.ts.map +1 -1
  10. package/dist/auth/auth-config.js +17 -7
  11. package/dist/auth/auth-config.js.map +1 -1
  12. package/dist/auth/auth-service.d.ts +2 -0
  13. package/dist/auth/auth-service.d.ts.map +1 -1
  14. package/dist/auth/auth-service.js +9 -5
  15. package/dist/auth/auth-service.js.map +1 -1
  16. package/dist/auth/auth-test-helpers.js +2 -2
  17. package/dist/auth/auth-test-helpers.js.map +1 -1
  18. package/dist/auth/github-oauth-client.d.ts +20 -0
  19. package/dist/auth/github-oauth-client.d.ts.map +1 -0
  20. package/dist/auth/github-oauth-client.js +79 -0
  21. package/dist/auth/github-oauth-client.js.map +1 -0
  22. package/dist/auth/oidc-client.d.ts +4 -4
  23. package/dist/{chunk-G4PCN46L.js → chunk-GIWHTJSL.js} +18 -93
  24. package/dist/chunk-GIWHTJSL.js.map +1 -0
  25. package/dist/{chunk-KVHAK5UR.js → chunk-OO6VH4FX.js} +515 -400
  26. package/dist/chunk-OO6VH4FX.js.map +1 -0
  27. package/dist/config/config-loader.d.ts.map +1 -1
  28. package/dist/config/config-loader.js +13 -88
  29. package/dist/config/config-loader.js.map +1 -1
  30. package/dist/config/config-types.d.ts +13 -3
  31. package/dist/config/config-types.d.ts.map +1 -1
  32. package/dist/domain-logic/outcome-tools.d.ts +2 -2
  33. package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
  34. package/dist/domain-logic/outcome-tools.js +1 -1
  35. package/dist/domain-logic/outcome-tools.js.map +1 -1
  36. package/dist/index.d.ts +1 -1
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +1 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/integration-tool-registry.d.ts +23 -0
  41. package/dist/integration-tool-registry.d.ts.map +1 -0
  42. package/dist/integration-tool-registry.js +140 -0
  43. package/dist/integration-tool-registry.js.map +1 -0
  44. package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
  45. package/dist/process-ui-snapshot-presenter.js +0 -2
  46. package/dist/process-ui-snapshot-presenter.js.map +1 -1
  47. package/dist/process-watcher-registry.d.ts +3 -31
  48. package/dist/process-watcher-registry.d.ts.map +1 -1
  49. package/dist/process-watcher-registry.js +96 -97
  50. package/dist/process-watcher-registry.js.map +1 -1
  51. package/dist/routes/watchers.d.ts.map +1 -1
  52. package/dist/routes/watchers.js +14 -50
  53. package/dist/routes/watchers.js.map +1 -1
  54. package/dist/semantic-state-json.d.ts +1 -2
  55. package/dist/semantic-state-json.d.ts.map +1 -1
  56. package/dist/semantic-state-json.js +1 -2
  57. package/dist/semantic-state-json.js.map +1 -1
  58. package/dist/supervisor/ipc-handler.d.ts +4 -1
  59. package/dist/supervisor/ipc-handler.d.ts.map +1 -1
  60. package/dist/supervisor/ipc-handler.js +18 -0
  61. package/dist/supervisor/ipc-handler.js.map +1 -1
  62. package/dist/supervisor/worker-start-payload-builder.d.ts +3 -0
  63. package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
  64. package/dist/supervisor/worker-start-payload-builder.js +9 -0
  65. package/dist/supervisor/worker-start-payload-builder.js.map +1 -1
  66. package/dist/supervisor/worker-supervisor.d.ts +3 -1
  67. package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
  68. package/dist/supervisor/worker-supervisor.js +6 -0
  69. package/dist/supervisor/worker-supervisor.js.map +1 -1
  70. package/dist/test-helpers/fake-worker-supervisor.d.ts.map +1 -1
  71. package/dist/test-helpers/fake-worker-supervisor.js +1 -0
  72. package/dist/test-helpers/fake-worker-supervisor.js.map +1 -1
  73. package/dist/test-helpers/ipc-handler-harness.d.ts +1 -0
  74. package/dist/test-helpers/ipc-handler-harness.d.ts.map +1 -1
  75. package/dist/test-helpers/ipc-handler-harness.js +4 -3
  76. package/dist/test-helpers/ipc-handler-harness.js.map +1 -1
  77. package/dist/test-helpers/process-fixtures.js +3 -3
  78. package/dist/test-helpers/process-fixtures.js.map +1 -1
  79. package/dist/{writes-irEq9SlA.d.ts → writes-DUwQe8MD.d.ts} +22 -4
  80. package/package.json +10 -10
  81. package/src/app.ts +34 -35
  82. package/src/auth/auth-client.ts +40 -0
  83. package/src/auth/auth-config.ts +34 -9
  84. package/src/auth/auth-service.ts +11 -5
  85. package/src/auth/auth-test-helpers.ts +2 -2
  86. package/src/auth/github-oauth-client.ts +111 -0
  87. package/src/auth/oidc-client.ts +5 -5
  88. package/src/config/config-loader.ts +18 -102
  89. package/src/config/config-types.ts +15 -3
  90. package/src/domain-logic/outcome-tools.ts +3 -3
  91. package/src/index.ts +1 -4
  92. package/src/integration-tool-registry.ts +186 -0
  93. package/src/process-ui-snapshot-presenter.ts +0 -2
  94. package/src/process-watcher-registry.ts +124 -167
  95. package/src/routes/watchers.ts +14 -50
  96. package/src/semantic-state-json.ts +0 -2
  97. package/src/supervisor/ipc-handler.ts +35 -0
  98. package/src/supervisor/worker-start-payload-builder.ts +19 -0
  99. package/src/supervisor/worker-supervisor.ts +13 -0
  100. package/src/test-helpers/fake-worker-supervisor.ts +1 -0
  101. package/src/test-helpers/ipc-handler-harness.ts +7 -3
  102. package/src/test-helpers/process-fixtures.ts +3 -3
  103. package/dist/chunk-G4PCN46L.js.map +0 -1
  104. package/dist/chunk-KVHAK5UR.js.map +0 -1
  105. package/dist/domain-logic/idempotency.d.ts +0 -3
  106. package/dist/domain-logic/idempotency.d.ts.map +0 -1
  107. package/dist/domain-logic/idempotency.js +0 -3
  108. package/dist/domain-logic/idempotency.js.map +0 -1
  109. package/dist/domain-logic/index.d.ts +0 -7
  110. package/dist/domain-logic/index.d.ts.map +0 -1
  111. package/dist/domain-logic/index.js +0 -6
  112. package/dist/domain-logic/index.js.map +0 -1
  113. package/dist/domain-logic/input-sequencer.d.ts +0 -16
  114. package/dist/domain-logic/input-sequencer.d.ts.map +0 -1
  115. package/dist/domain-logic/input-sequencer.js +0 -2
  116. package/dist/domain-logic/input-sequencer.js.map +0 -1
  117. package/dist/domain-logic/string-normalize.d.ts +0 -2
  118. package/dist/domain-logic/string-normalize.d.ts.map +0 -1
  119. package/dist/domain-logic/string-normalize.js +0 -2
  120. package/dist/domain-logic/string-normalize.js.map +0 -1
  121. package/dist/filesystem-process-watchers.d.ts +0 -22
  122. package/dist/filesystem-process-watchers.d.ts.map +0 -1
  123. package/dist/filesystem-process-watchers.js +0 -79
  124. package/dist/filesystem-process-watchers.js.map +0 -1
  125. package/dist/test-helpers/in-process-worker.d.ts +0 -2
  126. package/dist/test-helpers/in-process-worker.d.ts.map +0 -1
  127. package/dist/test-helpers/in-process-worker.js +0 -2
  128. package/dist/test-helpers/in-process-worker.js.map +0 -1
  129. package/dist/test-helpers/leaf-outcome-fixtures.d.ts +0 -37
  130. package/dist/test-helpers/leaf-outcome-fixtures.d.ts.map +0 -1
  131. package/dist/test-helpers/leaf-outcome-fixtures.js +0 -81
  132. package/dist/test-helpers/leaf-outcome-fixtures.js.map +0 -1
  133. package/src/domain-logic/idempotency.ts +0 -3
  134. package/src/domain-logic/index.ts +0 -31
  135. package/src/domain-logic/input-sequencer.ts +0 -18
  136. package/src/domain-logic/string-normalize.ts +0 -1
  137. package/src/filesystem-process-watchers.ts +0 -147
  138. package/src/test-helpers/in-process-worker.ts +0 -2
  139. package/src/test-helpers/leaf-outcome-fixtures.ts +0 -140
@@ -60,7 +60,7 @@ export interface AuthSessionConfig {
60
60
  }
61
61
 
62
62
  export interface AuthOidcProviderConfig {
63
- id: "forgejo" | "gitlab";
63
+ id: string;
64
64
  kind: "oidc";
65
65
  issuer: string;
66
66
  client_id: string;
@@ -70,12 +70,24 @@ export interface AuthOidcProviderConfig {
70
70
  identity_claim?: string;
71
71
  }
72
72
 
73
+ export interface AuthGithubProviderConfig {
74
+ id: "github";
75
+ kind: "oauth2";
76
+ client_id: string;
77
+ client_secret: string;
78
+ /** GitHub organization whose active members may sign in. */
79
+ organization: string;
80
+ redirect_uri?: string;
81
+ }
82
+
83
+ export type AuthProviderConfig = AuthOidcProviderConfig | AuthGithubProviderConfig;
84
+
73
85
  export interface AuthConfig {
74
86
  /** Only true enables authentication; omit or set false to keep the leitwerk unauthenticated. */
75
87
  enabled?: boolean;
76
88
  session?: AuthSessionConfig;
77
- providers?: AuthOidcProviderConfig[];
78
- /** Raw identity claim values that may sign in. Empty/omitted denies all when enabled. */
89
+ providers?: AuthProviderConfig[];
90
+ /** Raw OIDC identity claim values that may sign in. GitHub uses organization membership. */
79
91
  allowlist?: string[];
80
92
  }
81
93
 
@@ -1,13 +1,13 @@
1
- import type { TurnOutcomePayload } from "@leitwerk-dev/domain";
2
- import type { OutcomeToolParameterSpec, TurnDefinition } from "@leitwerk-dev/process-sdk";
1
+ import { normalizeStringArray, type TurnOutcomePayload, trimString } from "@leitwerk-dev/domain";
3
2
  import {
4
3
  isAutomaticTurnDefinition,
5
4
  isLlmTurnDefinition,
6
5
  isServerAutomaticTurnDefinition,
6
+ type OutcomeToolParameterSpec,
7
+ type TurnDefinition,
7
8
  } from "@leitwerk-dev/process-sdk";
8
9
  import * as v from "valibot";
9
10
  import { isTurnAvailableForProcessGraph, type ProcessGraphRegistry } from "../process-graph.js";
10
- import { normalizeStringArray, trimString } from "./string-normalize.js";
11
11
 
12
12
  interface OutcomeToolValidationContract {
13
13
  kind: "outcome_tool";
package/src/index.ts CHANGED
@@ -116,10 +116,7 @@ export {
116
116
  type ProcessTitleGenerator,
117
117
  type ProcessTitleLogger,
118
118
  } from "./process-title-generator.js";
119
- export {
120
- buildProcessWatcherRegistry,
121
- validateConfiguredProcessWatchersAgainstCatalog,
122
- } from "./process-watcher-registry.js";
119
+ export { buildProcessWatcherRegistry } from "./process-watcher-registry.js";
123
120
  export {
124
121
  buildProjectUpdatedEffect,
125
122
  buildProjectUpdatedFrame,
@@ -0,0 +1,186 @@
1
+ import {
2
+ type IntegrationToolDefinition,
3
+ type IntegrationToolExecutionContext,
4
+ RESERVED_INTEGRATION_TOOL_NAMES,
5
+ } from "@leitwerk-dev/process-sdk";
6
+ import type {
7
+ IntegrationToolDeclaration,
8
+ WorkerIntegrationToolCancelPayload,
9
+ WorkerIntegrationToolRequestPayload,
10
+ WorkerIntegrationToolResultPayload,
11
+ } from "@leitwerk-dev/worker-protocol";
12
+ import type { RepositoryBundle } from "./db/repositories.js";
13
+ import type { ProcessActionRegistry } from "./process-action-registry.js";
14
+
15
+ type RegisteredIntegrationTool = IntegrationToolDefinition<unknown>;
16
+ type IntegrationToolExecutionInput = Omit<IntegrationToolExecutionContext, "signal">;
17
+
18
+ interface PendingIntegrationToolExecution {
19
+ readonly controller: AbortController;
20
+ readonly promise: Promise<unknown>;
21
+ }
22
+
23
+ const TOOL_NAME = /^[a-z][a-z0-9_]{0,63}$/;
24
+ const RESERVED_TOOL_NAMES = new Set<string>(RESERVED_INTEGRATION_TOOL_NAMES);
25
+
26
+ function parseToolArgs(value: unknown): Record<string, unknown> {
27
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
28
+ throw new Error("Integration tool arguments must be an object");
29
+ }
30
+ return value as Record<string, unknown>;
31
+ }
32
+
33
+ export class IntegrationToolRegistry {
34
+ private readonly tools = new Map<string, RegisteredIntegrationTool>();
35
+ private readonly executions = new Map<string, PendingIntegrationToolExecution>();
36
+
37
+ register<TArgs>(definition: IntegrationToolDefinition<TArgs>): void {
38
+ const name = definition.name;
39
+ if (!TOOL_NAME.test(name)) {
40
+ throw new Error(`Integration tool '${definition.name}' must match ${TOOL_NAME}`);
41
+ }
42
+ if (RESERVED_TOOL_NAMES.has(name)) {
43
+ throw new Error(`Integration tool '${name}' uses a reserved tool name`);
44
+ }
45
+ if (definition.description.trim() === "") {
46
+ throw new Error(`Integration tool '${name}' requires a description`);
47
+ }
48
+ if (this.tools.has(name)) throw new Error(`Duplicate integration tool '${name}'`);
49
+ this.tools.set(name, definition as RegisteredIntegrationTool);
50
+ }
51
+
52
+ declarations(names: readonly string[]): IntegrationToolDeclaration[] {
53
+ return names.map((name) => {
54
+ const definition = this.tools.get(name);
55
+ if (!definition) throw new Error(`Unknown integration tool '${name}'`);
56
+ return {
57
+ name: definition.name,
58
+ description: definition.description,
59
+ parameters: definition.parameters,
60
+ };
61
+ });
62
+ }
63
+
64
+ execute(name: string, args: unknown, ctx: IntegrationToolExecutionInput): Promise<unknown> {
65
+ const definition = this.tools.get(name);
66
+ if (!definition) return Promise.reject(new Error(`Unknown integration tool '${name}'`));
67
+ const existing = this.executions.get(ctx.idempotencyKey);
68
+ if (existing) return existing.promise;
69
+ const controller = new AbortController();
70
+ const promise = Promise.resolve()
71
+ .then(() => {
72
+ if (controller.signal.aborted) {
73
+ throw new Error("Integration tool execution cancelled");
74
+ }
75
+ return definition.execute(
76
+ { ...ctx, signal: controller.signal },
77
+ definition.parse?.(args) ?? parseToolArgs(args),
78
+ );
79
+ })
80
+ .finally(() => this.executions.delete(ctx.idempotencyKey));
81
+ this.executions.set(ctx.idempotencyKey, { controller, promise });
82
+ return promise;
83
+ }
84
+
85
+ cancel(idempotencyKey: string): boolean {
86
+ const execution = this.executions.get(idempotencyKey);
87
+ if (!execution) return false;
88
+ execution.controller.abort();
89
+ return true;
90
+ }
91
+ }
92
+
93
+ function integrationToolIdempotencyKey(input: {
94
+ instanceId: string;
95
+ turnRecordId: string;
96
+ toolCallId: string;
97
+ toolName: string;
98
+ }): string {
99
+ return `${input.instanceId}:${input.turnRecordId}:${input.toolCallId}:${input.toolName}`;
100
+ }
101
+
102
+ export function createIntegrationToolRequestService(input: {
103
+ registry: IntegrationToolRegistry;
104
+ repos: Pick<RepositoryBundle, "processes" | "projects" | "turnRecords" | "turnStarts">;
105
+ processActionRegistry: Pick<ProcessActionRegistry, "getTurnDefinition">;
106
+ }) {
107
+ return {
108
+ cancel(instanceId: string, payload: WorkerIntegrationToolCancelPayload): boolean {
109
+ return input.registry.cancel(
110
+ integrationToolIdempotencyKey({
111
+ instanceId,
112
+ turnRecordId: payload.turnRecordId,
113
+ toolCallId: payload.toolCallId,
114
+ toolName: payload.toolName,
115
+ }),
116
+ );
117
+ },
118
+ async handle(
119
+ instanceId: string,
120
+ payload: WorkerIntegrationToolRequestPayload,
121
+ ): Promise<WorkerIntegrationToolResultPayload> {
122
+ const fail = (error: string): WorkerIntegrationToolResultPayload => ({
123
+ turnRecordId: payload.turnRecordId,
124
+ toolCallId: payload.toolCallId,
125
+ ok: false,
126
+ error,
127
+ });
128
+ const process = input.repos.processes.getById(instanceId);
129
+ const turn = input.repos.turnRecords.getById(payload.turnRecordId);
130
+ if (!process || !turn || turn.instanceId !== instanceId || turn.status !== "running") {
131
+ return fail("Integration tool call does not belong to the active running turn");
132
+ }
133
+ const currentStart =
134
+ process.currentExecution?.kind === "worker_start"
135
+ ? input.repos.turnStarts.getById(process.currentExecution.id)
136
+ : null;
137
+ const expectedTurnRecordId =
138
+ currentStart?.state.kind === "accepted" ? currentStart.state.turnRecordId : null;
139
+ if (expectedTurnRecordId !== payload.turnRecordId) {
140
+ return fail("Integration tool call targets a stale turn record");
141
+ }
142
+ if (process.selectedTurnId !== turn.turnId) {
143
+ return fail("Integration tool call targets a stale turn");
144
+ }
145
+ const turnDefinition = input.processActionRegistry.getTurnDefinition(
146
+ process.processId,
147
+ turn.turnId,
148
+ );
149
+ if (
150
+ turnDefinition?.kind !== "llm" ||
151
+ !turnDefinition.integrationTools?.includes(payload.toolName)
152
+ ) {
153
+ return fail(`Integration tool '${payload.toolName}' is not authorized for this turn`);
154
+ }
155
+ const projects = input.repos.projects.listByInstance(instanceId);
156
+ const projectKey =
157
+ typeof payload.args.projectKey === "string" ? payload.args.projectKey : null;
158
+ const project = projectKey
159
+ ? (projects.find((candidate) => candidate.key === projectKey) ?? null)
160
+ : null;
161
+ if (projectKey && !project) return fail(`Unknown process project '${projectKey}'`);
162
+ try {
163
+ const result = await input.registry.execute(payload.toolName, payload.args, {
164
+ process,
165
+ projects,
166
+ turn,
167
+ project,
168
+ idempotencyKey: integrationToolIdempotencyKey({
169
+ instanceId,
170
+ turnRecordId: payload.turnRecordId,
171
+ toolCallId: payload.toolCallId,
172
+ toolName: payload.toolName,
173
+ }),
174
+ });
175
+ return {
176
+ turnRecordId: payload.turnRecordId,
177
+ toolCallId: payload.toolCallId,
178
+ ok: true,
179
+ result,
180
+ };
181
+ } catch (error) {
182
+ return fail(error instanceof Error ? error.message : "Integration tool execution failed");
183
+ }
184
+ },
185
+ };
186
+ }
@@ -180,9 +180,7 @@ function readPersistedModelSelectionWarning(
180
180
 
181
181
  const IDENTIFIER_WORD_LABELS: Record<string, string> = {
182
182
  api: "API",
183
- gitlab: "GitLab",
184
183
  id: "ID",
185
- jira: "Jira",
186
184
  llm: "LLM",
187
185
  mr: "MR",
188
186
  pi: "Pi",
@@ -7,53 +7,26 @@ import type {
7
7
  ProcessLaunchConfig,
8
8
  ProcessLaunchPlan,
9
9
  ProcessWatcherDefinition,
10
+ ProcessWatcherPresentation,
11
+ ProcessWatcherServiceLike,
12
+ ProcessWatcherSource,
13
+ RegisteredProcessWatcherLike,
10
14
  } from "@leitwerk-dev/process-sdk";
11
15
  import { buildProcessWatchers } from "@leitwerk-dev/process-sdk";
12
- import type { ProcessWatcherType } from "@leitwerk-dev/protocol";
13
- import type {
14
- ProcessWatcherConfigSnapshot,
15
- WatcherLaunchConfigSnapshot,
16
- } from "@leitwerk-dev/protocol/config-snapshot";
17
16
  import type { LeitwerkConfig } from "./config/config-types.js";
18
17
  import { buildProcessLaunchPlan } from "./process-launch-plan.js";
19
18
  import type { ServerProcessModelPolicy } from "./process-model-policy/index.js";
20
19
 
21
- export interface RegisteredProcessWatcherLike {
22
- processId: string;
23
- processDisplayName: string;
24
- watcherId: string;
25
- watcherLabel: string;
26
- watcherDescription: string;
27
- type: ProcessWatcherType;
28
- enabled: boolean;
29
- pollInterval: string;
30
- configPath: string;
31
- config: ProcessWatcherConfigSnapshot;
32
- launchModelConfig: LaunchModelConfigInputLike;
33
- }
34
-
35
- export interface ResolvedProcessWatcherStartLike {
36
- watcher: RegisteredProcessWatcherLike;
37
- launchPlan: ProcessLaunchPlan;
38
- }
39
-
40
- export interface ProcessWatcherServiceLike {
41
- listAll(): readonly RegisteredProcessWatcherLike[];
42
- listByType(type: ProcessWatcherType): readonly RegisteredProcessWatcherLike[];
43
- resolveLaunch(
44
- processId: string,
45
- watcherId: string,
46
- payload: unknown,
47
- ctx?: LauncherContext,
48
- ): Promise<ResolvedProcessWatcherStartLike | null>;
49
- }
20
+ export type { ProcessWatcherServiceLike, RegisteredProcessWatcherLike };
50
21
 
51
22
  interface RegisteredProcessWatcher {
52
23
  processId: string;
53
24
  processDisplayName: string;
54
25
  processDef: ExtensionProcessDefinition;
55
26
  definition: ProcessWatcherDefinition;
56
- config: ProcessWatcherConfigSnapshot;
27
+ config: unknown;
28
+ enabled: boolean;
29
+ presentation: ProcessWatcherPresentation;
57
30
  configPath: string;
58
31
  launchModelConfig: LaunchModelConfigInputLike;
59
32
  }
@@ -61,6 +34,7 @@ interface RegisteredProcessWatcher {
61
34
  interface ProcessWatcherRegistryOptions {
62
35
  modelProfiles?: readonly LauncherModelProfileSummary[];
63
36
  getModelProfilesForProcess?: (processId: string) => readonly LauncherModelProfileSummary[];
37
+ processModelPolicy?: ServerProcessModelPolicy;
64
38
  }
65
39
 
66
40
  function collectProcessWatchers(
@@ -80,7 +54,7 @@ function buildLaunchPlan(
80
54
  launcherId: `${watcher.processId}.${watcher.definition.id}`,
81
55
  metadataAdditions: {
82
56
  processWatcherId: watcher.definition.id,
83
- processWatcherType: watcher.definition.type,
57
+ processWatcherSourceId: watcher.definition.source.id,
84
58
  processWatcherConfigPath: watcher.configPath,
85
59
  },
86
60
  errorSubject: `Process watcher '${watcher.definition.id}'`,
@@ -88,39 +62,41 @@ function buildLaunchPlan(
88
62
  });
89
63
  }
90
64
 
91
- function buildWatcherLaunchModelConfig(
92
- launch: WatcherLaunchConfigSnapshot | undefined,
93
- ): LaunchModelConfigInputLike {
94
- const turnConfigs = Object.fromEntries(
95
- Object.entries(launch?.turn_configs ?? {}).map(([turnId, turnConfig]) => [
96
- turnId,
97
- { modelProfileId: turnConfig.model_profile ?? null },
98
- ]),
99
- );
100
- return {
101
- defaultModelProfileId: launch?.default_model_profile ?? null,
102
- turnConfigs,
103
- };
104
- }
105
-
106
- function buildRegistrationView(watcher: RegisteredProcessWatcher): RegisteredProcessWatcherLike {
107
- return {
108
- processId: watcher.processId,
109
- processDisplayName: watcher.processDisplayName,
110
- watcherId: watcher.definition.id,
111
- watcherLabel: watcher.definition.label,
112
- watcherDescription: watcher.definition.description,
113
- type: watcher.definition.type,
114
- enabled: watcher.config.enabled,
115
- pollInterval: watcher.config.poll_interval,
116
- configPath: watcher.configPath,
117
- config: structuredClone(watcher.config),
118
- launchModelConfig: structuredClone(watcher.launchModelConfig),
119
- };
120
- }
121
-
122
- function watcherKey(processId: string, watcherId: string): string {
123
- return `${processId}:${watcherId}`;
65
+ function parseConfiguredWatcher(input: {
66
+ processId: string;
67
+ processDisplayName: string;
68
+ processDef: ExtensionProcessDefinition;
69
+ definition: ProcessWatcherDefinition;
70
+ rawConfig: unknown;
71
+ configPath: string;
72
+ }): RegisteredProcessWatcher {
73
+ try {
74
+ const parsed = input.definition.source.parseConfig(input.rawConfig);
75
+ if (typeof parsed.enabled !== "boolean") {
76
+ throw new Error("parseConfig() must return a boolean enabled value");
77
+ }
78
+ const presentation = input.definition.source.presentConfig(parsed.config);
79
+ if (presentation.targetSummary.trim() === "") {
80
+ throw new Error("presentConfig() must return a non-empty targetSummary");
81
+ }
82
+ return {
83
+ processId: input.processId,
84
+ processDisplayName: input.processDisplayName,
85
+ processDef: input.processDef,
86
+ definition: input.definition,
87
+ config: parsed.config,
88
+ enabled: parsed.enabled,
89
+ presentation,
90
+ configPath: input.configPath,
91
+ launchModelConfig: parsed.launchModelConfig ?? {
92
+ defaultModelProfileId: null,
93
+ turnConfigs: {},
94
+ },
95
+ };
96
+ } catch (error) {
97
+ const message = error instanceof Error ? error.message : String(error);
98
+ throw new Error(`${input.configPath}: ${message}`, { cause: error });
99
+ }
124
100
  }
125
101
 
126
102
  function validateWatcherLaunchModelConfig(
@@ -131,9 +107,6 @@ function validateWatcherLaunchModelConfig(
131
107
  const errors: string[] = [];
132
108
  const schema = policy.project({ kind: "launcher_schema", processId });
133
109
  const llmTurnIds = new Set(schema.turns.map((turn) => turn.turnId));
134
- // Watcher launch profiles are inherited server defaults. Unknown or newly
135
- // disallowed profiles are skipped when the launch plan is prepared; only
136
- // structural turn mistakes make watcher configuration invalid at startup.
137
110
  for (const turnId of Object.keys(modelConfig.turnConfigs ?? {})) {
138
111
  if (!llmTurnIds.has(turnId)) {
139
112
  errors.push(`turn '${turnId}': is not a known LLM turn for process '${processId}'`);
@@ -142,48 +115,49 @@ function validateWatcherLaunchModelConfig(
142
115
  return errors;
143
116
  }
144
117
 
145
- export function validateConfiguredProcessWatchersAgainstCatalog(input: {
118
+ function collectConfiguredProcessWatchers(input: {
146
119
  config: LeitwerkConfig;
147
120
  catalog: Pick<ExtensionCatalog, "processes">;
148
121
  processModelPolicy?: ServerProcessModelPolicy;
149
- }): string[] {
122
+ }): { watchers: RegisteredProcessWatcher[]; errors: string[] } {
123
+ const watchers: RegisteredProcessWatcher[] = [];
150
124
  const errors: string[] = [];
151
125
  for (const [processId, processConfig] of Object.entries(input.config.process_configs ?? {})) {
152
- if (!input.catalog.processes.has(processId)) {
153
- continue;
154
- }
155
126
  const processDef = input.catalog.processes.get(processId);
156
- if (!processDef) {
157
- continue;
158
- }
159
- const built = collectProcessWatchers(processDef);
160
- const configuredWatchers =
161
- (processConfig as { watchers?: Record<string, ProcessWatcherConfigSnapshot> }).watchers ?? {};
162
- for (const [watcherId, watcherConfig] of Object.entries(configuredWatchers)) {
163
- const watcherDef = built.get(watcherId);
164
- if (!watcherDef) {
165
- errors.push(
166
- `Unknown watcher '${watcherId}' at process_configs.${processId}.watchers.${watcherId}`,
167
- );
127
+ if (!processDef) continue;
128
+ const definitions = collectProcessWatchers(processDef);
129
+ for (const [watcherId, rawConfig] of Object.entries(processConfig.watchers ?? {})) {
130
+ const configPath = `process_configs.${processId}.watchers.${watcherId}`;
131
+ const definition = definitions.get(watcherId);
132
+ if (!definition) {
133
+ errors.push(`Unknown watcher '${watcherId}' at ${configPath}`);
168
134
  continue;
169
135
  }
170
- if (watcherDef.type !== watcherConfig.type) {
171
- errors.push(
172
- `Watcher '${watcherId}' at process_configs.${processId}.watchers.${watcherId} configured type '${watcherConfig.type}' does not match declared watcher type '${watcherDef.type}'`,
173
- );
174
- }
175
- if (input.processModelPolicy) {
176
- errors.push(
177
- ...validateWatcherLaunchModelConfig(
178
- processId,
179
- buildWatcherLaunchModelConfig(watcherConfig.launch),
180
- input.processModelPolicy,
181
- ),
182
- );
136
+ try {
137
+ const watcher = parseConfiguredWatcher({
138
+ processId,
139
+ processDisplayName: processDef.displayName,
140
+ processDef,
141
+ definition,
142
+ rawConfig,
143
+ configPath,
144
+ });
145
+ watchers.push(watcher);
146
+ if (input.processModelPolicy) {
147
+ errors.push(
148
+ ...validateWatcherLaunchModelConfig(
149
+ processId,
150
+ watcher.launchModelConfig,
151
+ input.processModelPolicy,
152
+ ),
153
+ );
154
+ }
155
+ } catch (error) {
156
+ errors.push(error instanceof Error ? error.message : String(error));
183
157
  }
184
158
  }
185
159
  }
186
- return errors;
160
+ return { watchers, errors };
187
161
  }
188
162
 
189
163
  export function buildProcessWatcherRegistry(
@@ -191,82 +165,65 @@ export function buildProcessWatcherRegistry(
191
165
  config: LeitwerkConfig,
192
166
  options: ProcessWatcherRegistryOptions = {},
193
167
  ): ProcessWatcherServiceLike {
194
- const watchers = new Map<string, RegisteredProcessWatcher>();
195
-
196
- for (const [processId, processDef] of catalog.processes) {
197
- const built = collectProcessWatchers(processDef);
198
- const processWatchers =
199
- (
200
- config.process_configs?.[processId] as
201
- | { watchers?: Record<string, ProcessWatcherConfigSnapshot> }
202
- | undefined
203
- )?.watchers ?? {};
204
- for (const [watcherId, watcherConfig] of Object.entries(processWatchers)) {
205
- const watcherDef = built.get(watcherId);
206
- if (!watcherDef || watcherDef.type !== watcherConfig.type) {
207
- continue;
208
- }
209
- watchers.set(watcherKey(processId, watcherId), {
210
- processId,
211
- processDisplayName: processDef.displayName,
212
- processDef,
213
- definition: watcherDef,
214
- config: watcherConfig,
215
- configPath: `process_configs.${processId}.watchers.${watcherId}`,
216
- launchModelConfig: buildWatcherLaunchModelConfig(watcherConfig.launch),
217
- });
218
- }
168
+ const { watchers, errors } = collectConfiguredProcessWatchers({
169
+ catalog,
170
+ config,
171
+ processModelPolicy: options.processModelPolicy,
172
+ });
173
+ if (errors.length > 0) {
174
+ throw new Error(`Invalid process watcher config:\n${errors.join("\n")}`);
219
175
  }
220
176
 
221
177
  const getDefaultModelProfilesForProcess = (
222
178
  processId: string,
223
179
  ): readonly LauncherModelProfileSummary[] =>
224
180
  options.getModelProfilesForProcess?.(processId) ?? options.modelProfiles ?? [];
225
-
226
181
  const createWatcherContext = (processId: string, ctx: LauncherContext = {}): LauncherContext => ({
227
182
  ...ctx,
228
183
  modelProfiles: ctx.modelProfiles ?? getDefaultModelProfilesForProcess(processId),
229
184
  });
230
185
 
231
- const sortedWatchers = [...watchers.values()].sort((a, b) => {
232
- const processCompare = a.processDisplayName.localeCompare(b.processDisplayName);
233
- if (processCompare !== 0) {
234
- return processCompare;
235
- }
236
- return a.definition.label.localeCompare(b.definition.label);
237
- });
186
+ const registrations = watchers
187
+ .sort((a, b) => {
188
+ const processCompare = a.processDisplayName.localeCompare(b.processDisplayName);
189
+ return processCompare !== 0
190
+ ? processCompare
191
+ : a.definition.label.localeCompare(b.definition.label);
192
+ })
193
+ .map((watcher) => ({
194
+ source: watcher.definition.source,
195
+ view: {
196
+ processId: watcher.processId,
197
+ processDisplayName: watcher.processDisplayName,
198
+ watcherId: watcher.definition.id,
199
+ watcherLabel: watcher.definition.label,
200
+ watcherDescription: watcher.definition.description,
201
+ sourceId: watcher.definition.source.id,
202
+ sourceLabel: watcher.definition.source.label,
203
+ enabled: watcher.enabled,
204
+ configPath: watcher.configPath,
205
+ config: structuredClone(watcher.config),
206
+ presentation: structuredClone(watcher.presentation),
207
+ launchModelConfig: structuredClone(watcher.launchModelConfig),
208
+ async resolveLaunch(event: unknown, ctx: LauncherContext = {}) {
209
+ const watcherContext = createWatcherContext(watcher.processId, ctx);
210
+ if (
211
+ watcher.definition.matches &&
212
+ !(await watcher.definition.matches(event, watcherContext))
213
+ ) {
214
+ return null;
215
+ }
216
+ const launchConfig = await watcher.definition.resolveLaunchConfig(event, watcherContext);
217
+ return buildLaunchPlan(watcher, launchConfig, config.commit_messages);
218
+ },
219
+ } satisfies RegisteredProcessWatcherLike,
220
+ }));
238
221
 
239
222
  return {
240
- listAll() {
241
- return sortedWatchers.map(buildRegistrationView);
242
- },
243
- listByType(type: ProcessWatcherType) {
244
- return sortedWatchers
245
- .filter((watcher) => watcher.definition.type === type)
246
- .map(buildRegistrationView);
247
- },
248
- async resolveLaunch(
249
- processId: string,
250
- watcherId: string,
251
- payload: unknown,
252
- ctx: LauncherContext = {},
253
- ) {
254
- const watcher = watchers.get(watcherKey(processId, watcherId));
255
- if (!watcher) {
256
- return null;
257
- }
258
- const watcherContext = createWatcherContext(processId, ctx);
259
- if (watcher.definition.matches) {
260
- const matches = await watcher.definition.matches(payload, watcherContext);
261
- if (!matches) {
262
- return null;
263
- }
264
- }
265
- const launchConfig = await watcher.definition.resolveLaunchConfig(payload, watcherContext);
266
- return {
267
- watcher: buildRegistrationView(watcher),
268
- launchPlan: buildLaunchPlan(watcher, launchConfig, config.commit_messages),
269
- };
270
- },
223
+ listAll: () => registrations.map(({ view }) => view),
224
+ listBySource: <TConfig, TEvent>(source: ProcessWatcherSource<TConfig, TEvent>) =>
225
+ registrations
226
+ .filter((watcher) => watcher.source === source)
227
+ .map(({ view }) => view) as RegisteredProcessWatcherLike<TConfig, TEvent>[],
271
228
  };
272
229
  }