mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* refactor: prune internal seams and preserve configuration ownership * fix(setup): keep YAML aliases and inherited registration settings on refresh - Reuse an unchanged YAML collection only while every alias in it still resolves after the edit; otherwise rebuild it so the alias becomes its value. A sibling anchor replaced or moved in the same refresh no longer leaves an unresolved alias in Hermes or Goose output. - Keep a connection selector whose existing value already chooses stdio (for example an inherited Hermes transport: stdio) instead of superseding it; remote selectors are still replaced. Explicit stdio selectors are therefore no longer rewritten (Kimi expectation updated). - When the whole Hermes registration is inherited through a merge, override it with that merge and write only the changed fields. * fix(setup): edit registration fields whose YAML key is an alias A key written as an alias to a string scalar (for example *field with field: &field env) was treated as a source-only key, so any change to its value was refused in Hermes and Goose. Resolve such keys to their string, keep the aliased spelling while its anchor survives the edit, and write the plain key when the same edit rebuilds the anchor. * test(process): bound cleanup-boundary waits and always release gates Both cancellation tests awaited a bare started promise and released their gates only on success, so a missed boundary waited for the outer deadline and could leave cleanup parked. Use awaitAcknowledgement and release and join the cleanup in finally. * fix(setup): repair only aliases an edit leaves unresolved Replace the pre-edit alias survival guess, which expanded aliases to sibling anchors that survive unchanged, with an exact repair: record what each alias in the replaced value resolves to, write the edit, then replace only aliases left without a preceding anchor by that value. - Keep an aliased key's source position, so its anchor stays ahead of later aliases. - Treat an aliased key as its string only when no other key in the map resolves to the same string; an ambiguous one stays guarded. - Override an inherited Hermes registration only when the requested entry keeps every inherited field, so an explicit replacement can drop remote selectors. * fix(setup): repair aliases whose binding an edit changes, keeping comments An alias whose own anchor was removed could resolve to an earlier anchor with the same name and silently change value. Record the node each alias resolved to and repair any alias that no longer resolves to that node. Carry the alias's comments and spacing onto the repaired value. * test(setup): split the Hermes anchor and merge refresh cases * fix(setup): refuse removing an overridden key its merge would restore The merge-retention check skipped keys the mapping also overrides, so dropping such a key (for example a remote url during an explicit stdio replacement) let the retained merge restore it. Check removal before skipping overridden keys, so the edit is refused with the existing message instead of producing a registration that cannot be applied.
265 lines
8.5 KiB
TypeScript
265 lines
8.5 KiB
TypeScript
import {
|
|
Alias,
|
|
isAlias,
|
|
isMap,
|
|
isNode,
|
|
isSeq,
|
|
isScalar,
|
|
visit,
|
|
type Document,
|
|
type Node,
|
|
} from "yaml";
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
|
|
/** Resolve literal and aliased string keys to their source nodes for editing. */
|
|
export const resolveYamlEditPath = (
|
|
document: Document,
|
|
path: readonly string[],
|
|
): (string | Node)[] => {
|
|
const resolved: (string | Node)[] = [];
|
|
let current: unknown = document.contents;
|
|
for (const name of path) {
|
|
if (isAlias(current)) current = current.resolve(document);
|
|
if (!isMap(current)) {
|
|
resolved.push(name);
|
|
current = undefined;
|
|
continue;
|
|
}
|
|
const matches = current.items.filter((pair) => {
|
|
const key = isAlias(pair.key) ? pair.key.resolve(document) : pair.key;
|
|
return isScalar(key) && key.value === name;
|
|
});
|
|
if (matches.length > 1)
|
|
throw new ConfigurationError(
|
|
`YAML mapping contains more than one key resolving to ${JSON.stringify(name)}.`,
|
|
);
|
|
const pair = matches[0];
|
|
resolved.push(pair !== undefined && isNode(pair.key) ? pair.key : name);
|
|
current = pair?.value;
|
|
}
|
|
return resolved;
|
|
};
|
|
|
|
interface AliasCopies {
|
|
readonly targets: ReadonlyMap<Alias, Node | undefined>;
|
|
readonly removed: ReadonlySet<Node>;
|
|
readonly retained: Map<Node, Node>;
|
|
readonly copied: Set<Node>;
|
|
readonly survivingNames: Map<Node, string>;
|
|
readonly visiting: Set<Node>;
|
|
readonly validated: Set<Node>;
|
|
readonly names: Set<string>;
|
|
readonly reusedNames: ReadonlySet<string>;
|
|
}
|
|
|
|
const copyAliasComments = (source: Alias, copy: Node): Node => {
|
|
for (const key of ["commentBefore", "comment"] as const) {
|
|
const comment = source[key];
|
|
if (comment === undefined) continue;
|
|
copy[key] = copy[key] === undefined ? comment : `${copy[key]}\n${comment}`;
|
|
}
|
|
return copy;
|
|
};
|
|
|
|
const aliasTarget = (source: Alias, context: AliasCopies): Node => {
|
|
const target = context.targets.get(source);
|
|
if (target === undefined)
|
|
throw new TypeError("Cannot preserve an unresolved YAML alias");
|
|
return target;
|
|
};
|
|
|
|
/** Check copied dependencies even when their definitions will stay in place. */
|
|
const validateAliasValue = (source: Node, context: AliasCopies): void => {
|
|
if (context.validated.has(source)) return;
|
|
if (context.visiting.has(source))
|
|
throw new TypeError("Cannot preserve cyclic YAML aliases");
|
|
context.visiting.add(source);
|
|
visit(source, {
|
|
Alias: (_key, node) =>
|
|
validateAliasValue(aliasTarget(node, context), context),
|
|
});
|
|
context.visiting.delete(source);
|
|
context.validated.add(source);
|
|
};
|
|
|
|
const newAnchorName = (anchor: string, context: AliasCopies): string => {
|
|
let index = 1;
|
|
let name = `${anchor}_rea_preserved`;
|
|
while (context.names.has(name)) name = `${anchor}_rea_preserved_${index++}`;
|
|
context.names.add(name);
|
|
return name;
|
|
};
|
|
|
|
/** Reuse the surviving owner; a later same-name definition must not capture it. */
|
|
const survivingAlias = (target: Node, context: AliasCopies): Alias => {
|
|
if (isAlias(target) || target.anchor === undefined)
|
|
throw new TypeError("Invalid YAML alias target");
|
|
if (!context.reusedNames.has(target.anchor)) return new Alias(target.anchor);
|
|
let name = context.survivingNames.get(target);
|
|
if (name === undefined) {
|
|
name = newAnchorName(target.anchor, context);
|
|
context.survivingNames.set(target, name);
|
|
}
|
|
return new Alias(name);
|
|
};
|
|
|
|
/** Keep source bindings, including nested aliases whose names were later reused. */
|
|
const copyAliasValue = (
|
|
source: Node,
|
|
context: AliasCopies,
|
|
cloned?: Node,
|
|
): Node => {
|
|
if (isAlias(source)) {
|
|
const target = aliasTarget(source, context);
|
|
return copyAliasComments(
|
|
source,
|
|
context.removed.has(target)
|
|
? copyAliasValue(target, context)
|
|
: survivingAlias(target, context),
|
|
);
|
|
}
|
|
if (context.visiting.has(source))
|
|
throw new TypeError("Cannot preserve cyclic YAML aliases");
|
|
const retained = context.retained.get(source);
|
|
if (
|
|
retained !== undefined &&
|
|
!isAlias(retained) &&
|
|
retained.anchor !== undefined
|
|
)
|
|
return new Alias(retained.anchor);
|
|
const copy = cloned ?? source.clone();
|
|
if (!isNode(copy) || isAlias(copy))
|
|
throw new TypeError("Invalid YAML value copy");
|
|
context.copied.add(source);
|
|
if (source.anchor !== undefined) {
|
|
copy.anchor = newAnchorName(source.anchor, context);
|
|
context.retained.set(source, copy);
|
|
}
|
|
context.visiting.add(source);
|
|
const child = (value: unknown, copied: unknown): unknown =>
|
|
isNode(value)
|
|
? copyAliasValue(value, context, isNode(copied) ? copied : undefined)
|
|
: copied;
|
|
if (isMap(source) && isMap(copy)) {
|
|
for (const [index, pair] of source.items.entries()) {
|
|
const copied = copy.items[index];
|
|
if (copied === undefined) continue;
|
|
copied.key = child(pair.key, copied.key);
|
|
copied.value = child(pair.value, copied.value);
|
|
}
|
|
} else if (isSeq(source) && isSeq(copy)) {
|
|
copy.items = source.items.map((value, index) =>
|
|
child(value, copy.items[index]),
|
|
);
|
|
}
|
|
context.visiting.delete(source);
|
|
return copy;
|
|
};
|
|
|
|
/** Preserve other consumers before removing anchored configuration nodes. */
|
|
export const preserveYamlAliasValues = (
|
|
document: Document,
|
|
previous: readonly unknown[],
|
|
): ReadonlySet<Node> => {
|
|
const roots = previous.filter(isNode);
|
|
const removed = new Set<Node>();
|
|
for (const root of roots)
|
|
visit(root, {
|
|
Value: (_key, node) => {
|
|
if (node.anchor !== undefined) removed.add(node);
|
|
},
|
|
});
|
|
if (removed.size === 0) return new Set();
|
|
const targets = new Map<Alias, Node | undefined>();
|
|
const names = new Set<string>();
|
|
const reusedNames = new Set<string>();
|
|
visit(document, {
|
|
Alias: (_key, node) => {
|
|
targets.set(node, node.resolve(document));
|
|
},
|
|
Value: (_key, node) => {
|
|
if (node.anchor === undefined) return;
|
|
if (names.has(node.anchor)) reusedNames.add(node.anchor);
|
|
names.add(node.anchor);
|
|
},
|
|
});
|
|
const context: AliasCopies = {
|
|
targets,
|
|
removed,
|
|
retained: new Map(),
|
|
copied: new Set(),
|
|
survivingNames: new Map(),
|
|
visiting: new Set(),
|
|
validated: new Set(),
|
|
names,
|
|
reusedNames,
|
|
};
|
|
const replacements = new Map<Node, Node>();
|
|
visit(document, {
|
|
Alias: (_key, node, path) => {
|
|
if (!roots.some((root) => path.includes(root))) {
|
|
const target = targets.get(node);
|
|
if (target === undefined || !removed.has(target)) return;
|
|
validateAliasValue(target, context);
|
|
replacements.set(
|
|
node,
|
|
copyAliasComments(node, copyAliasValue(target, context)),
|
|
);
|
|
}
|
|
},
|
|
});
|
|
// Plan every binding against the original document before moving any anchor.
|
|
for (const [target, name] of context.survivingNames)
|
|
if (!isAlias(target)) target.anchor = name;
|
|
visit(document, {
|
|
Alias: (_key, node) => {
|
|
const replacement = replacements.get(node);
|
|
if (replacement !== undefined) return replacement;
|
|
const target = targets.get(node);
|
|
const name =
|
|
target === undefined ? undefined : context.survivingNames.get(target);
|
|
return name === undefined
|
|
? undefined
|
|
: copyAliasComments(node, new Alias(name));
|
|
},
|
|
});
|
|
return context.copied;
|
|
};
|
|
|
|
/** Copy original bindings so moved aliases cannot bind to later anchors. */
|
|
export const copyYamlAliasSource = (
|
|
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 YAML alias");
|
|
if (!isNode(node)) return node;
|
|
if (visiting.has(node))
|
|
throw new TypeError("Cannot edit cyclic 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) =>
|
|
copyYamlAliasSource(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 = copyYamlAliasSource(original.key, document, visiting);
|
|
pair.value = copyYamlAliasSource(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;
|
|
};
|