@leitwerk-dev/server 0.3.1 → 0.4.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.
- package/dist/{app-BuJCdkvy.d.ts → app-CsuxFZT5.d.ts} +262 -121
- package/dist/app-JM4CBTND.js +8 -0
- package/dist/app.d.ts +9 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/auth/fastify-auth.d.ts +9 -0
- package/dist/auth/fastify-auth.d.ts.map +1 -1
- package/dist/{chunk-QCHS3X33.js → chunk-FPMDWQF7.js} +2617 -562
- package/dist/chunk-FPMDWQF7.js.map +1 -0
- package/dist/{chunk-HK6BLLHA.js → chunk-MBHRCQFE.js} +2303 -1576
- package/dist/chunk-MBHRCQFE.js.map +1 -0
- package/dist/config/config-loader.d.ts +1 -1
- package/dist/config/config-loader.d.ts.map +1 -1
- package/dist/config/config-types.d.ts +14 -1
- package/dist/config/config-types.d.ts.map +1 -1
- package/dist/config/kubernetes-docker-isolation.d.ts +7 -0
- package/dist/config/kubernetes-docker-isolation.d.ts.map +1 -0
- package/dist/db/database.d.ts.map +1 -1
- package/dist/db/execution-inspection-repo.d.ts +15 -0
- package/dist/db/execution-inspection-repo.d.ts.map +1 -0
- package/dist/db/history-window.d.ts +11 -0
- package/dist/db/history-window.d.ts.map +1 -0
- package/dist/db/process-event-repo.d.ts +2 -0
- package/dist/db/process-event-repo.d.ts.map +1 -1
- package/dist/db/process-instance-repo.d.ts +4 -1
- package/dist/db/process-instance-repo.d.ts.map +1 -1
- package/dist/db/process-leaf-outcome-snapshot-repo.d.ts +2 -1
- package/dist/db/process-leaf-outcome-snapshot-repo.d.ts.map +1 -1
- package/dist/db/process-project-repo.d.ts +2 -0
- package/dist/db/process-project-repo.d.ts.map +1 -1
- package/dist/db/process-skill-repo.d.ts.map +1 -1
- package/dist/db/process-turn-annotation-repo.d.ts +4 -1
- package/dist/db/process-turn-annotation-repo.d.ts.map +1 -1
- package/dist/db/process-turn-record-repo.d.ts +4 -0
- package/dist/db/process-turn-record-repo.d.ts.map +1 -1
- package/dist/db/publication-repo.d.ts +5 -0
- package/dist/db/publication-repo.d.ts.map +1 -0
- package/dist/db/repositories.d.ts +12 -0
- package/dist/db/repositories.d.ts.map +1 -1
- package/dist/db/schema.d.ts +693 -2
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/scoped-settings-repo.d.ts +37 -0
- package/dist/db/scoped-settings-repo.d.ts.map +1 -0
- package/dist/db/skill-repo.d.ts +3 -0
- package/dist/db/skill-repo.d.ts.map +1 -1
- package/dist/db/turn-start-record-repo.d.ts +2 -1
- package/dist/db/turn-start-record-repo.d.ts.map +1 -1
- package/dist/db/turn-summary-repo.d.ts +1 -1
- package/dist/db/turn-summary-repo.d.ts.map +1 -1
- package/dist/db/worker-lease-repo.d.ts +2 -1
- package/dist/db/worker-lease-repo.d.ts.map +1 -1
- package/dist/domain-logic/outcome-tools.d.ts.map +1 -1
- package/dist/external-source-service.d.ts.map +1 -1
- package/dist/future-execution/execution.d.ts +4 -1
- package/dist/future-execution/execution.d.ts.map +1 -1
- package/dist/future-execution/launch-lifecycle.d.ts +9 -25
- package/dist/future-execution/launch-lifecycle.d.ts.map +1 -1
- package/dist/future-execution/lifecycle.d.ts +3 -28
- package/dist/future-execution/lifecycle.d.ts.map +1 -1
- package/dist/future-execution/model-projection.d.ts.map +1 -1
- package/dist/future-execution/reconciliation.d.ts +1 -0
- package/dist/future-execution/reconciliation.d.ts.map +1 -1
- package/dist/future-execution-presenter.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/integration-tool-registry.d.ts +11 -2
- package/dist/integration-tool-registry.d.ts.map +1 -1
- package/dist/launch-coordinator.d.ts.map +1 -1
- package/dist/launcher-model-config-service.d.ts +7 -1
- package/dist/launcher-model-config-service.d.ts.map +1 -1
- package/dist/leaf-outcome-snapshot-capture.d.ts +1 -0
- package/dist/leaf-outcome-snapshot-capture.d.ts.map +1 -1
- package/dist/main.js +3 -3
- package/dist/primary-path-snapshot.d.ts.map +1 -1
- package/dist/process-action-presenter.d.ts +1 -6
- package/dist/process-action-presenter.d.ts.map +1 -1
- package/dist/process-action-registry.d.ts.map +1 -1
- package/dist/process-engine/decision.d.ts +2 -0
- package/dist/process-engine/decision.d.ts.map +1 -1
- package/dist/process-engine/engine.d.ts.map +1 -1
- package/dist/process-engine/mapped-turns.d.ts.map +1 -1
- package/dist/process-engine/ops/continue-failed-turn.d.ts.map +1 -1
- package/dist/process-engine/ops/process-observation.d.ts +6 -0
- package/dist/process-engine/ops/process-observation.d.ts.map +1 -0
- package/dist/process-engine/ops/retry-failed-turn.d.ts.map +1 -1
- package/dist/process-engine/ops/retry-startup.d.ts.map +1 -1
- package/dist/process-engine/ops/start-process.d.ts +2 -0
- package/dist/process-engine/ops/start-process.d.ts.map +1 -1
- package/dist/process-engine/ops/turn-outcome.d.ts.map +1 -1
- package/dist/process-engine/ops/turn-wait.d.ts +30 -0
- package/dist/process-engine/ops/turn-wait.d.ts.map +1 -0
- package/dist/process-engine/ops/update-model-config.d.ts +9 -0
- package/dist/process-engine/ops/update-model-config.d.ts.map +1 -0
- package/dist/process-engine/recorder.d.ts.map +1 -1
- package/dist/process-engine/turn-start-preflight.d.ts +6 -0
- package/dist/process-engine/turn-start-preflight.d.ts.map +1 -1
- package/dist/process-engine/turn-wait-state.d.ts +24 -0
- package/dist/process-engine/turn-wait-state.d.ts.map +1 -0
- package/dist/process-engine/turn-worker-requirement.d.ts +2 -0
- package/dist/process-engine/turn-worker-requirement.d.ts.map +1 -1
- package/dist/process-engine/types.d.ts +5 -1
- package/dist/process-engine/types.d.ts.map +1 -1
- package/dist/process-engine/writes/build-process-abort-writes.d.ts +3 -1
- package/dist/process-engine/writes/build-process-abort-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/build-turn-outcome-writes.d.ts +2 -0
- package/dist/process-engine/writes/build-turn-outcome-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/commit-writes.d.ts +2 -0
- package/dist/process-engine/writes/commit-writes.d.ts.map +1 -1
- package/dist/process-engine/writes/writes.d.ts +28 -60
- package/dist/process-engine/writes/writes.d.ts.map +1 -1
- package/dist/process-graph.d.ts +1 -1
- package/dist/process-graph.d.ts.map +1 -1
- package/dist/process-inspection-reader.d.ts +21 -0
- package/dist/process-inspection-reader.d.ts.map +1 -0
- package/dist/process-inspection-trace.d.ts +13 -0
- package/dist/process-inspection-trace.d.ts.map +1 -0
- package/dist/process-inspection.d.ts +22 -0
- package/dist/process-inspection.d.ts.map +1 -0
- package/dist/process-instance-tree-presenter.d.ts +4 -3
- package/dist/process-instance-tree-presenter.d.ts.map +1 -1
- package/dist/process-launch-executor.d.ts +2 -0
- package/dist/process-launch-executor.d.ts.map +1 -1
- package/dist/process-launch-plan.d.ts +1 -0
- package/dist/process-launch-plan.d.ts.map +1 -1
- package/dist/process-launcher-registry.d.ts.map +1 -1
- package/dist/process-model-config.d.ts +19 -0
- package/dist/process-model-config.d.ts.map +1 -0
- package/dist/process-model-policy/construct.d.ts.map +1 -1
- package/dist/process-model-policy/evaluate.d.ts.map +1 -1
- package/dist/process-model-policy/index.d.ts +4 -1
- package/dist/process-model-policy/index.d.ts.map +1 -1
- package/dist/process-model-policy/project.d.ts.map +1 -1
- package/dist/process-model-policy/types.d.ts +18 -0
- package/dist/process-model-policy/types.d.ts.map +1 -1
- package/dist/process-model-policy-presenter.d.ts.map +1 -1
- package/dist/process-runtime-availability.d.ts +2 -1
- package/dist/process-runtime-availability.d.ts.map +1 -1
- package/dist/process-turn-trace.d.ts.map +1 -1
- package/dist/process-ui-snapshot-presenter.d.ts +14 -3
- package/dist/process-ui-snapshot-presenter.d.ts.map +1 -1
- package/dist/routes/future-executions.d.ts.map +1 -1
- package/dist/routes/launchers.d.ts.map +1 -1
- package/dist/routes/process-actions.d.ts.map +1 -1
- package/dist/routes/process-detail.d.ts.map +1 -1
- package/dist/routes/process-model-config.d.ts +4 -0
- package/dist/routes/process-model-config.d.ts.map +1 -0
- package/dist/routes/process-route-helpers.d.ts +8 -9
- package/dist/routes/process-route-helpers.d.ts.map +1 -1
- package/dist/routes/processes.d.ts.map +1 -1
- package/dist/routes/settings.d.ts +11 -0
- package/dist/routes/settings.d.ts.map +1 -0
- package/dist/routes/ticket-creation.d.ts.map +1 -1
- package/dist/scoped-settings-service.d.ts +83 -0
- package/dist/scoped-settings-service.d.ts.map +1 -0
- package/dist/server-bootstrap/build-host-capabilities.d.ts +2 -0
- package/dist/server-bootstrap/build-host-capabilities.d.ts.map +1 -1
- package/dist/server-bootstrap/register-http.d.ts.map +1 -1
- package/dist/session-summary-projection.d.ts.map +1 -1
- package/dist/skills/bundled-skills.d.ts +5 -0
- package/dist/skills/bundled-skills.d.ts.map +1 -0
- package/dist/skills/catalog-service.d.ts +1 -0
- package/dist/skills/catalog-service.d.ts.map +1 -1
- package/dist/skills/extension-importer.d.ts +11 -0
- package/dist/skills/extension-importer.d.ts.map +1 -0
- package/dist/skills/launch-skills.d.ts +11 -0
- package/dist/skills/launch-skills.d.ts.map +1 -0
- package/dist/skills/skill-frontmatter.d.ts +5 -0
- package/dist/skills/skill-frontmatter.d.ts.map +1 -1
- package/dist/skills/source-importer.d.ts +3 -0
- package/dist/skills/source-importer.d.ts.map +1 -1
- package/dist/startup-evidence.d.ts +3 -0
- package/dist/startup-evidence.d.ts.map +1 -1
- package/dist/supervisor/execution-inspection-ingestor.d.ts +9 -0
- package/dist/supervisor/execution-inspection-ingestor.d.ts.map +1 -0
- package/dist/supervisor/ipc-handler.d.ts +2 -0
- package/dist/supervisor/ipc-handler.d.ts.map +1 -1
- package/dist/supervisor/worker-event-ingestor.d.ts +1 -0
- package/dist/supervisor/worker-event-ingestor.d.ts.map +1 -1
- package/dist/supervisor/worker-start-payload-builder.d.ts.map +1 -1
- package/dist/supervisor/worker-supervisor.d.ts +2 -0
- package/dist/supervisor/worker-supervisor.d.ts.map +1 -1
- package/dist/test-helpers/prepared-fixture-start.d.ts.map +1 -1
- package/dist/test-helpers/process-model-fixtures.d.ts +1 -0
- package/dist/test-helpers/process-model-fixtures.d.ts.map +1 -1
- package/dist/test-helpers/scoped-settings-fixtures.d.ts +16 -0
- package/dist/test-helpers/scoped-settings-fixtures.d.ts.map +1 -0
- package/dist/test-helpers/skill-fixtures.d.ts +4 -0
- package/dist/test-helpers/skill-fixtures.d.ts.map +1 -0
- package/dist/testing.js +2 -1
- package/dist/testing.js.map +1 -1
- package/dist/tool-approval-gate.d.ts +3 -0
- package/dist/tool-approval-gate.d.ts.map +1 -1
- package/dist/turn-progress.d.ts.map +1 -1
- package/dist/turn-wait-service.d.ts +28 -0
- package/dist/turn-wait-service.d.ts.map +1 -0
- package/dist/worker-runtime-profile-selection.d.ts +2 -0
- package/dist/worker-runtime-profile-selection.d.ts.map +1 -1
- package/package.json +20 -19
- package/src/app.ts +115 -5
- package/src/auth/fastify-auth.ts +13 -0
- package/src/config/config-loader.ts +40 -2
- package/src/config/config-types.ts +14 -1
- package/src/config/kubernetes-docker-isolation.ts +21 -0
- package/src/db/database.ts +131 -40
- package/src/db/execution-inspection-repo.ts +163 -0
- package/src/db/history-window.ts +18 -0
- package/src/db/process-event-repo.ts +88 -10
- package/src/db/process-instance-repo.ts +9 -2
- package/src/db/process-leaf-outcome-snapshot-repo.ts +8 -2
- package/src/db/process-project-repo.ts +4 -0
- package/src/db/process-skill-repo.ts +9 -2
- package/src/db/process-turn-annotation-repo.ts +27 -9
- package/src/db/process-turn-record-repo.ts +45 -12
- package/src/db/publication-repo.ts +63 -0
- package/src/db/repositories.ts +17 -0
- package/src/db/schema.ts +103 -1
- package/src/db/scoped-settings-repo.ts +234 -0
- package/src/db/skill-repo.ts +89 -27
- package/src/db/turn-start-record-repo.ts +8 -2
- package/src/db/turn-summary-repo.ts +12 -3
- package/src/db/worker-lease-repo.ts +8 -2
- package/src/domain-logic/outcome-tools.ts +4 -193
- package/src/external-source-service.ts +33 -7
- package/src/future-execution/execution.ts +9 -2
- package/src/future-execution/launch-lifecycle.ts +87 -78
- package/src/future-execution/lifecycle.ts +5 -34
- package/src/future-execution/model-projection.ts +2 -0
- package/src/future-execution/reconciliation.ts +7 -0
- package/src/future-execution-presenter.ts +7 -8
- package/src/integration-tool-registry.ts +59 -26
- package/src/launch-coordinator.ts +10 -1
- package/src/launcher-model-config-service.ts +24 -3
- package/src/leaf-outcome-snapshot-capture.ts +2 -0
- package/src/primary-path-snapshot.ts +3 -4
- package/src/process-action-presenter.ts +22 -57
- package/src/process-action-registry.ts +31 -39
- package/src/process-engine/decision.ts +10 -0
- package/src/process-engine/engine.ts +11 -2
- package/src/process-engine/mapped-turns.ts +5 -7
- package/src/process-engine/ops/continue-failed-turn.ts +2 -13
- package/src/process-engine/ops/process-observation.ts +119 -0
- package/src/process-engine/ops/retry-failed-turn.ts +2 -12
- package/src/process-engine/ops/retry-startup.ts +9 -13
- package/src/process-engine/ops/start-process.ts +34 -2
- package/src/process-engine/ops/turn-outcome.ts +61 -60
- package/src/process-engine/ops/turn-wait.ts +225 -0
- package/src/process-engine/ops/update-model-config.ts +66 -0
- package/src/process-engine/recorder.ts +21 -5
- package/src/process-engine/turn-start-preflight.ts +168 -105
- package/src/process-engine/turn-wait-state.ts +126 -0
- package/src/process-engine/turn-worker-requirement.ts +4 -0
- package/src/process-engine/types.ts +9 -0
- package/src/process-engine/writes/build-process-abort-writes.ts +30 -20
- package/src/process-engine/writes/build-turn-outcome-writes.ts +39 -2
- package/src/process-engine/writes/commit-writes.ts +15 -2
- package/src/process-engine/writes/writes.ts +59 -74
- package/src/process-graph.ts +0 -1
- package/src/process-inspection-reader.ts +305 -0
- package/src/process-inspection-trace.ts +176 -0
- package/src/process-inspection.ts +169 -0
- package/src/process-instance-tree-presenter.ts +56 -216
- package/src/process-launch-executor.ts +5 -2
- package/src/process-launch-plan.ts +17 -2
- package/src/process-launcher-registry.ts +28 -16
- package/src/process-model-config.ts +100 -0
- package/src/process-model-policy/construct.ts +1 -0
- package/src/process-model-policy/evaluate.ts +34 -32
- package/src/process-model-policy/index.ts +45 -3
- package/src/process-model-policy/project.ts +62 -76
- package/src/process-model-policy/types.ts +26 -0
- package/src/process-model-policy-presenter.ts +7 -3
- package/src/process-runtime-availability.ts +9 -4
- package/src/process-turn-trace.ts +12 -24
- package/src/process-ui-snapshot-presenter.ts +210 -295
- package/src/routes/future-executions.ts +5 -5
- package/src/routes/launchers.ts +51 -1
- package/src/routes/process-actions.ts +9 -9
- package/src/routes/process-detail.ts +25 -2
- package/src/routes/process-model-config.ts +70 -0
- package/src/routes/process-route-helpers.ts +153 -162
- package/src/routes/processes.ts +2 -0
- package/src/routes/settings.ts +173 -0
- package/src/routes/ticket-creation.ts +28 -5
- package/src/scoped-settings-service.ts +736 -0
- package/src/server-bootstrap/build-host-capabilities.ts +42 -0
- package/src/server-bootstrap/register-http.ts +7 -1
- package/src/session-summary-projection.ts +1 -2
- package/src/skills/bundled-skills.ts +64 -0
- package/src/skills/catalog-service.ts +35 -6
- package/src/skills/extension-importer.ts +97 -0
- package/src/skills/launch-skills.ts +19 -0
- package/src/skills/skill-frontmatter.ts +13 -0
- package/src/skills/source-importer.ts +3 -0
- package/src/startup-evidence.ts +20 -0
- package/src/supervisor/execution-inspection-ingestor.ts +55 -0
- package/src/supervisor/ipc-handler.ts +2 -0
- package/src/supervisor/worker-event-ingestor.ts +30 -14
- package/src/supervisor/worker-start-payload-builder.ts +17 -0
- package/src/supervisor/worker-supervisor.ts +3 -0
- package/src/test-helpers/prepared-fixture-start.ts +1 -0
- package/src/test-helpers/process-model-fixtures.ts +2 -0
- package/src/test-helpers/scoped-settings-fixtures.ts +126 -0
- package/src/test-helpers/skill-fixtures.ts +28 -0
- package/src/tool-approval-gate.ts +16 -0
- package/src/turn-progress.ts +3 -2
- package/src/turn-wait-service.ts +228 -0
- package/src/worker-runtime-profile-selection.ts +8 -1
- package/dist/app-MSJXFK33.js +0 -8
- package/dist/chunk-HK6BLLHA.js.map +0 -1
- package/dist/chunk-QCHS3X33.js.map +0 -1
- /package/dist/{app-MSJXFK33.js.map → app-JM4CBTND.js.map} +0 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
import { normalize } from "node:path";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
import type {
|
|
4
|
+
ProcessInstance,
|
|
5
|
+
ResolvedSetting,
|
|
6
|
+
ScopedSettingsSnapshot,
|
|
7
|
+
SettingsContext,
|
|
8
|
+
SettingsOverride,
|
|
9
|
+
SettingsSubject,
|
|
10
|
+
} from "@leitwerk-dev/domain";
|
|
11
|
+
import { SYSTEM_ACTOR } from "@leitwerk-dev/domain";
|
|
12
|
+
import type { ExtensionCatalog } from "@leitwerk-dev/extension-runtime";
|
|
13
|
+
import type {
|
|
14
|
+
ExecutionPurposeDefinition,
|
|
15
|
+
ProcessLaunchPlan,
|
|
16
|
+
ScopedSettingsResolver,
|
|
17
|
+
SettingDefinition,
|
|
18
|
+
SettingsSubjectInput,
|
|
19
|
+
} from "@leitwerk-dev/process-sdk";
|
|
20
|
+
import { repositorySettingsIdentity } from "@leitwerk-dev/process-sdk";
|
|
21
|
+
import type {
|
|
22
|
+
SettingFieldView,
|
|
23
|
+
SettingsOverviewResponse,
|
|
24
|
+
SettingsPreview,
|
|
25
|
+
SettingsScopesResponse,
|
|
26
|
+
} from "@leitwerk-dev/protocol/http-contracts";
|
|
27
|
+
import { settingSelectionValues } from "@leitwerk-dev/protocol/http-contracts";
|
|
28
|
+
import type { LeitwerkConfig } from "./config/config-types.js";
|
|
29
|
+
import type { RepositoryBundle } from "./db/repositories.js";
|
|
30
|
+
import type { SettingsWrite } from "./db/scoped-settings-repo.js";
|
|
31
|
+
|
|
32
|
+
/** @internal */
|
|
33
|
+
export class SettingsError extends Error {
|
|
34
|
+
constructor(
|
|
35
|
+
message: string,
|
|
36
|
+
readonly statusCode = 422,
|
|
37
|
+
) {
|
|
38
|
+
super(message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Do not equate SSH/HTTPS, strip .git, resolve symlinks, or case-fold paths.
|
|
43
|
+
/** @internal */
|
|
44
|
+
export function normalizeSettingsLocator(locator: string): string {
|
|
45
|
+
const value = locator.trim();
|
|
46
|
+
if (/^(\/|\.\/|\.\.\/|~\/)/.test(value)) return normalize(value);
|
|
47
|
+
try {
|
|
48
|
+
const url = new URL(value);
|
|
49
|
+
if (url.password || ((url.protocol === "https:" || url.protocol === "http:") && url.username))
|
|
50
|
+
throw new SettingsError("Repository settings locators must not contain credentials");
|
|
51
|
+
return url.toString();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof SettingsError) throw error;
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @internal */
|
|
59
|
+
type SettingsDraft = Omit<SettingsOverride, "actor" | "createdAt" | "updatedAt">;
|
|
60
|
+
/** @internal */
|
|
61
|
+
type SettingsChange = Omit<SettingsWrite, "schemaVersion" | "mode"> &
|
|
62
|
+
Partial<Pick<SettingsWrite, "mode">>;
|
|
63
|
+
/** @internal */
|
|
64
|
+
type SubjectProcess = Pick<ProcessInstance, "id" | "processId" | "metadata">;
|
|
65
|
+
/** @internal */
|
|
66
|
+
type Project = Pick<
|
|
67
|
+
import("@leitwerk-dev/process-sdk").ProcessLaunchProjectConfig,
|
|
68
|
+
"key" | "repoLocator" | "metadata"
|
|
69
|
+
>;
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
interface ProcessScopeContext {
|
|
73
|
+
/** @internal */
|
|
74
|
+
context: SettingsContext;
|
|
75
|
+
/** @internal */
|
|
76
|
+
subjects: Array<{
|
|
77
|
+
/** @internal */
|
|
78
|
+
project: Project;
|
|
79
|
+
/** @internal */
|
|
80
|
+
subject: SettingsSubject;
|
|
81
|
+
}>;
|
|
82
|
+
/** @internal */
|
|
83
|
+
explanations: string[];
|
|
84
|
+
}
|
|
85
|
+
/** @internal */
|
|
86
|
+
interface ProcessSettingsPreview {
|
|
87
|
+
/** @internal */
|
|
88
|
+
turnId: string;
|
|
89
|
+
/** @internal */
|
|
90
|
+
settings: ScopedSettingsSnapshot | null;
|
|
91
|
+
/** @internal */
|
|
92
|
+
error: string | null;
|
|
93
|
+
}
|
|
94
|
+
/** @internal */
|
|
95
|
+
export interface ScopedSettingsService extends ScopedSettingsResolver {
|
|
96
|
+
/** @internal */
|
|
97
|
+
capture(
|
|
98
|
+
process: SubjectProcess,
|
|
99
|
+
turnId: string,
|
|
100
|
+
includeModelDefault?: boolean,
|
|
101
|
+
): ScopedSettingsSnapshot | undefined;
|
|
102
|
+
/** @internal */
|
|
103
|
+
modelDefault(
|
|
104
|
+
processId: string,
|
|
105
|
+
turnId: string,
|
|
106
|
+
process?: SubjectProcess,
|
|
107
|
+
plan?: ProcessLaunchPlan,
|
|
108
|
+
): string | null;
|
|
109
|
+
/** @internal */
|
|
110
|
+
preview(subjectId: string, draft?: SettingsDraft): Promise<SettingsPreview>;
|
|
111
|
+
/** @internal */
|
|
112
|
+
choices(subjectId: string, key: string, search?: string): Promise<SettingFieldView["choices"]>;
|
|
113
|
+
/** @internal */
|
|
114
|
+
write(change: SettingsChange): SettingsOverride;
|
|
115
|
+
/** @internal */
|
|
116
|
+
listScopes(): SettingsScopesResponse;
|
|
117
|
+
/** @internal */
|
|
118
|
+
overview(scopeType: string): Promise<SettingsOverviewResponse>;
|
|
119
|
+
/** @internal */
|
|
120
|
+
forProcess(process: SubjectProcess): ProcessScopeContext;
|
|
121
|
+
/** @internal */
|
|
122
|
+
forLaunch(plan: ProcessLaunchPlan): ProcessScopeContext;
|
|
123
|
+
/** @internal */
|
|
124
|
+
previewProcess(process: SubjectProcess): ProcessSettingsPreview[];
|
|
125
|
+
/** @internal */
|
|
126
|
+
definitions(): Array<
|
|
127
|
+
Pick<SettingFieldView, "key" | "owner" | "schemaVersion" | "scopes" | "merge" | "form">
|
|
128
|
+
>;
|
|
129
|
+
/** @internal */
|
|
130
|
+
refresh(): Promise<SettingsScopesResponse>;
|
|
131
|
+
/** @internal */
|
|
132
|
+
previewDraft(change: Omit<SettingsChange, "expectedRevision">): Promise<SettingsPreview>;
|
|
133
|
+
}
|
|
134
|
+
/** @internal */
|
|
135
|
+
|
|
136
|
+
export function createScopedSettingsService(input: {
|
|
137
|
+
repos: RepositoryBundle;
|
|
138
|
+
catalog: ExtensionCatalog;
|
|
139
|
+
config: LeitwerkConfig;
|
|
140
|
+
modelChoices?: () => SettingFieldView["choices"];
|
|
141
|
+
}): ScopedSettingsService {
|
|
142
|
+
const { repos, catalog } = input;
|
|
143
|
+
const definitions = new Map<string, { owner: string; definition: SettingDefinition }>();
|
|
144
|
+
const purposes = new Map<string, ExecutionPurposeDefinition>();
|
|
145
|
+
const scopes = new Map<string, SettingsScopesResponse["scopes"][number]>([
|
|
146
|
+
["instance", { id: "instance", label: "Instance" }],
|
|
147
|
+
[
|
|
148
|
+
"repository",
|
|
149
|
+
{
|
|
150
|
+
id: "repository",
|
|
151
|
+
label: "Repository",
|
|
152
|
+
overview: { itemLabel: "Repository", itemLabelPlural: "Repositories" },
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
]);
|
|
156
|
+
type DiscoveryProvider = NonNullable<
|
|
157
|
+
Parameters<ScopedSettingsResolver["registerDiscovery"]>[2]
|
|
158
|
+
> & { discover: () => Promise<readonly SettingsSubjectInput[]> };
|
|
159
|
+
const discoveries = new Map<string, DiscoveryProvider[]>();
|
|
160
|
+
for (const loaded of catalog.modules) {
|
|
161
|
+
const owner = loaded.module.manifest.id;
|
|
162
|
+
const declarations = loaded.module.scopedSettings;
|
|
163
|
+
for (const scope of declarations?.scopes ?? []) {
|
|
164
|
+
if (scopes.has(scope.id) || !scope.id.startsWith(`${owner}.`))
|
|
165
|
+
throw new Error(`Invalid or duplicate settings scope '${scope.id}'`);
|
|
166
|
+
scopes.set(scope.id, scope);
|
|
167
|
+
}
|
|
168
|
+
for (const definition of declarations?.settings ?? []) {
|
|
169
|
+
if (definitions.has(definition.key) || !definition.key.startsWith(`${owner}.`))
|
|
170
|
+
throw new Error(`Invalid or duplicate setting '${definition.key}'`);
|
|
171
|
+
if (
|
|
172
|
+
!Number.isSafeInteger(definition.schemaVersion) ||
|
|
173
|
+
definition.schemaVersion < 1 ||
|
|
174
|
+
definition.merge !== "replace" ||
|
|
175
|
+
!definition.scopes.length ||
|
|
176
|
+
new Set(definition.scopes).size !== definition.scopes.length
|
|
177
|
+
)
|
|
178
|
+
throw new Error(`Invalid setting declaration '${definition.key}'`);
|
|
179
|
+
definition.schema.parse(definition.defaultValue);
|
|
180
|
+
definitions.set(definition.key, { owner, definition });
|
|
181
|
+
}
|
|
182
|
+
for (const purpose of declarations?.purposes ?? []) {
|
|
183
|
+
if (purposes.has(purpose.id) || !purpose.id.startsWith(`${owner}.`))
|
|
184
|
+
throw new Error(`Invalid or duplicate execution purpose '${purpose.id}'`);
|
|
185
|
+
purposes.set(purpose.id, purpose);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const { definition } of definitions.values()) {
|
|
189
|
+
for (const scope of definition.scopes)
|
|
190
|
+
if (!scopes.has(scope)) throw new Error(`Unknown scope '${scope}' for '${definition.key}'`);
|
|
191
|
+
}
|
|
192
|
+
for (const purpose of purposes.values()) {
|
|
193
|
+
for (const key of [
|
|
194
|
+
...(purpose.settingKeys ?? []),
|
|
195
|
+
...(purpose.modelSettingKey ? [purpose.modelSettingKey] : []),
|
|
196
|
+
]) {
|
|
197
|
+
if (!definitions.has(key)) throw new Error(`Unknown setting '${key}' for '${purpose.id}'`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const process of catalog.processes.values())
|
|
201
|
+
for (const binding of process.turns.values()) {
|
|
202
|
+
const def = binding.definition;
|
|
203
|
+
if (def.kind === "llm" && def.executionPurpose && !purposes.has(def.executionPurpose))
|
|
204
|
+
throw new Error(`Unknown execution purpose '${def.executionPurpose}'`);
|
|
205
|
+
}
|
|
206
|
+
const instance =
|
|
207
|
+
repos.scopedSettings.findIdentity("instance", "installation") ??
|
|
208
|
+
repos.scopedSettings.putSubject({
|
|
209
|
+
id: "instance",
|
|
210
|
+
scopeType: "instance",
|
|
211
|
+
identity: "installation",
|
|
212
|
+
label: "Instance",
|
|
213
|
+
context: {},
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const choiceValues = settingSelectionValues;
|
|
217
|
+
|
|
218
|
+
function requireSubject(id: string): SettingsSubject {
|
|
219
|
+
const subject = repos.scopedSettings.getSubject(id);
|
|
220
|
+
if (!subject) throw new SettingsError("Settings scope was not found", 404);
|
|
221
|
+
return subject;
|
|
222
|
+
}
|
|
223
|
+
function requireDefinition(key: string): SettingDefinition {
|
|
224
|
+
const definition = definitions.get(key)?.definition;
|
|
225
|
+
if (!definition)
|
|
226
|
+
throw new SettingsError(
|
|
227
|
+
`Setting '${key}' is inactive. Install its owning extension to edit it.`,
|
|
228
|
+
409,
|
|
229
|
+
);
|
|
230
|
+
return definition;
|
|
231
|
+
}
|
|
232
|
+
function contextForSubject(subject: SettingsSubject): SettingsContext {
|
|
233
|
+
return { instance: instance.id, ...subject.context, [subject.scopeType]: subject.id };
|
|
234
|
+
}
|
|
235
|
+
function parse(definition: SettingDefinition, value: unknown): unknown {
|
|
236
|
+
try {
|
|
237
|
+
const parsed = definition.schema.parse(value);
|
|
238
|
+
if (JSON.stringify(parsed) === undefined) throw new Error("Value must be JSON-compatible");
|
|
239
|
+
if (
|
|
240
|
+
definition.form.control === "model" &&
|
|
241
|
+
parsed !== null &&
|
|
242
|
+
(typeof parsed !== "string" ||
|
|
243
|
+
!input.config.pi.model_profiles.some((profile) => profile.id === parsed))
|
|
244
|
+
)
|
|
245
|
+
throw new Error(`Unknown model profile '${String(parsed)}'`);
|
|
246
|
+
return parsed;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw new SettingsError(
|
|
249
|
+
`${definition.form.label}: ${error instanceof Error ? error.message : "Invalid value"}. Correct '${definition.key}' in Settings.`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function resolveDefinition<T>(
|
|
254
|
+
definition: SettingDefinition<T>,
|
|
255
|
+
context: SettingsContext,
|
|
256
|
+
draft?: SettingsDraft,
|
|
257
|
+
omitSubject?: string,
|
|
258
|
+
): ResolvedSetting<T> {
|
|
259
|
+
let value: unknown = definition.schema.parse(definition.defaultValue);
|
|
260
|
+
let sources: ResolvedSetting["sources"] = [
|
|
261
|
+
{
|
|
262
|
+
subjectId: null,
|
|
263
|
+
scopeType: "default",
|
|
264
|
+
label: "Code default",
|
|
265
|
+
revision: 0,
|
|
266
|
+
schemaVersion: definition.schemaVersion,
|
|
267
|
+
mode: "replace",
|
|
268
|
+
},
|
|
269
|
+
];
|
|
270
|
+
for (const scopeType of definition.scopes) {
|
|
271
|
+
const contextId = scopeType === "instance" ? instance.id : context[scopeType];
|
|
272
|
+
if (!contextId) continue;
|
|
273
|
+
const subject = requireSubject(contextId);
|
|
274
|
+
const subjectId = subject.id;
|
|
275
|
+
if (subjectId === omitSubject) continue;
|
|
276
|
+
if (subject.scopeType !== scopeType)
|
|
277
|
+
throw new SettingsError(`Scope '${subjectId}' does not belong to '${scopeType}'`);
|
|
278
|
+
const row =
|
|
279
|
+
draft?.subjectId === subjectId && draft.key === definition.key
|
|
280
|
+
? draft
|
|
281
|
+
: repos.scopedSettings.getOverride(subjectId, definition.key);
|
|
282
|
+
if (!row || row.reset) continue;
|
|
283
|
+
if (row.schemaVersion !== definition.schemaVersion)
|
|
284
|
+
throw new SettingsError(
|
|
285
|
+
`${definition.form.label} on ${subject.label} uses an incompatible schema. Correct '${definition.key}' in Settings.`,
|
|
286
|
+
);
|
|
287
|
+
if (row.mode !== "replace")
|
|
288
|
+
throw new SettingsError(
|
|
289
|
+
`Append overrides are no longer supported. Reset '${definition.key}' in Settings.`,
|
|
290
|
+
);
|
|
291
|
+
value = parse(definition, row.value);
|
|
292
|
+
const source = {
|
|
293
|
+
subjectId,
|
|
294
|
+
scopeType,
|
|
295
|
+
label: subject.label,
|
|
296
|
+
revision: row.revision,
|
|
297
|
+
schemaVersion: row.schemaVersion,
|
|
298
|
+
mode: row.mode,
|
|
299
|
+
};
|
|
300
|
+
sources = [source];
|
|
301
|
+
}
|
|
302
|
+
return { key: definition.key, value: value as T, sources };
|
|
303
|
+
}
|
|
304
|
+
function discover(subjectInput: SettingsSubjectInput): SettingsSubject {
|
|
305
|
+
if (!scopes.has(subjectInput.scopeType))
|
|
306
|
+
throw new SettingsError(`Unknown settings scope '${subjectInput.scopeType}'`);
|
|
307
|
+
if (!subjectInput.identity.trim() || !subjectInput.label.trim())
|
|
308
|
+
throw new SettingsError("Scope identity and label are required");
|
|
309
|
+
const aliases =
|
|
310
|
+
subjectInput.scopeType === "repository"
|
|
311
|
+
? (subjectInput.aliases ?? []).map(normalizeSettingsLocator)
|
|
312
|
+
: [];
|
|
313
|
+
return repos.transaction((tx) => {
|
|
314
|
+
const repo = tx.scopedSettings;
|
|
315
|
+
let previous = repo.findIdentity(subjectInput.scopeType, subjectInput.identity);
|
|
316
|
+
const candidates = new Map<string, SettingsSubject>();
|
|
317
|
+
if (previous) candidates.set(previous.id, previous);
|
|
318
|
+
for (const alias of aliases) {
|
|
319
|
+
const known = repo.findAlias(alias);
|
|
320
|
+
if (!known) continue;
|
|
321
|
+
if (known.identity !== subjectInput.identity && !known.identity.startsWith("locator:"))
|
|
322
|
+
throw new SettingsError(
|
|
323
|
+
`Repository alias '${alias}' belongs to another provider identity`,
|
|
324
|
+
409,
|
|
325
|
+
);
|
|
326
|
+
candidates.set(known.id, known);
|
|
327
|
+
}
|
|
328
|
+
previous ??= [...candidates.values()].sort((a, b) => a.id.localeCompare(b.id))[0];
|
|
329
|
+
// Only provider-verified aliases can coalesce previously unrecognized locators.
|
|
330
|
+
if (candidates.size > 1) {
|
|
331
|
+
if (subjectInput.identity.startsWith("locator:"))
|
|
332
|
+
throw new SettingsError("Repository aliases require a verified provider identity", 409);
|
|
333
|
+
const overrides = new Map<string, SettingsOverride>();
|
|
334
|
+
for (const candidate of candidates.values()) {
|
|
335
|
+
for (const row of repo.listOverrides(candidate.id)) {
|
|
336
|
+
if (row.reset) continue;
|
|
337
|
+
const saved = overrides.get(row.key);
|
|
338
|
+
if (
|
|
339
|
+
saved &&
|
|
340
|
+
(saved.mode !== row.mode ||
|
|
341
|
+
saved.schemaVersion !== row.schemaVersion ||
|
|
342
|
+
!isDeepStrictEqual(saved.value, row.value))
|
|
343
|
+
)
|
|
344
|
+
throw new SettingsError(
|
|
345
|
+
`Repository aliases have conflicting overrides for '${row.key}'. Compare /settings?scope=${saved.subjectId} and /settings?scope=${row.subjectId}; make the values and modes agree or use inherited value on one scope, then refresh repositories.`,
|
|
346
|
+
409,
|
|
347
|
+
);
|
|
348
|
+
overrides.set(row.key, row);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
for (const candidate of candidates.values())
|
|
352
|
+
if (previous && candidate.id !== previous.id)
|
|
353
|
+
repo.mergeLocatorSubject(candidate.id, previous.id);
|
|
354
|
+
}
|
|
355
|
+
const context = subjectInput.context ?? previous?.context ?? {};
|
|
356
|
+
for (const [type, id] of Object.entries(context))
|
|
357
|
+
if (requireSubject(id).scopeType !== type || type === subjectInput.scopeType)
|
|
358
|
+
throw new SettingsError("Invalid scope ancestry");
|
|
359
|
+
const changed =
|
|
360
|
+
!previous ||
|
|
361
|
+
previous.identity !== subjectInput.identity ||
|
|
362
|
+
previous.label !== subjectInput.label ||
|
|
363
|
+
JSON.stringify(previous.context) !== JSON.stringify(context);
|
|
364
|
+
const subject =
|
|
365
|
+
changed || !previous
|
|
366
|
+
? repo.putSubject({ ...subjectInput, id: previous?.id, context })
|
|
367
|
+
: previous;
|
|
368
|
+
for (const alias of aliases) repo.putAlias(alias, subject.id);
|
|
369
|
+
return subject;
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function repository(project: Project): SettingsSubject {
|
|
373
|
+
const locator = normalizeSettingsLocator(project.repoLocator);
|
|
374
|
+
const binding = project.metadata?.settingsRepository;
|
|
375
|
+
if (
|
|
376
|
+
binding &&
|
|
377
|
+
typeof binding === "object" &&
|
|
378
|
+
"origin" in binding &&
|
|
379
|
+
"repositoryId" in binding &&
|
|
380
|
+
typeof binding.origin === "string" &&
|
|
381
|
+
(typeof binding.repositoryId === "string" || typeof binding.repositoryId === "number")
|
|
382
|
+
) {
|
|
383
|
+
const aliases =
|
|
384
|
+
"aliases" in binding && Array.isArray(binding.aliases)
|
|
385
|
+
? binding.aliases.filter((alias): alias is string => typeof alias === "string")
|
|
386
|
+
: [];
|
|
387
|
+
const identity = repositorySettingsIdentity(binding.origin, binding.repositoryId);
|
|
388
|
+
const known = repos.scopedSettings.findIdentity("repository", identity);
|
|
389
|
+
return discover({
|
|
390
|
+
scopeType: "repository",
|
|
391
|
+
identity,
|
|
392
|
+
label: known?.label ?? locator,
|
|
393
|
+
aliases: [locator, ...aliases],
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
const known = repos.scopedSettings.findAlias(locator);
|
|
397
|
+
if (known) return known;
|
|
398
|
+
return discover({
|
|
399
|
+
scopeType: "repository",
|
|
400
|
+
identity: `locator:${locator}`,
|
|
401
|
+
label: locator,
|
|
402
|
+
aliases: [locator],
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
function contextFor(
|
|
406
|
+
process: { metadata?: Record<string, unknown> | null },
|
|
407
|
+
projects: readonly Project[],
|
|
408
|
+
preview = false,
|
|
409
|
+
) {
|
|
410
|
+
const supplied = process.metadata?.settingsContext;
|
|
411
|
+
const context: Record<string, string> = { instance: instance.id };
|
|
412
|
+
if (supplied && typeof supplied === "object")
|
|
413
|
+
for (const [type, id] of Object.entries(supplied)) {
|
|
414
|
+
if (typeof id !== "string" || requireSubject(id).scopeType !== type)
|
|
415
|
+
throw new SettingsError("Invalid retained settings context");
|
|
416
|
+
context[type] = id;
|
|
417
|
+
}
|
|
418
|
+
const subjects = projects.map((project) => ({ project, subject: repository(project) }));
|
|
419
|
+
const primaryKey = process.metadata?.primaryRepositoryKey;
|
|
420
|
+
const primary =
|
|
421
|
+
typeof primaryKey === "string"
|
|
422
|
+
? subjects.find((entry) => entry.project.key === primaryKey)
|
|
423
|
+
: subjects.length === 1
|
|
424
|
+
? subjects[0]
|
|
425
|
+
: undefined;
|
|
426
|
+
if (typeof primaryKey === "string" && !primary)
|
|
427
|
+
throw new SettingsError(`Primary repository '${primaryKey}' is no longer bound`);
|
|
428
|
+
// A preview can retain a known repository's settings without clone/branch discovery.
|
|
429
|
+
if (!preview || projects.length > 0) delete context.repository;
|
|
430
|
+
if (primary) context.repository = primary.subject.id;
|
|
431
|
+
return {
|
|
432
|
+
context,
|
|
433
|
+
subjects,
|
|
434
|
+
explanations:
|
|
435
|
+
subjects.length > 1 && !primary
|
|
436
|
+
? ["No primary repository is bound. Process-wide model defaults use Instance settings."]
|
|
437
|
+
: [],
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function purposeFor(processId: string, turnId: string) {
|
|
441
|
+
const turn = catalog.processes.get(processId)?.turns.get(turnId)?.definition;
|
|
442
|
+
return turn?.kind === "llm" && turn.executionPurpose
|
|
443
|
+
? purposes.get(turn.executionPurpose)
|
|
444
|
+
: undefined;
|
|
445
|
+
}
|
|
446
|
+
function forProcess(process: SubjectProcess) {
|
|
447
|
+
return contextFor(process, repos.projects.listByInstance(process.id));
|
|
448
|
+
}
|
|
449
|
+
function capture(
|
|
450
|
+
process: SubjectProcess,
|
|
451
|
+
turnId: string,
|
|
452
|
+
includeModelDefault = true,
|
|
453
|
+
): ScopedSettingsSnapshot | undefined {
|
|
454
|
+
const purpose = purposeFor(process.processId, turnId);
|
|
455
|
+
if (!purpose) return undefined;
|
|
456
|
+
const { context, explanations } = forProcess(process);
|
|
457
|
+
const keys = new Set([
|
|
458
|
+
...(purpose.settingKeys ?? []),
|
|
459
|
+
...(includeModelDefault && purpose.modelSettingKey ? [purpose.modelSettingKey] : []),
|
|
460
|
+
]);
|
|
461
|
+
const values = [...keys].map((key) => resolveDefinition(requireDefinition(key), context));
|
|
462
|
+
return { version: 1, purpose: purpose.id, context, values, explanations };
|
|
463
|
+
}
|
|
464
|
+
function modelDefault(
|
|
465
|
+
processId: string,
|
|
466
|
+
turnId: string,
|
|
467
|
+
process?: SubjectProcess,
|
|
468
|
+
plan?: ProcessLaunchPlan,
|
|
469
|
+
): string | null {
|
|
470
|
+
const purpose = purposeFor(processId, turnId);
|
|
471
|
+
if (!purpose?.modelSettingKey) return null;
|
|
472
|
+
const context = process
|
|
473
|
+
? forProcess(process).context
|
|
474
|
+
: plan
|
|
475
|
+
? contextFor(plan.processInput, plan.projectInputs, plan.preview).context
|
|
476
|
+
: { instance: instance.id };
|
|
477
|
+
const value = resolveDefinition(requireDefinition(purpose.modelSettingKey), context).value;
|
|
478
|
+
if (value !== null && typeof value !== "string")
|
|
479
|
+
throw new SettingsError(`Purpose '${purpose.id}' must select a model profile or null`);
|
|
480
|
+
return value;
|
|
481
|
+
}
|
|
482
|
+
function definitionView(owner: string, definition: SettingDefinition) {
|
|
483
|
+
const { key, schemaVersion, scopes, merge, form } = definition;
|
|
484
|
+
return { key, owner, schemaVersion, scopes, merge, form };
|
|
485
|
+
}
|
|
486
|
+
async function preview(subjectId: string, draft?: SettingsDraft): Promise<SettingsPreview> {
|
|
487
|
+
const subject = requireSubject(subjectId);
|
|
488
|
+
const context = contextForSubject(subject);
|
|
489
|
+
const fields: SettingFieldView[] = [];
|
|
490
|
+
for (const { owner, definition } of definitions.values()) {
|
|
491
|
+
if (!definition.scopes.includes(subject.scopeType)) continue;
|
|
492
|
+
let effective: ResolvedSetting | null = null;
|
|
493
|
+
let inherited: ResolvedSetting | null = null;
|
|
494
|
+
let error: string | null = null;
|
|
495
|
+
let choices: SettingFieldView["choices"] = [];
|
|
496
|
+
try {
|
|
497
|
+
inherited = resolveDefinition(definition, context, undefined, subject.id);
|
|
498
|
+
effective = resolveDefinition(definition, context, draft);
|
|
499
|
+
if (definition.form.control === "model") choices = input.modelChoices?.() ?? [];
|
|
500
|
+
else if (definition.choiceLabel) {
|
|
501
|
+
const label = definition.choiceLabel;
|
|
502
|
+
choices = choiceValues(effective.value).map((value) => ({ value, label: label(value) }));
|
|
503
|
+
}
|
|
504
|
+
if (definition.form.control === "model" && effective.value !== null) {
|
|
505
|
+
const selected = choices.find((choice) => choice.value === effective?.value);
|
|
506
|
+
if (selected?.disabledReason)
|
|
507
|
+
error = `${selected.label}: ${selected.disabledReason}. Restore availability or choose another model before execution.`;
|
|
508
|
+
}
|
|
509
|
+
} catch (caught) {
|
|
510
|
+
error = caught instanceof Error ? caught.message : "Could not resolve setting";
|
|
511
|
+
}
|
|
512
|
+
fields.push({
|
|
513
|
+
...definitionView(owner, definition),
|
|
514
|
+
choices,
|
|
515
|
+
...(definition.choices && definition.form.control !== "model"
|
|
516
|
+
? { choicesDeferred: true }
|
|
517
|
+
: {}),
|
|
518
|
+
inherited,
|
|
519
|
+
effective,
|
|
520
|
+
override: repos.scopedSettings.getOverride(subject.id, definition.key),
|
|
521
|
+
error,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
subject,
|
|
526
|
+
fields,
|
|
527
|
+
inactive: repos.scopedSettings
|
|
528
|
+
.listOverrides(subjectId)
|
|
529
|
+
.filter((row) => !row.reset && !definitions.has(row.key)),
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
async function choices(subjectId: string, key: string, search = "") {
|
|
533
|
+
const subject = requireSubject(subjectId);
|
|
534
|
+
const definition = requireDefinition(key);
|
|
535
|
+
if (!definition.scopes.includes(subject.scopeType))
|
|
536
|
+
throw new SettingsError(`'${key}' does not apply to ${subject.scopeType}`);
|
|
537
|
+
if (definition.form.control === "model") return input.modelChoices?.() ?? [];
|
|
538
|
+
if (search.trim().length < (definition.form.search?.minimumLength ?? 0)) return [];
|
|
539
|
+
const context = contextForSubject(subject);
|
|
540
|
+
const values = choiceValues(resolveDefinition(definition, context).value);
|
|
541
|
+
return (await definition.choices?.(context, { search, values })) ?? [];
|
|
542
|
+
}
|
|
543
|
+
function validateWrite({
|
|
544
|
+
subjectId,
|
|
545
|
+
key,
|
|
546
|
+
value,
|
|
547
|
+
mode,
|
|
548
|
+
reset,
|
|
549
|
+
}: Omit<SettingsChange, "expectedRevision">) {
|
|
550
|
+
const subject = requireSubject(subjectId);
|
|
551
|
+
const definition = requireDefinition(key);
|
|
552
|
+
if (!definition.scopes.includes(subject.scopeType))
|
|
553
|
+
throw new SettingsError(`'${key}' does not apply to ${subject.scopeType}`);
|
|
554
|
+
mode ??= "replace";
|
|
555
|
+
if (mode !== "replace")
|
|
556
|
+
throw new SettingsError("Settings overrides must replace inherited values");
|
|
557
|
+
return {
|
|
558
|
+
subjectId: subject.id,
|
|
559
|
+
value: reset ? null : parse(definition, value),
|
|
560
|
+
mode,
|
|
561
|
+
schemaVersion: definition.schemaVersion,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function write(change: SettingsChange) {
|
|
565
|
+
if (!Number.isSafeInteger(change.expectedRevision) || change.expectedRevision < 0)
|
|
566
|
+
throw new SettingsError("expectedRevision must be a non-negative integer", 400);
|
|
567
|
+
const updated = repos.scopedSettings.write({ ...change, ...validateWrite(change) });
|
|
568
|
+
if (!updated)
|
|
569
|
+
throw new SettingsError(
|
|
570
|
+
"This setting changed since you opened it. Your draft is preserved. Reload the current revision before saving again.",
|
|
571
|
+
409,
|
|
572
|
+
);
|
|
573
|
+
return updated;
|
|
574
|
+
}
|
|
575
|
+
function listScopes(): SettingsScopesResponse {
|
|
576
|
+
discoverLocal();
|
|
577
|
+
const scopesWithFields = new Set(
|
|
578
|
+
[...definitions.values()].flatMap(({ definition }) => definition.scopes),
|
|
579
|
+
);
|
|
580
|
+
const subjectsWithInactiveOverrides = new Set(
|
|
581
|
+
repos.scopedSettings
|
|
582
|
+
.listOverrides()
|
|
583
|
+
.filter((row) => !row.reset && !definitions.has(row.key))
|
|
584
|
+
.map((row) => row.subjectId),
|
|
585
|
+
);
|
|
586
|
+
const subjects = repos.scopedSettings.listSubjects();
|
|
587
|
+
const byId = new Map(subjects.map((subject) => [subject.id, subject]));
|
|
588
|
+
return {
|
|
589
|
+
scopes: [...scopes.values()],
|
|
590
|
+
subjects: subjects
|
|
591
|
+
.filter((subject) => {
|
|
592
|
+
const providers = discoveries.get(subject.scopeType);
|
|
593
|
+
if (!providers) return true;
|
|
594
|
+
const context = Object.fromEntries(
|
|
595
|
+
Object.entries(subject.context).flatMap(([scope, id]) => {
|
|
596
|
+
const parent = byId.get(id);
|
|
597
|
+
return parent ? [[scope, parent]] : [];
|
|
598
|
+
}),
|
|
599
|
+
);
|
|
600
|
+
return providers.some(
|
|
601
|
+
({ includesSubject: includes }) => !includes || includes(subject, context),
|
|
602
|
+
);
|
|
603
|
+
})
|
|
604
|
+
.map((subject) => ({
|
|
605
|
+
...subject,
|
|
606
|
+
active: scopes.has(subject.scopeType),
|
|
607
|
+
hasSettings:
|
|
608
|
+
scopesWithFields.has(subject.scopeType) ||
|
|
609
|
+
subjectsWithInactiveOverrides.has(subject.id),
|
|
610
|
+
})),
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
function discoverLocal() {
|
|
614
|
+
for (const [key, component] of Object.entries(input.config.components))
|
|
615
|
+
repository({ key, repoLocator: component.repo });
|
|
616
|
+
for (const project of repos.projects.listAll()) {
|
|
617
|
+
try {
|
|
618
|
+
repository(project);
|
|
619
|
+
} catch (error) {
|
|
620
|
+
// Retained conflicts block affected execution, while Settings must stay
|
|
621
|
+
// available so the operator can correct the competing overrides.
|
|
622
|
+
if (!(error instanceof SettingsError) || error.statusCode !== 409) throw error;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
discoverLocal();
|
|
627
|
+
return {
|
|
628
|
+
async overview(scopeType) {
|
|
629
|
+
const subjects = listScopes().subjects.filter(
|
|
630
|
+
(subject) => subject.scopeType === scopeType && subject.hasSettings,
|
|
631
|
+
);
|
|
632
|
+
return {
|
|
633
|
+
items: await Promise.all(
|
|
634
|
+
subjects.map(async (subject) => {
|
|
635
|
+
const result = await preview(subject.id);
|
|
636
|
+
const mappingFields = result.fields.filter(
|
|
637
|
+
(field) => field.form.overview === "repository-mapping",
|
|
638
|
+
);
|
|
639
|
+
return {
|
|
640
|
+
subjectId: subject.id,
|
|
641
|
+
customized: [
|
|
642
|
+
...result.fields
|
|
643
|
+
.filter((field) => field.override && !field.override.reset)
|
|
644
|
+
.map((field) => field.form.label),
|
|
645
|
+
...result.inactive.map((field) => field.key),
|
|
646
|
+
],
|
|
647
|
+
...(mappingFields.length
|
|
648
|
+
? {
|
|
649
|
+
mapping: mappingFields.flatMap((field) =>
|
|
650
|
+
settingSelectionValues(field.effective?.value).map(
|
|
651
|
+
(value) =>
|
|
652
|
+
field.choices.find((choice) => choice.value === value) ?? {
|
|
653
|
+
value,
|
|
654
|
+
label: "Unavailable repository",
|
|
655
|
+
},
|
|
656
|
+
),
|
|
657
|
+
),
|
|
658
|
+
}
|
|
659
|
+
: {}),
|
|
660
|
+
errors: result.fields.flatMap((field) => (field.error ? [field.error] : [])),
|
|
661
|
+
};
|
|
662
|
+
}),
|
|
663
|
+
),
|
|
664
|
+
};
|
|
665
|
+
},
|
|
666
|
+
resolve: (definition, context) =>
|
|
667
|
+
resolveDefinition(requireDefinition(definition.key) as typeof definition, context),
|
|
668
|
+
findSubject: (scopeType, identity) => repos.scopedSettings.findIdentity(scopeType, identity),
|
|
669
|
+
getSubject: (id) => repos.scopedSettings.getSubject(id),
|
|
670
|
+
seedOverride(subjectId, definition, value) {
|
|
671
|
+
const subject = requireSubject(subjectId);
|
|
672
|
+
if (repos.scopedSettings.getOverride(subject.id, definition.key)) return false;
|
|
673
|
+
write({
|
|
674
|
+
subjectId: subject.id,
|
|
675
|
+
key: definition.key,
|
|
676
|
+
value,
|
|
677
|
+
mode: "replace",
|
|
678
|
+
reset: false,
|
|
679
|
+
expectedRevision: 0,
|
|
680
|
+
actor: SYSTEM_ACTOR,
|
|
681
|
+
});
|
|
682
|
+
return true;
|
|
683
|
+
},
|
|
684
|
+
discover,
|
|
685
|
+
registerDiscovery(scopeType, discoverer, options) {
|
|
686
|
+
if (!scopes.has(scopeType)) throw new SettingsError(`Unknown scope '${scopeType}'`);
|
|
687
|
+
discoveries.set(scopeType, [
|
|
688
|
+
...(discoveries.get(scopeType) ?? []),
|
|
689
|
+
{ discover: discoverer, includesSubject: options?.includesSubject },
|
|
690
|
+
]);
|
|
691
|
+
},
|
|
692
|
+
capture,
|
|
693
|
+
modelDefault,
|
|
694
|
+
preview,
|
|
695
|
+
choices,
|
|
696
|
+
write,
|
|
697
|
+
listScopes,
|
|
698
|
+
forProcess,
|
|
699
|
+
forLaunch: (plan) => contextFor(plan.processInput, plan.projectInputs, plan.preview),
|
|
700
|
+
previewProcess(process: SubjectProcess) {
|
|
701
|
+
return [...(catalog.processes.get(process.processId)?.turns.keys() ?? [])]
|
|
702
|
+
.filter((turnId) => purposeFor(process.processId, turnId))
|
|
703
|
+
.map((turnId) => {
|
|
704
|
+
try {
|
|
705
|
+
return { turnId, settings: capture(process, turnId) ?? null, error: null };
|
|
706
|
+
} catch (caught) {
|
|
707
|
+
return {
|
|
708
|
+
turnId,
|
|
709
|
+
settings: null,
|
|
710
|
+
error: caught instanceof Error ? caught.message : "Invalid scoped settings",
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
},
|
|
715
|
+
definitions: () =>
|
|
716
|
+
[...definitions.values()].map(({ owner, definition }) => definitionView(owner, definition)),
|
|
717
|
+
async refresh() {
|
|
718
|
+
discoverLocal();
|
|
719
|
+
for (const [scope, providers] of discoveries)
|
|
720
|
+
for (const { discover: provider } of providers)
|
|
721
|
+
for (const subject of await provider()) {
|
|
722
|
+
if (subject.scopeType !== scope)
|
|
723
|
+
throw new SettingsError("Discovery returned a different scope type");
|
|
724
|
+
discover(subject);
|
|
725
|
+
}
|
|
726
|
+
return listScopes();
|
|
727
|
+
},
|
|
728
|
+
async previewDraft(change) {
|
|
729
|
+
return preview(change.subjectId, {
|
|
730
|
+
...change,
|
|
731
|
+
...validateWrite(change),
|
|
732
|
+
revision: 0,
|
|
733
|
+
});
|
|
734
|
+
},
|
|
735
|
+
};
|
|
736
|
+
}
|