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", () => {
it("returns shard0/console for official server (no shard provided)", () => {
expect(buildSubscribePath("screeps.com")).toBe("shard0/console");
it("returns console for official server (no shard provided)", () => {
expect(buildSubscribePath("screeps.com")).toBe("console");
});
it("returns console for private server (no shard provided)", () => {
expect(buildSubscribePath("builder64")).toBe("console");
});
it("returns <shard>/console when shard is provided (official)", () => {
expect(buildSubscribePath("screeps.com", "shard3")).toBe("shard3/console");
it("returns console when shard is provided (official)", () => {
expect(buildSubscribePath("screeps.com", "shard3")).toBe("console");
});
it("returns <shard>/console when shard is provided (private)", () => {
expect(buildSubscribePath("builder64", "myshard")).toBe("myshard/console");
it("returns console when shard is provided (private)", () => {
expect(buildSubscribePath("builder64", "myshard")).toBe("console");
});
});
@@ -228,6 +228,21 @@ describe("handleConsoleEvent", () => {
handleConsoleEvent(event, { logToFile: false }, stdoutBuffer, state),
).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({
hostname: "screeps.com",
ticks: [100, 101, 102, 103],
});
await monitorConsole(api, { ...BASE_OPTS, hostname: "screeps.com" });
expect(api.socket.subscribe).toHaveBeenCalledWith(
"shard0/console",
"console",
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({
hostname: "screeps.com",
ticks: [100, 101, 102, 103],
@@ -380,10 +395,10 @@ describe("monitorConsole", () => {
shard: "shard3",
});
expect(api.socket.subscribe).toHaveBeenCalledWith(
"shard3/console",
"console",
expect.any(Function),
);
// Verify polling also uses shard3
// Verify polling still uses shard3 for timing
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);
if (monitorTicks > 0) {
const result = await monitorConsole(api, {
+56 -48
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.
@@ -33,14 +29,11 @@ export function isOfficialServer(hostname) {
* @returns {string}
*/
export function buildSubscribePath(hostname, shard) {
if (shard) return `${shard}/console`;
return isOfficialServer(hostname) ? "shard0/console" : "console";
// The console channel on Screeps official and most private servers is '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.
* Screeps places runtime errors (including stack traces) in event.data.error.
@@ -76,17 +69,38 @@ export function detectWarning(logLines) {
* @returns {string}
*/
export function safeDecode(text) {
if (typeof text !== "string" || !text.includes("%")) return text;
try {
return decodeURIComponent(text);
} catch (err) {
return text;
if (typeof text !== "string") return text;
let result = text;
if (result.includes("%")) {
try {
result = decodeURIComponent(result);
} catch (err) {
// 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().
@@ -99,10 +113,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.
*
@@ -142,10 +152,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.
@@ -173,52 +179,55 @@ export async function uploadLogArtifact(
* @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state
*/
export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const { logToFile } = opts;
const { logToFile, shard: targetShard } = opts;
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 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) => 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))) {
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) => core.info(l));
allStdout.forEach((l) => outputMultiline(l, "info"));
}
}
// ── Error field (always live) ─────────────────────────────────────────────
// Error field (always live)
if (errorText) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
core.error(decodedError);
outputMultiline(decodedError, "error");
if (detectTraceback(decodedError)) {
state.sawTraceback = true;
}
}
}
// ────────────────────────────────────────────────────────────────────────────
// Tick polling
// ────────────────────────────────────────────────────────────────────────────
/**
* Sleeps for the given number of milliseconds.
*
@@ -262,10 +271,6 @@ export async function pollUntilDone(
return elapsed;
}
// ────────────────────────────────────────────────────────────────────────────
// Main orchestrator
// ────────────────────────────────────────────────────────────────────────────
/**
* @typedef {Object} MonitorOptions
* @property {number} monitor - Number of game ticks to collect.
@@ -325,10 +330,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);
@@ -340,7 +345,7 @@ export async function monitorConsole(api, opts) {
"...",
);
// ── Step 3 & 4: tick-poll loop ───────────────────────────────────────────
// Step 3 & 4: tick-poll loop
try {
await pollUntilDone(
api,
@@ -368,13 +373,16 @@ 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");
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 uploadLogArtifact(tmpFile);
} else if (logToFile) {
+2 -2
View File
@@ -1,7 +1,7 @@
{
"name": "screeps-deploy-action",
"version": "0.2.0",
"description": "Deploys screeps code to the official game or an pirvate server.",
"version": "0.2.1",
"description": "Deploys screeps code to the official game or a private server.",
"type": "module",
"main": "index.js",
"scripts": {