4 Commits

Author SHA1 Message Date
Philipp d02f44f6c7 fix(monitor): use global console channel and implement shard filtering
Lint / pre-commit Linting (push) Successful in 44s
Test / Run Tests (push) Successful in 1m4s
2026-05-16 20:26:32 +02:00
Philipp 6384addc42 fix: correct typo in package.json and use unique temp dir for logs
Lint / pre-commit Linting (push) Successful in 50s
Test / Run Tests (push) Successful in 1m10s
2026-05-16 19:39:27 +02:00
Philipp f62ac427c2 style: remove redundant section comments
Lint / pre-commit Linting (push) Successful in 45s
Test / Run Tests (push) Successful in 1m4s
2026-05-16 19:09:59 +02:00
Philipp c3d595e2e1 feat: support multiline console output and decode HTML entities
Lint / pre-commit Linting (push) Successful in 56s
Test / Run Tests (push) Successful in 1m14s
2026-05-16 18:55:44 +02:00
5 changed files with 86 additions and 63 deletions
+26 -11
View File
@@ -53,20 +53,20 @@ describe("isOfficialServer", () => {
}); });
describe("buildSubscribePath", () => { describe("buildSubscribePath", () => {
it("returns shard0/console for official server (no shard provided)", () => { it("returns console for official server (no shard provided)", () => {
expect(buildSubscribePath("screeps.com")).toBe("shard0/console"); expect(buildSubscribePath("screeps.com")).toBe("console");
}); });
it("returns console for private server (no shard provided)", () => { it("returns console for private server (no shard provided)", () => {
expect(buildSubscribePath("builder64")).toBe("console"); expect(buildSubscribePath("builder64")).toBe("console");
}); });
it("returns <shard>/console when shard is provided (official)", () => { it("returns console when shard is provided (official)", () => {
expect(buildSubscribePath("screeps.com", "shard3")).toBe("shard3/console"); expect(buildSubscribePath("screeps.com", "shard3")).toBe("console");
}); });
it("returns <shard>/console when shard is provided (private)", () => { it("returns console when shard is provided (private)", () => {
expect(buildSubscribePath("builder64", "myshard")).toBe("myshard/console"); expect(buildSubscribePath("builder64", "myshard")).toBe("console");
}); });
}); });
@@ -228,6 +228,21 @@ describe("handleConsoleEvent", () => {
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state), handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
).not.toThrow(); ).not.toThrow();
}); });
it("filters messages by shard when targetShard is provided", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, { shard: "shard1" }, stdoutBuffer, state);
expect(core.info).not.toHaveBeenCalled();
handleConsoleEvent(event, { shard: "shard0" }, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("msg0");
});
it("does not filter when targetShard is undefined", () => {
const event = { data: { shard: "shard0", messages: { log: ["msg0"] } } };
handleConsoleEvent(event, {}, stdoutBuffer, state);
expect(core.info).toHaveBeenCalledWith("msg0");
});
}); });
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
@@ -357,19 +372,19 @@ describe("monitorConsole", () => {
); );
}); });
it("subscribes to 'shard0/console' for the official server (default)", async () => { it("subscribes to 'console' for the official server (default)", async () => {
const api = buildMockApi({ const api = buildMockApi({
hostname: "screeps.com", hostname: "screeps.com",
ticks: [100, 101, 102, 103], ticks: [100, 101, 102, 103],
}); });
await monitorConsole(api, { ...BASE_OPTS, hostname: "screeps.com" }); await monitorConsole(api, { ...BASE_OPTS, hostname: "screeps.com" });
expect(api.socket.subscribe).toHaveBeenCalledWith( expect(api.socket.subscribe).toHaveBeenCalledWith(
"shard0/console", "console",
expect.any(Function), expect.any(Function),
); );
}); });
it("subscribes to custom shard when provided", async () => { it("subscribes to 'console' even when custom shard is provided", async () => {
const api = buildMockApi({ const api = buildMockApi({
hostname: "screeps.com", hostname: "screeps.com",
ticks: [100, 101, 102, 103], ticks: [100, 101, 102, 103],
@@ -380,10 +395,10 @@ describe("monitorConsole", () => {
shard: "shard3", shard: "shard3",
}); });
expect(api.socket.subscribe).toHaveBeenCalledWith( expect(api.socket.subscribe).toHaveBeenCalledWith(
"shard3/console", "console",
expect.any(Function), expect.any(Function),
); );
// Verify polling also uses shard3 // Verify polling still uses shard3 for timing
expect(api.time).toHaveBeenCalledWith("shard3"); expect(api.time).toHaveBeenCalledWith("shard3");
}); });
+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); const monitorTicks = parseInt(core.getInput("monitor") || "0", 10);
if (monitorTicks > 0) { if (monitorTicks > 0) {
const result = await monitorConsole(api, { const result = await monitorConsole(api, {
+54 -46
View File
@@ -4,10 +4,6 @@ import fs from "fs";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
// ────────────────────────────────────────────────────────────────────────────
// Shard / subscribe-path helpers
// ────────────────────────────────────────────────────────────────────────────
/** /**
* Returns true if the hostname is the official Screeps server. * Returns true if the hostname is the official Screeps server.
* Used to decide whether to prefix the subscribe path with a shard name. * Used to decide whether to prefix the subscribe path with a shard name.
@@ -33,14 +29,11 @@ export function isOfficialServer(hostname) {
* @returns {string} * @returns {string}
*/ */
export function buildSubscribePath(hostname, shard) { export function buildSubscribePath(hostname, shard) {
if (shard) return `${shard}/console`; // The console channel on Screeps official and most private servers is 'console'.
return isOfficialServer(hostname) ? "shard0/console" : "console"; // We subscribe to the aggregate feed and filter by shard in handleConsoleEvent.
return "console";
} }
// ────────────────────────────────────────────────────────────────────────────
// Detection helpers
// ────────────────────────────────────────────────────────────────────────────
/** /**
* Returns true when errorText contains JavaScript stack-frame lines. * Returns true when errorText contains JavaScript stack-frame lines.
* Screeps places runtime errors (including stack traces) in event.data.error. * Screeps places runtime errors (including stack traces) in event.data.error.
@@ -76,17 +69,38 @@ export function detectWarning(logLines) {
* @returns {string} * @returns {string}
*/ */
export function safeDecode(text) { export function safeDecode(text) {
if (typeof text !== "string" || !text.includes("%")) return text; if (typeof text !== "string") return text;
let result = text;
if (result.includes("%")) {
try { try {
return decodeURIComponent(text); result = decodeURIComponent(result);
} catch (err) { } catch (err) {
return text; // Ignore decoding errors
} }
} }
// Screeps console often contains HTML entities
return result
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&amp;/g, "&");
}
// ──────────────────────────────────────────────────────────────────────────── /**
// Output helpers * 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);
});
}
/** /**
* Builds a CI-friendly progress string for core.info(). * Builds a CI-friendly progress string for core.info().
@@ -99,10 +113,6 @@ export function buildProgressMessage(elapsed, total) {
return `[Monitor] ${elapsed}/${total} ticks elapsed...`; 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. * Writes an array of log lines to a UTF-8 text file, one line per entry.
* *
@@ -142,10 +152,6 @@ export async function uploadLogArtifact(
} }
} }
// ────────────────────────────────────────────────────────────────────────────
// Console event handler
// ────────────────────────────────────────────────────────────────────────────
/** /**
* Processes a single 'console' WebSocket event from the Screeps socket. * Processes a single 'console' WebSocket event from the Screeps socket.
* Mutates `state` and `stdoutBuffer` in place; never throws. * Mutates `state` and `stdoutBuffer` in place; never throws.
@@ -173,52 +179,55 @@ export async function uploadLogArtifact(
* @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state * @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state
*/ */
export function handleConsoleEvent(event, opts, stdoutBuffer, state) { export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const { logToFile } = opts; const { logToFile, shard: targetShard } = opts;
const data = event?.data ?? {}; const data = event?.data ?? {};
// Shard filtering: If a shard is specified in opts, only process messages from that shard.
// Official server events include a 'shard' property in event.data.
if (targetShard && data.shard && data.shard !== targetShard) {
return;
}
const logLines = data?.messages?.log ?? []; const logLines = data?.messages?.log ?? [];
const results = data?.messages?.results ?? []; const results = data?.messages?.results ?? [];
const errorText = data?.error ?? null; const errorText = data?.error ?? null;
// ── Warn detection (always live regardless of logToFile) ───────────────── // Warn detection (always live regardless of logToFile)
if (detectWarning(logLines)) { if (detectWarning(logLines)) {
state.sawWarningLog = true; state.sawWarningLog = true;
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i; const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines logLines
.filter((l) => warnPattern.test(l)) .filter((l) => warnPattern.test(l))
.forEach((l) => core.warning(safeDecode(l))); .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))) { if (logLines.some((l) => detectTraceback(l))) {
state.sawTraceback = true; state.sawTraceback = true;
state.sawErrorLog = true; state.sawErrorLog = true;
} }
// ── Stdout lines ────────────────────────────────────────────────────────── // Stdout lines
const allStdout = [...logLines, ...results].map(safeDecode); const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) { if (allStdout.length > 0) {
if (logToFile) { if (logToFile) {
stdoutBuffer.push(...allStdout); stdoutBuffer.push(...allStdout);
} else { } else {
allStdout.forEach((l) => core.info(l)); allStdout.forEach((l) => outputMultiline(l, "info"));
} }
} }
// ── Error field (always live) ───────────────────────────────────────────── // Error field (always live)
if (errorText) { if (errorText) {
state.sawErrorLog = true; state.sawErrorLog = true;
const decodedError = safeDecode(errorText); const decodedError = safeDecode(errorText);
core.error(decodedError); outputMultiline(decodedError, "error");
if (detectTraceback(decodedError)) { if (detectTraceback(decodedError)) {
state.sawTraceback = true; state.sawTraceback = true;
} }
} }
} }
// ────────────────────────────────────────────────────────────────────────────
// Tick polling
// ────────────────────────────────────────────────────────────────────────────
/** /**
* Sleeps for the given number of milliseconds. * Sleeps for the given number of milliseconds.
* *
@@ -262,10 +271,6 @@ export async function pollUntilDone(
return elapsed; return elapsed;
} }
// ────────────────────────────────────────────────────────────────────────────
// Main orchestrator
// ────────────────────────────────────────────────────────────────────────────
/** /**
* @typedef {Object} MonitorOptions * @typedef {Object} MonitorOptions
* @property {number} monitor - Number of game ticks to collect. * @property {number} monitor - Number of game ticks to collect.
@@ -325,10 +330,10 @@ export async function monitorConsole(api, opts) {
}; };
let lastProgressTick = 0; let lastProgressTick = 0;
// ── Step 1: record starting tick ───────────────────────────────────────── // Step 1: record starting tick
const { time: startTick } = await api.time(shard); const { time: startTick } = await api.time(shard);
// ── Step 2: connect socket + subscribe ─────────────────────────────────── // Step 2: connect socket + subscribe
await api.socket.connect(); await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => { await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state); handleConsoleEvent(event, opts, stdoutBuffer, state);
@@ -340,7 +345,7 @@ export async function monitorConsole(api, opts) {
"...", "...",
); );
// ── Step 3 & 4: tick-poll loop ─────────────────────────────────────────── // Step 3 & 4: tick-poll loop
try { try {
await pollUntilDone( await pollUntilDone(
api, api,
@@ -368,13 +373,16 @@ export async function monitorConsole(api, opts) {
}, },
); );
} finally { } finally {
// ── Step 5: always disconnect cleanly ──────────────────────────────── // Step 5: always disconnect cleanly
api.socket.disconnect(); api.socket.disconnect();
} }
// ── Step 6: artifact upload ─────────────────────────────────────────────── // Step 6: artifact upload
if (logToFile && stdoutBuffer.length > 0) { if (logToFile && stdoutBuffer.length > 0) {
const tmpFile = path.join(os.tmpdir(), "screeps_console_log.txt"); const tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "screeps-monitor-"),
);
const tmpFile = path.join(tmpDir, "screeps_console_log.txt");
await writeLogFile(stdoutBuffer, tmpFile); await writeLogFile(stdoutBuffer, tmpFile);
await uploadLogArtifact(tmpFile); await uploadLogArtifact(tmpFile);
} else if (logToFile) { } else if (logToFile) {
+2 -2
View File
@@ -1,7 +1,7 @@
{ {
"name": "screeps-deploy-action", "name": "screeps-deploy-action",
"version": "0.2.0", "version": "0.2.1",
"description": "Deploys screeps code to the official game or an pirvate server.", "description": "Deploys screeps code to the official game or a private server.",
"type": "module", "type": "module",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {