mirror of
https://github.com/morluto/rea.git
synced 2026-11-14 10:43:30 +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.
184 lines
7.2 KiB
JavaScript
184 lines
7.2 KiB
JavaScript
import { isDeepStrictEqual } from "node:util";
|
|
import { authenticateParsedEvidenceBundle } from "../../dist/domain/evidenceBundle.js";
|
|
import { analysisErrorProjectionSchema } from "../../dist/contracts/errorSchemas.js";
|
|
import { toolContract } from "../../dist/contracts/toolContracts.js";
|
|
import {
|
|
expandProviderCapabilities,
|
|
expandToolAvailability,
|
|
providerCapabilitySet,
|
|
} from "../../dist/contracts/toolOutputSchemaPrimitives.js";
|
|
import { compareUnicodeCodePoints } from "../../dist/domain/unicodeCodePointOrder.js";
|
|
|
|
/** Read the textual JSON projection from one MCP tool call. */
|
|
export const mcpTextValue = (result) => {
|
|
const text = result.content?.find((item) => item.type === "text")?.text;
|
|
if (typeof text !== "string") throw new Error("Tool result omitted text");
|
|
return text;
|
|
};
|
|
|
|
const jsonValue = (result) => JSON.parse(mcpTextValue(result));
|
|
|
|
/** Sort canonical contract names using REA's code-point ordering. */
|
|
export const sortVerifierNames = (names) =>
|
|
[...names].sort(compareUnicodeCodePoints);
|
|
|
|
/** Read the complete canonical diagnostic from an MCP tool failure. */
|
|
export const requireMcpToolError = (result) => {
|
|
if (result.isError !== true) throw new Error("Expected an MCP tool failure");
|
|
return analysisErrorProjectionSchema.parse(jsonValue(result).error);
|
|
};
|
|
|
|
/** Validate the complete output against its exact named contract, retaining inline Evidence. */
|
|
export const requireMcpOutput = (
|
|
result,
|
|
operation,
|
|
contract = toolContract(operation),
|
|
) => {
|
|
if (result.isError === true)
|
|
throw new Error(`${operation} failed: ${mcpTextValue(result)}`);
|
|
if (contract.name !== operation)
|
|
throw new Error(`${operation} received the ${contract.name} contract`);
|
|
const text = jsonValue(result);
|
|
const output = contract.outputSchema.parse(text);
|
|
if (result.structuredContent !== undefined) {
|
|
// The producer emits both representations from the same JSON value.
|
|
// Compare before schema projection can discard disagreeing source fields.
|
|
if (!isDeepStrictEqual(text, result.structuredContent))
|
|
throw new Error(`${operation} text and structured results disagree`);
|
|
}
|
|
// Downstream parseEvidence must authenticate source facts, not a schema projection.
|
|
return Object.hasOwn(output, "normalized_result") ? text : output;
|
|
};
|
|
|
|
const operationResultKey = (contract) => {
|
|
const shape = contract.outputSchema.shape;
|
|
if (shape === undefined)
|
|
throw new Error(`${contract.name} has an unsupported MCP output schema`);
|
|
const evidence = Object.hasOwn(shape, "normalized_result");
|
|
const lifecycle = Object.hasOwn(shape, "result");
|
|
if (evidence === lifecycle)
|
|
throw new Error(`${contract.name} has an unsupported MCP output schema`);
|
|
return evidence ? "normalized_result" : "result";
|
|
};
|
|
|
|
/**
|
|
* Expand compact session metadata once at the verifier boundary. The caller
|
|
* has validated the status against the binary_session contract, so a summary
|
|
* carries summary candidates without capability sets; complete detail must
|
|
* still expand every candidate's capabilities.
|
|
*/
|
|
export const expandMcpSessionStatus = (status) => ({
|
|
...status,
|
|
capabilities: expandProviderCapabilities(status.capabilities),
|
|
analysis_provider_candidates:
|
|
status.detail === "summary"
|
|
? status.analysis_provider_candidates
|
|
: expandProviderCandidates(status.analysis_provider_candidates),
|
|
tool_availability: expandToolAvailability(status.tool_availability),
|
|
});
|
|
|
|
/** Expand provider capability groups while retaining candidate identity/state. */
|
|
const expandProviderCandidates = (candidates) =>
|
|
candidates.map((candidate) => ({
|
|
...candidate,
|
|
capabilities: expandProviderCapabilities(
|
|
providerCapabilitySet.array().parse(candidate.capabilities),
|
|
),
|
|
}));
|
|
|
|
/** Expand CLI provider capability groups at the same owning boundary. */
|
|
export const expandProviderOutput = (providers) => ({
|
|
...providers,
|
|
capabilities: expandProviderCapabilities(
|
|
providerCapabilitySet.array().parse(providers.capabilities),
|
|
),
|
|
analysis_provider_candidates: expandProviderCandidates(
|
|
providers.analysis_provider_candidates,
|
|
),
|
|
});
|
|
|
|
/** Expand the CLI capabilities command's compact provider descriptor groups. */
|
|
export const expandCapabilitiesOutput = (capabilities) => ({
|
|
...capabilities,
|
|
capabilities: expandProviderCapabilities(
|
|
providerCapabilitySet.array().parse(capabilities.capabilities),
|
|
),
|
|
});
|
|
|
|
/** Validate an Evidence-producing tool output and return its normalized result. */
|
|
export const requireMcpEvidenceResult = (result, operation) => {
|
|
const contract = toolContract(operation);
|
|
if (operationResultKey(contract) !== "normalized_result")
|
|
throw new Error(`${operation} requires the lifecycle result extractor`);
|
|
return requireMcpOutput(result, operation, contract).normalized_result;
|
|
};
|
|
|
|
/** Validate a lifecycle tool output and return its operation result. */
|
|
export const requireMcpLifecycleResult = (result, operation) => {
|
|
const contract = toolContract(operation);
|
|
if (operationResultKey(contract) !== "result")
|
|
throw new Error(`${operation} requires the Evidence result extractor`);
|
|
const projected = requireMcpOutput(result, operation, contract).result;
|
|
return operation === "binary_session"
|
|
? expandMcpSessionStatus(projected)
|
|
: projected;
|
|
};
|
|
|
|
/** Require an authenticated complete bundle without changing summary-capable readers. */
|
|
export const requireMcpEvidenceBundle = (result) => {
|
|
const operation = "get_evidence_bundle";
|
|
const contract = toolContract(operation);
|
|
const bundle = requireMcpOutput(result, operation, contract).result;
|
|
if (bundle.kind === "evidence-bundle-summary")
|
|
throw new Error(
|
|
"get_evidence_bundle returned summary metadata instead of complete Evidence",
|
|
);
|
|
return authenticateParsedEvidenceBundle(bundle);
|
|
};
|
|
|
|
/** Project a mixed verifier call using its exact named canonical output schema. */
|
|
export const requireMcpOperationResult = (
|
|
result,
|
|
operation,
|
|
contract = toolContract(operation),
|
|
) => {
|
|
const key = operationResultKey(contract);
|
|
const projected = requireMcpOutput(result, operation, contract)[key];
|
|
return operation === "binary_session"
|
|
? expandMcpSessionStatus(projected)
|
|
: projected;
|
|
};
|
|
|
|
/** Verify provider identity directly from the tool result. */
|
|
export const requireEvidenceProvider = (result, operation, providerId) => {
|
|
const evidence = jsonValue(result);
|
|
if (
|
|
evidence?.provider?.id !== providerId ||
|
|
evidence?.analysis_profile?.provider?.id !== providerId ||
|
|
typeof evidence.analysis_profile.provider.version !== "string"
|
|
) {
|
|
throw new Error(`${operation} omitted its concrete provider provenance`);
|
|
}
|
|
};
|
|
|
|
/** Verify composed workflow and upstream provider provenance inline. */
|
|
export const requireWorkflowEvidenceProvider = (
|
|
result,
|
|
operation,
|
|
expected,
|
|
) => {
|
|
const evidence = jsonValue(result);
|
|
const profile = evidence?.analysis_profile;
|
|
const upstream = profile?.parameters?.upstream_analysis_profile;
|
|
if (
|
|
evidence?.provider?.id !== expected.workflowProviderId ||
|
|
profile?.provider?.id !== expected.workflowProviderId ||
|
|
upstream?.provider?.id !== expected.upstreamProviderId ||
|
|
typeof upstream.provider.version !== "string"
|
|
) {
|
|
throw new Error(
|
|
`${operation} omitted its composed workflow or upstream provenance`,
|
|
);
|
|
}
|
|
};
|