@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
115 lines • 4.29 kB
JavaScript
import { normalizeModelRef } from "@earendil-works/pi-ai";
import { appendDebugLogEntry } from "./hidden-stdout-log.js";
import { parseSettingsJson } from "./settings-json.js";
export const STATE_KEYS = new Set([
"tipsHistory",
"changelogSeen",
"lastChangelogVersion",
"modelThinkingLevels",
"modelLastOnThinkingLevels",
"modelServiceTiers",
]);
const failedMigrations = new Set();
const reportedStateErrors = new Set();
export function logStateError(storage, error) {
const target = storage.selectSource?.("global")?.path ?? storage;
if (reportedStateErrors.has(target))
return;
reportedStateErrors.add(target);
const detail = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
try {
appendDebugLogEntry("settings: could not read or save state.json; using empty bookkeeping", detail);
}
catch {
// Diagnostics cannot disable configuration when the debug log is also unwritable.
}
}
/** State is disposable bookkeeping: malformed bytes never disable config loading or saving. */
export function parseStateSettings(storage, content) {
let raw;
try {
raw = content ? parseSettingsJson(content) : {};
}
catch (error) {
logStateError(storage, error);
return {};
}
for (const field of ["modelThinkingLevels", "modelLastOnThinkingLevels", "modelServiceTiers"]) {
const map = raw[field];
if (typeof map !== "object" || map === null || Array.isArray(map))
continue;
const normalized = {};
for (const [key, value] of Object.entries(map)) {
const canonical = normalizeModelRef(key);
if (key === canonical || !Object.hasOwn(map, canonical))
normalized[canonical] = value;
}
raw[field] = normalized;
}
return raw;
}
/**
* Move raw global bookkeeping only after its state write succeeds. Re-read both files under
* their locks, preserving existing state keys and every unrelated config key's value/order.
*/
export function migrateSettingsState(storage) {
const withStateLock = storage.withStateLock?.bind(storage);
if (!withStateLock)
return;
const target = storage.selectSource?.("global")?.path ?? storage;
if (failedMigrations.has(target))
return;
try {
storage.withLock("global", (current) => {
if (!current)
return undefined;
const raw = parseSettingsJson(current);
if (![...STATE_KEYS].some((key) => Object.hasOwn(raw, key)))
return undefined;
withStateLock((stateContent) => {
const state = stateContent ? parseSettingsJson(stateContent) : {};
let changed = false;
for (const key of STATE_KEYS) {
if (Object.hasOwn(raw, key) && !Object.hasOwn(state, key)) {
state[key] = raw[key];
changed = true;
}
}
return changed ? JSON.stringify(state, null, 2) : undefined;
});
for (const key of STATE_KEYS)
delete raw[key];
return JSON.stringify(raw, null, 2);
});
}
catch (error) {
failedMigrations.add(target);
const detail = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
try {
appendDebugLogEntry("settings: could not migrate global state keys", detail);
}
catch {
// Best-effort diagnostics must not prevent reading the legacy settings fallback.
}
}
}
/** Only the six bookkeeping keys overlay config; arbitrary state-file keys are never settings. */
export function readSettingsState(storage) {
const state = {};
try {
storage.withStateLock?.((current) => {
const raw = parseStateSettings(storage, current);
for (const key of STATE_KEYS) {
if (Object.hasOwn(raw, key))
state[key] = raw[key];
}
return undefined;
});
}
catch (error) {
logStateError(storage, error);
return {};
}
return state;
}
//# sourceMappingURL=settings-state.js.map