Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ jobs:
run: |
pip install -e .

- name: Run Azure Pipelines task unit tests
if: matrix.test-type == 'unit'
run: npm test --prefix azure-pipelines/MicrobotsLogAnalyzerTask

- name: Build Docker images for integration tests
if: matrix.test-type != 'unit'
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Log files
*.log

node_modules/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
Expand All @@ -30,6 +32,7 @@ share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
*.vsix
MANIFEST

# PyInstaller
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ The `WritingBot` will read and write the files inside `code` folder based on spe

The MicroBots create a containerized environment and mount the specified directory with restricting the permissions to read-only or read/write based on Bot used. It ensures that the AI agents operate within defined boundaries which enhances security and control over code modifications as well as protecting the local environment.

## Azure Pipelines Log Analyzer

Microbots includes a published Azure DevOps Marketplace task, `MicrobotsLogAnalyzer@0`, for analyzing logs with Azure OpenAI models through an Azure Resource Manager Service Connection. See [docs/azure-pipelines-log-analyzer.md](docs/azure-pipelines-log-analyzer.md) to install and use the task in Azure Pipelines.

#Legal Notice

Trademarks This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.
218 changes: 218 additions & 0 deletions azure-pipelines/MicrobotsLogAnalyzerTask/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
"use strict";

const fs = require("fs");
const path = require("path");
const { spawnSync } = require("child_process");
const tl = require("azure-pipelines-task-lib/task");
const { loginAzureRM } = require("azure-pipelines-tasks-azure-arm-rest/azCliUtility");

const DEFAULT_TIMEOUT_SECONDS = "600";
const VENV_NAME = "microbots-log-analyzer-venv";
const VENV_READY_MARKER = ".microbots-venv-ready-v1";

function runCommand(command, args, env) {
const result = spawnSync(command, args, {
stdio: ["ignore", "inherit", "inherit"],
env: env || process.env,
});

if (result.error) throw new Error(`Failed to run ${command}: ${result.error.message}`);
if (result.status !== 0) {
throw new Error(`${command} ${args.join(" ")} -> exit ${result.status}`);
}
}

function input(name, required) {
const value = tl.getInput(name, required);
return value ? value.trim() : value;
}

function azureSubscriptionInput() {
const value = input("azureSubscription", false) || input("serviceConnection", false);
if (!value) throw new Error("azureSubscription is required");
return value;
}

function resolveLogPath(codebasePath, logFilePath) {
return path.isAbsolute(logFilePath)
? path.resolve(logFilePath)
: path.resolve(codebasePath, logFilePath);
}

function getInputs() {
const inputs = {
serviceConnection: azureSubscriptionInput(),
deploymentName: input("deploymentName", true),
endpoint: input("endpoint", true),
apiVersion: input("apiVersion", true),
codebasePath: tl.getPathInput("codebasePath", true, true),
logFilePath: input("logFilePath", true),
outputFilePath: input("outputFilePath", false),
timeoutSeconds: input("timeoutSeconds", false) || DEFAULT_TIMEOUT_SECONDS,
maxIterations: input("maxIterations", false),
};

validateInputs(inputs);
return inputs;
}

function validateInputs(inputs) {
if (!fs.existsSync(inputs.codebasePath)) {
throw new Error(`codebasePath does not exist: ${inputs.codebasePath}`);
}

if (!fs.statSync(inputs.codebasePath).isDirectory()) {
throw new Error(`codebasePath must be a directory: ${inputs.codebasePath}`);
}

const logPath = resolveLogPath(inputs.codebasePath, inputs.logFilePath);
if (!fs.existsSync(logPath) || !fs.statSync(logPath).isFile()) {
throw new Error(`logFilePath does not exist: ${logPath}`);
}
inputs.logFilePath = logPath;

try {
const endpoint = new URL(inputs.endpoint);
if (endpoint.protocol !== "https:") throw new Error();
} catch (_) {
throw new Error(`endpoint must be a valid HTTPS URL: ${inputs.endpoint}`);
}

const timeoutSeconds = Number(inputs.timeoutSeconds);
if (!Number.isSafeInteger(timeoutSeconds) || timeoutSeconds <= 0) {
throw new Error(`timeoutSeconds must be a positive integer: ${inputs.timeoutSeconds}`);
}
inputs.timeoutSeconds = String(timeoutSeconds);

if (inputs.maxIterations) {
const maxIterations = Number(inputs.maxIterations);
if (!Number.isSafeInteger(maxIterations) || maxIterations <= 0) {
throw new Error(`maxIterations must be a positive integer: ${inputs.maxIterations}`);
}
inputs.maxIterations = String(maxIterations);
}

if (inputs.outputFilePath) {
if (!path.isAbsolute(inputs.outputFilePath)) {
throw new Error(`outputFilePath must be an absolute path: ${inputs.outputFilePath}`);
}

inputs.outputFilePath = path.resolve(inputs.outputFilePath);
const extension = path.extname(inputs.outputFilePath).toLowerCase();
if (extension !== ".txt" && extension !== ".md" && extension !== ".log") {
throw new Error(`outputFilePath must end with .txt, .md, or .log: ${inputs.outputFilePath}`);
}

if (fs.existsSync(inputs.outputFilePath) && fs.statSync(inputs.outputFilePath).isDirectory()) {
throw new Error(`outputFilePath must be a file path, not a directory: ${inputs.outputFilePath}`);
}
}
}

async function loginWithServiceConnection(serviceConnection) {
console.log("##[section]MicrobotsLogAnalyzer: authenticating with Azure service connection");
const previousAzureOutput = process.env.AZURE_CORE_OUTPUT;
const originalLoc = tl.loc;

process.env.AZURE_CORE_OUTPUT = "none";
tl.loc = function loc(key, ...args) {
if (key === "LoginFailed" || key === "ErrorInSettingUpSubscription") return key;
return originalLoc(key, ...args);
};

try {
await loginAzureRM(serviceConnection);
} catch (error) {
throw new Error(`Azure service connection login failed for '${serviceConnection}': ${error.message || String(error)}`);
} finally {
tl.loc = originalLoc;
if (previousAzureOutput === undefined) delete process.env.AZURE_CORE_OUTPUT;
else process.env.AZURE_CORE_OUTPUT = previousAzureOutput;
}

console.log("##[section]MicrobotsLogAnalyzer: Azure authentication complete");
}

function venvPythonPath(venvDir) {
return process.platform === "win32"
? path.join(venvDir, "Scripts", "python.exe")
: path.join(venvDir, "bin", "python");
}

function setupVenv() {
const venvRoot = process.env.AGENT_TEMPDIRECTORY
|| process.env.PIPELINE_WORKSPACE
|| process.env.RUNNER_TEMP
|| "/tmp";
const venvDir = path.join(venvRoot, VENV_NAME);
const python = venvPythonPath(venvDir);
const venvReadyFile = path.join(venvDir, VENV_READY_MARKER);

if (fs.existsSync(python) && fs.existsSync(venvReadyFile)) {
console.log(`##[section]MicrobotsLogAnalyzer: reusing Python environment at ${venvDir}`);
return python;
}

if (fs.existsSync(venvDir)) fs.rmSync(venvDir, { recursive: true, force: true });

console.log(`##[section]MicrobotsLogAnalyzer: creating Python environment at ${venvDir}`);
runCommand("python3", ["-m", "venv", venvDir]);
console.log("Installing Python dependencies (microbots, Azure identity)...");
runCommand(python, ["-m", "pip", "install", "--quiet", "--upgrade", "pip"]);
runCommand(python, ["-m", "pip", "install", "--quiet", "microbots[azure_ad]"]);
fs.writeFileSync(venvReadyFile, new Date().toISOString());

return python;
}

function microbotsEnvironment(inputs) {
return Object.assign({}, process.env, {
AZURE_OPENAI_DEPLOYMENT_NAME: inputs.deploymentName,
AZURE_OPENAI_ENDPOINT: inputs.endpoint,
AZURE_OPENAI_API_VERSION: inputs.apiVersion,
});
}

function ensureOutputParentDirectory(outputFilePath) {
if (!outputFilePath) return;

const outputDirectory = path.dirname(outputFilePath);
if (fs.existsSync(outputDirectory) && !fs.statSync(outputDirectory).isDirectory()) {
throw new Error(`outputFilePath parent must be a directory: ${outputDirectory}`);
}

fs.mkdirSync(outputDirectory, { recursive: true });
console.log(`##[section]MicrobotsLogAnalyzer: analysis output will overwrite ${outputFilePath}`);
}

function runLogAnalyzer(python, inputs) {
const scriptPath = path.join(__dirname, "log_analyzer_runner.py");
const args = [
scriptPath,
"--codebase-path", inputs.codebasePath,
"--log-file-path", inputs.logFilePath,
"--timeout-seconds", inputs.timeoutSeconds,
];

if (inputs.outputFilePath) args.push("--output-file", inputs.outputFilePath);
if (inputs.maxIterations) args.push("--max-iterations", inputs.maxIterations);

ensureOutputParentDirectory(inputs.outputFilePath);
runCommand(python, args, microbotsEnvironment(inputs));
}

async function run() {
try {
const inputs = getInputs();
await loginWithServiceConnection(inputs.serviceConnection);
const python = setupVenv();
runLogAnalyzer(python, inputs);
tl.setResult(tl.TaskResult.Succeeded, "LogAnalysisBot completed");
} catch (error) {
tl.setResult(tl.TaskResult.Failed, error.message || String(error));
} finally {
try { spawnSync("az", ["account", "clear"], { stdio: "ignore" }); } catch (_) {}
}
}

run();
108 changes: 108 additions & 0 deletions azure-pipelines/MicrobotsLogAnalyzerTask/log_analyzer_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import argparse
import os
import sys
import textwrap

from azure.identity import AzureCliCredential, get_bearer_token_provider
from microbots import LogAnalysisBot


def is_docker_access_error(error):
return "docker" in type(error).__module__.lower() or "docker" in str(error).lower()


def parse_args():
parser = argparse.ArgumentParser(description="Run Microbots LogAnalysisBot.")
parser.add_argument("--codebase-path", required=True)
parser.add_argument("--log-file-path", required=True)
parser.add_argument("--timeout-seconds", required=True, type=int)
parser.add_argument("--output-file")
parser.add_argument("--max-iterations", type=int)
return parser.parse_args()


def log(message, *, file=sys.stdout):
print(message, file=file, flush=True)


def safe_analysis_line(message):
if message.startswith("##vso[") or message.startswith("##["):
return " " + message
return message


def write_text_file(file_path, content):
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "w", encoding="utf-8") as output_file:
output_file.write(content)


def main():
args = parse_args()
codebase_path = os.path.abspath(args.codebase_path)
log_file_path = args.log_file_path
timeout_seconds = args.timeout_seconds
max_iterations = args.max_iterations

os.chdir(codebase_path)
log(
f"MicrobotsLogAnalyzer: analyzing {log_file_path} with deployment "
f"{os.environ['AZURE_OPENAI_DEPLOYMENT_NAME']}"
)
log(f"MicrobotsLogAnalyzer: timeout is {timeout_seconds} seconds")
if max_iterations is not None:
log(f"MicrobotsLogAnalyzer: max iterations is {max_iterations}")
if args.output_file:
log(f"MicrobotsLogAnalyzer: analysis output file is {args.output_file}")

token_provider = get_bearer_token_provider(
AzureCliCredential(),
"https://cognitiveservices.azure.com/.default",
)
run_kwargs = {
"file_name": log_file_path,
"timeout_in_seconds": timeout_seconds,
}
if max_iterations is not None:
run_kwargs["max_iterations"] = max_iterations

try:
bot = LogAnalysisBot(
model=f"azure-openai/{os.environ['AZURE_OPENAI_DEPLOYMENT_NAME']}",
folder_to_mount=codebase_path,
token_provider=token_provider,
)
result = bot.run(**run_kwargs)
except Exception as error:
if not is_docker_access_error(error):
raise
log(
"MicrobotsLogAnalyzer: Docker-compatible daemon was not accessible "
"while starting the Microbots sandbox.",
file=sys.stderr,
)
log(f"Details: {error}", file=sys.stderr)
return 1

message = result.result or result.error or ""
if args.output_file:
write_text_file(args.output_file, str(message))
log(f"MicrobotsLogAnalyzer: wrote analysis output to {args.output_file}")

log("##[section]MicrobotsLogAnalyzer: LLM analysis")
log("============================================================")
log("MICROBOTS LOG ANALYSIS")
log("============================================================")
for paragraph in str(message).splitlines() or [""]:
if paragraph.strip():
for line in textwrap.wrap(paragraph, width=125) or [""]:
log(safe_analysis_line(line))
else:
log("")
log("============================================================")

return 0 if result.status else 1


if __name__ == "__main__":
sys.exit(main())
Loading
Loading