mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-16 11:19:33 +01:00
Merge pull request #719: fix(settings): serialize settings.json writes so a quick-start cannot tear the file
Conflict in src/web/routes/system-routes.ts (PUT /api/settings) resolved by keeping both sides: #718's line that never stores an unauthorized tunnelEnabled:true runs first, then #719's serialized mutateSettingsFile() write replaces the inline read and plain writeFile. test/routes/system-routes-tunnel-guard.test.ts: #718's new tests asserted plain writeFile calls on settings.json. With #719 every write goes to a temp file renamed over it, so the assertions read writes through a settingsWrites() helper (writeFile contents keyed by their rename), and the concurrent-stop harness applies a write and the stop at the rename. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
82ef8b1edd
20 files changed
+532
-131
No files matched your search
@@ -302,7 +302,7 @@ One module per domain in `src/web/routes/` (plus a barrel; `ls src/web/routes/`
|
||||
- **API endpoint**: Types in `src/types/` domain file, route in `src/web/routes/*-routes.ts`. Return the `ApiResponse` envelope (`{ success: true, data }`; errors via `createErrorResponse()` with proper status code). Validate with Zod schemas in `schemas.ts`.
|
||||
- **SSE event**: Add to `src/web/sse-events.ts` + `SSE_EVENTS` in `constants.js`, emit via `broadcast()`, handle in `app.js` (`addListener(`)
|
||||
- **Session setting**: Add to `SessionState`, include in `session.toState()`, call `persistSessionState()`
|
||||
- **App setting**: decide per-device vs synced first. Per-device keys go in the `displayKeys` set in settings-ui.js and must NOT be added to `SettingsUpdateSchema` (it is `.strict()`). ⚠️ Anything in `PUT /api/settings` that acts on a setting (the `toggleService` watcher calls) must resolve from **`merged`** (persisted + incoming), never from the raw request body: a partial PUT omits keys it doesn't intend to change, and `body.x ?? default` turns every omission into "apply the default" and silently resets live services. Pinned by `test/routes/system-routes-settings-partial-put.test.ts`. ⚠️ A synced setting is admin-only to write in multi-user mode (the PUT 403s a non-admin, whose browser does not send it): a new route that writes `settings.json` takes the same `isMultiUserMode() && !requireAdmin(req, reply)` gate.
|
||||
- **App setting**: decide per-device vs synced first. Per-device keys go in the `displayKeys` set in settings-ui.js and must NOT be added to `SettingsUpdateSchema` (it is `.strict()`). ⚠️ Anything in `PUT /api/settings` that acts on a setting (the `toggleService` watcher calls) must resolve from **`merged`** (persisted + incoming), never from the raw request body: a partial PUT omits keys it doesn't intend to change, and `body.x ?? default` turns every omission into "apply the default" and silently resets live services. Pinned by `test/routes/system-routes-settings-partial-put.test.ts`. ⚠️ A synced setting is admin-only to write in multi-user mode (the PUT 403s a non-admin, whose browser does not send it): a new route that writes `settings.json` takes the same `isMultiUserMode() && !requireAdmin(req, reply)` gate. ⚠️ Every write to the server's `settings.json` goes through `mutateSettingsFile()` (`src/web/settings-writer.ts`: one at a time, tmp+rename, and a file that does not parse is refused, never read as `{}`), and a route awaits it before answering: two unserialized plain `writeFile`s tore the file, and the `{}` fallback then erased every other setting. The one direct writer is config-backup restore (`restoreConfigBackup()`, as for `clis.json`): it must work on a file that does not parse, which the writer refuses. Pinned by `test/settings-json-write-serialization.test.ts`.
|
||||
- **Hook event**: Add to `HookEventType`, add hook in `hooks-config.ts:generateHooksConfig()`, update `HookEventSchema`
|
||||
- **Mobile feature**: Add to relevant singleton, guard with `MobileDetection.isMobile()`. New header buttons must stay off phones (`test/mobile-header-buttons-policy.test.ts`).
|
||||
- **New test**: a test that constructs `WebServer` itself binds port 0 (`new WebServer(0, …)`, then `server.boundPort`), and a raw `http`/`net`/Fastify/`ws` server listens on `0` and reads `address().port`; mobile tests get theirs from `createTestServer()` and read `boundPort` too. `test/test-ports-guard.test.ts` refuses a fixed port in either. Route tests use `app.inject()` (no port needed); see `test/routes/_route-test-utils.ts`.
|
||||
|
||||
@@ -1213,7 +1213,7 @@ A local case whose folder is the top of a git repository can keep **worktrees**
|
||||
|
||||
## Settings
|
||||
|
||||
`~/.codeman/settings.json` is one file for the whole server: the synced App Settings (feature switches, the plan-usage collection switch `showPlanUsageLimits`, `runMode`, `respawnPresets`, `tunnelEnabled`, ...), plus `modelConfig`, `caseOrder` and `lastUsedCase`. Reads are open to every signed-in user; in multi-user mode a non-admin reads it without what is the admin's (`voiceSettings.apiKey`, the Deepgram key an admin saved, and the admin's case names in `caseOrder` and `lastUsedCase`). **In multi-user mode every write is admin only:** for a non-admin the routes below answer `403 FORBIDDEN` before the body is even validated, so nothing is written, no watcher is toggled and no tunnel is started. Single-user mode is unaffected.
|
||||
`~/.codeman/settings.json` is one file for the whole server: the synced App Settings (feature switches, the plan-usage collection switch `showPlanUsageLimits`, `runMode`, `respawnPresets`, `tunnelEnabled`, ...), plus `modelConfig`, `caseOrder` and `lastUsedCase`. Reads are open to every signed-in user; in multi-user mode a non-admin reads it without what is the admin's (`voiceSettings.apiKey`, the Deepgram key an admin saved, and the admin's case names in `caseOrder` and `lastUsedCase`). **In multi-user mode every write is admin only:** for a non-admin the routes below answer `403 FORBIDDEN` before the body is even validated, so nothing is written, no watcher is toggled and no tunnel is started. Single-user mode is unaffected. Every write to the file (these routes, and the `lastUsedCase` that `POST /api/quick-start` and `POST /api/ralph-loop/start` record before they answer) runs one at a time and replaces the file atomically (mode `0600`); the one write outside that queue is a config-backup restore, which must work on a file that does not parse. A write to a `settings.json` that exists but is not a JSON object is refused rather than starting from `{}`, which would erase every other setting: the routes below answer `409 CONFLICT` with a message naming the fix and leave the file untouched, the two starts skip their `lastUsedCase`, and an admin's `POST /api/cases/:name/rename` answers the same `409 CONFLICT`, checked before it ends any session or moves the folder. A missing or empty file still starts from `{}`.
|
||||
|
||||
| Method | Path | Body | Notes |
|
||||
| ------ | ----------------------------- | ------------------------------------ | ----- |
|
||||
|
||||
@@ -750,7 +750,7 @@ Matching semantics: a query containing `/` matches the relative path, otherwise
|
||||
|
||||
⚠️ **The origin is a random id in `<data dir>/config-backup-id`, never the hostname.** Instances that share a folder (two machines on one synced folder, prod and a beta) must keep to their own snapshots, so the change check, list, prune, Compare and restore are all scoped to it; without that, each instance snapshotted on every tick and Restore offered the other machine's `users.json` and `.env`. The hostname was tried first and was wrong: a Docker Compose container gets a new one on every recreate, which unlisted every earlier snapshot, and since prune is origin-scoped they were never deleted either. The id is not the hook secret and is never tracked or restored. Listing never creates it (`readConfigBackupOrigin()`; only a pass or a restore does). A fresh or reinstalled data dir is a new owner; copying an `origin` from an old manifest into the file makes those snapshots visible again.
|
||||
|
||||
⚠️ **Retention survives a bad `settings.json`.** Each manifest records the retention in force. `runConfigBackup()` reads `settings.json` itself, because `readJsonConfig` answers `{}` for any failure, including a read that lands inside the non-atomic write of `PUT /api/settings`: a file that does not parse still gets its snapshot but prunes nothing, and a retention key ABSENT from the file (a reset) means what the newest manifest that recorded one holds (`withRecordedRetention()`, per key, in the folder the file names, so re-entering a custom folder after a reset finds that folder's own history). `GET /api/config-backups` reports the same values the pass uses, the form fills from them, and Save sends a retention key only when it was saved before or edited there (`_configBackupRetentionPayload()`), so merely displaying the recorded retention never writes it back. Restoring `settings.json` keeps the four `configBackup*` keys at their live values (an old `configBackupDir` would point the list at another folder and hide the undo copy just made), except a retention key the live file lacks, which keeps the backup's. While `settings.json` does not parse a pass reads it as `{}`, so its snapshot goes to the default folder; nothing is widened.
|
||||
⚠️ **Retention survives a bad `settings.json`.** Each manifest records the retention in force. `runConfigBackup()` reads `settings.json` itself, because `readJsonConfig` answers `{}` for any failure, including a file that does not parse: a file that does not parse still gets its snapshot but prunes nothing, and a retention key ABSENT from the file (a reset) means what the newest manifest that recorded one holds (`withRecordedRetention()`, per key, in the folder the file names, so re-entering a custom folder after a reset finds that folder's own history). `GET /api/config-backups` reports the same values the pass uses, the form fills from them, and Save sends a retention key only when it was saved before or edited there (`_configBackupRetentionPayload()`), so merely displaying the recorded retention never writes it back. Restoring `settings.json` keeps the four `configBackup*` keys at their live values (an old `configBackupDir` would point the list at another folder and hide the undo copy just made), except a retention key the live file lacks, which keeps the backup's. While `settings.json` does not parse a pass reads it as `{}`, so its snapshot goes to the default folder; nothing is widened.
|
||||
|
||||
⚠️ **A custom folder is probed BOUNDED before anything reads it** (#516: it may be a network mount that stopped answering, and the GET runs every time App Settings opens). `probeBackupDir()` runs `probePathKind(dir, { pastCap: true })` before every pass and in every route, and `readEffectiveBackupSettings()` probes BEFORE the recorded-retention fallback lists the folder (that fallback once did a `readdirSync` first whenever only `configBackupDir` was set). `unknown` answers 503 `OPERATION_FAILED` through `describeUnknownPath()` and skips the pass; a missing custom folder is created only when its parent is present. The default folder needs no probe. `test/routes/config-backup-unreachable.test.ts` pins all four routes with a recording `node:fs` mock.
|
||||
|
||||
|
||||
+3
-2
@@ -1158,8 +1158,9 @@ export async function readKeepWarmSettings(): Promise<KeepWarmSettings> {
|
||||
/**
|
||||
* The same two keys for the keep-warm service, which keeps what it reads until the next
|
||||
* settings change. A missing file is the defaults, but any other read or parse failure rejects
|
||||
* rather than falling back: settings.json is rewritten in place, so a read can land mid-write,
|
||||
* and a fallback kept from it would hold keep-warm off until the next keep-warm settings change.
|
||||
* rather than falling back: settings.json is replaced atomically, so such a failure is a real
|
||||
* read error or a file that does not parse (a hand edit), never a write in progress, and a
|
||||
* fallback kept from it would hold keep-warm off until the next keep-warm settings change.
|
||||
*/
|
||||
export async function readKeepWarmSettingsOrThrow(): Promise<KeepWarmSettings> {
|
||||
let text: string;
|
||||
|
||||
@@ -338,9 +338,11 @@ export class PromptCacheKeepWarmService implements KeepWarmScheduler {
|
||||
try {
|
||||
settings = await this.readSettings();
|
||||
} catch (err) {
|
||||
// A read that lands mid-write is retried. A settings.json that stays broken (a hand edit
|
||||
// with a trailing comma) fails every read until it is fixed, so it is logged once rather
|
||||
// than once a minute per session, and only a session that could nudge retries.
|
||||
// A failed read is retried (settings.json is replaced atomically, so a read never sees a
|
||||
// half-written file; a failure is a read error or a file that does not parse). A settings.json
|
||||
// that stays broken (a hand edit with a trailing comma) fails every read until it is fixed, so
|
||||
// it is logged once rather than once a minute per session, and only a session that could
|
||||
// nudge retries.
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
if (message !== this.settingsFailure) {
|
||||
this.settingsFailure = message;
|
||||
|
||||
@@ -78,6 +78,12 @@ import {
|
||||
parseBody,
|
||||
readJsonConfig,
|
||||
} from '../route-helpers.js';
|
||||
import {
|
||||
assertSettingsWritable,
|
||||
mutateSettingsFile,
|
||||
SettingsWriteRefusedError,
|
||||
settingsWriteErrorResponse,
|
||||
} from '../settings-writer.js';
|
||||
import { isMultiUserMode } from '../../config/multiuser.js';
|
||||
import type { AuthUser } from '../../types.js';
|
||||
import { SseEvent } from '../sse-events.js';
|
||||
@@ -1849,6 +1855,17 @@ export function registerCaseRoutes(
|
||||
if (open.some((s) => !canAccessOwned(user, s.owner))) {
|
||||
return fail(403, ApiErrorCode.FORBIDDEN, "Another user's session has this case open");
|
||||
}
|
||||
// The admin's rename writes settings.json only after the sessions are ended and the folder moved, and
|
||||
// the writer refuses a file that is not a JSON object: check it by the writer's own rule now, so no
|
||||
// session is ended for a rename that cannot complete. Not between the starting check and the lock below.
|
||||
if (isAdmin(req)) {
|
||||
try {
|
||||
await assertSettingsWritable();
|
||||
} catch (err) {
|
||||
if (err instanceof SettingsWriteRefusedError) return fail(409, ApiErrorCode.CONFLICT, err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
// A session being created in the folder is not in ctx.sessions yet, so it could be neither listed nor
|
||||
// stopped: it would start in the old path after the move. Checked in the same tick that takes the lock
|
||||
// below (nothing is awaited in between), and the Session constructor refuses the folder from then on.
|
||||
@@ -1876,27 +1893,23 @@ export function registerCaseRoutes(
|
||||
}
|
||||
if (moveFolder) await moveCaseFolder(oldPath, newPath);
|
||||
const originalLinks = isLinked ? await readLinkedCases() : undefined;
|
||||
// The settings.json content before this rename wrote it, set only once a write is attempted (an
|
||||
// absent file reads as {}, which must not be written back as a file that never existed).
|
||||
let originalSettings: Record<string, unknown> | undefined;
|
||||
// The settings.json values this rename replaced, set only once it wrote them. A failure puts back
|
||||
// these keys alone, so a settings save that landed in between is kept.
|
||||
let settingsUndo: Record<string, unknown> | undefined;
|
||||
try {
|
||||
// settings.json is server-wide and caseOrder/lastUsedCase name the admin's cases, so only the
|
||||
// admin's rename touches it (a non-admin's own rename must not rewrite someone else's list).
|
||||
// Written before linked-cases.json so a failure here leaves the registry untouched.
|
||||
if (isAdmin(req)) {
|
||||
const existing = await readJsonConfig<Record<string, unknown>>(SETTINGS_PATH, 'settings.json', {});
|
||||
const order = renameInOrder(existing.caseOrder, name, newName);
|
||||
const last = existing.lastUsedCase === name ? newName : undefined;
|
||||
if (order || last) {
|
||||
originalSettings = existing;
|
||||
await fs.writeFile(
|
||||
SETTINGS_PATH,
|
||||
JSON.stringify(
|
||||
{ ...existing, ...(order ? { caseOrder: order } : {}), ...(last ? { lastUsedCase: last } : {}) },
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
const { before, after } = await mutateSettingsFile((existing) => {
|
||||
const order = renameInOrder(existing.caseOrder, name, newName);
|
||||
const last = existing.lastUsedCase === name ? newName : undefined;
|
||||
if (!order && !last) return undefined;
|
||||
return { ...existing, ...(order ? { caseOrder: order } : {}), ...(last ? { lastUsedCase: last } : {}) };
|
||||
});
|
||||
if (after) {
|
||||
const keys = ['caseOrder', 'lastUsedCase'].filter((k) => after[k] !== before[k]);
|
||||
settingsUndo = Object.fromEntries(keys.map((k) => [k, before[k]]));
|
||||
}
|
||||
}
|
||||
if (originalLinks) {
|
||||
@@ -1913,9 +1926,8 @@ export function registerCaseRoutes(
|
||||
} catch (err) {
|
||||
// The folder moved but the registries did not follow: put settings.json and the folder back
|
||||
// rather than leave a case whose name and folder disagree.
|
||||
if (originalSettings) {
|
||||
await fs.writeFile(SETTINGS_PATH, JSON.stringify(originalSettings, null, 2)).catch(() => {});
|
||||
}
|
||||
const undo = settingsUndo;
|
||||
if (undo) await mutateSettingsFile((current) => ({ ...current, ...undo })).catch(() => {});
|
||||
if (moveFolder) {
|
||||
const movedBack = await fs.rename(newPath, oldPath).then(
|
||||
() => true,
|
||||
@@ -1970,22 +1982,11 @@ export function registerCaseRoutes(
|
||||
const { order } = parseBody(CaseOrderSchema, req.body, 'Invalid order data');
|
||||
|
||||
try {
|
||||
const dir = getDataDir();
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
let existing: Record<string, unknown> = {};
|
||||
try {
|
||||
existing = JSON.parse(await fs.readFile(SETTINGS_PATH, 'utf-8'));
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
const merged = { ...existing, caseOrder: order };
|
||||
await fs.writeFile(SETTINGS_PATH, JSON.stringify(merged, null, 2));
|
||||
await mutateSettingsFile((current) => ({ ...current, caseOrder: order }));
|
||||
ctx.broadcast(SseEvent.CaseOrderChanged, { order });
|
||||
return { success: true, data: { order } };
|
||||
} catch (err) {
|
||||
return createErrorResponse(ApiErrorCode.OPERATION_FAILED, getErrorMessage(err));
|
||||
return settingsWriteErrorResponse(err);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -128,8 +128,8 @@ export async function runConfigBackup(
|
||||
reason: string,
|
||||
opts: { force?: boolean } = {}
|
||||
): Promise<{ result: ConfigBackupResult; removed: string[] } | null> {
|
||||
// Read settings.json directly: readJsonConfig answers {} for ANY failure, so a syntax error or a read
|
||||
// that lands inside PUT /api/settings's non-atomic write used to prune with the defaults (20 / 30
|
||||
// Read settings.json directly: readJsonConfig answers {} for ANY failure, so a syntax error (or, before
|
||||
// settings writes became atomic, a read that landed inside one) used to prune with the defaults (20 / 30
|
||||
// days) and drop the longer history the user asked to keep.
|
||||
const raw = await readSettingsForBackup();
|
||||
const unreadable = raw === 'unreadable';
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { join, dirname, resolve, relative, isAbsolute } from 'node:path';
|
||||
import { join, resolve, relative, isAbsolute } from 'node:path';
|
||||
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
||||
import fs from 'node:fs/promises';
|
||||
import { ApiErrorCode, createErrorResponse, getErrorMessage, type ApiResponse } from '../../types.js';
|
||||
@@ -20,10 +20,10 @@ import {
|
||||
ownerFor,
|
||||
resolveCasesDir,
|
||||
sessionCapacityMessage,
|
||||
SETTINGS_PATH,
|
||||
findSessionOrFail,
|
||||
parseBody,
|
||||
} from '../route-helpers.js';
|
||||
import { mutateSettingsFile } from '../settings-writer.js';
|
||||
import { resolveClaudeModeForUsername } from '../../user-store.js';
|
||||
import { writeHooksConfig, stripCaseEnvKeys } from '../../hooks-config.js';
|
||||
import { generateClaudeMd } from '../../templates/claude-md.js';
|
||||
@@ -470,18 +470,10 @@ export function registerRalphRoutes(
|
||||
// in single-user mode.
|
||||
if (isAdmin(req)) {
|
||||
try {
|
||||
let settings: Record<string, unknown> = {};
|
||||
try {
|
||||
settings = JSON.parse(await fs.readFile(SETTINGS_PATH, 'utf-8'));
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
settings.lastUsedCase = caseName;
|
||||
const dir = dirname(SETTINGS_PATH);
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
||||
fs.writeFile(SETTINGS_PATH, JSON.stringify(settings, null, 2)).catch(() => {}); // Ignore - persisting lastUsedCase is non-critical
|
||||
} catch {
|
||||
/* non-critical */
|
||||
await mutateSettingsFile((settings) => ({ ...settings, lastUsedCase: caseName }));
|
||||
} catch (err) {
|
||||
// Non-critical: the loop starts regardless.
|
||||
console.warn('[Server] Failed to save settings (lastUsedCase):', getErrorMessage(err));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -114,11 +114,11 @@ import {
|
||||
resolveCasesDir,
|
||||
resolveParentSessionId,
|
||||
sessionCapacityMessage,
|
||||
SETTINGS_PATH,
|
||||
validatePathWithinBase,
|
||||
waitCapacityResponse,
|
||||
abortOnClientHangUp,
|
||||
} from '../route-helpers.js';
|
||||
import { mutateSettingsFile } from '../settings-writer.js';
|
||||
import { buildAgentCaseMarker, writeAgentCaseMarker } from '../../agent-case-marker.js';
|
||||
import { agentInbox } from '../agent-inbox.js';
|
||||
import { canUsernameRunPrivilegedCommands, resolveClaudeModeForUsername } from '../../user-store.js';
|
||||
@@ -4576,28 +4576,14 @@ export function registerSessionRoutes(
|
||||
// settings.json, readable by every user through GET /api/settings, so a non-admin's
|
||||
// case name is not written there in multi-user mode (writes are admin-only, like
|
||||
// PUT /api/settings). isAdmin is always true in single-user mode.
|
||||
// Awaited and serialized with every other settings.json writer (mutateSettingsFile): the
|
||||
// un-awaited write this replaced could land after the response, over a settings save.
|
||||
if (isAdmin(req)) {
|
||||
try {
|
||||
const settingsFilePath = SETTINGS_PATH;
|
||||
let settings: Record<string, unknown> = {};
|
||||
try {
|
||||
settings = JSON.parse(await fs.readFile(settingsFilePath, 'utf-8'));
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err;
|
||||
}
|
||||
settings.lastUsedCase = caseName;
|
||||
const dir = dirname(settingsFilePath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
// Use async write to avoid blocking event loop
|
||||
fs.writeFile(settingsFilePath, JSON.stringify(settings, null, 2)).catch((err) => {
|
||||
// Non-critical but log for debugging
|
||||
console.warn('[Server] Failed to save settings (lastUsedCase):', err);
|
||||
});
|
||||
await mutateSettingsFile((settings) => ({ ...settings, lastUsedCase: caseName }));
|
||||
} catch (err) {
|
||||
// Non-critical but log for debugging
|
||||
console.warn('[Server] Failed to prepare settings update:', err);
|
||||
// Non-critical: the session is already running.
|
||||
console.warn('[Server] Failed to save settings (lastUsedCase):', getErrorMessage(err));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import {
|
||||
toggleService,
|
||||
SETTINGS_PATH,
|
||||
} from '../route-helpers.js';
|
||||
import { mutateSettingsFile, settingsWriteErrorResponse } from '../settings-writer.js';
|
||||
import { SseEvent } from '../sse-events.js';
|
||||
import { getInstallInfo, checkForUpdate, startUpdate, getUpdateStatusForApi } from '../self-update.js';
|
||||
|
||||
@@ -1076,11 +1077,10 @@ export function registerSystemRoutes(
|
||||
|
||||
app.get('/api/settings', async (req) => {
|
||||
// A plain read. This route must NEVER write settings.json: readJsonConfig()
|
||||
// answers `{}` for ANY read failure (a parse error, EACCES, EMFILE, a read
|
||||
// that lands inside PUT's non-atomic write), not only for a missing file,
|
||||
// and every page load calls this route, so a "persist the default when the
|
||||
// key is absent" reconcile here replaced a whole settings file with one key
|
||||
// on the first unlucky read. The plan-usage default is resolved by the
|
||||
// answers `{}` for ANY read failure (a parse error, EACCES, EMFILE), not
|
||||
// only for a missing file, and every page load calls this route, so a
|
||||
// "persist the default when the key is absent" reconcile here replaced a
|
||||
// whole settings file with one key on the first unlucky read. The plan-usage default is resolved by the
|
||||
// READERS instead: an absent `showPlanUsageLimits` means ON to
|
||||
// readPlanUsageTelemetryEnabled() (hooks-config.ts), the same way an absent
|
||||
// `workspaceHooksEnabled` means ON, and the client resolves its own display
|
||||
@@ -1133,16 +1133,6 @@ export function registerSystemRoutes(
|
||||
}
|
||||
|
||||
try {
|
||||
const dir = dirname(SETTINGS_PATH);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
let existing: Record<string, unknown> = {};
|
||||
try {
|
||||
existing = JSON.parse(await fs.readFile(SETTINGS_PATH, 'utf-8'));
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
// acknowledgeUnauthTunnel is an ACTION field (not a stored setting) — strip
|
||||
// it before persisting so settings.json stays clean.
|
||||
const { acknowledgeUnauthTunnel, ...settingsToStore } = settings;
|
||||
@@ -1150,8 +1140,8 @@ export function registerSystemRoutes(
|
||||
// It must never write tunnelEnabled:true: a concurrent stop has written false by now,
|
||||
// and true on disk would show a stopped tunnel as on. The stored value stays as it is.
|
||||
if (settings.tunnelEnabled === true && !tunnelEnableAuthorized) delete settingsToStore.tunnelEnabled;
|
||||
const { before: existing } = await mutateSettingsFile((current) => ({ ...current, ...settingsToStore }));
|
||||
const merged = { ...existing, ...settingsToStore };
|
||||
await fs.writeFile(SETTINGS_PATH, JSON.stringify(merged, null, 2));
|
||||
|
||||
// tmux 3.7+ resizes tracked panes; older versions apply this to new panes.
|
||||
// Already-evicted history cannot be recovered on either version.
|
||||
@@ -1251,7 +1241,7 @@ export function registerSystemRoutes(
|
||||
|
||||
return {};
|
||||
} catch (err) {
|
||||
return createErrorResponse(ApiErrorCode.OPERATION_FAILED, getErrorMessage(err));
|
||||
return settingsWriteErrorResponse(err);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1269,25 +1259,10 @@ export function registerSystemRoutes(
|
||||
const modelConfig = parseBody(ModelConfigUpdateSchema, req.body, 'Invalid model config') as Record<string, unknown>;
|
||||
|
||||
try {
|
||||
let existingSettings: Record<string, unknown> = {};
|
||||
try {
|
||||
const content = await fs.readFile(SETTINGS_PATH, 'utf-8');
|
||||
existingSettings = JSON.parse(content);
|
||||
} catch {
|
||||
// File doesn't exist yet, start fresh
|
||||
}
|
||||
|
||||
existingSettings.modelConfig = modelConfig;
|
||||
|
||||
const dir = dirname(SETTINGS_PATH);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
await fs.writeFile(SETTINGS_PATH, JSON.stringify(existingSettings, null, 2));
|
||||
|
||||
await mutateSettingsFile((current) => ({ ...current, modelConfig }));
|
||||
return {};
|
||||
} catch (err) {
|
||||
return createErrorResponse(ApiErrorCode.OPERATION_FAILED, getErrorMessage(err));
|
||||
return settingsWriteErrorResponse(err);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+5
-8
@@ -174,6 +174,7 @@ import { MAX_PASTE_IMAGE_BYTES } from '../config/buffer-limits.js';
|
||||
import { resolveTerminalHistoryConfig } from '../config/terminal-history.js';
|
||||
import { SseEvent } from './sse-events.js';
|
||||
import { MAX_ROUTE_PARAM_LENGTH } from './route-helpers.js';
|
||||
import { mutateSettingsFile } from './settings-writer.js';
|
||||
import { getLatestPlanUsage, setLatestCodexPlanUsage, setLatestCopilotPlanUsage } from './plan-usage-latest.js';
|
||||
import { telemetrySignature } from '../usage-telemetry.js';
|
||||
import { readCodexPlanUsage, resolveCodexBinaryPath } from '../utils/codex-cli-resolver.js';
|
||||
@@ -3351,16 +3352,12 @@ export class WebServer extends EventEmitter {
|
||||
// Tunnel only starts when user clicks the toggle in the UI — never on boot.
|
||||
// Reset persisted tunnelEnabled so the UI toggle reflects actual state.
|
||||
if (await this.isTunnelEnabled()) {
|
||||
const settingsPath = dataPath('settings.json');
|
||||
try {
|
||||
const content = await fs.readFile(settingsPath, 'utf-8');
|
||||
const settings = JSON.parse(content);
|
||||
settings.tunnelEnabled = false;
|
||||
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
} catch {
|
||||
/* ignore */
|
||||
await mutateSettingsFile((settings) => ({ ...settings, tunnelEnabled: false }));
|
||||
console.log('Cloudflare tunnel setting reset (tunnel only starts on explicit UI toggle)');
|
||||
} catch (err) {
|
||||
console.warn('Could not reset the Cloudflare tunnel setting:', getErrorMessage(err));
|
||||
}
|
||||
console.log('Cloudflare tunnel setting reset (tunnel only starts on explicit UI toggle)');
|
||||
}
|
||||
|
||||
// Start team watcher for agent team awareness (always on — lightweight polling)
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* @fileoverview The writer of the server's `settings.json` (`SETTINGS_PATH`).
|
||||
*
|
||||
* Every read-modify-write of that file goes through `mutateSettingsFile()`, the same shape as
|
||||
* `mutateRegistryFile()` (config/cli-registry/registry-writer.ts). The one direct writer is
|
||||
* config-backup restore (`restoreConfigBackup()`, as for `clis.json`): it must work on a file that
|
||||
* does not parse, which this writer refuses.
|
||||
*
|
||||
* - **Serialized.** Mutations run one at a time on a single promise chain, each one reading,
|
||||
* changing and writing before the next starts. The writers used to run unserialized with a
|
||||
* plain truncate-then-write `fs.writeFile`, and quick-start did not even await its
|
||||
* `lastUsedCase` write, so a quick-start followed by a settings save could tear the file (one
|
||||
* write's bytes over the tail of a longer one) or drop the other writer's key.
|
||||
* - **Atomic.** Each write goes to its own temp file beside the target and is renamed over it,
|
||||
* so a reader never sees a half-written file.
|
||||
* - **Refuses a file it must not trust.** Only a missing (or empty) file starts from `{}`. A file
|
||||
* that cannot be read or does not parse is left alone and the write throws
|
||||
* `SettingsWriteRefusedError`: the writers used to read such a file as `{}` and write back only
|
||||
* their own keys, which erased every other setting.
|
||||
*/
|
||||
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import fs from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
import { ApiErrorCode, createErrorResponse, getErrorMessage, type ApiResponse } from '../types.js';
|
||||
import { SETTINGS_PATH } from './route-helpers.js';
|
||||
|
||||
export type SettingsFile = Record<string, unknown>;
|
||||
|
||||
/** A write refused because the existing `settings.json` must not be overwritten. The message is user-facing. */
|
||||
export class SettingsWriteRefusedError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'SettingsWriteRefusedError';
|
||||
}
|
||||
}
|
||||
|
||||
async function readSettingsForWrite(path: string): Promise<SettingsFile> {
|
||||
let raw: string;
|
||||
try {
|
||||
raw = await fs.readFile(path, 'utf-8');
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return {};
|
||||
throw new SettingsWriteRefusedError(
|
||||
`Cannot read ${path} (${(err as Error).message}), so this change was not saved.`
|
||||
);
|
||||
}
|
||||
// An empty file holds no settings to lose.
|
||||
if (raw.trim() === '') return {};
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = undefined;
|
||||
}
|
||||
// The parser's own message is not repeated: it can quote the file, which may hold an API key.
|
||||
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
||||
throw new SettingsWriteRefusedError(
|
||||
`${path} is not a valid JSON object, so this change was not saved (saving would have replaced every other setting in it). Fix or delete the file, or restore it from a config backup, then try again.`
|
||||
);
|
||||
}
|
||||
return parsed as SettingsFile;
|
||||
}
|
||||
|
||||
async function writeSettingsFile(path: string, settings: SettingsFile): Promise<void> {
|
||||
// Through a symlink, as the plain writeFile this replaces did: the rename lands on the real file.
|
||||
const target = await fs.realpath(path).catch(() => path);
|
||||
await fs.mkdir(dirname(target), { recursive: true });
|
||||
const tmp = `${target}.${process.pid}.${randomUUID()}.tmp`;
|
||||
try {
|
||||
await fs.writeFile(tmp, JSON.stringify(settings, null, 2), { mode: 0o600 });
|
||||
await fs.rename(tmp, target);
|
||||
} catch (err) {
|
||||
await fs.rm(tmp, { force: true }).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
let mutationChain: Promise<unknown> = Promise.resolve();
|
||||
|
||||
/**
|
||||
* Run one change to `settings.json`. `fn` receives the current settings and returns the settings
|
||||
* to write, or `undefined` to write nothing. Resolves with the settings before and after the
|
||||
* change (`after` is `undefined` when nothing was written). A refused or failed mutation rejects
|
||||
* its own caller only; the chain keeps going.
|
||||
*/
|
||||
export function mutateSettingsFile(
|
||||
fn: (current: SettingsFile) => SettingsFile | undefined
|
||||
): Promise<{ before: SettingsFile; after: SettingsFile | undefined }> {
|
||||
const run = mutationChain.then(async () => {
|
||||
const before = await readSettingsForWrite(SETTINGS_PATH).catch((err: unknown) => {
|
||||
console.warn(`[settings] write refused: ${getErrorMessage(err)}`);
|
||||
throw err;
|
||||
});
|
||||
const after = fn({ ...before });
|
||||
if (after) await writeSettingsFile(SETTINGS_PATH, after);
|
||||
return { before, after };
|
||||
});
|
||||
mutationChain = run.catch(() => {});
|
||||
return run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects with `SettingsWriteRefusedError` when `mutateSettingsFile()` would refuse a write right
|
||||
* now: the writer's own read-for-write rule, run on its own. For a route that does something it
|
||||
* cannot take back (case rename ends sessions) before its settings write. A check only, outside
|
||||
* the chain (writes are atomic, so it never reads a half-written file); the file can still change
|
||||
* before the write.
|
||||
*/
|
||||
export async function assertSettingsWritable(): Promise<void> {
|
||||
await readSettingsForWrite(SETTINGS_PATH);
|
||||
}
|
||||
|
||||
/** A refused write answers 409 CONFLICT with the fix; any other failure stays OPERATION_FAILED. */
|
||||
export function settingsWriteErrorResponse(err: unknown): ApiResponse<never> {
|
||||
if (err instanceof SettingsWriteRefusedError) return createErrorResponse(ApiErrorCode.CONFLICT, err.message);
|
||||
return createErrorResponse(ApiErrorCode.OPERATION_FAILED, getErrorMessage(err));
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
symlinkSync,
|
||||
@@ -25,6 +26,7 @@ import { SETTINGS_PATH, resolveCasesDir } from '../../src/web/route-helpers.js';
|
||||
import { caseSessionSettled, claudeProjectDirName } from '../../src/case-rename.js';
|
||||
import { Session } from '../../src/session.js';
|
||||
import { safeRmHomeTree } from '../mocks/index.js';
|
||||
import { mutateSettingsFile } from '../../src/web/settings-writer.js';
|
||||
|
||||
const LINKED = () => dataPath('linked-cases.json');
|
||||
const CASES_DIR = () => join(homedir(), 'codeman-cases');
|
||||
@@ -286,6 +288,64 @@ describe('POST /api/cases/:name/rename (a linked case)', () => {
|
||||
expect(settings()).toEqual({ caseOrder: ['a', 'proj', 'b'], lastUsedCase: 'proj' });
|
||||
});
|
||||
|
||||
it('puts back only the keys it wrote, keeping a settings write made in between', async () => {
|
||||
const { app } = await createRouteTestHarness(registerCaseRoutes);
|
||||
const real = join(work, 'my-project');
|
||||
mkdirSync(real);
|
||||
writeFileSync(LINKED(), JSON.stringify({ proj: real }));
|
||||
writeFileSync(SETTINGS_PATH, JSON.stringify({ caseOrder: ['a', 'proj', 'b'], lastUsedCase: 'proj' }));
|
||||
chmodSync(LINKED(), 0o444);
|
||||
// Another writer (a settings save) lands after the rename wrote settings.json and before the
|
||||
// registry write fails.
|
||||
const realWriteFile = fs.writeFile;
|
||||
const spy = vi.spyOn(fs, 'writeFile').mockImplementation(async (file, data, options) => {
|
||||
if (file === LINKED()) await mutateSettingsFile((current) => ({ ...current, agentSkillEnabled: true }));
|
||||
return realWriteFile(file, data, options);
|
||||
});
|
||||
try {
|
||||
const res = await rename(app, 'proj', { newName: 'renamed', renameFolder: true });
|
||||
expect(res.statusCode).toBe(500);
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
expect(settings()).toEqual({ caseOrder: ['a', 'proj', 'b'], lastUsedCase: 'proj', agentSkillEnabled: true });
|
||||
});
|
||||
|
||||
it('refuses to rename while settings.json is not a JSON object, leaving the folder and the file alone', async () => {
|
||||
const { app } = await createRouteTestHarness(registerCaseRoutes);
|
||||
const oldDir = localCase('alpha');
|
||||
const torn = '{\n "agentSkillEnabled": true\n}tail"\n}';
|
||||
writeFileSync(SETTINGS_PATH, torn);
|
||||
const res = await rename(app, 'alpha', { newName: 'beta' });
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json()).toEqual({
|
||||
success: false,
|
||||
errorCode: 'CONFLICT',
|
||||
error: `${SETTINGS_PATH} is not a valid JSON object, so this change was not saved (saving would have replaced every other setting in it). Fix or delete the file, or restore it from a config backup, then try again.`,
|
||||
});
|
||||
expect(existsSync(oldDir)).toBe(true);
|
||||
expect(existsSync(join(CASES_DIR(), 'beta'))).toBe(false);
|
||||
expect(readFileSync(SETTINGS_PATH, 'utf8')).toBe(torn);
|
||||
});
|
||||
|
||||
it('checks settings.json before it stops any session, so none is ended for a rename that cannot complete', async () => {
|
||||
const { app, ctx } = await createRouteTestHarness(registerCaseRoutes);
|
||||
const oldDir = localCase('alpha');
|
||||
const torn = '{\n "agentSkillEnabled": true\n}tail"\n}';
|
||||
writeFileSync(SETTINGS_PATH, torn);
|
||||
(ctx.sessions as Map<string, unknown>).set('s1', { id: 's1', name: 'w1', workingDir: oldDir });
|
||||
|
||||
const res = await rename(app, 'alpha', { newName: 'beta', stopSessions: true });
|
||||
|
||||
expect(res.statusCode).toBe(409);
|
||||
expect(res.json().errorCode).toBe('CONFLICT');
|
||||
expect(res.json().error).toContain('is not a valid JSON object');
|
||||
expect(ctx.cleanupSession).not.toHaveBeenCalled();
|
||||
expect(existsSync(oldDir)).toBe(true);
|
||||
expect(existsSync(join(CASES_DIR(), 'beta'))).toBe(false);
|
||||
expect(readFileSync(SETTINGS_PATH, 'utf8')).toBe(torn);
|
||||
});
|
||||
|
||||
it('says where the folder is when it cannot be moved back', async () => {
|
||||
const { app } = await createRouteTestHarness(registerCaseRoutes);
|
||||
const real = join(work, 'my-project');
|
||||
@@ -328,12 +388,24 @@ describe('POST /api/cases/:name/rename (a linked case)', () => {
|
||||
mkdirSync(real);
|
||||
writeFileSync(LINKED(), JSON.stringify({ proj: real, other: join(work, 'x') }));
|
||||
writeFileSync(SETTINGS_PATH, JSON.stringify({ caseOrder: ['proj'] }));
|
||||
chmodSync(SETTINGS_PATH, 0o444);
|
||||
const res = await rename(app, 'proj', { newName: 'renamed', renameFolder: true });
|
||||
chmodSync(SETTINGS_PATH, 0o644);
|
||||
// settings.json is replaced by renaming a temp file over it, which a read-only file does not stop,
|
||||
// so the failure is injected at that rename.
|
||||
const realRename = fs.rename;
|
||||
const spy = vi.spyOn(fs, 'rename').mockImplementation(async (from, to) => {
|
||||
if (to === SETTINGS_PATH) throw Object.assign(new Error('permission denied'), { code: 'EACCES' });
|
||||
return realRename(from, to);
|
||||
});
|
||||
let res;
|
||||
try {
|
||||
res = await rename(app, 'proj', { newName: 'renamed', renameFolder: true });
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(existsSync(real)).toBe(true);
|
||||
expect(linked()).toEqual({ proj: real, other: join(work, 'x') });
|
||||
// The failed write leaves no temp file behind.
|
||||
expect(readdirSync(dirname(SETTINGS_PATH)).filter((n) => n.startsWith('settings.json.'))).toEqual([]);
|
||||
});
|
||||
|
||||
it('answers 404 for a linked case a non-admin cannot see in multi-user mode', async () => {
|
||||
|
||||
@@ -54,6 +54,11 @@ vi.mock('node:fs/promises', () => ({
|
||||
default: {
|
||||
readFile: vi.fn(async () => JSON.stringify(STORED)),
|
||||
writeFile: vi.fn(async () => undefined),
|
||||
// settings.json is written to a temp file and renamed over the real one (settings-writer.ts).
|
||||
realpath: vi.fn(async (p: string) => p),
|
||||
mkdir: vi.fn(async () => undefined),
|
||||
rename: vi.fn(async () => undefined),
|
||||
rm: vi.fn(async () => undefined),
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
*
|
||||
* PR #361 once made it reconcile `showPlanUsageLimits` on first read: if the
|
||||
* key was absent, the route persisted `true`. But readJsonConfig() answers
|
||||
* `{}` for ANY read failure (parse error, EACCES, EMFILE, a read landing inside
|
||||
* PUT's non-atomic write), not only ENOENT, and every page load calls this
|
||||
* `{}` for ANY read failure (parse error, EACCES, EMFILE, and at the time a read
|
||||
* landing inside PUT's non-atomic write), not only ENOENT, and every page load calls this
|
||||
* route, so one unlucky read replaced the whole settings file with a one-key
|
||||
* file. The default now lives in the readers instead: an absent key means ON
|
||||
* to readPlanUsageTelemetryEnabled() (pinned in test/hooks-config.test.ts) and
|
||||
|
||||
@@ -70,6 +70,11 @@ vi.mock('node:fs/promises', () => ({
|
||||
default: {
|
||||
readFile: vi.fn(async () => JSON.stringify(EXISTING_SETTINGS)),
|
||||
writeFile: vi.fn(async () => undefined),
|
||||
// settings.json is written to a temp file and renamed over the real one (settings-writer.ts).
|
||||
realpath: vi.fn(async (p: string) => p),
|
||||
mkdir: vi.fn(async () => undefined),
|
||||
rename: vi.fn(async () => undefined),
|
||||
rm: vi.fn(async () => undefined),
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -11,6 +11,11 @@ vi.mock('node:fs/promises', () => ({
|
||||
default: {
|
||||
readFile: vi.fn(async () => '{}'),
|
||||
writeFile: vi.fn(async () => undefined),
|
||||
// settings.json is written to a temp file and renamed over the real one (settings-writer.ts).
|
||||
realpath: vi.fn(async (p: string) => p),
|
||||
mkdir: vi.fn(async () => undefined),
|
||||
rename: vi.fn(async () => undefined),
|
||||
rm: vi.fn(async () => undefined),
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -27,6 +32,18 @@ vi.mock('node:fs', async (importOriginal) => {
|
||||
import fs from 'node:fs/promises';
|
||||
import { SETTINGS_PATH } from '../../src/web/route-helpers.js';
|
||||
const mockedWriteFile = vi.mocked(fs.writeFile);
|
||||
const mockedRename = vi.mocked(fs.rename);
|
||||
|
||||
/**
|
||||
* settings.json writes as [path, contents]. The writer (settings-writer.ts) writes each one to a
|
||||
* temp file and renames it over settings.json, so a write counts once its rename lands.
|
||||
*/
|
||||
function settingsWrites(): [string, string][] {
|
||||
return mockedRename.mock.calls.map(([from, to]) => {
|
||||
const write = mockedWriteFile.mock.calls.find(([path]) => path === from);
|
||||
return [String(to), String(write?.[1])];
|
||||
});
|
||||
}
|
||||
|
||||
const STATUS = { running: false, url: null };
|
||||
|
||||
@@ -94,7 +111,7 @@ describe('tunnel authentication policy', () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(tunnel.start.mock.calls).toEqual([[harness.ctx.port, harness.ctx.https]]);
|
||||
expect(tunnel.stop.mock.calls).toEqual([]);
|
||||
expect(mockedWriteFile.mock.calls).toEqual([[SETTINGS_PATH, JSON.stringify({ tunnelEnabled: true }, null, 2)]]);
|
||||
expect(settingsWrites()).toEqual([[SETTINGS_PATH, JSON.stringify({ tunnelEnabled: true }, null, 2)]]);
|
||||
}
|
||||
|
||||
it('refuses the incident: network acknowledgement does not authorize a public tunnel', async () => {
|
||||
@@ -154,7 +171,7 @@ describe('tunnel authentication policy', () => {
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(tunnel.start.mock.calls).toEqual([]);
|
||||
expect(tunnel.stop.mock.calls).toEqual([[]]);
|
||||
expect(mockedWriteFile.mock.calls).toEqual([[SETTINGS_PATH, JSON.stringify({ tunnelEnabled: false }, null, 2)]]);
|
||||
expect(settingsWrites()).toEqual([[SETTINGS_PATH, JSON.stringify({ tunnelEnabled: false }, null, 2)]]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -210,9 +227,13 @@ describe('tunnel enable authorization across a concurrent stop', () => {
|
||||
expect(path).toBe(SETTINGS_PATH);
|
||||
return JSON.stringify(storedSettings);
|
||||
});
|
||||
const pending = new Map<string, string>();
|
||||
mockedWriteFile.mockImplementation(async (path, contents) => {
|
||||
expect(path).toBe(SETTINGS_PATH);
|
||||
storedSettings = JSON.parse(contents as string);
|
||||
pending.set(String(path), contents as string);
|
||||
});
|
||||
mockedRename.mockImplementation(async (from, to) => {
|
||||
expect(to).toBe(SETTINGS_PATH);
|
||||
storedSettings = JSON.parse(pending.get(String(from)) as string);
|
||||
// The stop becomes visible during the awaited write, regardless of isRunning() call count.
|
||||
if (stopDuringWrite) running = false;
|
||||
afterWrite?.();
|
||||
@@ -229,6 +250,7 @@ describe('tunnel enable authorization across a concurrent stop', () => {
|
||||
});
|
||||
vi.mocked(fs.readFile).mockResolvedValue('{}');
|
||||
mockedWriteFile.mockResolvedValue(undefined);
|
||||
mockedRename.mockResolvedValue(undefined);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -243,7 +265,7 @@ describe('tunnel enable authorization across a concurrent stop', () => {
|
||||
const expected = { tunnelEnabled, subagentTrackingEnabled: false, autoNameSessions: false };
|
||||
// Check every write so a transient unauthorized enable followed by a rollback also fails.
|
||||
expect
|
||||
.soft(mockedWriteFile.mock.calls.map(([path, contents]) => ({ path, settings: JSON.parse(contents as string) })))
|
||||
.soft(settingsWrites().map(([path, contents]) => ({ path, settings: JSON.parse(contents) })))
|
||||
.toEqual([{ path: SETTINGS_PATH, settings: expected }]);
|
||||
expect.soft(storedSettings).toEqual(expected);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,11 @@ vi.mock('node:fs/promises', () => ({
|
||||
default: {
|
||||
readFile: vi.fn(async () => '{}'),
|
||||
writeFile: vi.fn(async () => undefined),
|
||||
// settings.json is written to a temp file and renamed over the real one (settings-writer.ts).
|
||||
realpath: vi.fn(async (p: string) => p),
|
||||
mkdir: vi.fn(async () => undefined),
|
||||
rename: vi.fn(async () => undefined),
|
||||
rm: vi.fn(async () => undefined),
|
||||
},
|
||||
}));
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ async function send(method: string, path: string, headers: Record<string, string
|
||||
return { status: res.status, body: (await res.json()) as Record<string, any> };
|
||||
}
|
||||
|
||||
/** The quick-start write of lastUsedCase is fire-and-forget: poll for it briefly. */
|
||||
/** Read settings.json until `predicate` holds (briefly), so a write that never comes fails on its value. */
|
||||
async function settleSettings(predicate: (s: Record<string, unknown>) => boolean): Promise<Record<string, unknown>> {
|
||||
let last: Record<string, unknown> = {};
|
||||
for (let i = 0; i < 40; i++) {
|
||||
@@ -61,7 +61,7 @@ async function settleSettings(predicate: (s: Record<string, unknown>) => boolean
|
||||
last = JSON.parse(await readSettingsBytes());
|
||||
if (predicate(last)) return last;
|
||||
} catch {
|
||||
// the write is not atomic: a read that lands between its truncate and its data is empty, so poll again
|
||||
// not there yet, or not JSON: poll again
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 25));
|
||||
}
|
||||
@@ -127,7 +127,7 @@ describe('a non-admin and the server-wide settings.json', () => {
|
||||
const mine = await send('POST', '/api/quick-start', bob, { caseName: 'bobproj' });
|
||||
expect(mine.status).toBe(200);
|
||||
expect(mine.body.success).toBe(true);
|
||||
// Give a (wrong) fire-and-forget write every chance to land before checking.
|
||||
// Give a (wrong) write every chance to land before checking.
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
expect(JSON.parse(await readSettingsBytes()).lastUsedCase).toBe('rootcase');
|
||||
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
/**
|
||||
* @fileoverview Every writer of the server's settings.json is serialized, atomic, and refuses to
|
||||
* overwrite a file that does not parse (live server, ephemeral port, real file on disk).
|
||||
*
|
||||
* The upstream CI flake in test/quick-start.test.ts ("accepts the setting via PUT, then injects")
|
||||
* was a torn settings.json: `SyntaxError: Unexpected non-whitespace character after JSON at
|
||||
* position 31`. Quick-start saved `lastUsedCase` with an un-awaited, non-atomic read-modify-write,
|
||||
* PUT /api/settings ran its own unserialized one, and the two overlapping truncate-then-write
|
||||
* calls left one write's bytes over the tail of the other. The PUT then read the torn file as `{}`
|
||||
* and wrote back only its own keys, erasing every other setting.
|
||||
*
|
||||
* Writes to settings.json (and its temp files) are slowed down here, which is what a busy CI disk
|
||||
* does: a writer that answers before its write lands, or two writers that both read before either
|
||||
* writes, then fail deterministically instead of once in a while.
|
||||
*/
|
||||
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import fsp from 'node:fs/promises';
|
||||
import {
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
readlinkSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
symlinkSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { WebServer } from '../src/web/server.js';
|
||||
import { TmuxManager } from '../src/tmux-manager.js';
|
||||
import { SETTINGS_PATH } from '../src/web/route-helpers.js';
|
||||
import { getCasesDir } from '../src/config/cases-dir.js';
|
||||
|
||||
vi.spyOn(TmuxManager, 'isTmuxAvailable').mockReturnValue(true);
|
||||
|
||||
/** How long every write to settings.json (or a temp file beside it) is held before it starts. */
|
||||
const SLOW_WRITE_MS = 200;
|
||||
const realWriteFile = fsp.writeFile.bind(fsp);
|
||||
vi.spyOn(fsp, 'writeFile').mockImplementation((async (file: unknown, data: unknown, options?: unknown) => {
|
||||
if (typeof file === 'string' && file.startsWith(SETTINGS_PATH)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, SLOW_WRITE_MS));
|
||||
}
|
||||
return realWriteFile(file as string, data as string, options as undefined);
|
||||
}) as typeof fsp.writeFile);
|
||||
|
||||
/** The bytes the CI run left behind: the PUT's 31-byte file over the tail of quick-start's longer one. */
|
||||
const TORN = '{\n "agentSkillEnabled": true\n}' + 'rt-1761000000000"\n}';
|
||||
|
||||
const REFUSAL = {
|
||||
success: false,
|
||||
errorCode: 'CONFLICT',
|
||||
error: `${SETTINGS_PATH} is not a valid JSON object, so this change was not saved (saving would have replaced every other setting in it). Fix or delete the file, or restore it from a config backup, then try again.`,
|
||||
};
|
||||
|
||||
let server: WebServer;
|
||||
const url = (p: string) => `http://localhost:${server.boundPort}${p}`;
|
||||
const seed = (value: unknown) => writeFileSync(SETTINGS_PATH, JSON.stringify(value, null, 2));
|
||||
const readSettings = () => JSON.parse(readFileSync(SETTINGS_PATH, 'utf-8')) as Record<string, unknown>;
|
||||
|
||||
async function send(method: string, path: string, body: unknown) {
|
||||
const res = await fetch(url(path), {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return { status: res.status, body: (await res.json()) as Record<string, unknown> };
|
||||
}
|
||||
|
||||
let caseCounter = 0;
|
||||
const newCaseName = () => `settings-race-${process.pid}-${++caseCounter}`;
|
||||
|
||||
beforeAll(async () => {
|
||||
// The boot pass resets a persisted tunnelEnabled (the tunnel only starts from the UI); that write
|
||||
// goes through the same writer and must keep every other key.
|
||||
mkdirSync(join(SETTINGS_PATH, '..'), { recursive: true });
|
||||
seed({ tunnelEnabled: true, caseOrder: ['boot'] });
|
||||
server = new WebServer(0, false, true);
|
||||
await server.start();
|
||||
}, 30000);
|
||||
|
||||
afterAll(async () => {
|
||||
await server?.stop();
|
||||
vi.restoreAllMocks();
|
||||
}, 30000);
|
||||
|
||||
describe('settings.json writes', () => {
|
||||
it('resets tunnelEnabled at boot without dropping the other keys', () => {
|
||||
expect(readSettings()).toEqual({ tunnelEnabled: false, caseOrder: ['boot'] });
|
||||
});
|
||||
|
||||
describe('with a valid file', () => {
|
||||
beforeEach(() => seed({ lastUsedCase: 'seeded' }));
|
||||
|
||||
it('answers a quick-start only after its lastUsedCase has landed', async () => {
|
||||
const caseName = newCaseName();
|
||||
const res = await send('POST', '/api/quick-start', { caseName });
|
||||
expect(res.body.success).toBe(true);
|
||||
expect(readSettings()).toEqual({ lastUsedCase: caseName });
|
||||
});
|
||||
|
||||
it('answers a Ralph loop start only after its lastUsedCase has landed', async () => {
|
||||
const caseName = newCaseName();
|
||||
const res = await send('POST', '/api/ralph-loop/start', { caseName, taskDescription: 'x' });
|
||||
expect(res.body.success).toBe(true);
|
||||
expect(readSettings()).toEqual({ lastUsedCase: caseName });
|
||||
});
|
||||
|
||||
it('keeps every key when quick-start, the settings PUT, model config and case order write at once', async () => {
|
||||
const caseName = newCaseName();
|
||||
const [quick, put, model, order] = await Promise.all([
|
||||
send('POST', '/api/quick-start', { caseName }),
|
||||
send('PUT', '/api/settings', { agentSkillEnabled: true }),
|
||||
send('PUT', '/api/execution/model-config', { defaultModel: 'opus' }),
|
||||
send('PUT', '/api/cases/order', { order: ['a', 'b'] }),
|
||||
]);
|
||||
expect(quick.body.success).toBe(true);
|
||||
expect(put).toEqual({ status: 200, body: { success: true, data: {} } });
|
||||
expect(model).toEqual({ status: 200, body: { success: true, data: {} } });
|
||||
expect(order).toEqual({ status: 200, body: { success: true, data: { order: ['a', 'b'] } } });
|
||||
expect(readSettings()).toEqual({
|
||||
lastUsedCase: caseName,
|
||||
agentSkillEnabled: true,
|
||||
modelConfig: { defaultModel: 'opus' },
|
||||
caseOrder: ['a', 'b'],
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps a settings PUT that lands while a case rename rewrites caseOrder', async () => {
|
||||
const oldName = newCaseName();
|
||||
const newName = newCaseName();
|
||||
mkdirSync(join(getCasesDir(), oldName), { recursive: true });
|
||||
seed({ lastUsedCase: oldName, caseOrder: [oldName, 'other'] });
|
||||
const [rename, put] = await Promise.all([
|
||||
send('POST', `/api/cases/${oldName}/rename`, { newName }),
|
||||
send('PUT', '/api/settings', { agentSkillEnabled: true }),
|
||||
]);
|
||||
expect(rename.body.success).toBe(true);
|
||||
expect(put).toEqual({ status: 200, body: { success: true, data: {} } });
|
||||
expect(readSettings()).toEqual({ lastUsedCase: newName, caseOrder: [newName, 'other'], agentSkillEnabled: true });
|
||||
});
|
||||
|
||||
it('writes the file 0600, since it can hold an API key', async () => {
|
||||
await send('PUT', '/api/settings', { agentSkillEnabled: true });
|
||||
expect(statSync(SETTINGS_PATH).mode & 0o777).toBe(0o600);
|
||||
});
|
||||
});
|
||||
|
||||
it('starts from {} when the file is missing or empty', async () => {
|
||||
rmSync(SETTINGS_PATH, { force: true });
|
||||
expect(await send('PUT', '/api/settings', { agentSkillEnabled: true })).toEqual({
|
||||
status: 200,
|
||||
body: { success: true, data: {} },
|
||||
});
|
||||
expect(readFileSync(SETTINGS_PATH, 'utf-8')).toBe('{\n "agentSkillEnabled": true\n}');
|
||||
|
||||
writeFileSync(SETTINGS_PATH, ' \n');
|
||||
await send('PUT', '/api/execution/model-config', { defaultModel: 'opus' });
|
||||
expect(readSettings()).toEqual({ modelConfig: { defaultModel: 'opus' } });
|
||||
});
|
||||
|
||||
it('writes through a symlinked settings.json to its target', async () => {
|
||||
const elsewhere = mkdtempSync(join(tmpdir(), 'codeman-settings-link-'));
|
||||
const target = join(elsewhere, 'settings.json');
|
||||
writeFileSync(target, JSON.stringify({ lastUsedCase: 'linked' }));
|
||||
rmSync(SETTINGS_PATH, { force: true });
|
||||
symlinkSync(target, SETTINGS_PATH);
|
||||
try {
|
||||
await send('PUT', '/api/settings', { agentSkillEnabled: true });
|
||||
expect(lstatSync(SETTINGS_PATH).isSymbolicLink()).toBe(true);
|
||||
expect(readlinkSync(SETTINGS_PATH)).toBe(target);
|
||||
expect(JSON.parse(readFileSync(target, 'utf-8'))).toEqual({ lastUsedCase: 'linked', agentSkillEnabled: true });
|
||||
} finally {
|
||||
rmSync(SETTINGS_PATH, { force: true });
|
||||
rmSync(elsewhere, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe('refuses to overwrite a file that is not a JSON object, and leaves its bytes alone', () => {
|
||||
for (const [label, bytes] of [
|
||||
['a torn file', TORN],
|
||||
['a JSON array', '[1, 2]'],
|
||||
['JSON null', 'null'],
|
||||
['a JSON string', '"settings"'],
|
||||
] as const) {
|
||||
it(`${label}: every settings write route answers 409 CONFLICT`, async () => {
|
||||
writeFileSync(SETTINGS_PATH, bytes);
|
||||
expect(await send('PUT', '/api/settings', { agentSkillEnabled: true })).toEqual({ status: 409, body: REFUSAL });
|
||||
expect(await send('PUT', '/api/execution/model-config', { defaultModel: 'opus' })).toEqual({
|
||||
status: 409,
|
||||
body: REFUSAL,
|
||||
});
|
||||
expect(await send('PUT', '/api/cases/order', { order: ['a'] })).toEqual({ status: 409, body: REFUSAL });
|
||||
expect(readFileSync(SETTINGS_PATH, 'utf-8')).toBe(bytes);
|
||||
});
|
||||
}
|
||||
|
||||
it('a torn file: quick-start and a Ralph loop start still succeed without touching it', async () => {
|
||||
writeFileSync(SETTINGS_PATH, TORN);
|
||||
expect((await send('POST', '/api/quick-start', { caseName: newCaseName() })).body.success).toBe(true);
|
||||
expect(
|
||||
(await send('POST', '/api/ralph-loop/start', { caseName: newCaseName(), taskDescription: 'x' })).body.success
|
||||
).toBe(true);
|
||||
expect(readFileSync(SETTINGS_PATH, 'utf-8')).toBe(TORN);
|
||||
});
|
||||
|
||||
it('a torn file: a refusal does not wedge later writes once the file is fixed', async () => {
|
||||
writeFileSync(SETTINGS_PATH, TORN);
|
||||
expect((await send('PUT', '/api/settings', { agentSkillEnabled: true })).status).toBe(409);
|
||||
seed({ lastUsedCase: 'fixed' });
|
||||
expect(await send('PUT', '/api/settings', { agentSkillEnabled: true })).toEqual({
|
||||
status: 200,
|
||||
body: { success: true, data: {} },
|
||||
});
|
||||
expect(readSettings()).toEqual({ lastUsedCase: 'fixed', agentSkillEnabled: true });
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user