feat: add Screeps console monitoring with configurable error handling and shard support #84

Merged
Philipp merged 9 commits from feature/console-monitor into main 2026-05-16 19:44:39 +02:00
2 changed files with 10 additions and 38 deletions
Showing only changes of commit f62ac427c2 - Show all commits
+1 -1
View File
@@ -197,7 +197,7 @@ export async function postCode() {
});
}
// ── Console monitoring (optional) ────────────────────────────────────────
// Console monitoring (optional)
const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
if (monitorTicks > 0) {
const result = await monitorConsole(api, {
+9 -37
View File
@@ -4,10 +4,6 @@ import fs from "fs";
import path from "path";
import os from "os";
// ────────────────────────────────────────────────────────────────────────────
// Shard / subscribe-path helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Returns true if the hostname is the official Screeps server.
* Used to decide whether to prefix the subscribe path with a shard name.
@@ -37,10 +33,6 @@ export function buildSubscribePath(hostname, shard) {
return isOfficialServer(hostname) ? "shard0/console" : "console";
}
// ────────────────────────────────────────────────────────────────────────────
// Detection helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Returns true when errorText contains JavaScript stack-frame lines.
* Screeps places runtime errors (including stack traces) in event.data.error.
@@ -109,10 +101,6 @@ export function outputMultiline(text, level = "info") {
});
}
// ────────────────────────────────────────────────────────────────────────────
// Output helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Builds a CI-friendly progress string for core.info().
*
@@ -124,10 +112,6 @@ export function buildProgressMessage(elapsed, total) {
return `[Monitor] ${elapsed}/${total} ticks elapsed...`;
}
// ────────────────────────────────────────────────────────────────────────────
// File / artifact helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Writes an array of log lines to a UTF-8 text file, one line per entry.
*
@@ -167,10 +151,6 @@ export async function uploadLogArtifact(
}
}
// ────────────────────────────────────────────────────────────────────────────
// Console event handler
// ────────────────────────────────────────────────────────────────────────────
/**
* Processes a single 'console' WebSocket event from the Screeps socket.
* Mutates `state` and `stdoutBuffer` in place; never throws.
@@ -204,7 +184,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const results = data?.messages?.results ?? [];
const errorText = data?.error ?? null;
// ── Warn detection (always live regardless of logToFile) ─────────────────
// Warn detection (always live regardless of logToFile)
if (detectWarning(logLines)) {
state.sawWarningLog = true;
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
@@ -213,13 +193,13 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
.forEach((l) => outputMultiline(safeDecode(l), "warning"));
}
// ── Traceback detection in log lines (Screeps sometimes sends errors here)
// Traceback detection in log lines (Screeps sometimes sends errors here)
if (logLines.some((l) => detectTraceback(l))) {
state.sawTraceback = true;
state.sawErrorLog = true;
}
// ── Stdout lines ──────────────────────────────────────────────────────────
// Stdout lines
const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) {
if (logToFile) {
@@ -229,7 +209,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
}
}
// ── Error field (always live) ─────────────────────────────────────────────
// Error field (always live)
if (errorText) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
@@ -240,10 +220,6 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
}
}
// ────────────────────────────────────────────────────────────────────────────
// Tick polling
// ────────────────────────────────────────────────────────────────────────────
/**
* Sleeps for the given number of milliseconds.
*
@@ -287,10 +263,6 @@ export async function pollUntilDone(
return elapsed;
}
// ────────────────────────────────────────────────────────────────────────────
// Main orchestrator
// ────────────────────────────────────────────────────────────────────────────
/**
* @typedef {Object} MonitorOptions
* @property {number} monitor - Number of game ticks to collect.
@@ -350,10 +322,10 @@ export async function monitorConsole(api, opts) {
};
let lastProgressTick = 0;
// ── Step 1: record starting tick ─────────────────────────────────────────
// Step 1: record starting tick
const { time: startTick } = await api.time(shard);
// ── Step 2: connect socket + subscribe ───────────────────────────────────
// Step 2: connect socket + subscribe
await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state);
@@ -365,7 +337,7 @@ export async function monitorConsole(api, opts) {
"...",
);
// ── Step 3 & 4: tick-poll loop ───────────────────────────────────────────
// Step 3 & 4: tick-poll loop
try {
await pollUntilDone(
api,
@@ -393,11 +365,11 @@ export async function monitorConsole(api, opts) {
},
);
} finally {
// ── Step 5: always disconnect cleanly ────────────────────────────────
// Step 5: always disconnect cleanly
api.socket.disconnect();
}
// ── Step 6: artifact upload ───────────────────────────────────────────────
// Step 6: artifact upload
if (logToFile && stdoutBuffer.length > 0) {
const tmpFile = path.join(os.tmpdir(), "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile);