fix(monitor): use global console channel and implement shard filtering (#87)
Lint / pre-commit Linting (push) Successful in 47s
Test / Run Tests (push) Successful in 1m3s

This PR fixes the issue where console monitoring was empty when a specific shard was targeted on the official Screeps server.

### Changes:
- **Unified Subscription**: Changed WebSocket subscription path from `shard/console` (invalid) to the global `console` channel.
- **Shard Filtering**: Implemented client-side filtering in `handleConsoleEvent` to only display logs matching the targeted shard.
- **Precise Timing**: Retained the shard-specific `api.time()` call for accurate tick duration tracking.
- **Tests**: Added and updated 48 unit tests verifying the fix and shard filtering logic.
- **Distribution**: Rebuilt `dist/index.js` and bumped version to `v0.2.1`.

Reviewed-on: #87
This commit was merged in pull request #87.
This commit is contained in:
2026-05-16 22:29:24 +02:00
parent bf52580bf3
commit 076e96f3de
5 changed files with 263 additions and 1972 deletions
+61 -36
View File
@@ -1,5 +1,5 @@
import * as core from "@actions/core";
import { DefaultArtifactClient } from "@actions/artifact";
import { create } from "@actions/artifact";
import fs from "fs";
import path from "path";
import os from "os";
@@ -29,8 +29,9 @@ 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";
}
/**
@@ -87,17 +88,21 @@ export function safeDecode(text) {
/**
* Outputs text to the action log, splitting by newline to ensure proper display.
* Optionally prepends a [shard] prefix.
*
* @param {string} text
* @param {"info"|"warning"|"error"} level
* @param {string} [shard]
*/
export function outputMultiline(text, level = "info") {
export function outputMultiline(text, level = "info", shard = null) {
if (!text) return;
const prefix = shard ? `[${shard}] ` : "";
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);
const formattedLine = `${prefix}${line}`;
if (level === "error") core.error(formattedLine);
else if (level === "warning") core.warning(formattedLine);
else core.info(formattedLine);
});
}
@@ -124,29 +129,29 @@ export async function writeLogFile(lines, filePath) {
}
/**
* Uploads a file as a named workflow artifact using @actions/artifact.
* Uploads one or more files as a named workflow artifact using @actions/artifact.
* Degrades gracefully to core.warning() if the runner does not support the
* artifact service (e.g. a bare self-hosted runner without the service configured).
* artifact service.
*
* @param {string} filePath - Absolute path of the file to upload
* @param {string} [artifactName="screeps-console-log"] - Artifact display name
* @param {string[]} filePaths - Absolute paths of the files to upload
* @param {string} [artifactName="screeps-console-log"] - Artifact display name
* @returns {Promise<void>}
*/
export async function uploadLogArtifact(
filePath,
export async function uploadLogArtifacts(
filePaths,
artifactName = "screeps-console-log",
) {
if (!filePaths || filePaths.length === 0) return;
try {
const client = new DefaultArtifactClient();
await client.uploadArtifact(
artifactName,
[filePath],
path.dirname(filePath),
);
core.info(`[Monitor] Console log uploaded as artifact '${artifactName}'.`);
const client = create();
const rootDir = path.dirname(filePaths[0]);
await client.uploadArtifact(artifactName, filePaths, rootDir, {
continueOnError: true,
});
core.info(`[Monitor] Console logs uploaded as artifact '${artifactName}'.`);
} catch (err) {
core.warning(
`[Monitor] Could not upload console log as artifact: ${err.message}`,
`[Monitor] Could not upload console logs as artifact: ${err.message}`,
);
}
}
@@ -172,14 +177,21 @@ export async function uploadLogArtifact(
* - All stdout lines → core.info() when logToFile=false,
* pushed to stdoutBuffer when logToFile=true.
*
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string } }} event
* @param {{ logToFile: boolean }} opts
* @param {string[]} stdoutBuffer - Mutable buffer used in logToFile mode
* @param {{ data?: { messages?: { log?: string[], results?: string[] }, error?: string, shard?: string } }} event
* @param {{ logToFile: boolean, shard?: string }} opts
* @param {Record<string, string[]>} shardBuffers - Mutable buffer Map used in logToFile mode
* @param {{ sawTraceback: boolean, sawErrorLog: boolean, sawWarningLog: boolean }} state
*/
export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const { logToFile } = opts;
export function handleConsoleEvent(event, opts, shardBuffers, state) {
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;
@@ -190,7 +202,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const warnPattern = /<font\s+color=['"](?:orange|yellow)['"]/i;
logLines
.filter((l) => warnPattern.test(l))
.forEach((l) => outputMultiline(safeDecode(l), "warning"));
.forEach((l) => outputMultiline(safeDecode(l), "warning", data.shard));
}
// Traceback detection in log lines (Screeps sometimes sends errors here)
@@ -203,9 +215,11 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
const allStdout = [...logLines, ...results].map(safeDecode);
if (allStdout.length > 0) {
if (logToFile) {
stdoutBuffer.push(...allStdout);
const shard = data.shard || "default";
if (!shardBuffers[shard]) shardBuffers[shard] = [];
shardBuffers[shard].push(...allStdout);
} else {
allStdout.forEach((l) => outputMultiline(l, "info"));
allStdout.forEach((l) => outputMultiline(l, "info", data.shard));
}
}
@@ -213,7 +227,7 @@ export function handleConsoleEvent(event, opts, stdoutBuffer, state) {
if (errorText) {
state.sawErrorLog = true;
const decodedError = safeDecode(errorText);
outputMultiline(decodedError, "error");
outputMultiline(decodedError, "error", data.shard);
if (detectTraceback(decodedError)) {
state.sawTraceback = true;
}
@@ -314,7 +328,7 @@ export async function monitorConsole(api, opts) {
const subscribePath = buildSubscribePath(hostname, providedShard);
// Shared mutable state — updated by handleConsoleEvent via event listener
const stdoutBuffer = [];
const shardBuffers = {}; // { [shardName]: string[] }
const state = {
sawTraceback: false,
sawErrorLog: false,
@@ -328,7 +342,7 @@ export async function monitorConsole(api, opts) {
// Step 2: connect socket + subscribe
await api.socket.connect();
await api.socket.subscribe(subscribePath, (event) => {
handleConsoleEvent(event, opts, stdoutBuffer, state);
handleConsoleEvent(event, opts, shardBuffers, state);
});
core.info(
@@ -370,13 +384,24 @@ export async function monitorConsole(api, opts) {
}
// Step 6: artifact upload
if (logToFile && stdoutBuffer.length > 0) {
const shardKeys = Object.keys(shardBuffers);
if (logToFile && shardKeys.length > 0) {
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);
const filePaths = [];
for (const sName of shardKeys) {
const fileName =
sName === "default"
? "screeps_console_log.txt"
: `${sName}_console_log.txt`;
const tmpFile = path.join(tmpDir, fileName);
await writeLogFile(shardBuffers[sName], tmpFile);
filePaths.push(tmpFile);
}
await uploadLogArtifacts(filePaths);
} else if (logToFile) {
core.info(
"[Monitor] No stdout lines were collected; skipping artifact upload.",