From 93e75bc8fe21780ab1ee517bfda4f4a7730d678d Mon Sep 17 00:00:00 2001 From: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:57:57 +0800 Subject: [PATCH] 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 <208281322+kaoru0822-kitauji@users.noreply.github.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- .../HermesConfigurationYaml.test.ts | 28 +++ src/application/HermesConfigurationYaml.ts | 56 +++++- .../setup/hermesClientSetup.test.ts | 162 ++++++++++-------- 3 files changed, 176 insertions(+), 70 deletions(-) diff --git a/src/application/HermesConfigurationYaml.test.ts b/src/application/HermesConfigurationYaml.test.ts index 8f56f8f79a..4297b33a0d 100644 --- a/src/application/HermesConfigurationYaml.test.ts +++ b/src/application/HermesConfigurationYaml.test.ts @@ -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), diff --git a/src/application/HermesConfigurationYaml.ts b/src/application/HermesConfigurationYaml.ts index 509201187b..43471fa290 100644 --- a/src/application/HermesConfigurationYaml.ts +++ b/src/application/HermesConfigurationYaml.ts @@ -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, + } + : {}), + }); }; diff --git a/tests/acceptance/setup/hermesClientSetup.test.ts b/tests/acceptance/setup/hermesClientSetup.test.ts index 4dff5c7ed3..3b2a146a0b 100644 --- a/tests/acceptance/setup/hermesClientSetup.test.ts +++ b/tests/acceptance/setup/hermesClientSetup.test.ts @@ -6,75 +6,101 @@ import { expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; -cliTest( - "Hermes setup preserves string environment values under the client's YAML 1.1 schema", - async ({ cli }) => { - const home = await createTestTempDirectory("rea-hermes-yaml-strings-"); - const profile = join(home, "hermes"); - const config = join(profile, "config.yaml"); - const environment = { - REA_MCP_OUTPUT_SCHEMA_PROFILE: "off", - ON: "on", - YES: "yes", - NO: "no", - SHORT_TRUE: "y", - SHORT_FALSE: "n", - DATE: "2026-10-14", - TIME: "12:34:56", - FLOAT: "1.0e-7", - OCTAL: "0600", - NULL_WORD: "NULL", - NULL_MARKER: "~", - }; - const settings = - "# User settings\nnotifications: on\ndate: 2026-10-14\n" + - "timestamp: 2026-10-14T00:00:00.123456+08:00\n"; - const original = - settings + - "mcp_servers:\n rea:\n command: rea-old\n args: [mcp]\n env:\n" + - Object.entries(environment) - .map( - ([key, value]) => - ` ${JSON.stringify(key)}: ${JSON.stringify(value)}\n`, - ) - .join(""); - await mkdir(profile); - await writeFile(config, original); - const run = (arguments_: readonly string[]) => - cli.run({ - arguments: arguments_, - cwd: home, - environment: { USERPROFILE: home, HERMES_HOME: profile }, - timeoutMs: 20_000, +for (const [version, keyStyle] of [ + ["1.1", "quoted"], + ["1.2", "quoted"], + ["1.2", "plain"], +] as const) { + cliTest( + `Hermes setup preserves string environment values under YAML ${version} with ${keyStyle} keys`, + async ({ cli }) => { + const home = await createTestTempDirectory("rea-hermes-yaml-strings-"); + const profile = join(home, "hermes"); + const config = join(profile, "config.yaml"); + const environment = { + REA_MCP_OUTPUT_SCHEMA_PROFILE: "off", + ON: "on", + YES: "yes", + NO: "no", + SHORT_TRUE: "y", + SHORT_FALSE: "n", + DATE: "2026-10-14", + TIME: "12:34:56", + FLOAT: "1.0e-7", + OCTAL: "0600", + NULL_WORD: "NULL", + NULL_MARKER: "~", + }; + const directive = version === "1.2" ? "%YAML 1.2\n---\n" : ""; + const settings = + directive + + "# User settings\nnotifications: on\ndate: 2026-10-14\n" + + "timestamp: 2026-10-14T00:00:00.123456+08:00\n"; + const original = + settings + + "mcp_servers:\n rea:\n command: rea-old\n args: [mcp]\n env:\n" + + Object.entries(environment) + .map( + ([key, value]) => + ` ${keyStyle === "plain" ? key : JSON.stringify(key)}: ${JSON.stringify(value)}\n`, + ) + .join(""); + await mkdir(profile); + await writeFile(config, original); + const run = (arguments_: readonly string[]) => + cli.run({ + arguments: arguments_, + cwd: home, + environment: { USERPROFILE: home, HERMES_HOME: profile }, + timeoutMs: 20_000, + }); + const setup = [ + "setup", + "--client", + "hermes", + "--skill=false", + "--yes", + "--json", + ]; + expect((await run(setup)).json).toMatchObject({ status: "ready" }); + const configured = await readFile(config, "utf8"); + expect(parse(configured, { version })).toMatchObject({ + mcp_servers: { rea: { env: environment, enabled: true } }, }); - const setup = [ - "setup", - "--client", - "hermes", - "--skill=false", - "--yes", - "--json", - ]; - expect((await run(setup)).json).toMatchObject({ status: "ready" }); - const configured = await readFile(config, "utf8"); - expect(parse(configured, { version: "1.1" })).toMatchObject({ - mcp_servers: { rea: { env: environment, enabled: true } }, - }); - expect(configured.startsWith(settings)).toBe(true); - expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original); - expect( - (await run(["doctor", "--client", "hermes", "--json"])).json, - ).toMatchObject({ - healthy: true, - identity: { registrations: [{ client: "hermes", state: "aligned" }] }, - }); - expect((await run(setup)).json).toMatchObject({ - status: "ready", - appliedActions: [], - }); - expect(await readFile(config, "utf8")).toBe(configured); - }, -); + // Hermes's C loader applies YAML 1.1 even with the explicit directive. + // Independently model that schema; real Hermes loader verification is + // recorded in the PR rather than requiring Python dependencies in CI. + if (keyStyle === "plain") { + // The real C loader rejects these YAML 1.2 string keys as duplicate + // boolean keys. Preserve that fallback rather than changing how + // Hermes interprets unrelated user settings. + for (const text of [original, configured]) + expect(() => + parse(text.slice(directive.length), { version: "1.1" }), + ).toThrow("Map keys must be unique"); + } else { + expect( + parse(configured.slice(directive.length), { version: "1.1" }), + ).toMatchObject({ + mcp_servers: { rea: { env: environment, enabled: true } }, + }); + } + expect(configured.startsWith(settings)).toBe(true); + expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original); + expect( + (await run(["doctor", "--client", "hermes", "--json"])).json, + ).toMatchObject({ + healthy: true, + identity: { registrations: [{ client: "hermes", state: "aligned" }] }, + }); + expect((await run(setup)).json).toMatchObject({ + status: "ready", + appliedActions: [], + }); + expect(await readFile(config, "utf8")).toBe(configured); + }, + ); +} cliTest( "Hermes setup, doctor and uninstall use the expanded profile and its personal skill",