export const shellProcessStatusTrackingDependencyDeclarations = [
    {
        dependencyId: "runtime.execEngine.shellExecution.monitorExecution",
        kind: "runtime",
        required: true,
        description: "Runtime-owned shell process observation port exposed through BaseToolExecutorPort.shell.monitorExecution",
    },
    {
        dependencyId: "runtime.governancePlane.toolInvocationGrant",
        kind: "permission",
        required: true,
        description: "Runtime governance decision carried in context.guard for real status dispatch",
    },
    {
        dependencyId: "runtime.shellProcessOwnership",
        kind: "runtime",
        required: true,
        description: "Runtime owns process lifecycle and supplies status snapshots; baseTool only normalizes them",
    },
];
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stringValue(value) {
    return typeof value === "string" ? value : undefined;
}
function numberValue(value) {
    return typeof value === "number" ? value : undefined;
}
function invalidNumberValue(value) {
    return value !== undefined && value !== null && typeof value !== "number" ? Number.NaN : undefined;
}
function isoFromMs(value) {
    if (typeof value !== "number" || !Number.isFinite(value))
        return undefined;
    const date = new Date(value);
    return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
}
function invalidTimestampFromMs(value) {
    return value !== undefined && isoFromMs(value) === undefined ? "__invalid_timestamp__" : undefined;
}
function timestampValue(textValue, msValue) {
    return stringValue(textValue) ?? isoFromMs(msValue) ?? invalidTimestampFromMs(msValue);
}
function signalValue(value) {
    if (value === undefined)
        return undefined;
    if (value === null)
        return null;
    return stringValue(value) ?? "__invalid_signal__";
}
function runtimeStatus(value, exitCode, signal) {
    if (typeof value !== "string")
        return undefined;
    if (value === "exited") {
        if (typeof signal === "string" && signal.trim().length > 0)
            return "terminated";
        if (exitCode === 0)
            return "completed";
        if (typeof exitCode === "number")
            return "failed";
        return "unknown";
    }
    if (value === "healthy")
        return "running";
    if (value === "stalled")
        return "running";
    return value;
}
function snapshotFromRecord(value) {
    return {
        pid: invalidNumberValue(value.pid) ?? numberValue(value.pid) ?? invalidNumberValue(value.processId) ?? numberValue(value.processId),
        status: runtimeStatus(value.status ?? value.state ?? value.health, value.exitCode, value.signal),
        exitCode: invalidNumberValue(value.exitCode) ?? (typeof value.exitCode === "number" || value.exitCode === null ? value.exitCode : undefined),
        signal: signalValue(value.signal),
        startedAt: timestampValue(value.startedAt, value.startedAtMs),
        observedAt: timestampValue(value.observedAt, value.observedAtMs),
        lastOutputAt: timestampValue(value.lastOutputAt, value.lastActivityAtMs),
    };
}
export function createHostExecutorShellProcessStatusTrackingProvider(executor) {
    const monitorExecution = executor?.shell?.monitorExecution;
    if (monitorExecution === undefined)
        return undefined;
    return async (request, context) => {
        const result = await monitorExecution({
            target: { toolId: "shell.processStatusTracking", executionId: request.executionId, command: request.command },
            observation: {
                snapshot: request.snapshot,
                expectedStatuses: request.expectedStatuses,
                staleAfterMs: request.staleAfterMs,
            },
            context: context,
        });
        if (!result.ok)
            throw new Error(result.error.message);
        const output = result.output;
        if (!isRecord(output))
            return {};
        const runtimeEnvelope = isRecord(output.observation) ? { ...output.observation, ...(isRecord(output.target) ? output.target : {}) } : output;
        const snapshot = isRecord(output.snapshot) ? snapshotFromRecord(output.snapshot) : snapshotFromRecord(runtimeEnvelope);
        return {
            command: stringValue(output.command),
            snapshot,
            expectedStatuses: Array.isArray(output.expectedStatuses)
                ? output.expectedStatuses.filter((value) => typeof value === "string")
                : undefined,
        };
    };
}
