/**
 * Where the build map lives and how every client touches it (plan
 * 2026-11-03-extension-build-map §6.1).
 *
 * ONE MAP PER PROJECT, whoever asks. Agents often work on one extension in
 * parallel, each in its own git worktree, and `.extenshi/` is usually ignored by
 * git, so a per-checkout file would give every agent its own map and the author
 * one browser tab per agent. The map therefore lives in the MAIN checkout: from
 * a linked worktree, `resolveMapHome` maps the directory to the same path in the
 * main working tree. Every agent, from any worktree, reads and writes the same
 * `.extenshi/build-map.json` and finds the same server.
 *
 * Files under `<home>/.extenshi/`:
 *   build-map.json         the state (BuildMapState); written under build-map.lock
 *   build-map.lock/        held for one read-modify-write; stale after a crash is taken over
 *   build-map.server.json  the running server: pid, port, instance id, shutdown token (0600);
 *                          a stopped server leaves it with pid 0 so the next start reuses the port
 *   build-map.server.lock/ held while a server starts, so two starts cannot both listen
 *   build-map.log          output of the background server
 *
 * Writes go to a temporary file in the same directory and are renamed over the
 * target, so a reader never sees half a file.
 *
 * The stdio MCP server (`@extenshi/mcp`) is the map's other client and ships a
 * byte-identical copy at tools/extenshi-mcp/src/build-map-store.ts, so both find
 * the same home and the same server; build-map-store-sync.test.ts there fails on
 * any drift. Edit this file, then copy it over.
 */
import { type BuildMapState } from '@extenshi/contracts';
export declare const MAP_DIR = ".extenshi";
export declare const MAP_STATE_FILE = "build-map.json";
export declare const MAP_SERVER_FILE = "build-map.server.json";
export declare const MAP_LOG_FILE = "build-map.log";
/** Bumped when the page/server protocol changes: a newer CLI replaces an older server instead of reusing it. */
export declare const MAP_PROTOCOL = 8;
/** The record token, from the owner-only record file: authorizes /api/shutdown and a client's hello. */
export declare const MAP_TOKEN_HEADER = "x-extenshi-map-token";
/** Who asks: `cli/<version> graph=<revision>` or `mcp/<version> graph=<revision>` (see mapClientHeader). */
export declare const MAP_CLIENT_HEADER = "x-extenshi-map-client";
/**
 * A program that talks to the map server on an agent's behalf: the CLI or the
 * stdio MCP server, its package version, and the graph revision it was built
 * with (BUILD_GRAPH_REVISION of its @extenshi/contracts).
 */
export type MapClient = {
    kind: 'cli' | 'mcp';
    version: string;
    graph: number;
};
/** This program as a map client: the graph revision is the one it was built with. */
export declare const mapClient: (kind: MapClient["kind"], version: string) => MapClient;
export declare const mapClientHeader: (client: MapClient) => string;
export declare function parseMapClient(value: unknown): MapClient | undefined;
/** Order of two `x.y.z` versions (a pre-release sorts before its release); undefined when either is not one. */
export declare function compareVersions(a: string, b: string): number | undefined;
/**
 * How the running map and a client differ. `map_older`: the map draws an older
 * graph than the client knows, or — for the CLI, the program that serves the
 * map — it runs an older CLI release; `client_older`: the client knows an older
 * graph than the map draws. A server from before graph revisions counts as
 * revision 0.
 */
export declare function mapSkew(server: {
    version: string;
    graph?: number;
}, client: MapClient): 'map_older' | 'client_older' | undefined;
export type MapHome = {
    /** Where the map's files live: this project directory in the main checkout. */
    home: string;
    /** The directory the command ran in. */
    dir: string;
    /** `dir` is inside a linked git worktree, and the map lives in the main checkout. */
    worktree: boolean;
};
export declare const mapPath: (home: string, file: string) => string;
/**
 * The map home for `dir`: `dir` itself, unless it sits in a linked git worktree
 * whose main working tree has the same directory — then that one. Outside git,
 * in a bare-repository worktree or a submodule, the directory stays its own home.
 */
export declare function resolveMapHome(dir: string): Promise<MapHome>;
/** Every checkout of the repository `home` belongs to (main first), mapped to the project directory in each. */
export declare function projectCheckouts(home: string): Promise<string[]>;
export declare function pidAlive(pid: number): boolean;
/**
 * Run `work` holding a directory lock. `mkdir` is atomic, so exactly one
 * process creates the directory; its `owner.json` carries a random token. A
 * lock whose owner died on this machine, or that is older than `staleMs`, is
 * taken over — but the verdict is checked again under the breaker
 * (`withBreaker`) before anything is removed, and a release removes the lock
 * only while it still carries this holder's token.
 */
export declare function withDirLock<T>(lockDir: string, work: () => Promise<T>, options?: {
    timeoutMs?: number;
    staleMs?: number;
    what?: string;
}): Promise<T>;
/** Write JSON so a concurrent reader sees the old file or the new one, never a mix. */
export declare function writeJsonAtomic(file: string, value: unknown, mode?: number): Promise<void>;
export type StateRead = {
    state: BuildMapState;
    exists: boolean;
    problem?: string;
};
/** Read the state for display: a missing or broken file reads as empty, with the problem named. */
export declare function readMapState(home: string): Promise<StateRead>;
/**
 * Read, change and write the state as one step, under the state lock, so two
 * agents reporting at the same moment both land. Refuses to write over a file
 * it cannot read rather than replace someone's edit with an empty map.
 */
export declare function updateMapState(home: string, change: (state: BuildMapState) => BuildMapState | undefined): Promise<BuildMapState>;
export type MapServerRecord = {
    pid: number;
    host: string;
    port: number;
    url: string;
    /** Random per start: tells this server from another that later got the same pid or port. */
    instance: string;
    /** Authorizes /api/shutdown; the file is readable by the owner only. */
    token: string;
    protocol: number;
    version: string;
    /** The graph revision the server draws (absent before protocol 7). */
    graph?: number;
    home: string;
    startedAt: string;
};
export type MapHealth = {
    instance: string;
    home: string;
    protocol: number;
    version: string;
    clients: number;
    /** The graph revision the server draws (absent before protocol 7). */
    graph?: number;
    /** A newer @extenshi/cli on npm that the map can restart on. */
    available?: string;
    /** The version the map is restarting on right now. */
    updating?: string;
};
export declare function readServerRecord(home: string): Promise<MapServerRecord | undefined>;
export declare function fetchHealth(port: number, timeoutMs?: number, headers?: Record<string, string>): Promise<MapHealth | undefined>;
export type RunningServer = MapServerRecord & Pick<MapHealth, 'available' | 'updating'> & {
    clients: number;
    graph: number;
};
/**
 * The server recorded for `home`, if it is really up: same machine, process
 * alive, and its health answer carries the recorded instance id and home. A
 * record left by a crash, by another machine (a synced folder) or a reused pid
 * reads as no server. With `client`, the probe also tells the server which
 * program asks and which graph it knows, so the map can offer an update when
 * an agent works with newer steps than the map draws.
 */
export declare function probeServer(home: string, client?: MapClient): Promise<RunningServer | undefined>;
export declare const serverLockPath: (home: string) => string;
/**
 * Mark the server record stopped, but only if it still names `instance` —
 * never a successor's. The port stays in it, so the next start binds the same
 * address and a tab left open reconnects by itself; pid 0 and no token make it
 * read as no server.
 */
export declare function retireServerRecord(home: string, instance: string): Promise<void>;
//# sourceMappingURL=build-map-store.d.ts.map