fix(setup): preserve initial backups and Codex comments (#1204)

This commit is contained in:
Ysopwnable
2026-10-09 12:57:00 +08:00
committed by GitHub
parent db675f1d52
commit 6051cb2c0c
6 changed files with 221 additions and 46 deletions
+29 -25
View File
@@ -832,15 +832,15 @@ const disabledServerListLines = (
/**
* Replace REA's TOML server tables and assignments. Comments that introduce a
* following table stay. Text inside strings is not treated as a header. Only
* Grok Build reads the root `disabled_mcp_servers` list that registration
* also edits.
* following table stay. Codex also retains comments around REA's own table.
* Text inside strings is not treated as a header. Only Grok Build reads the
* root `disabled_mcp_servers` list that registration also edits.
*/
const upsertTomlServerSection = (
originalText: string,
serverKey: string,
entry: unknown,
editDisabledList: boolean,
format: "toml" | "grok",
): string => {
// smol-toml accepts a leading BOM. Leave it in place or the first header is missed.
const bom = originalText.startsWith("\uFEFF");
@@ -917,24 +917,25 @@ const upsertTomlServerSection = (
mode = "none";
continue;
}
const disabledLines = editDisabledList
? disabledServerListLines(
disabledServerListEdit({
text,
lineStart,
valueOffset,
valueEnd,
statementEnd,
tablePath,
segments: assignment.segments,
serverKey,
entry,
}),
lines,
lineIndex,
endLine,
)
: undefined;
const disabledLines =
format === "grok"
? disabledServerListLines(
disabledServerListEdit({
text,
lineStart,
valueOffset,
valueEnd,
statementEnd,
tablePath,
segments: assignment.segments,
serverKey,
entry,
}),
lines,
lineIndex,
endLine,
)
: undefined;
if (disabledLines !== undefined) {
flushPending();
kept.push(...disabledLines);
@@ -975,7 +976,10 @@ const upsertTomlServerSection = (
continue;
}
if (isBlankOrComment(line)) {
pending.push(physical);
if (format === "toml" && line.trimStart().startsWith("#")) {
flushPending();
kept.push(physical);
} else pending.push(physical);
lineIndex += 1;
continue;
}
@@ -1044,7 +1048,7 @@ const serializeGrokConfiguration = (
originalText,
PRODUCT_IDENTITY.mcpServerKey,
entry,
true,
"grok",
);
};
@@ -1077,7 +1081,7 @@ const serializeCodexConfiguration = (
originalText,
PRODUCT_IDENTITY.mcpServerKey,
tomlServerEntry(document, PRODUCT_IDENTITY.mcpServerKey),
false,
"toml",
);
// Removing a final table also drops the file's last line break.
const edited =
+10 -7
View File
@@ -7,6 +7,7 @@ import {
type ClientConfigurationDocument,
} from "./ClientConfigurationDocument.js";
import { copyFile, lstat, readFile, realpath, rm } from "node:fs/promises";
import { constants as fsConstants } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
@@ -31,6 +32,7 @@ interface ManagedPathStats {
/** Injectable filesystem operations used to prove uninstall failure recovery. */
export interface UninstallFileSystem {
readText(path: string): Promise<string>;
/** Preserve the first backup; reject an existing destination with EEXIST. */
copy(source: string, destination: string): Promise<void>;
writeText(path: string, contents: string): Promise<void>;
stat(path: string): Promise<ManagedPathStats>;
@@ -40,7 +42,8 @@ export interface UninstallFileSystem {
const systemFileSystem: UninstallFileSystem = {
readText: (path) => readFile(path, "utf8"),
copy: (source, destination) => copyFile(source, destination),
copy: (source, destination) =>
copyFile(source, destination, fsConstants.COPYFILE_EXCL),
writeText: (path, contents) =>
writeFileAtomic(path, contents, { encoding: "utf8" }),
stat: (path) => lstat(path),
@@ -274,12 +277,12 @@ const removeClient = async (
try {
await fileSystem.copy(transactionPath, backupPath);
} catch (cause: unknown) {
void cause;
return item(
client.name,
"failed",
"Configuration could not be backed up, so no change was made. Check file permissions, then rerun uninstall.",
);
if (!(cause instanceof Error && "code" in cause && cause.code === "EEXIST"))
return item(
client.name,
"failed",
"Configuration could not be backed up, so no change was made. Check file permissions, then rerun uninstall.",
);
}
try {
await fileSystem.writeText(