master-orchestrator 0.0.0-stage → 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.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/package.json +56 -4
- package/src/adapters/agy.ts +285 -0
- package/src/adapters/claude.ts +317 -0
- package/src/adapters/codex.ts +162 -0
- package/src/adapters/common.ts +211 -0
- package/src/adapters/dsh.ts +356 -0
- package/src/adapters/fake-agent.mjs +50 -0
- package/src/adapters/fake.ts +88 -0
- package/src/adapters/gemini.ts +239 -0
- package/src/adapters/grok.ts +328 -0
- package/src/adapters/omp.ts +119 -0
- package/src/adapters/opencode.ts +228 -0
- package/src/adapters/pi.ts +284 -0
- package/src/adapters/registry.ts +27 -0
- package/src/adapters/types.ts +201 -0
- package/src/config/load.ts +290 -0
- package/src/config/profiles.ts +64 -0
- package/src/config/schema.ts +158 -0
- package/src/config/write.ts +83 -0
- package/src/engine/artifacts.ts +145 -0
- package/src/engine/baseline.ts +132 -0
- package/src/engine/execute.ts +437 -0
- package/src/engine/git.ts +32 -0
- package/src/engine/hostenv.ts +54 -0
- package/src/engine/registry.ts +280 -0
- package/src/engine/runner.ts +249 -0
- package/src/engine/types.ts +55 -0
- package/src/engine/verify.ts +100 -0
- package/src/index.ts +602 -0
- package/src/prompts/envelope.ts +29 -0
- package/src/prompts/templates.ts +389 -0
- package/src/ui/configure.ts +174 -0
- package/src/ui/dialogs.ts +88 -0
- package/src/ui/doctor.ts +120 -0
- package/src/ui/format.ts +94 -0
- package/src/ui/host.ts +100 -0
- package/src/ui/render.ts +86 -0
- package/src/ui/widget.ts +70 -0
- package/src/workflows/budget.ts +66 -0
- package/src/workflows/common.ts +100 -0
- package/src/workflows/debate.ts +488 -0
- package/src/workflows/host.ts +78 -0
- package/src/workflows/plan-exec.ts +807 -0
- package/src/workflows/plan.ts +407 -0
- package/src/workflows/planfile.ts +380 -0
- package/src/workflows/planstate.ts +229 -0
- package/src/workflows/verdict.ts +74 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { TSchema } from "typebox";
|
|
2
|
+
|
|
3
|
+
export type Mode = "analyze" | "implement";
|
|
4
|
+
|
|
5
|
+
export type RunStatus =
|
|
6
|
+
| "completed" // final answer present, no denial ended the run
|
|
7
|
+
| "denied" // the run ended because a rule or hook of the user's blocked a required action
|
|
8
|
+
| "failed" // agent or model error, or exit 0 without a result
|
|
9
|
+
| "timeout"
|
|
10
|
+
| "cancelled"
|
|
11
|
+
| "auth_required"
|
|
12
|
+
| "quota"
|
|
13
|
+
| "unavailable"; // binary missing, startup failure, unsupported flag
|
|
14
|
+
|
|
15
|
+
/** When several conditions hold, the first status of this list that applies wins. */
|
|
16
|
+
export const STATUS_PRECEDENCE: readonly RunStatus[] = [
|
|
17
|
+
"cancelled",
|
|
18
|
+
"timeout",
|
|
19
|
+
"unavailable",
|
|
20
|
+
"auth_required",
|
|
21
|
+
"quota",
|
|
22
|
+
"denied",
|
|
23
|
+
"failed",
|
|
24
|
+
"completed",
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/** Statuses after which a role's fallback chain moves to the next profile. */
|
|
28
|
+
export const FALLBACK_STATUSES: readonly RunStatus[] = ["unavailable", "auth_required", "quota"];
|
|
29
|
+
|
|
30
|
+
export interface ExecResult {
|
|
31
|
+
code: number | null;
|
|
32
|
+
stdout: string;
|
|
33
|
+
stderr: string;
|
|
34
|
+
/** Set when the process could not be started (for example ENOENT). */
|
|
35
|
+
error?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ExecOptions {
|
|
39
|
+
cwd?: string;
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
env?: Record<string, string | undefined>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What an adapter may use to look at the host. Everything that touches the system goes through here. */
|
|
45
|
+
export interface HostEnv {
|
|
46
|
+
/** Executable to run: the configured `agents.<id>.bin` or the adapter's default binary. */
|
|
47
|
+
bin: string;
|
|
48
|
+
cwd: string;
|
|
49
|
+
env: Record<string, string | undefined>;
|
|
50
|
+
exec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;
|
|
51
|
+
/** Absolute path of an executable found on PATH, or undefined. */
|
|
52
|
+
which(command: string): Promise<string | undefined>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Detection {
|
|
56
|
+
available: boolean;
|
|
57
|
+
path?: string;
|
|
58
|
+
version?: string;
|
|
59
|
+
/** Whether `version` satisfies the adapter's `testedVersions`; undefined when the version is unknown. */
|
|
60
|
+
inTestedRange?: boolean;
|
|
61
|
+
error?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AuthStatus {
|
|
65
|
+
state: "ok" | "missing" | "unknown";
|
|
66
|
+
detail?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface RunRequest {
|
|
70
|
+
workspace: string; // absolute
|
|
71
|
+
prompt: string; // final text, envelope already applied
|
|
72
|
+
runDir: string; // artifact directory
|
|
73
|
+
projectTrusted: boolean;
|
|
74
|
+
model?: string;
|
|
75
|
+
effort?: string;
|
|
76
|
+
sessionId?: string; // present on follow-ups
|
|
77
|
+
outputSchema?: object;
|
|
78
|
+
options?: Record<string, unknown>; // profile `options`, already validated against optionsSchema
|
|
79
|
+
extraArgs?: string[];
|
|
80
|
+
env?: Record<string, string>;
|
|
81
|
+
/** Adapter-specific configuration that is not a profile option (for example `agents.grok.persistTrust`). */
|
|
82
|
+
agentConfig?: Record<string, unknown>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A file the engine writes before the child starts. Paths are absolute and inside the run directory. */
|
|
86
|
+
export interface InvocationFile {
|
|
87
|
+
path: string;
|
|
88
|
+
content: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface Invocation {
|
|
92
|
+
/** Arguments after the executable. Never a shell string. */
|
|
93
|
+
args: string[];
|
|
94
|
+
cwd: string;
|
|
95
|
+
/** Environment additions. `null` removes a variable inherited from the parent. */
|
|
96
|
+
env?: Record<string, string | null>;
|
|
97
|
+
/** Payload written to stdin. Undefined means stdin is /dev/null. */
|
|
98
|
+
stdin?: string;
|
|
99
|
+
files?: InvocationFile[];
|
|
100
|
+
/** Files the engine reads after the run and passes to `finalize`, by name. */
|
|
101
|
+
outputFiles?: Record<string, string>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type NormalizedEvent =
|
|
105
|
+
| { type: "session"; sessionId: string }
|
|
106
|
+
| { type: "activity"; kind: "tool" | "text" | "thinking"; summary: string }
|
|
107
|
+
| { type: "denied"; action: string }
|
|
108
|
+
| { type: "usage"; inputTokens?: number; outputTokens?: number; costUsd?: number }
|
|
109
|
+
| { type: "warning"; message: string };
|
|
110
|
+
|
|
111
|
+
export interface Usage {
|
|
112
|
+
inputTokens?: number;
|
|
113
|
+
outputTokens?: number;
|
|
114
|
+
costUsd?: number;
|
|
115
|
+
turns?: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Mutable state shared by `parseEvent` calls of one run and read by `finalize`. */
|
|
119
|
+
export interface ParserState {
|
|
120
|
+
sessionId?: string;
|
|
121
|
+
/** Assistant text in stream order; the last entry is the usual final answer. */
|
|
122
|
+
texts: string[];
|
|
123
|
+
/** Authoritative final answer when the stream carries one (for example a `result` event). */
|
|
124
|
+
finalText?: string;
|
|
125
|
+
denied: string[];
|
|
126
|
+
/** Error messages from the event stream. */
|
|
127
|
+
errors: string[];
|
|
128
|
+
warnings: string[];
|
|
129
|
+
usage: Usage;
|
|
130
|
+
/** Set by an explicit end-of-run event of the stream. */
|
|
131
|
+
terminal?: { ok: boolean; reason?: string };
|
|
132
|
+
/** Number of lines that were not valid JSON. */
|
|
133
|
+
nonJsonLines: number;
|
|
134
|
+
/** Adapter-specific data. */
|
|
135
|
+
extra: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface RawRunResult {
|
|
139
|
+
exitCode: number | null;
|
|
140
|
+
signal: string | null;
|
|
141
|
+
/** Tail of stderr (the full log is in the run directory). */
|
|
142
|
+
stderr: string;
|
|
143
|
+
/** Contents of `Invocation.outputFiles`, undefined for files that do not exist. */
|
|
144
|
+
outputFiles: Record<string, string | undefined>;
|
|
145
|
+
timedOut: boolean;
|
|
146
|
+
cancelled: boolean;
|
|
147
|
+
/** Set when the process could not be started. */
|
|
148
|
+
spawnError?: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface AdapterOutcome {
|
|
152
|
+
status: RunStatus;
|
|
153
|
+
reason?: string; // short machine-readable code plus message, e.g. "empty_result: exit 0 without an answer"
|
|
154
|
+
finalText?: string;
|
|
155
|
+
structuredOutput?: unknown;
|
|
156
|
+
sessionId?: string;
|
|
157
|
+
deniedActions: string[];
|
|
158
|
+
usage?: Usage;
|
|
159
|
+
/** Conditions the user should know about, added to the run's warnings. */
|
|
160
|
+
warnings?: string[];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface AdapterCapabilities {
|
|
164
|
+
resume: boolean;
|
|
165
|
+
structuredOutput: boolean; // native JSON schema support
|
|
166
|
+
listModels: boolean;
|
|
167
|
+
effortLevels?: readonly string[];
|
|
168
|
+
reportsCost: boolean;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface AgentAdapter {
|
|
172
|
+
readonly id: string; // "codex", "claude", ...
|
|
173
|
+
readonly displayName: string;
|
|
174
|
+
readonly binary: string; // default executable name
|
|
175
|
+
readonly testedVersions: string; // semver range the adapter was verified against
|
|
176
|
+
readonly stability: "stable" | "experimental";
|
|
177
|
+
/** What real runs have confirmed so far (version, date, behaviors), shown by `doctor` while the adapter is experimental. */
|
|
178
|
+
readonly liveChecked?: string;
|
|
179
|
+
readonly stopSignal: "SIGINT" | "SIGTERM"; // first signal on cancellation
|
|
180
|
+
readonly optionsSchema?: TSchema; // adapter-specific profile options
|
|
181
|
+
readonly capabilities: AdapterCapabilities;
|
|
182
|
+
/** Per-run trust flag passed when the pi session trusts the project, for `doctor`. */
|
|
183
|
+
readonly trustFlag?: string;
|
|
184
|
+
/**
|
|
185
|
+
* A headless run that waits on an interactive prompt (a login URL or device code) never exits on its own. Called
|
|
186
|
+
* with the stderr received so far; a returned status makes the engine stop the run and report it.
|
|
187
|
+
*/
|
|
188
|
+
watchStderr?(stderr: string): { status: "auth_required" | "quota" | "unavailable"; reason: string } | undefined;
|
|
189
|
+
|
|
190
|
+
detect(env: HostEnv): Promise<Detection>;
|
|
191
|
+
checkAuth?(env: HostEnv): Promise<AuthStatus>;
|
|
192
|
+
listModels?(env: HostEnv): Promise<string[]>;
|
|
193
|
+
|
|
194
|
+
buildInvocation(req: RunRequest): Invocation;
|
|
195
|
+
parseEvent(line: string, state: ParserState): NormalizedEvent[];
|
|
196
|
+
finalize(raw: RawRunResult, state: ParserState): AdapterOutcome;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function createParserState(): ParserState {
|
|
200
|
+
return { texts: [], denied: [], errors: [], warnings: [], usage: {}, nonJsonLines: 0, extra: {} };
|
|
201
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { Value } from "typebox/value";
|
|
4
|
+
import type { AgentAdapter } from "../adapters/types.ts";
|
|
5
|
+
import {
|
|
6
|
+
ConfigFileSchema,
|
|
7
|
+
defaultConfig,
|
|
8
|
+
ROLE_NAMES,
|
|
9
|
+
type ConfigFile,
|
|
10
|
+
type OrchestratorConfig,
|
|
11
|
+
} from "./schema.ts";
|
|
12
|
+
|
|
13
|
+
export const CONFIG_FILE_NAME = "master-orchestrator.json";
|
|
14
|
+
|
|
15
|
+
export interface ConfigError {
|
|
16
|
+
file: string;
|
|
17
|
+
pointer: string;
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ConfigSource {
|
|
22
|
+
scope: "user" | "project";
|
|
23
|
+
path: string;
|
|
24
|
+
present: boolean;
|
|
25
|
+
/** Project files of untrusted projects are not read. */
|
|
26
|
+
ignored?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface LoadedConfig {
|
|
30
|
+
config: OrchestratorConfig;
|
|
31
|
+
errors: ConfigError[];
|
|
32
|
+
warnings: string[];
|
|
33
|
+
sources: ConfigSource[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface LoadOptions {
|
|
37
|
+
agentDir: string;
|
|
38
|
+
cwd: string;
|
|
39
|
+
configDirName: string;
|
|
40
|
+
projectTrusted: boolean;
|
|
41
|
+
adapters: ReadonlyMap<string, AgentAdapter>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function userConfigPath(agentDir: string): string {
|
|
45
|
+
return join(agentDir, CONFIG_FILE_NAME);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function projectConfigPath(cwd: string, configDirName: string): string {
|
|
49
|
+
return join(cwd, configDirName, CONFIG_FILE_NAME);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Removes `//` and `/* *\/` comments and trailing commas outside strings, so commented examples load. */
|
|
53
|
+
export function stripJsonComments(text: string): string {
|
|
54
|
+
let out = "";
|
|
55
|
+
let inString = false;
|
|
56
|
+
for (let i = 0; i < text.length; i++) {
|
|
57
|
+
const char = text[i];
|
|
58
|
+
if (inString) {
|
|
59
|
+
out += char;
|
|
60
|
+
if (char === "\\") {
|
|
61
|
+
out += text[i + 1] ?? "";
|
|
62
|
+
i++;
|
|
63
|
+
} else if (char === '"') inString = false;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (char === '"') {
|
|
67
|
+
inString = true;
|
|
68
|
+
out += char;
|
|
69
|
+
} else if (char === "/" && text[i + 1] === "/") {
|
|
70
|
+
while (i < text.length && text[i] !== "\n") i++;
|
|
71
|
+
out += "\n";
|
|
72
|
+
} else if (char === "/" && text[i + 1] === "*") {
|
|
73
|
+
i += 2;
|
|
74
|
+
while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++;
|
|
75
|
+
i++;
|
|
76
|
+
} else {
|
|
77
|
+
out += char;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out.replace(/,(\s*[}\]])/g, "$1");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function readConfigFile(path: string): Promise<{ present: boolean; value?: unknown; error?: string }> {
|
|
84
|
+
let text: string;
|
|
85
|
+
try {
|
|
86
|
+
text = await readFile(path, "utf8");
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return { present: false };
|
|
89
|
+
return { present: true, error: `cannot read: ${(error as Error).message}` };
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
return { present: true, value: JSON.parse(stripJsonComments(text)) };
|
|
93
|
+
} catch (error) {
|
|
94
|
+
return { present: true, error: `invalid JSON: ${(error as Error).message}` };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Schema errors, one per JSON pointer. */
|
|
99
|
+
export function validateConfigFile(value: unknown, file: string): ConfigError[] {
|
|
100
|
+
if (Value.Check(ConfigFileSchema, value)) return [];
|
|
101
|
+
const byPointer = new Map<string, string[]>();
|
|
102
|
+
for (const error of Value.Errors(ConfigFileSchema, value)) {
|
|
103
|
+
if (error.keyword === "boolean") continue; // duplicate of additionalProperties
|
|
104
|
+
let message = error.message;
|
|
105
|
+
if (error.keyword === "additionalProperties") {
|
|
106
|
+
const extra = (error.params as { additionalProperties?: string[] }).additionalProperties ?? [];
|
|
107
|
+
message = `unknown key${extra.length > 1 ? "s" : ""}: ${extra.join(", ")}`;
|
|
108
|
+
}
|
|
109
|
+
if (error.keyword === "const") {
|
|
110
|
+
const allowed = (error.params as { allowedValue?: unknown }).allowedValue;
|
|
111
|
+
message = `must be one of the allowed values (for example ${JSON.stringify(allowed)})`;
|
|
112
|
+
}
|
|
113
|
+
const pointer = error.instancePath || "/";
|
|
114
|
+
const list = byPointer.get(pointer) ?? [];
|
|
115
|
+
if (!list.includes(message)) list.push(message);
|
|
116
|
+
byPointer.set(pointer, list);
|
|
117
|
+
}
|
|
118
|
+
return [...byPointer].map(([pointer, messages]) => ({ file, pointer, message: messages.join("; ") }));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type Json = Record<string, unknown>;
|
|
122
|
+
|
|
123
|
+
function isPlainObject(value: unknown): value is Json {
|
|
124
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Objects merge key by key; arrays and scalars replace. */
|
|
128
|
+
export function mergeConfig<T>(base: T, override: unknown): T {
|
|
129
|
+
if (!isPlainObject(base) || !isPlainObject(override)) return (override === undefined ? base : override) as T;
|
|
130
|
+
const result: Json = { ...base };
|
|
131
|
+
for (const [key, value] of Object.entries(override)) {
|
|
132
|
+
if (value === undefined) continue;
|
|
133
|
+
result[key] = isPlainObject(result[key]) && isPlainObject(value) ? mergeConfig(result[key], value) : value;
|
|
134
|
+
}
|
|
135
|
+
return result as T;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const STRICTNESS: Record<string, string[]> = {
|
|
139
|
+
implementFromTool: ["auto", "ask"],
|
|
140
|
+
workflowStart: ["auto", "ask"],
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A project must not choose executables, flags, environment or overlay files, and must not loosen approval.
|
|
145
|
+
*/
|
|
146
|
+
export function checkProjectRestrictions(project: ConfigFile, userEffective: OrchestratorConfig, file: string): ConfigError[] {
|
|
147
|
+
const errors: ConfigError[] = [];
|
|
148
|
+
for (const [agent, settings] of Object.entries(project.agents ?? {})) {
|
|
149
|
+
for (const key of ["bin", "env", "extraArgs"] as const) {
|
|
150
|
+
if (settings[key] !== undefined) {
|
|
151
|
+
errors.push({ file, pointer: `/agents/${agent}/${key}`, message: "not allowed in project configuration (user configuration only)" });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const [name, profile] of Object.entries(project.profiles ?? {})) {
|
|
156
|
+
if (profile.options !== undefined) {
|
|
157
|
+
errors.push({ file, pointer: `/profiles/${name}/options`, message: "not allowed in project configuration (user configuration only)" });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const [key, levels] of Object.entries(STRICTNESS)) {
|
|
161
|
+
const projectValue = (project.approval as Record<string, string> | undefined)?.[key];
|
|
162
|
+
if (projectValue === undefined) continue;
|
|
163
|
+
const userValue = (userEffective.approval as Record<string, string>)[key];
|
|
164
|
+
if (levels.indexOf(projectValue) < levels.indexOf(userValue)) {
|
|
165
|
+
errors.push({ file, pointer: `/approval/${key}`, message: `cannot loosen the user setting "${userValue}" to "${projectValue}"` });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return errors;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Checks references between sections and against the adapters. */
|
|
172
|
+
export function crossValidate(config: OrchestratorConfig, adapters: ReadonlyMap<string, AgentAdapter>, file: string): ConfigError[] {
|
|
173
|
+
const errors: ConfigError[] = [];
|
|
174
|
+
for (const [agent, settings] of Object.entries(config.agents)) {
|
|
175
|
+
if (!adapters.has(agent)) {
|
|
176
|
+
errors.push({ file, pointer: `/agents/${agent}`, message: `unknown agent; known: ${[...adapters.keys()].join(", ")}` });
|
|
177
|
+
}
|
|
178
|
+
if (settings.persistTrust !== undefined && agent !== "grok") {
|
|
179
|
+
errors.push({ file, pointer: `/agents/${agent}/persistTrust`, message: "only valid for grok" });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const [name, profile] of Object.entries(config.profiles)) {
|
|
183
|
+
const adapter = adapters.get(profile.agent);
|
|
184
|
+
if (!adapter) {
|
|
185
|
+
errors.push({ file, pointer: `/profiles/${name}/agent`, message: `unknown agent "${profile.agent}"` });
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const levels = adapter.capabilities.effortLevels;
|
|
189
|
+
if (profile.effort !== undefined) {
|
|
190
|
+
if (!levels) errors.push({ file, pointer: `/profiles/${name}/effort`, message: `${adapter.displayName} has no effort setting` });
|
|
191
|
+
else if (!levels.includes(profile.effort)) {
|
|
192
|
+
errors.push({ file, pointer: `/profiles/${name}/effort`, message: `must be one of: ${levels.join(", ")}` });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (profile.options !== undefined) {
|
|
196
|
+
if (!adapter.optionsSchema) {
|
|
197
|
+
errors.push({ file, pointer: `/profiles/${name}/options`, message: `${adapter.displayName} takes no options` });
|
|
198
|
+
} else if (!Value.Check(adapter.optionsSchema, profile.options)) {
|
|
199
|
+
const messages = [...Value.Errors(adapter.optionsSchema, profile.options)]
|
|
200
|
+
.filter((error) => error.keyword !== "boolean")
|
|
201
|
+
.map((error) => {
|
|
202
|
+
if (error.keyword === "additionalProperties") {
|
|
203
|
+
return `unknown option: ${((error.params as { additionalProperties?: string[] }).additionalProperties ?? []).join(", ")}`;
|
|
204
|
+
}
|
|
205
|
+
return `${error.instancePath || "/"} ${error.message}`;
|
|
206
|
+
});
|
|
207
|
+
errors.push({ file, pointer: `/profiles/${name}/options`, message: [...new Set(messages)].join("; ") });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
for (const role of ROLE_NAMES) {
|
|
212
|
+
for (const entry of config.roles[role] ?? []) {
|
|
213
|
+
if (!config.profiles[entry] && !adapters.has(entry)) {
|
|
214
|
+
errors.push({ file, pointer: `/roles/${role}`, message: `unknown profile or agent "${entry}"` });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (config.planning.execution.commit === "per-block" && !config.planning.execution.requireCleanTree) {
|
|
219
|
+
errors.push({
|
|
220
|
+
file,
|
|
221
|
+
pointer: "/planning/execution/commit",
|
|
222
|
+
message: '"per-block" requires requireCleanTree: true, so nothing that predates the plan can enter a commit',
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return errors;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function resolvePromptsDir(fileValue: ConfigFile, file: string): string | null | undefined {
|
|
229
|
+
const dir = fileValue.prompts?.dir;
|
|
230
|
+
if (dir === undefined || dir === null) return dir;
|
|
231
|
+
return isAbsolute(dir) ? dir : resolve(dirname(file), dir);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Built-in defaults, then the user file, then the project file when the project is trusted. */
|
|
235
|
+
export async function loadConfig(options: LoadOptions): Promise<LoadedConfig> {
|
|
236
|
+
const errors: ConfigError[] = [];
|
|
237
|
+
const warnings: string[] = [];
|
|
238
|
+
const sources: ConfigSource[] = [];
|
|
239
|
+
let config = defaultConfig();
|
|
240
|
+
|
|
241
|
+
const userPath = userConfigPath(options.agentDir);
|
|
242
|
+
const user = await readConfigFile(userPath);
|
|
243
|
+
sources.push({ scope: "user", path: userPath, present: user.present });
|
|
244
|
+
if (user.error) errors.push({ file: userPath, pointer: "/", message: user.error });
|
|
245
|
+
else if (user.present) {
|
|
246
|
+
const schemaErrors = validateConfigFile(user.value, userPath);
|
|
247
|
+
errors.push(...schemaErrors);
|
|
248
|
+
if (schemaErrors.length === 0) {
|
|
249
|
+
const value = user.value as ConfigFile;
|
|
250
|
+
const { $schema: _ignored, ...rest } = value;
|
|
251
|
+
config = mergeConfig(config, rest);
|
|
252
|
+
const dir = resolvePromptsDir(value, userPath);
|
|
253
|
+
if (dir !== undefined) config.prompts.dir = dir;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const projectPath = projectConfigPath(options.cwd, options.configDirName);
|
|
258
|
+
if (!options.projectTrusted) {
|
|
259
|
+
sources.push({ scope: "project", path: projectPath, present: false, ignored: "project not trusted" });
|
|
260
|
+
} else {
|
|
261
|
+
const project = await readConfigFile(projectPath);
|
|
262
|
+
sources.push({ scope: "project", path: projectPath, present: project.present });
|
|
263
|
+
if (project.error) errors.push({ file: projectPath, pointer: "/", message: project.error });
|
|
264
|
+
else if (project.present) {
|
|
265
|
+
const schemaErrors = validateConfigFile(project.value, projectPath);
|
|
266
|
+
errors.push(...schemaErrors);
|
|
267
|
+
if (schemaErrors.length === 0) {
|
|
268
|
+
const value = project.value as ConfigFile;
|
|
269
|
+
const restrictionErrors = checkProjectRestrictions(value, config, projectPath);
|
|
270
|
+
errors.push(...restrictionErrors);
|
|
271
|
+
if (restrictionErrors.length === 0) {
|
|
272
|
+
const { $schema: _ignored, ...rest } = value;
|
|
273
|
+
config = mergeConfig(config, rest);
|
|
274
|
+
const dir = resolvePromptsDir(value, projectPath);
|
|
275
|
+
if (dir !== undefined) config.prompts.dir = dir;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (errors.length === 0) {
|
|
282
|
+
const file = sources.filter((source) => source.present).map((source) => source.path).join(" + ") || "built-in defaults";
|
|
283
|
+
errors.push(...crossValidate(config, options.adapters, file));
|
|
284
|
+
}
|
|
285
|
+
return { config, errors, warnings, sources };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function formatConfigErrors(errors: readonly ConfigError[]): string {
|
|
289
|
+
return errors.map((error) => `${error.file}: ${error.pointer}: ${error.message}`).join("\n");
|
|
290
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { AgentAdapter } from "../adapters/types.ts";
|
|
2
|
+
import { MULTI_ROLES, type OrchestratorConfig, type RoleName } from "./schema.ts";
|
|
3
|
+
|
|
4
|
+
export interface ResolvedProfile {
|
|
5
|
+
name: string;
|
|
6
|
+
agent: string;
|
|
7
|
+
model?: string;
|
|
8
|
+
effort?: string;
|
|
9
|
+
timeoutMinutes?: number;
|
|
10
|
+
options?: Record<string, unknown>;
|
|
11
|
+
/** True for the profile every agent gets, named after it, with no model or effort. */
|
|
12
|
+
implicit: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A configured profile, or the implicit profile of an agent id. */
|
|
16
|
+
export function resolveProfile(
|
|
17
|
+
config: OrchestratorConfig,
|
|
18
|
+
adapters: ReadonlyMap<string, AgentAdapter>,
|
|
19
|
+
name: string,
|
|
20
|
+
): ResolvedProfile | undefined {
|
|
21
|
+
const profile = config.profiles[name];
|
|
22
|
+
if (profile) {
|
|
23
|
+
return {
|
|
24
|
+
name,
|
|
25
|
+
agent: profile.agent,
|
|
26
|
+
model: profile.model,
|
|
27
|
+
effort: profile.effort,
|
|
28
|
+
timeoutMinutes: profile.timeoutMinutes,
|
|
29
|
+
options: profile.options,
|
|
30
|
+
implicit: false,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (adapters.has(name)) return { name, agent: name, implicit: true };
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** All selectable profiles: configured ones first, then one implicit profile per agent. */
|
|
38
|
+
export function listProfiles(config: OrchestratorConfig, adapters: ReadonlyMap<string, AgentAdapter>): ResolvedProfile[] {
|
|
39
|
+
const result: ResolvedProfile[] = [];
|
|
40
|
+
for (const name of Object.keys(config.profiles)) {
|
|
41
|
+
const profile = resolveProfile(config, adapters, name);
|
|
42
|
+
if (profile) result.push(profile);
|
|
43
|
+
}
|
|
44
|
+
for (const id of adapters.keys()) {
|
|
45
|
+
if (!config.profiles[id]) result.push({ name: id, agent: id, implicit: true });
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function roleProfiles(config: OrchestratorConfig, role: RoleName): string[] | undefined {
|
|
51
|
+
const list = config.roles[role];
|
|
52
|
+
return list && list.length > 0 ? [...list] : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isMultiRole(role: RoleName): boolean {
|
|
56
|
+
return MULTI_ROLES.includes(role);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function describeProfile(profile: ResolvedProfile): string {
|
|
60
|
+
const parts = [profile.agent];
|
|
61
|
+
if (profile.model) parts.push(profile.model);
|
|
62
|
+
if (profile.effort) parts.push(`effort ${profile.effort}`);
|
|
63
|
+
return profile.implicit ? `${profile.name} (agent defaults)` : `${profile.name} (${parts.join(", ")})`;
|
|
64
|
+
}
|