fix(setup): retain Hermes YAML scalar source during updates

Retain source nodes for unchanged scalar values and string keys when
replacing a Hermes registration. Discarding quoted YAML 1.2 keys could
make Hermes' C reader reject duplicate boolean keys and switch to the
pure reader, changing the interpretation of unrelated settings.

Use linear mapping lookup, quote newly created YAML 1.2 strings for
YAML 1.1 compatibility, and preserve direct numeric edit formatting.
Extend actual CLI coverage for both existing reader-selection directions;
verify strings, exact unrelated numeric/timestamp values, backups and
idempotence with the actual Hermes Python loader.

Follow-up to #3102 and main's independent Hermes scalar fix.

Co-authored-by: Kaoru0822 <[email protected]>
Co-authored-by: gensokyo-dev <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-15 00:57:57 +08:00
1 parent 1579b6865e
commit 93e75bc8fe
3 files changed
+176 -70

No files matched your search

@@ -8,6 +8,34 @@ import {
} from "./HermesConfigurationYaml.js";
describe("Hermes configuration YAML", () => {
it("retains a quoted sequence element when replacing its registration", () => {
const source =
"mcp_servers:\n rea:\n command: old\n args:\n - 'mcp'\n";
expect(
editHermesConfigurationYaml(source, ["mcp_servers", "rea"], {
command: "rea",
args: ["mcp"],
}),
).toBe(source.replace("command: old", "command: rea"));
});
it("retains explicit string tags on unchanged keys and values", () => {
const source =
"%YAML 1.2\n---\nmcp_servers:\n rea:\n command: old\n env:\n !!str ON: !!str off\n";
const written = editHermesConfigurationYaml(
source,
["mcp_servers", "rea"],
{
command: "rea",
env: { ON: "off" },
},
);
expect(written).toBe(source.replace("command: old", 'command: "rea"'));
expect(
parse(written.slice("%YAML 1.2\n---\n".length), { version: "1.1" }),
).toMatchObject({ mcp_servers: { rea: { env: { ON: "off" } } } });
});
it("writes an edited number from its new value", () => {
expect(
editHermesConfigurationYaml("mode: 0600\nmask: 0x1F\n", ["mode"], 420),
+54 -2
View File
@@ -1,9 +1,13 @@
import {
isMap,
isNode,
isScalar,
isSeq,
parseDocument,
Document,
Schema,
type ParsedNode,
type Node,
type ScalarTag,
type Tags,
} from "yaml";
@@ -322,6 +326,36 @@ export const parseHermesConfigurationYaml = (text: string): unknown => {
});
};
/** Keep unchanged scalar nodes, including their quoting and explicit tags. */
const retainHermesScalarSource = (previous: unknown, next: Node): Node => {
if (isScalar(previous) && isScalar(next) && previous.value === next.value)
return previous;
if (isMap(previous) && isMap(next)) {
const entries = new Map(
previous.items.flatMap((pair) =>
isScalar(pair.key) && typeof pair.key.value === "string"
? [[pair.key.value, pair] as const]
: [],
),
);
for (const pair of next.items) {
const key = isScalar(pair.key) ? pair.key.value : undefined;
const prior = typeof key === "string" ? entries.get(key) : undefined;
if (prior === undefined) continue;
pair.key = prior.key;
if (isNode(pair.value))
pair.value = retainHermesScalarSource(prior.value, pair.value);
}
} else if (isSeq(previous) && isSeq(next)) {
next.items = next.items.map((item, index) =>
isNode(item)
? retainHermesScalarSource(previous.items[index], item)
: item,
);
}
return next;
};
/** Set or delete one value in a Hermes `config.yaml`, keeping the rest as written. */
export const editHermesConfigurationYaml = (
text: string,
@@ -348,8 +382,26 @@ export const editHermesConfigurationYaml = (
if (!document.hasIn(parent))
document.setIn(parent, document.createNode({}));
}
document.setIn(path, value);
const previous = document.getIn(path, true);
// Let YAML's scalar update preserve numeric formats for a direct edit.
// Replacing containers needs explicit retention of unchanged child nodes.
document.setIn(
path,
isMap(previous) || isSeq(previous)
? retainHermesScalarSource(previous, document.createNode(value))
: value,
);
}
// Editing REA must not reflow unrelated user prompts or other YAML scalars.
return document.toString({ lineWidth: 0 });
return document.toString({
lineWidth: 0,
// Hermes tries the YAML 1.1 C loader before its directive-aware pure
// fallback. Quote new strings to avoid introducing YAML 1.1 coercions.
...(document.directives?.yaml.version === "1.2"
? {
defaultStringType: "QUOTE_DOUBLE" as const,
defaultKeyType: "QUOTE_DOUBLE" as const,
}
: {}),
});
};