fix: repair broken IPC wiring and cut the cost of repository polling

Three handlers referenced a dependency they were never given, which made them
throw a ReferenceError as soon as they ran:

- deployment:preflight for Gitea Actions profiles (`preflight` was passed to
  registerOperationsIpc but not to registerDeploymentIpc)
- Unraid write-access repair (`safeRelativeRemoteFile` was missing from
  createUnraidAccessMethods)
- a dead reference of the same name in unraid-state-methods

no-undef and no-unused-vars were disabled for every file, which is why none of
these were caught. Both are now enabled for src/main and src/shared, where the
dependency graph is explicit. The renderer keeps them off because its functions
are deliberately cross-script globals.

Performance:

- git.status() spawned three processes (rev-parse, status, remote get-url) per
  call. A directory holding its own .git is by definition the work tree root, so
  rev-parse is unnecessary, and the remote URL is cached against the mtime of
  .git/config, including the failure for a repository without that remote.
- git status runs with --no-optional-locks so a read no longer rewrites the
  index. That stops it fighting a concurrent Git command for the index lock, and
  is what makes filesystem watching viable at all.
- One commit issued four `git status` reads; callers that already hold the
  status now pass it on, leaving two.
- The repository monitor is event driven. A watched repository is read on
  filesystem activity, with a 30s safety net for watchers that stop delivering
  and a 1s floor so a busy tree cannot drive a read per event. Repositories that
  cannot be watched keep using the interval. Idle cost for one repository over
  35s: 24 git processes before, 3 after.
- Resolving one repository by name no longer refreshes the whole workspace.
- Concurrent configuration saves share a single write of the latest state.
- Repository discovery follows directory junctions again. The filter that
  skipped them made the realpath cycle guard dead code, and hid any project
  folder reached through a junction.

Renderer:

- render() replaced the whole shell on every poll, discarding focus, caret and
  scroll position while the user was typing. Those are preserved now, and an
  unchanged render leaves the DOM alone entirely.
- The four sections that enhanceRenderedUi() injected after render moved into
  the views, so the rendered markup is the single source of truth.
- The monitor no longer keeps a repository paused forever when it is unlinked
  mid-mutation, scheduleAutoRefresh honours its delay argument, the demo bridges
  no longer block startup, and #app is no longer an aria-live region announcing
  the entire UI on every render.

IPC channel plumbing moved to src/main/ipc/channel.cjs, replacing a module-level
mutable diagnostics singleton with an argument.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
NuklearRabbit
2026-08-23 14:31:58 +02:00
co-authored by Claude Opus 5
parent cf1da8a2fa
commit 9260d35957
21 changed files with 858 additions and 147 deletions
+14 -68
View File
@@ -1,13 +1,17 @@
"use strict";
const path = require("node:path");
const fs = require("node:fs/promises");
const { fileURLToPath } = require("node:url");
const { ipcMain, dialog, shell, app } = require("electron");
const { dialog, shell, app } = require("electron");
const { matchRemoteToRepository } = require("../shared/repository-match.cjs");
const {
cloneDirectoryName,
resolveCloneTarget,
} = require("../shared/clone-target.cjs");
const {
createChannelRegistrar,
assertTrustedSender,
toErrorPayload,
} = require("./ipc/channel.cjs");
const { registerRepositoryIpc } = require("./ipc/repository-handlers.cjs");
const { registerDeploymentIpc } = require("./ipc/deployment-handlers.cjs");
const { registerOperationsIpc } = require("./ipc/operations-handlers.cjs");
@@ -16,66 +20,6 @@ const {
readEncryptedBackup,
} = require("./configuration-backup.cjs");
const { evaluateDeploymentPolicy } = require("../shared/deployment-policy.cjs");
let diagnosticsService = null;
const TRUSTED_RENDERER_PATH = path.resolve(
__dirname,
"..",
"renderer",
"index.html",
);
function toErrorPayload(error) {
return {
message: error?.message || "Unknown error",
code: error?.code || null,
status: error?.status || null,
recoverable: Boolean(error?.recoverable),
commitSha: error?.commitSha || null,
};
}
function assertTrustedSender(event) {
const url = event?.senderFrame?.url || event?.sender?.getURL?.() || "";
try {
const parsed = new URL(url);
if (parsed.protocol !== "file:") throw new Error("not a file URL");
const senderPath = path.resolve(fileURLToPath(parsed));
const normalize = (value) =>
process.platform === "win32" ? value.toLowerCase() : value;
if (normalize(senderPath) !== normalize(TRUSTED_RENDERER_PATH))
throw new Error("unexpected renderer file");
} catch {
throw new Error("Rejected IPC request from an untrusted renderer origin.");
}
}
function register(channel, handler) {
ipcMain.handle(channel, async (event, payload) => {
const started = Date.now();
try {
assertTrustedSender(event);
const data = await handler(payload || {}, event);
await diagnosticsService?.debug("ipc.completed", {
channel,
durationMs: Date.now() - started,
});
return { ok: true, data };
} catch (error) {
await diagnosticsService?.error("ipc.failed", {
channel,
durationMs: Date.now() - started,
error: {
name: error?.name,
message: error?.message,
code: error?.code,
status: error?.status,
stack: error?.stack,
},
});
return { ok: false, error: toErrorPayload(error) };
}
});
}
function registerIpc({
store,
git,
@@ -95,7 +39,7 @@ function registerIpc({
monitor,
onPreferencesChanged,
}) {
diagnosticsService = diagnostics;
const register = createChannelRegistrar(diagnostics);
const repositoryMutations = new Map();
const withRepositoryPause = async (localPath, action) => {
monitor?.pause(localPath);
@@ -161,8 +105,11 @@ function registerIpc({
await repositories.refresh();
knownPaths = repositories.getWatchPaths();
}
const canonicalKnown = await Promise.all(knownPaths.map(canonicalPath));
if (!canonicalKnown.some((known) => known === candidate))
// Watch paths are already canonical, so re-resolving all of them on every
// guarded call is only needed when the cheap comparison finds no match.
const matched = knownPaths.some((known) => path.resolve(known) === candidate)
|| (await Promise.all(knownPaths.map(canonicalPath))).some((known) => known === candidate);
if (!matched)
throw new Error(
"The requested local repository is not linked or discovered by ForgeFlow.",
);
@@ -172,9 +119,7 @@ function registerIpc({
const resolveRepository = async (repositoryPayload) => {
const fullName = String(repositoryPayload?.fullName || "").trim();
if (!fullName) throw new Error("Repository identity is required.");
const current = (await repositories.refresh()).find(
(item) => item.fullName === fullName,
);
const current = await repositories.resolveByFullName(fullName);
if (!current)
throw new Error(
"The repository is no longer available through the configured Gitea account.",
@@ -732,6 +677,7 @@ function registerIpc({
registerDeploymentIpc({
register, store, resolveRepository, unraid, deployments, evaluateDeploymentPolicy,
audit, deployKeys, repositories, inventoryReviews, diagnostics, git, gitea, ssh,
preflight,
});
registerOperationsIpc({
register, store, unraid, deployments, diagnostics, shell, dialog, path, app,