/**
 * Project-local `run:` config discovery.
 *
 * The user/system `agents.yaml` is read through state.ts. Project-local
 * agents.yaml files are discovered from the current working directory upward.
 */
import * as fs from 'fs';
import * as path from 'path';
import * as yaml from 'yaml';
import { getUserAgentsDir } from './state.js';
function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
/** Return project-local run configs from nearest directory upward. */
export function getProjectRunConfigs(startPath = process.cwd()) {
    const configs = [];
    let dir = path.resolve(startPath);
    const userAgentsYaml = path.join(getUserAgentsDir(), 'agents.yaml');
    while (dir !== path.dirname(dir)) {
        const manifestPath = path.join(dir, 'agents.yaml');
        if (manifestPath !== userAgentsYaml && fs.existsSync(manifestPath)) {
            try {
                const parsed = yaml.parse(fs.readFileSync(manifestPath, 'utf-8'));
                if (isRecord(parsed?.run)) {
                    configs.push(parsed.run);
                }
            }
            catch {
                // Ignore malformed project config and keep walking.
            }
        }
        dir = path.dirname(dir);
    }
    return configs;
}
