@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
93 lines • 3.78 kB
JavaScript
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { CONFIG_DIR_NAME } from "../../../../config.js";
import { parseSettingsJson } from "../../../settings-manager.js";
/**
* Settings keys a running session already applies live (model/thinking defaults).
* A settings.json change limited
* to these keys — typically /model or a thinking-level change in another
* session or a background CLI run — must not hot-reload this session. Any
* other key change keeps the full validate-and-reload flow.
*/
const ROUTINE_SETTINGS_KEYS = new Set(["defaultModel", "defaultProvider", "defaultThinkingLevel"]);
export function joinConfigDir(cwd) {
return resolve(cwd, CONFIG_DIR_NAME);
}
export function isSettingsPath(path, agentDir, cwd) {
const resolvedPath = resolve(path);
return ["settings.jsonc", "settings.json"].some((name) => resolvedPath === resolve(agentDir, name) || resolvedPath === resolve(joinConfigDir(cwd), name));
}
function settingsSnapshotKey(path) {
return resolve(path);
}
export function updateSettingsContentSnapshot(contents, path) {
const key = settingsSnapshotKey(path);
try {
contents.set(key, readFileSync(path, "utf-8"));
}
catch {
contents.delete(key);
}
}
export function refreshSettingsContentSnapshots(contents, agentDir, cwd) {
contents.clear();
for (const name of ["settings.jsonc", "settings.json"]) {
updateSettingsContentSnapshot(contents, resolve(agentDir, name));
updateSettingsContentSnapshot(contents, resolve(joinConfigDir(cwd), name));
}
}
function parseSettingsObject(content) {
try {
return parseSettingsJson(content);
}
catch {
return undefined;
}
}
function changedTopLevelKeys(previous, next) {
const keys = new Set([...Object.keys(previous), ...Object.keys(next)]);
const changed = [];
for (const key of keys) {
if (JSON.stringify(previous[key]) !== JSON.stringify(next[key]))
changed.push(key);
}
return changed.sort();
}
function isRoutineOnlySettingsChange(previousContent, nextContent) {
if (previousContent === undefined || nextContent === undefined)
return false;
const previous = parseSettingsObject(previousContent);
const next = parseSettingsObject(nextContent);
if (previous === undefined || next === undefined)
return false;
const changedKeys = changedTopLevelKeys(previous, next);
return changedKeys.length > 0 && changedKeys.every((key) => ROUTINE_SETTINGS_KEYS.has(key));
}
/**
* Drops settings.json changes whose top-level diff is limited to routine keys
* (ROUTINE_SETTINGS_KEYS). The content diff works across processes, unlike the
* process-local self-write tracker: /model in one session no longer hot-reloads
* every other session. The snapshot base advances for every observed settings
* path, suppressed or not, so each event is classified against the previous
* event's content. Missing or unparseable content is never suppressed.
*/
export function excludeRoutineOnlySettingsChanges(paths, contents, agentDir, cwd, logger) {
return paths.filter((path) => {
if (!isSettingsPath(path, agentDir, cwd))
return true;
const previousContent = contents.get(settingsSnapshotKey(path));
let nextContent;
try {
nextContent = readFileSync(path, "utf-8");
}
catch {
nextContent = undefined;
}
updateSettingsContentSnapshot(contents, path);
if (!isRoutineOnlySettingsChange(previousContent, nextContent))
return true;
logger.debug("routine_settings_change_suppressed", { path });
return false;
});
}
//# sourceMappingURL=routine-settings.js.map