Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
dialog,
ipcMain,
logger,
powerMonitor,
registerNativeHandlers,
shell,
} from "./platform.js";
Expand All @@ -25,6 +26,8 @@ import {
foundationModelsConnection,
} from "./services/foundation-models-connection.js";
import { configStore } from "./services/config-store.js";
import { reloadPortableConfig } from "./services/portable-config.js";
import { createPortableConfigWatcher } from "./services/portable-config-watch-core.js";
import {
normalizeAppearanceConfig,
type DockIconPreference,
Expand Down Expand Up @@ -566,6 +569,15 @@ if (!ownsSingleInstanceLock) {

app.on("will-quit", cleanupApplication);

// Only a real content change reaches the renderer, and concurrent triggers
// coalesce, which is what makes this affordable on every focus.
const portableConfigWatcher = createPortableConfigWatcher(
reloadPortableConfig,
() => ipcMain.broadcast("app:config-externally-changed", {}),
(error: unknown) =>
logger.warn("portable-config", "Failed to re-read the portable config", error),
);

app
.whenReady()
.then(async () => {
Expand All @@ -589,6 +601,12 @@ if (!ownsSingleInstanceLock) {
void applyShortcutFromSettings();
void foundationModelsConnection.status();

// ~/.aiden/config.json is the user's to edit, so pick hand-edits up
// without a restart. Registered after whenReady because powerMonitor is
// only usable once the app is ready.
app.on("browser-window-focus", () => void portableConfigWatcher.refresh());
powerMonitor.on("resume", () => void portableConfigWatcher.refresh());

await createMainWindow();
await scheduleService.start();
appUpdateService.start();
Expand Down
2 changes: 2 additions & 0 deletions main/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ipcMain as electronIpcMain,
nativeImage,
nativeTheme,
powerMonitor,
safeStorage,
screen,
shell,
Expand Down Expand Up @@ -119,6 +120,7 @@ export {
dialog,
globalShortcut,
nativeImage,
powerMonitor,
safeStorage,
screen,
shell,
Expand Down
44 changes: 44 additions & 0 deletions main/services/aiden-config-dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert from "node:assert/strict";
import * as os from "node:os";
import * as path from "node:path";
import test from "node:test";
import { AIDEN_CONFIG_DIR_ENV, AIDEN_DIR_NAME, aidenConfigDir } from "./aiden-config-dir.js";

test("defaults to ~/.aiden when no override is set", () => {
assert.equal(aidenConfigDir({}), path.join(os.homedir(), AIDEN_DIR_NAME));
});

test("an absolute override replaces the default root", () => {
const override = path.join(path.sep, "srv", "aiden-config");
assert.equal(aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: override }), override);
});

test("an override is normalized so a trailing slash cannot fork the path", () => {
const override = path.join(path.sep, "srv", "aiden-config");
assert.equal(aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: `${override}${path.sep}` }), override);
assert.equal(
aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: path.join(override, "nested", "..") }),
override,
);
});

test("surrounding whitespace does not defeat the override", () => {
const override = path.join(path.sep, "srv", "aiden-config");
assert.equal(aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: ` ${override} ` }), override);
});

test("an empty or whitespace-only override falls back to the default", () => {
const expected = path.join(os.homedir(), AIDEN_DIR_NAME);
assert.equal(aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: "" }), expected);
assert.equal(aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: " " }), expected);
});

// A packaged app's working directory is not predictable from outside, so a
// relative override must fail loudly rather than scatter config folders.
test("a relative override is rejected instead of resolved against cwd", () => {
assert.throws(
() => aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: "relative/aiden" }),
/must be an absolute path/i,
);
assert.throws(() => aidenConfigDir({ [AIDEN_CONFIG_DIR_ENV]: "./aiden" }), /absolute/i);
});
38 changes: 38 additions & 0 deletions main/services/aiden-config-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Resolves Aiden's portable user config directory, `~/.aiden`.
//
// This folder belongs to the user and is meant to be edited by hand. It holds
// `config.json` (portable providers, MCP servers, and skills) alongside the
// `skill/`, `skills/`, and `scripts/` roots that skills-discovery.ts and
// schedule-script.ts already read. Machine-local state — secrets, discovery
// caches, chats, and UI preferences — stays in Electron's userData directory.

import * as os from "os";
import * as path from "path";

/** Redirects the portable config root. Used by tests and sandboxed dev runs. */
export const AIDEN_CONFIG_DIR_ENV = "AIDEN_CONFIG_DIR";

/** Basename of the portable root inside a home or workspace directory. */
export const AIDEN_DIR_NAME = ".aiden";

/**
* The portable config root: `$AIDEN_CONFIG_DIR`, else `~/.aiden`.
*
* A relative override is rejected rather than resolved against `process.cwd()` —
* the working directory of a packaged Electron app is not something the person
* setting the variable can predict, so silently accepting one would scatter
* config folders wherever the app happened to launch from.
*/
export function aidenConfigDir(env: NodeJS.ProcessEnv = process.env): string {
const override = env[AIDEN_CONFIG_DIR_ENV]?.trim();
if (!override) return path.join(os.homedir(), AIDEN_DIR_NAME);
if (!path.isAbsolute(override)) {
throw new Error(
`${AIDEN_CONFIG_DIR_ENV} must be an absolute path; received ${JSON.stringify(override)}.`,
);
}
// Already known absolute, so resolve() cannot reach for cwd. It is used over
// normalize() because it also drops a trailing separator, which would
// otherwise make "/x" and "/x/" two distinct cache and comparison keys.
return path.resolve(override);
}
Loading