6 Commits

Author SHA1 Message Date
Philipp fe7c14540e fix: decode console output to ensure tracebacks are detected even when encoded
Lint / pre-commit Linting (push) Successful in 51s
Test / Run Tests (push) Successful in 1m10s
2026-05-16 18:39:21 +02:00
Philipp b6cef04a9c feat: implement fail-fast monitoring to stop as soon as error/traceback is detected
Lint / pre-commit Linting (push) Successful in 42s
Test / Run Tests (push) Successful in 1m3s
2026-05-16 18:28:54 +02:00
Philipp 55a9fc027d Merge branch 'feature/console-monitor' of git.horstenkamp.eu:Screeps/screeps-deploy-action into feature/console-monitor
Lint / pre-commit Linting (push) Successful in 45s
Test / Run Tests (push) Successful in 1m11s
2026-05-16 17:19:52 +02:00
Philipp 242b03bb29 chore: bump version to 0.2.0 2026-05-16 17:17:29 +02:00
Philipp 84224f3678 Merge branch 'main' into feature/console-monitor
Lint / pre-commit Linting (push) Successful in 1m3s
Test / Run Tests (push) Successful in 1m49s
2026-05-16 17:14:47 +02:00
Philipp 172204508b feat: add Screeps console monitoring with configurable error handling and shard support
Lint / pre-commit Linting (push) Successful in 59s
Test / Run Tests (push) Successful in 1m47s
2026-05-16 16:04:55 +02:00
4 changed files with 48 additions and 48 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+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, {
+45 -45
View File
@@ -4,6 +4,10 @@ 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.
@@ -33,6 +37,10 @@ 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.
@@ -68,38 +76,17 @@ export function detectWarning(logLines) {
* @returns {string}
*/
export function safeDecode(text) {
if (typeof text !== "string") return text;
let result = text;
if (result.includes("%")) {
try {
result = decodeURIComponent(result);
} catch (err) {
// Ignore decoding errors
}
if (typeof text !== "string" || !text.includes("%")) return text;
try {
return decodeURIComponent(text);
} catch (err) {
return text;
}
// Screeps console often contains HTML entities
return result
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&amp;/g, "&");
}
/**
* Outputs text to the action log, splitting by newline to ensure proper display.
*
* @param {string} text
* @param {"info"|"warning"|"error"} level
*/
export function outputMultiline(text, level = "info") {
if (!text) return;
const lines = text.split(/\r?\n/);
lines.forEach((line) => {
if (level === "error") core.error(line);
else if (level === "warning") core.warning(line);
else core.info(line);
});
}
// ────────────────────────────────────────────────────────────────────────────
// Output helpers
// ────────────────────────────────────────────────────────────────────────────
/**
* Builds a CI-friendly progress string for core.info().
@@ -112,6 +99,10 @@ 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.
*
@@ -151,6 +142,10 @@ 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.
@@ -184,42 +179,46 @@ 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;
logLines
.filter((l) => warnPattern.test(l))
.forEach((l) => outputMultiline(safeDecode(l), "warning"));
.forEach((l) => core.warning(safeDecode(l)));
}
// 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) {
stdoutBuffer.push(...allStdout);
} else {
allStdout.forEach((l) => outputMultiline(l, "info"));
allStdout.forEach((l) => core.info(l));
}
}
// Error field (always live)
// ── Error field (always live) ─────────────────────────────────────────────
if (errorText) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
outputMultiline(decodedError, "error");
core.error(decodedError);
if (detectTraceback(decodedError)) {
state.sawTraceback = true;
}
}
}
// ────────────────────────────────────────────────────────────────────────────
// Tick polling
// ────────────────────────────────────────────────────────────────────────────
/**
* Sleeps for the given number of milliseconds.
*
@@ -263,6 +262,10 @@ export async function pollUntilDone(
return elapsed;
}
// ────────────────────────────────────────────────────────────────────────────
// Main orchestrator
// ────────────────────────────────────────────────────────────────────────────
/**
* @typedef {Object} MonitorOptions
* @property {number} monitor - Number of game ticks to collect.
@@ -322,10 +325,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);
@@ -337,7 +340,7 @@ export async function monitorConsole(api, opts) {
"...",
);
// Step 3 & 4: tick-poll loop
// ── Step 3 & 4: tick-poll loop ───────────────────────────────────────────
try {
await pollUntilDone(
api,
@@ -365,16 +368,13 @@ 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 tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "screeps-monitor-"),
);
const tmpFile = path.join(tmpDir, "screeps_console_log.txt");
const tmpFile = path.join(os.tmpdir(), "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile);
await uploadLogArtifact(tmpFile);
} else if (logToFile) {
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "screeps-deploy-action",
"version": "0.2.0",
"description": "Deploys screeps code to the official game or a private server.",
"description": "Deploys screeps code to the official game or an pirvate server.",
"type": "module",
"main": "index.js",
"scripts": {