/**
 * checkout.ts — detect whether `olam ask` is running inside an olam checkout.
 *
 * When inside a checkout, the SDK is granted read-only filesystem tools (cwd =
 * repo root) so it can pull live repo files for fresher answers. Outside a
 * checkout (npx install), the bundled knowledge pack is the only grounding.
 *
 * Detection walks up from the start dir looking for an olam-shaped repo root:
 * a directory containing BOTH `packages/host-cp` and `docs/architecture`.
 * Both markers together avoid false positives on unrelated monorepos that
 * merely have a `packages/` dir.
 */
import { existsSync } from 'node:fs';
import { dirname, join, parse } from 'node:path';
/** Returns true when `dir` looks like an olam repo root. */
function isOlamRoot(dir) {
    return (existsSync(join(dir, 'packages', 'host-cp')) &&
        existsSync(join(dir, 'docs', 'architecture')));
}
/**
 * Walk up from `startDir` (default cwd) to the filesystem root, returning the
 * first olam repo root found, or `null` when none. Pure relative to the
 * injected `startDir`; uses real `existsSync` for the marker checks.
 */
export function findOlamCheckoutRoot(startDir = process.cwd()) {
    let dir = startDir;
    const { root } = parse(dir);
    // Bound the walk by the filesystem root to avoid an infinite loop.
    for (;;) {
        if (isOlamRoot(dir))
            return dir;
        if (dir === root)
            return null;
        const parent = dirname(dir);
        if (parent === dir)
            return null;
        dir = parent;
    }
}
//# sourceMappingURL=checkout.js.map