import type { PackageSource } from "../core/settings-manager.ts";
import { type OmoLocalSpawnWorker } from "./omo-local-update-worker.ts";
/** Result of one spawned command, with output captured and timeout enforced. */
export interface OmoLocalRunResult {
    code: number | null;
    stdout: string;
    stderr: string;
    timedOut: boolean;
}
export interface OmoLocalRunOptions {
    cwd?: string;
    timeoutMs?: number;
    /** Merged over process.env (callers inject the isolated git env from the fixture contract). */
    env?: Record<string, string | undefined>;
}
/** exported for tests only */
export type OmoLocalRun = (command: string, args: string[], options: OmoLocalRunOptions) => Promise<OmoLocalRunResult>;
/**
 * exported for tests only
 *
 * Shared process seam for the whole module. spawnProcess/waitForChildProcess neither capture
 * output nor enforce timeouts; this adds both. The child is spawned detached on POSIX so it
 * owns a process group, and the timeout kill goes through killProcessTree (SIGKILL to the
 * whole group) - without that, bun/git descendants would keep mutating the build worktree
 * after the direct child died.
 */
export declare function defaultRun(command: string, args: string[], options: OmoLocalRunOptions): Promise<OmoLocalRunResult>;
/** exported for tests only */
export declare function isKillSwitched(env: Record<string, string | undefined>): boolean;
/** A detected locally-installed OMO plugin and its enclosing omo monorepo checkout. */
export interface OmoLocalInstall {
    pluginPath: string;
    repoRoot: string;
}
/** exported for tests only */
export interface DetectOmoLocalInstallOptions {
    packages: PackageSource[] | undefined;
    agentDir: string;
    run: OmoLocalRun;
    readJson?: (path: string) => unknown;
    exists?: (path: string) => boolean;
}
/**
 * exported for tests only
 *
 * Three-part detection gate (ALL must hold, else silent no-op):
 * 1. A global settings `packages` entry (string or object form) is a local path whose resolved
 *    dir's package.json name is `@code-yeongyu/omo-senpi`.
 * 2. The derived repo root (pluginPath/../../..) contains both workspace packages
 *    `@oh-my-opencode/omo-senpi` and `@oh-my-opencode/senpi-task`.
 * 3. `git -C <repoRoot> rev-parse --show-toplevel` succeeds AND its canonicalized output equals
 *    the canonicalized repo root - a mismatched toplevel would mean the derived root is not
 *    the checkout it claims to be.
 */
export declare function detectOmoLocalInstall(options: DetectOmoLocalInstallOptions): Promise<OmoLocalInstall | undefined>;
/** State stamp written after a successful install: `<agentDir>/omo-local-update-state.json`. */
export interface OmoLocalUpdateStamp {
    repoRoot: string;
    /** Frozen origin/dev sha the installed plugin was built from. */
    sha: string;
    /** `git rev-parse origin/dev:packages/omo-senpi` at install time. */
    omoSenpiTree: string;
    /** `git rev-parse origin/dev:packages/senpi-task` at install time. */
    senpiTaskTree: string;
    /** Build-input fingerprint of origin/dev at install time (see omo-local-update-fingerprint.ts). */
    buildInputsHash: string;
    installedAt: string;
    /** Post-install inventory: relative paths of every installed artifact present at stamp time. */
    artifacts: string[];
}
/** exported for tests only */
export declare function omoLocalUpdateStampPath(agentDir: string): string;
/** exported for tests only */
export declare function readStamp(agentDir: string): OmoLocalUpdateStamp | undefined;
/** exported for tests only */
export declare function writeStamp(agentDir: string, stamp: OmoLocalUpdateStamp): void;
/** exported for tests only */
export interface ShouldSkipUpdateOptions {
    stamp: OmoLocalUpdateStamp | undefined;
    repoRoot: string;
    remoteSha: string;
    remoteBuildInputsHash: string;
    /** True only when every path recorded in stamp.artifacts still exists on disk. */
    stampArtifactsExist: boolean;
    force: boolean;
}
/**
 * exported for tests only
 *
 * Skip decision (taken BEFORE any worktree op, install, or write): skip only when the stamp
 * belongs to this repo root, matches the frozen remote by sha OR by build-input fingerprint
 * (a moved sha whose build inputs are untouched cannot change the built plugin), recorded a
 * non-empty artifact inventory, and every inventoried path still exists. A repoRoot mismatch
 * always updates; an empty/absent inventory never skips; force always updates.
 */
export declare function shouldSkipUpdate(options: ShouldSkipUpdateOptions): boolean;
/**
 * exported for tests only
 *
 * Failure of one orchestrator-owned step (fetch/worktree/install/build/artifacts/swap/stamp).
 * The stage feeds `OMO local plugin update failed (<stage>): ...`; `output` (combined
 * stdout+stderr of the failed step) is echoed dim, last 40 lines.
 */
export declare class OmoLocalStepError extends Error {
    readonly stage: string;
    readonly output: string | undefined;
    constructor(stage: string, message: string, output?: string);
}
/** The frozen state of origin/dev: commit sha, subject, and both package tree hashes. */
export interface OmoRemoteState {
    sha: string;
    subject: string;
    omoSenpiTree: string;
    senpiTaskTree: string;
    buildInputsHash: string;
}
/** exported for tests only */
export interface ComputeRemoteStateOptions {
    repoRoot: string;
    run: OmoLocalRun;
}
/**
 * exported for tests only
 *
 * Inspect the two packages' state ON ORIGIN/DEV: ONE `git fetch origin dev` (120s, read-only
 * - the ONLY network/ref operation in the module), then rev-parse/log reads against the
 * frozen `origin/dev` ref. A fetch failure throws an OmoLocalStepError with stage "fetch"
 * before anything else is touched.
 */
export declare function computeRemoteState(options: ComputeRemoteStateOptions): Promise<OmoRemoteState>;
/** exported for tests only */
export declare function omoLocalUpdateLockPath(agentDir: string): string;
/** exported for tests only */
export declare function omoLocalUpdateBuildWorktreePath(agentDir: string): string;
/** exported for tests only */
export interface EnsureBuildWorktreeOptions {
    /** Feature-owned persistent worktree path (inside agentDir). */
    worktree: string;
    repoRoot: string;
    /** Frozen origin/dev sha to check out detached. */
    sha: string;
    run: OmoLocalRun;
}
/**
 * exported for tests only
 *
 * Validate/reuse the FEATURE-OWNED persistent build worktree:
 * - present AND owned by this repo (`git -C <wt> rev-parse --git-common-dir` realpath-equals
 *   realpath(repoRoot/.git)) -> reuse: `git -C <wt> checkout --detach --force <sha>`. The
 *   --force is SAFE here: this worktree is exclusively feature-owned, so it only discards
 *   our own previous build outputs, and untracked node_modules survives for incremental
 *   installs.
 * - present but foreign/invalid -> `git -C repoRoot worktree remove --force <wt>` (fs rm
 *   fallback), then re-add.
 * - absent -> `git -C repoRoot worktree add --detach <wt> <sha>`.
 * These are the ONLY git mutations in the module besides `git fetch`, and they touch only
 * the feature-owned worktree registration - never the user's checkout state.
 */
export declare function ensureBuildWorktree(options: EnsureBuildWorktreeOptions): Promise<void>;
/** exported for tests only: injectable fs seam for the atomic swap. */
export interface OmoLocalFsSeam {
    cpSync: (source: string, destination: string) => void;
    renameSync: (oldPath: string, newPath: string) => void;
    rmSync: (path: string) => void;
}
/** exported for tests only */
export interface SwapPluginDirOptions {
    pluginPath: string;
    /** The freshly built plugin dir inside the build worktree. */
    sourceDir: string;
    fs?: OmoLocalFsSeam;
}
/**
 * exported for tests only
 *
 * ATOMIC SWAP of the install target. staging/prev are siblings of pluginPath (same parent
 * => same filesystem => atomic renames): copy the built tree to staging, move pluginPath
 * aside to prev, move staging into place. If the staging rename fails, prev is restored
 * and staging removed before the error propagates - the local install is never left
 * half-swapped. On success prev is removed.
 */
export declare function swapPluginDir(options: SwapPluginDirOptions): void;
export interface RunOmoLocalUpdateBetaOptions {
    env: Record<string, string | undefined>;
    agentDir: string;
    settings?: {
        packages?: PackageSource[];
    };
    force?: boolean;
    log?: (message: string) => void;
    run?: OmoLocalRun;
    /** "build" (default) runs the full update inline; "dispatch" hands a needed rebuild to a detached worker. */
    mode?: "build" | "dispatch";
    /** exported-for-tests seam over the detached worker spawn used by "dispatch". */
    spawnWorker?: OmoLocalSpawnWorker;
}
/**
 * Beta hook entry point, called from bare `senpi update` before the self-update.
 *
 * SINGLE owner of the ORDERED state machine: gate chain (kill-switch -> detection) ->
 * atomic lock acquisition (held through fetch, worktree, build, swap, stamp AND notify;
 * owner-checked unlink in `finally`) -> computeRemoteState (ONE read-only fetch + frozen
 * rev-parse reads of origin/dev) -> skip decision BEFORE any worktree op, install, or
 * write (skip on matching sha OR matching build-input fingerprint) -> in "dispatch" mode
 * (bare `senpi update` foreground, unless SENPI_OMO_LOCAL_UPDATE_SYNC=1) a needed rebuild
 * is handed to a detached worker spawn (omo-local-update-worker.ts) and the hook returns ->
 * otherwise ("build": the default and the worker path) ensureBuildWorktree
 * (feature-owned persistent worktree, reuse-or-recreate) ->
 * `bun install` (600s, cwd = build worktree) -> `bun run build:senpi-plugin` (900s, cwd =
 * build worktree) -> completeness check in the WORKTREE plugin dir -> atomic swap of the
 * install target -> writeStamp (inventory from the NEW plugin dir) -> notify (green line
 * + dim per-package tree comparison when an old stamp existed).
 *
 * HARD GUARANTEES: the user's checkout receives ZERO git mutations (no
 * checkout/branch/commit/merge/reset/clean/stash/push anywhere; only `git fetch` and
 * feature-owned worktree add/remove). Only pluginPath content is replaced. This hook NEVER
 * throws and NEVER sets process.exitCode: every failure downgrades to a yellow warning +
 * dim manual hint so the senpi self-update continues untouched.
 */
export declare function runOmoLocalUpdateBeta(options: RunOmoLocalUpdateBetaOptions): Promise<void>;
//# sourceMappingURL=omo-local-update.d.ts.map