Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/application/HermesConfigurationYaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,57 @@ import {
stringifyHermesConfigurationYaml,
} from "./HermesConfigurationYaml.js";

describe("Hermes YAML deletion", () => {
for (const version of ["1.1", "1.2"] as const) {
for (const [name, body, path] of [
[
"an aliased table",
"server_defaults: &servers\n other:\n command: other-server\n\nmcp_servers: *servers # Selected table\n",
["mcp_servers", "rea"],
],
[
"nested aliases",
"defaults: &defaults\n other: {command: other-server}\nservers: &servers\n mcp_servers: *defaults\nclient: *servers\nobserver: *servers\n",
["client", "mcp_servers", "rea"],
],
[
"shadowed anchors",
"first: &server {command: original}\ndefaults: &servers\n other: *server\nsecond: &server {command: replacement}\nmcp_servers: *servers\nobserver: *server\n",
["mcp_servers", "rea"],
],
[
"shared consumers",
"defaults: &servers\n other: &other {command: other-server}\nmcp_servers: *servers\nobserver: *servers\nother_observer: *other\n",
["mcp_servers", "rea"],
],
[
"an explicit flow table with CRLF",
"# Keep formatting\r\nmcp_servers: {other: {command: other-server}}\r\n",
["mcp_servers", "rea"],
],
] as const) {
it(`preserves YAML ${version} bytes when deleting an absent key from ${name}`, () => {
const source = (version === "1.2" ? "%YAML 1.2\n---\n" : "") + body;
expect(editHermesConfigurationYaml(source, path, undefined)).toBe(
source,
);
});
}
}

for (const [name, reference] of [
["cyclic", "servers"],
["unresolved", "missing"],
]) {
it(`still refuses deletion through a table containing a ${name} alias`, () => {
const source = `defaults: &servers\n rea: {command: rea}\n other: *${reference}\nmcp_servers: *servers\n`;
expect(() =>
editHermesConfigurationYaml(source, ["mcp_servers", "rea"], undefined),
).toThrow(TypeError);
});
}
});

describe("Hermes configuration YAML", () => {
it("retains a quoted sequence element when replacing its registration", () => {
const source =
Expand Down
62 changes: 54 additions & 8 deletions src/application/HermesConfigurationYaml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
isAlias,
isMap,
isNode,
isScalar,
Expand Down Expand Up @@ -356,6 +357,43 @@ const retainHermesScalarSource = (previous: unknown, next: Node): Node => {
return next;
};

/** Copy original bindings so moved aliases cannot bind to later anchors. */
const copyHermesAliasSource = (
source: unknown,
document: Document,
visiting = new Set<Node>(),
): unknown => {
const node = isAlias(source) ? source.resolve(document) : source;
if (isAlias(source) && node === undefined)
throw new TypeError("Cannot edit an unresolved Hermes YAML alias");
if (!isNode(node)) return node;
if (visiting.has(node))
throw new TypeError("Cannot edit cyclic Hermes YAML aliases");
visiting.add(node);
const copied = node.clone();
if (isScalar(copied) || isMap(copied) || isSeq(copied)) delete copied.anchor;
if (isSeq(node) && isSeq(copied))
copied.items = node.items.map((item) =>
copyHermesAliasSource(item, document, visiting),
);
else if (isMap(node) && isMap(copied))
copied.items.forEach((pair, index) => {
const original = node.items[index];
if (original === undefined) return;
pair.key = copyHermesAliasSource(original.key, document, visiting);
pair.value = copyHermesAliasSource(original.value, document, visiting);
});
if (isAlias(source)) {
delete copied.commentBefore;
delete copied.comment;
if (source.commentBefore !== undefined)
copied.commentBefore = source.commentBefore;
if (source.comment !== undefined) copied.comment = source.comment;
}
visiting.delete(node);
return copied;
};

/** Set or delete one value in a Hermes `config.yaml`, keeping the rest as written. */
export const editHermesConfigurationYaml = (
text: string,
Expand All @@ -373,15 +411,23 @@ export const editHermesConfigurationYaml = (
.join("\n");
document.contents = map;
}
if (value === undefined) document.deleteIn(path);
else {
// setIn's implicit JS Map parents serialize as !!omap in YAML 1.1.
// Config settings are ordinary mappings, including absent server tables.
for (let length = 1; length < path.length; length++) {
const parent = path.slice(0, length);
if (!document.hasIn(parent))
document.setIn(parent, document.createNode({}));
for (let length = 1; length < path.length; length++) {
const parent = path.slice(0, length);
const previous = document.getIn(parent, true);
if (isAlias(previous)) {
const copied = copyHermesAliasSource(previous, document);
if (isMap(copied)) {
// Edit a private mapping, keeping the alias source and its consumers.
document.setIn(parent, copied);
}
} else if (value !== undefined && !document.hasIn(parent)) {
// Implicit JS Map parents serialize as !!omap in YAML 1.1.
document.setIn(parent, document.createNode({}));
}
}
if (value === undefined) {
if (!document.deleteIn(path)) return text;
} else {
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.
Expand Down
Loading
Loading