Files
reverse-engineering-anything/src/contracts/webScriptToolContracts.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* 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.
2026-11-14 04:08:37 -05:00

110 lines
5.8 KiB
TypeScript

import { z } from "zod";
import { analysisDetailOutputSchemaOf } from "./analysisDetail.js";
import {
webSourceLocationCompleteDeliveryShape,
webSourceLocationSummaryDeliveryShape,
} from "../domain/analysisView/webSourceLocationView.js";
import {
webSourceLocationSummaryViewResultSchema,
webSourceLocationMatchesViewResultSchema,
webSourceLocationTextViewResultSchema,
} from "../domain/analysisView/analysisViewResult.js";
import {
exportWebScriptsInputSchema,
webScriptExportResultSchema,
} from "../domain/webScriptExport.js";
import type { ToolContract } from "./toolContractTypes.js";
import {
webModuleTraceInputSchema,
webModuleTraceResultSchema,
} from "../domain/webModuleTrace.js";
import { toolContractMetadata } from "./toolEffects.js";
import { evidenceResultOf } from "./toolOutputSchemaPrimitives.js";
import {
webSourceLocationInputSchema,
webSourceLocationResultSchema,
} from "../domain/webSourceLocation.js";
export const webSourceLocationRequestSchema = z
.discriminatedUnion("detail", [
webSourceLocationInputSchema.extend(webSourceLocationCompleteDeliveryShape),
webSourceLocationInputSchema.extend(webSourceLocationSummaryDeliveryShape),
])
.meta({ "x-rea-root-discriminator": "detail" });
/** Local captured-script publication contracts shared by CLI and MCP. */
export const WEB_SCRIPT_TOOL_CONTRACTS = [
{
name: "trace_web_source_location",
...toolContractMetadata("trace_web_source_location"),
description:
'Map one generated position in an exported script through a caller-paired local source map, verifying manifest/script/map byte identities. Returns all equal-position matches, original positions, resolved source URLs and embedded sourcesContent; pairing does not prove deployment authenticity or execution. Lines are one-based, columns zero-based UTF-16; lookup uses the greatest generated position <= requested across lines. Regular maps and inline indexed sections only; external section URLs are unsupported. No network or application execution; decoding uses an owned codec process. Complete delivery is default. detail summary retains Evidence; view {kind:"source-location",collection:"matches",offset:0,limit:64} pages match metadata. source-text additionally requires match_index and pages UTF-16 text. inspect_analysis_view reuses the retained parent; export_evidence_bundle preserves it. source_map_limits changes byte/record/heap admission per call; decode_timeout_ms is separate. Refusals name the exhausted control; cleanup may outlast the deadline.',
kind: "application",
inputSchema: webSourceLocationRequestSchema,
outputSchema: analysisDetailOutputSchemaOf(
webSourceLocationResultSchema,
z.union([
webSourceLocationSummaryViewResultSchema,
webSourceLocationMatchesViewResultSchema,
webSourceLocationTextViewResultSchema,
]),
),
examples: [
{
title:
"Trace a selected captured JavaScript point with retained compact delivery",
input: {
detail: "summary",
manifest_path: "/analysis/scripts/manifest.json",
script_index: 0,
source_map: {
path: "/analysis/app.js.map",
url: "https://example.test/app.js.map",
},
generated_position: { line: 1, column: 25 },
},
},
],
},
{
name: "trace_web_module_imports",
...toolContractMetadata("trace_web_module_imports"),
description:
"Resolve native ES imports/re-exports from one script in a local export_web_scripts manifest. Verifies the selected source digest/size and returns source positions, resolved URLs/errors and every matching captured candidate. URL query/fragment identities remain distinct; computed imports, bundler IDs and execution remain unknown. Supply importer_url when inline/document-base context is unknown and an import_map when needed. Literal resolution requires absolute REA_BROWSER_EXECUTABLE: caller-supplied Chromium runs only a trusted REA resolver stub with page requests fulfilled locally or blocked, without captured app execution or asset refetch. The native deadline is 20 seconds; cleanup may extend it.",
kind: "application",
inputSchema: webModuleTraceInputSchema,
outputSchema: evidenceResultOf(webModuleTraceResultSchema),
examples: [
{
title: "Trace an exported module under a selected import map",
input: {
manifest_path: "/analysis/exported-scripts/manifest.json",
script_index: 0,
import_map: {
path: "/analysis/import-map.json",
base_url: "https://example.test/app/",
},
},
},
],
},
{
name: "export_web_scripts",
...toolContractMetadata("export_web_scripts"),
description:
"Export JavaScript bytes already retained in one local inspect_web_page or capture_browser_scenario JSON capture (normalized result or complete Evidence). Writes scripts and a digest-verified manifest into an absent absolute output_directory. Returns all source URLs, script or transaction/event references, unavailable states, and an analysis_input for analyze_javascript_application when any bytes were exported. Include script sources in inspect_web_page or select capture.network.response_body in scenarios. No refetch or execution. Safe unambiguous URL layouts preserve relative module paths; versions, query variants, inline scripts, and path collisions are isolated with explicit resolution limitations.",
kind: "application",
inputSchema: exportWebScriptsInputSchema,
outputSchema: evidenceResultOf(webScriptExportResultSchema),
examples: [
{
title: "Export a saved website capture for static JavaScript analysis",
input: {
capture_path: "/analysis/capture.json",
output_directory: "/analysis/exported-scripts",
},
},
],
},
] as const satisfies readonly ToolContract[];