import * as net from 'net';

type SessionTransport = 'pty';
type SessionHostCategory = 'cli' | 'acp' | 'shell';
type SessionLifecycle = 'starting' | 'running' | 'stopping' | 'stopped' | 'failed' | 'interrupted';
type SessionClientType = 'daemon' | 'web' | 'local-terminal';
type SessionOwnerType = 'agent' | 'user';
interface SessionLaunchCommand {
    command: string;
    args: string[];
    env?: Record<string, string>;
}
interface SessionWriteOwner {
    clientId: string;
    ownerType: SessionOwnerType;
    acquiredAt: number;
}
interface SessionAttachedClient {
    clientId: string;
    type: SessionClientType;
    readOnly: boolean;
    attachedAt: number;
    lastSeenAt: number;
}
interface SessionBufferSnapshot {
    seq: number;
    text: string;
    truncated: boolean;
    cols?: number;
    rows?: number;
}
interface SessionBufferState {
    scrollbackBytes: number;
    snapshotSeq: number;
}
interface SessionHostRecord {
    sessionId: string;
    runtimeKey: string;
    displayName: string;
    workspaceLabel: string;
    transport: SessionTransport;
    providerType: string;
    category: SessionHostCategory;
    workspace: string;
    launchCommand: SessionLaunchCommand;
    osPid?: number;
    createdAt: number;
    startedAt?: number;
    lastActivityAt: number;
    lifecycle: SessionLifecycle;
    writeOwner: SessionWriteOwner | null;
    attachedClients: SessionAttachedClient[];
    buffer: SessionBufferState;
    meta: Record<string, unknown>;
}
interface CreateSessionPayload {
    sessionId?: string;
    runtimeKey?: string;
    displayName?: string;
    providerType: string;
    category: SessionHostCategory;
    workspace: string;
    launchCommand: SessionLaunchCommand;
    cols?: number;
    rows?: number;
    clientId?: string;
    clientType?: SessionClientType;
    meta?: Record<string, unknown>;
}
interface AttachSessionPayload {
    sessionId: string;
    clientId: string;
    clientType: SessionClientType;
    readOnly?: boolean;
}
interface DetachSessionPayload {
    sessionId: string;
    clientId: string;
}
interface SendInputPayload {
    sessionId: string;
    clientId: string;
    data: string;
}
interface ResizeSessionPayload {
    sessionId: string;
    cols: number;
    rows: number;
}
interface StopSessionPayload {
    sessionId: string;
}
interface ResumeSessionPayload {
    sessionId: string;
}
interface AcquireWritePayload {
    sessionId: string;
    clientId: string;
    ownerType: SessionOwnerType;
    force?: boolean;
}
interface ReleaseWritePayload {
    sessionId: string;
    clientId: string;
}
interface GetSnapshotPayload {
    sessionId: string;
    sinceSeq?: number;
}
interface ClearSessionBufferPayload {
    sessionId: string;
}
interface UpdateSessionMetaPayload {
    sessionId: string;
    meta: Record<string, unknown>;
    replace?: boolean;
}
type SessionHostRequest = {
    type: 'create_session';
    payload: CreateSessionPayload;
} | {
    type: 'attach_session';
    payload: AttachSessionPayload;
} | {
    type: 'detach_session';
    payload: DetachSessionPayload;
} | {
    type: 'send_input';
    payload: SendInputPayload;
} | {
    type: 'resize_session';
    payload: ResizeSessionPayload;
} | {
    type: 'stop_session';
    payload: StopSessionPayload;
} | {
    type: 'resume_session';
    payload: ResumeSessionPayload;
} | {
    type: 'acquire_write';
    payload: AcquireWritePayload;
} | {
    type: 'release_write';
    payload: ReleaseWritePayload;
} | {
    type: 'get_snapshot';
    payload: GetSnapshotPayload;
} | {
    type: 'clear_session_buffer';
    payload: ClearSessionBufferPayload;
} | {
    type: 'update_session_meta';
    payload: UpdateSessionMetaPayload;
} | {
    type: 'list_sessions';
    payload?: {};
};
interface SessionHostResponse<T = unknown> {
    success: boolean;
    result?: T;
    error?: string;
}
type SessionHostEvent = {
    type: 'session_created';
    sessionId: string;
    record: SessionHostRecord;
} | {
    type: 'session_started';
    sessionId: string;
    pid?: number;
} | {
    type: 'session_resumed';
    sessionId: string;
    pid?: number;
} | {
    type: 'session_output';
    sessionId: string;
    seq: number;
    data: string;
} | {
    type: 'session_cleared';
    sessionId: string;
} | {
    type: 'session_exit';
    sessionId: string;
    exitCode: number | null;
} | {
    type: 'session_stopped';
    sessionId: string;
} | {
    type: 'session_resized';
    sessionId: string;
    cols: number;
    rows: number;
} | {
    type: 'write_owner_changed';
    sessionId: string;
    owner: SessionWriteOwner | null;
} | {
    type: 'client_attached';
    sessionId: string;
    client: SessionAttachedClient;
} | {
    type: 'client_detached';
    sessionId: string;
    clientId: string;
};
interface SessionHostRequestEnvelope {
    kind: 'request';
    requestId: string;
    request: SessionHostRequest;
}
interface SessionHostResponseEnvelope {
    kind: 'response';
    requestId: string;
    response: SessionHostResponse;
}
interface SessionHostEventEnvelope {
    kind: 'event';
    event: SessionHostEvent;
}
type SessionHostWireEnvelope = SessionHostRequestEnvelope | SessionHostResponseEnvelope | SessionHostEventEnvelope;

interface SessionRingBufferOptions {
    maxBytes?: number;
}
declare class SessionRingBuffer {
    private maxBytes;
    private chunks;
    private nextSeq;
    private totalBytes;
    constructor(options?: SessionRingBufferOptions);
    append(data: string): number;
    snapshot(sinceSeq?: number): SessionBufferSnapshot;
    getState(): {
        scrollbackBytes: number;
        snapshotSeq: number;
    };
    clear(): void;
    restore(snapshot: {
        seq: number;
        text: string;
    }): void;
    private trim;
}

declare class SessionHostRegistry {
    private sessions;
    createSession(payload: CreateSessionPayload): SessionHostRecord;
    restoreSession(record: SessionHostRecord, snapshot?: {
        seq: number;
        text: string;
    } | null): SessionHostRecord;
    listSessions(): SessionHostRecord[];
    getSession(sessionId: string): SessionHostRecord | null;
    attachClient(payload: AttachSessionPayload): SessionHostRecord;
    detachClient(payload: DetachSessionPayload): SessionHostRecord;
    acquireWrite(payload: AcquireWritePayload): SessionHostRecord;
    releaseWrite(payload: ReleaseWritePayload): SessionHostRecord;
    appendOutput(sessionId: string, data: string): {
        record: SessionHostRecord;
        seq: number;
    };
    getSnapshot(sessionId: string, sinceSeq?: number): SessionBufferSnapshot;
    clearBuffer(sessionId: string): SessionHostRecord;
    updateSessionMeta(sessionId: string, meta: Record<string, unknown>, replace?: boolean): SessionHostRecord;
    markStarted(sessionId: string, pid?: number): SessionHostRecord;
    markStopped(sessionId: string, lifecycle?: 'stopped' | 'failed'): SessionHostRecord;
    setLifecycle(sessionId: string, lifecycle: 'starting' | 'running' | 'stopping' | 'stopped' | 'failed' | 'interrupted'): SessionHostRecord;
    private requireSession;
    private cloneRecord;
}

declare function getWorkspaceLabel(workspace: string): string;
declare function buildRuntimeDisplayName(payload: Pick<CreateSessionPayload, 'displayName' | 'providerType' | 'workspace'>): string;
declare function buildRuntimeKey(payload: Pick<CreateSessionPayload, 'runtimeKey' | 'displayName' | 'providerType' | 'workspace'>, existingKeys: Iterable<string>): string;
declare function resolveRuntimeRecord(records: SessionHostRecord[], identifier: string): SessionHostRecord;
declare function formatRuntimeOwner(record: Pick<SessionHostRecord, 'writeOwner'>): string;

interface SessionHostEndpoint {
    kind: 'unix' | 'pipe';
    path: string;
}
declare function getDefaultSessionHostEndpoint(appName?: string): SessionHostEndpoint;
declare function createLineParser(onEnvelope: (envelope: SessionHostWireEnvelope) => void): (chunk: Buffer | string) => void;
interface SessionHostClientOptions {
    endpoint?: SessionHostEndpoint;
    appName?: string;
}
declare class SessionHostClient {
    readonly endpoint: SessionHostEndpoint;
    private socket;
    private requestWaiters;
    private eventListeners;
    constructor(options?: SessionHostClientOptions);
    connect(): Promise<void>;
    onEvent(listener: (event: SessionHostEvent) => void): () => void;
    request<T = unknown>(request: SessionHostRequest): Promise<SessionHostResponse<T>>;
    close(): Promise<void>;
}
declare function createResponseEnvelope(requestId: string, response: SessionHostResponse): SessionHostResponseEnvelope;
declare function writeEnvelope(socket: Pick<net.Socket, 'write'>, envelope: SessionHostWireEnvelope): void;

export { type AcquireWritePayload, type AttachSessionPayload, type ClearSessionBufferPayload, type CreateSessionPayload, type DetachSessionPayload, type GetSnapshotPayload, type ReleaseWritePayload, type ResizeSessionPayload, type ResumeSessionPayload, type SendInputPayload, type SessionAttachedClient, type SessionBufferSnapshot, type SessionClientType, type SessionHostCategory, SessionHostClient, type SessionHostClientOptions, type SessionHostEndpoint, type SessionHostEvent, type SessionHostEventEnvelope, type SessionHostRecord, SessionHostRegistry, type SessionHostRequest, type SessionHostRequestEnvelope, type SessionHostResponse, type SessionHostResponseEnvelope, type SessionHostWireEnvelope, type SessionLaunchCommand, type SessionLifecycle, type SessionOwnerType, SessionRingBuffer, type SessionRingBufferOptions, type SessionTransport, type SessionWriteOwner, type StopSessionPayload, type UpdateSessionMetaPayload, buildRuntimeDisplayName, buildRuntimeKey, createLineParser, createResponseEnvelope, formatRuntimeOwner, getDefaultSessionHostEndpoint, getWorkspaceLabel, resolveRuntimeRecord, writeEnvelope };
